The section kind specific maps are gone

I've REPLACED IT with the unified sections map. Interfaces, baby!
This commit is contained in:
Sasha Koshka 2022-09-04 19:30:59 -04:00
parent 9269161138
commit f3c72f8f30
5 changed files with 53 additions and 94 deletions

View File

@ -11,73 +11,43 @@ func (parser *ParsingOperation) parseBody () (err error) {
err = parser.expect(lexer.TokenKindName) err = parser.expect(lexer.TokenKindName)
if err != nil { return } if err != nil { return }
sectionType := parser.token.Value().(string) sectionType := parser.token.Value().(string)
switch sectionType { switch sectionType {
case "data": case "data":
var section *DataSection section, parseErr := parser.parseDataSection()
section, err = parser.parseDataSection() err = parser.tree.addSection(section)
if parser.tree.dataSections == nil { if err != nil { return }
parser.tree.dataSections = if parseErr != nil { return }
make(map[string] *DataSection)
}
parser.tree.dataSections[section.name] = section
parser.tree.sections[section.name] = section
if err != nil { return }
case "type": case "type":
var section *TypeSection section, parseErr := parser.parseTypeSection()
section, err = parser.parseTypeSection() err = parser.tree.addSection(section)
if parser.tree.typeSections == nil { if err != nil { return }
parser.tree.typeSections = if parseErr != nil { return }
make(map[string] *TypeSection)
}
parser.tree.typeSections[section.name] = section
parser.tree.sections[section.name] = section
if err != nil { return }
case "objt": case "objt":
var section *ObjtSection section, parseErr := parser.parseObjtSection()
section, err = parser.parseObjtSection() err = parser.tree.addSection(section)
if parser.tree.objtSections == nil { if err != nil { return }
parser.tree.objtSections = if parseErr != nil { return }
make(map[string] *ObjtSection)
}
parser.tree.objtSections[section.name] = section
parser.tree.sections[section.name] = section
if err != nil { return }
case "face": case "face":
var section *FaceSection section, parseErr := parser.parseFaceSection()
section, err = parser.parseFaceSection() err = parser.tree.addSection(section)
if parser.tree.faceSections == nil { if err != nil { return }
parser.tree.faceSections = if parseErr != nil { return }
make(map[string] *FaceSection)
}
parser.tree.faceSections[section.name] = section
parser.tree.sections[section.name] = section
if err != nil { return }
case "enum": case "enum":
var section *EnumSection section, parseErr := parser.parseEnumSection()
section, err = parser.parseEnumSection() err = parser.tree.addSection(section)
if parser.tree.enumSections == nil { if err != nil { return }
parser.tree.enumSections = if parseErr != nil { return }
make(map[string] *EnumSection)
}
parser.tree.enumSections[section.name] = section
parser.tree.sections[section.name] = section
if err != nil { return }
case "func": case "func":
var section *FuncSection section, parseErr := parser.parseFuncSection()
section, err = parser.parseFuncSection() err = parser.tree.addSection(section)
if parser.tree.funcSections == nil { if err != nil { return }
parser.tree.funcSections = if parseErr != nil { return }
make(map[string] *FuncSection)
}
parser.tree.funcSections[section.name] = section
parser.tree.sections[section.name] = section
if err != nil { return }
default: default:
err = parser.token.NewError ( err = parser.token.NewError (
@ -87,3 +57,18 @@ func (parser *ParsingOperation) parseBody () (err error) {
} }
} }
} }
// addSection adds a section to the tree, ensuring it has a unique name within
// the module.
func (tree *SyntaxTree) addSection (section Section) (err error) {
_, exists := tree.sections[section.Name()]
if exists {
err = section.NewError (
"cannot have multiple sections with the same name",
infoerr.ErrorKindError)
return
}
tree.sections[section.Name()] = section
return
}

View File

