From 050c956787fa7745dbfadfa1c604a300dceb1170 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 12 Aug 2022 13:33:21 -0500 Subject: [PATCH] Added expect and nextToken methods to parser --- parser/meta.go | 5 +++++ parser/parser.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 parser/meta.go diff --git a/parser/meta.go b/parser/meta.go new file mode 100644 index 0000000..b61608d --- /dev/null +++ b/parser/meta.go @@ -0,0 +1,5 @@ +package parser + +func (parser *ParsingOperation) parseMeta () (err error) { + +} diff --git a/parser/parser.go b/parser/parser.go index 8c16723..fb54943 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -1,5 +1,6 @@ package parser +import "io" import "os" import "path/filepath" import "git.tebibyte.media/sashakoshka/arf/file" @@ -51,14 +52,42 @@ func (parser *ParsingOperation) parse (sourceFile *file.File) (err error) { tokens, err = lexer.Tokenize(sourceFile) if err != nil { return } + // reset the parser if parser.tree == nil { parser.tree = &SyntaxTree { } } - if len(tokens) == 0 { return } parser.tokens = tokens parser.token = tokens[0] parser.tokenIndex = 0 + err = parser.parseMeta() + if err != nil { return } + + return +} + +// expect takes in a list of allowed token kinds, and returns an error if the +// current token isn't one of them. If the length of allowed is zero, this +// function will not return an error. +func (parser *ParsingOperation) expect (allowed []lexer.TokenKind) (err error) { + if len(allowed) == 0 { return } + + for _, kind := range allowed { + if parser.token.Is(kind) { return } + } + + err = file.NewError ( + parser.token.Location(), 1, + "unexpected token", file.ErrorKindError) + return +} + +// nextToken is the same as expect, but it advances to the next token first. +func (parser *ParsingOperation) nextToken (allowed []lexer.TokenKind) (err error) { + parser.tokenIndex ++ + if parser.tokenIndex >= len(parser.tokens) { return io.EOF } + parser.token = parser.tokens[parser.tokenIndex] + return }