From f23c3a234a089acafcd1d6fb0c8b35e115ea2b65 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 12 Aug 2022 16:22:51 -0500 Subject: [PATCH] Added metadata parser --- parser/meta.go | 38 +++++++++++++++++++++++++++++++++++++- parser/parser.go | 9 +++++---- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/parser/meta.go b/parser/meta.go index b61608d..0a37887 100644 --- a/parser/meta.go +++ b/parser/meta.go @@ -1,5 +1,41 @@ package parser +import "git.tebibyte.media/sashakoshka/arf/file" +import "git.tebibyte.media/sashakoshka/arf/lexer" + +// parseMeta parsese the metadata header at the top of an arf file. func (parser *ParsingOperation) parseMeta () (err error) { - + for { + err = parser.expect ( + lexer.TokenKindName, + lexer.TokenKindSeparator) + if err != nil { return } + + if parser.token.Is(lexer.TokenKindSeparator) { + err = parser.nextToken() + return + } + + field := parser.token.Value().(string) + + err = parser.nextToken(lexer.TokenKindString) + if err != nil { return } + value := parser.token.Value().(string) + + switch field { + case "author": + parser.tree.author = value + case "license": + parser.tree.license = value + case "require": + parser.tree.requires = append(parser.tree.requires, value) + default: + parser.token.NewError ( + "unrecognized metadata field: " + field, + file.ErrorKindError) + } + + err = parser.nextToken(lexer.TokenKindNewline) + if err != nil { return } + } } diff --git a/parser/parser.go b/parser/parser.go index fb54943..7ce8fcc 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -70,7 +70,7 @@ func (parser *ParsingOperation) parse (sourceFile *file.File) (err error) { // 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) { +func (parser *ParsingOperation) expect (allowed ...lexer.TokenKind) (err error) { if len(allowed) == 0 { return } for _, kind := range allowed { @@ -78,16 +78,17 @@ func (parser *ParsingOperation) expect (allowed []lexer.TokenKind) (err error) { } err = file.NewError ( - parser.token.Location(), 1, + parser.token.Location(), "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) { +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] - + + err = parser.expect(allowed...) return }