From 6fbda3430032f982580ed7521caccb9287b31051 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 21 Aug 2022 02:42:25 -0400 Subject: [PATCH] Add base enum parsing method --- parser/body.go | 8 ++++++++ parser/enum.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 parser/enum.go diff --git a/parser/body.go b/parser/body.go index 82b2635..a5bf767 100644 --- a/parser/body.go +++ b/parser/body.go @@ -40,6 +40,14 @@ func (parser *ParsingOperation) parseBody () (err error) { if err != nil { return } case "face": case "enum": + var section *EnumSection + section, err = parser.parseEnumSection() + if parser.tree.enumSections == nil { + parser.tree.enumSections = + make(map[string] *EnumSection) + } + parser.tree.enumSections[section.name] = section + if err != nil { return } case "func": default: err = parser.token.NewError ( diff --git a/parser/enum.go b/parser/enum.go new file mode 100644 index 0000000..0618df8 --- /dev/null +++ b/parser/enum.go @@ -0,0 +1,52 @@ +package parser + +import "git.tebibyte.media/sashakoshka/arf/types" +import "git.tebibyte.media/sashakoshka/arf/lexer" +import "git.tebibyte.media/sashakoshka/arf/infoerr" + +func (parser *ParsingOperation) parseEnumSection () ( + section *EnumSection, + err error, +) { + err = parser.expect(lexer.TokenKindName) + if err != nil { return } + + section = &EnumSection { + location: parser.token.Location(), + members: make(map[string] Argument), + } + + // get permission + err = parser.nextToken(lexer.TokenKindPermission) + if err != nil { return } + section.permission = parser.token.Value().(types.Permission) + + // get name + err = parser.nextToken(lexer.TokenKindName) + if err != nil { return } + section.name = parser.token.Value().(string) + + // parse inherited type + err = parser.nextToken(lexer.TokenKindColon) + if err != nil { return } + err = parser.nextToken() + if err != nil { return } + section.what, err = parser.parseType() + if err != nil { return } + err = parser.expect(lexer.TokenKindNewline) + if err != nil { return } + err = parser.nextToken() + if err != nil { return } + + // parse members + err = parser.parseEnumMembers(section) + if err != nil { return } + + if len(section.members) == 0 { + infoerr.NewError ( + section.location, + "defining an enum with no members", + infoerr.ErrorKindWarn).Print() + } + return +}