@ -20,7 +20,7 @@ func (trait locatable) NewError (
message string, message string,
kind infoerr.ErrorKind, kind infoerr.ErrorKind,
) ( ) (
err infoerr.Error, err error,
) { ) {
err = infoerr.NewError(trait.location, message, kind) err = infoerr.NewError(trait.location, message, kind)
return return

View File

@ -20,7 +20,12 @@ type ParsingOperation struct {
// Parse reads the files located in the module specified by modulePath, and // Parse reads the files located in the module specified by modulePath, and
// converts them into an abstract syntax tree. // converts them into an abstract syntax tree.
func Parse (modulePath string) (tree *SyntaxTree, err error) { func Parse (modulePath string) (tree *SyntaxTree, err error) {
parser := ParsingOperation { modulePath: modulePath } parser := ParsingOperation {
modulePath: modulePath,
tree: &SyntaxTree {
sections: make(map[string] Section),
},
}
if parser.modulePath[len(parser.modulePath) - 1] != '/' { if parser.modulePath[len(parser.modulePath) - 1] != '/' {
parser.modulePath += "/" parser.modulePath += "/"
@ -54,9 +59,6 @@ func (parser *ParsingOperation) parse (sourceFile *file.File) (err error) {
if err != nil { return } if err != nil { return }
// reset the parser // reset the parser
if parser.tree == nil {
parser.tree = &SyntaxTree { }
}
if len(tokens) == 0 { return } if len(tokens) == 0 { return }
parser.tokens = tokens parser.tokens = tokens
parser.token = tokens[0] parser.token = tokens[0]

View File

@ -47,35 +47,11 @@ func (tree *SyntaxTree) ToString (indent int) (output string) {
output += doIndent(indent, "---\n") output += doIndent(indent, "---\n")
typeSectionKeys := sortMapKeysAlphabetically(tree.typeSections) sectionKeys := sortMapKeysAlphabetically(tree.sections)
for _, name := range typeSectionKeys { for _, name := range sectionKeys {
output += tree.typeSections[name].ToString(indent) output += tree.sections[name].ToString(indent)
} }
objtSectionKeys := sortMapKeysAlphabetically(tree.objtSections)
for _, name := range objtSectionKeys {
output += tree.objtSections[name].ToString(indent)
}
enumSectionKeys := sortMapKeysAlphabetically(tree.enumSections)
for _, name := range enumSectionKeys {
output += tree.enumSections[name].ToString(indent)
}
faceSectionKeys := sortMapKeysAlphabetically(tree.faceSections)
for _, name := range faceSectionKeys {
output += tree.faceSections[name].ToString(indent)
}
dataSectionKeys := sortMapKeysAlphabetically(tree.dataSections)
for _, name := range dataSectionKeys {
output += tree.dataSections[name].ToString(indent)
}
funcSectionKeys := sortMapKeysAlphabetically(tree.funcSections)
for _, name := range funcSectionKeys {
output += tree.funcSections[name].ToString(indent)
}
return return
} }

View File

@ -2,6 +2,7 @@ package parser
import "git.tebibyte.media/arf/arf/file" import "git.tebibyte.media/arf/arf/file"
import "git.tebibyte.media/arf/arf/types" import "git.tebibyte.media/arf/arf/types"
import "git.tebibyte.media/arf/arf/infoerr"
// SyntaxTree represents an abstract syntax tree. It covers an entire module. It // SyntaxTree represents an abstract syntax tree. It covers an entire module. It
// can be expected to be syntactically correct, but it might not be semantically // can be expected to be syntactically correct, but it might not be semantically
@ -12,13 +13,6 @@ type SyntaxTree struct {
requires []string requires []string
sections map[string] Section sections map[string] Section
typeSections map[string] *TypeSection
objtSections map[string] *ObjtSection
enumSections map[string] *EnumSection
faceSections map[string] *FaceSection
dataSections map[string] *DataSection
funcSections map[string] *FuncSection
} }
// SectionKind differentiates Section interfaces. // SectionKind differentiates Section interfaces.
@ -40,6 +34,8 @@ type Section interface {
Kind () (kind SectionKind) Kind () (kind SectionKind)
Permission () (permission types.Permission) Permission () (permission types.Permission)
Name () (name string) Name () (name string)
NewError (message string, kind infoerr.ErrorKind) (err error)
ToString (indent int) (output string)
} }
// Identifier represents a chain of arguments separated by a dot. // Identifier represents a chain of arguments separated by a dot.