From 9269161138c658272237f982e4f720682050a955 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 4 Sep 2022 17:13:49 -0400 Subject: [PATCH] Tree returns a section interface when given a name --- parser/accessors.go | 7 +++++++ parser/body.go | 14 +++++++++++++- parser/tree.go | 4 +++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/parser/accessors.go b/parser/accessors.go index 37633cb..92c72f6 100644 --- a/parser/accessors.go +++ b/parser/accessors.go @@ -1,5 +1,12 @@ package parser +// LookupSection looks returns the section under the give name. If the section +// does not exist, nil is returned. +func (tree *SyntaxTree) LookupSection (name string) (section Section) { + section = tree.sections[name] + return +} + // Kind returns the section's kind (SectionKindType). func (section TypeSection) Kind () (kind SectionKind) { kind = SectionKindType diff --git a/parser/body.go b/parser/body.go index 8ca2722..f7f338a 100644 --- a/parser/body.go +++ b/parser/body.go @@ -10,8 +10,8 @@ func (parser *ParsingOperation) parseBody () (err error) { for { err = parser.expect(lexer.TokenKindName) if err != nil { return } - sectionType := parser.token.Value().(string) + switch sectionType { case "data": var section *DataSection @@ -21,7 +21,9 @@ func (parser *ParsingOperation) parseBody () (err error) { make(map[string] *DataSection) } parser.tree.dataSections[section.name] = section + parser.tree.sections[section.name] = section if err != nil { return } + case "type": var section *TypeSection section, err = parser.parseTypeSection() @@ -30,7 +32,9 @@ func (parser *ParsingOperation) parseBody () (err error) { make(map[string] *TypeSection) } parser.tree.typeSections[section.name] = section + parser.tree.sections[section.name] = section if err != nil { return } + case "objt": var section *ObjtSection section, err = parser.parseObjtSection() @@ -39,7 +43,9 @@ func (parser *ParsingOperation) parseBody () (err error) { make(map[string] *ObjtSection) } parser.tree.objtSections[section.name] = section + parser.tree.sections[section.name] = section if err != nil { return } + case "face": var section *FaceSection section, err = parser.parseFaceSection() @@ -48,7 +54,9 @@ func (parser *ParsingOperation) parseBody () (err error) { make(map[string] *FaceSection) } parser.tree.faceSections[section.name] = section + parser.tree.sections[section.name] = section if err != nil { return } + case "enum": var section *EnumSection section, err = parser.parseEnumSection() @@ -57,7 +65,9 @@ func (parser *ParsingOperation) parseBody () (err error) { make(map[string] *EnumSection) } parser.tree.enumSections[section.name] = section + parser.tree.sections[section.name] = section if err != nil { return } + case "func": var section *FuncSection section, err = parser.parseFuncSection() @@ -66,7 +76,9 @@ func (parser *ParsingOperation) parseBody () (err error) { make(map[string] *FuncSection) } parser.tree.funcSections[section.name] = section + parser.tree.sections[section.name] = section if err != nil { return } + default: err = parser.token.NewError ( "unknown section type \"" + sectionType + "\"", diff --git a/parser/tree.go b/parser/tree.go index 707f201..250bd8c 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -10,7 +10,9 @@ type SyntaxTree struct { license string author string - requires []string + requires []string + sections map[string] Section + typeSections map[string] *TypeSection objtSections map[string] *ObjtSection enumSections map[string] *EnumSection