From 69aaae8f14b043dd65163e4c2f0c126e5d4e178a Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 18 Aug 2022 23:38:32 -0400 Subject: [PATCH] Restructured type definitions to use a node tree --- parser/body.go | 2 +- parser/tree-tostring.go | 26 +++++++++++++++++--------- parser/tree.go | 13 ++++--------- parser/type.go | 9 +++++---- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/parser/body.go b/parser/body.go index 3f181d9..5def988 100644 --- a/parser/body.go +++ b/parser/body.go @@ -27,7 +27,7 @@ func (parser *ParsingOperation) parseBody () (err error) { parser.tree.typeSections = make(map[string] *TypeSection) } - parser.tree.typeSections[section.name] = section + parser.tree.typeSections[section.root.name] = section if err != nil { return } case "face": case "enum": diff --git a/parser/tree-tostring.go b/parser/tree-tostring.go index 894d720..ebdac69 100644 --- a/parser/tree-tostring.go +++ b/parser/tree-tostring.go @@ -253,22 +253,30 @@ func (section *DataSection) ToString (indent int) (output string) { } func (section *TypeSection) ToString (indent int) (output string) { - output += doIndent ( - indent, - "type ", - section.permission.ToString(), " ", - section.name, ":", - section.inherits.ToString()) + output += section.root.ToString(indent, true) + return +} + +func (node TypeNode) ToString (indent int, isRoot bool) (output string) { + doIndent(indent) + if isRoot { + output += "type " + } - if section.defaultValue.value == nil { - if len(section.members) > 0 { + output += node.permission.ToString() + " " + output += node.name + ":" + output += node.what.ToString() + + if node.defaultValue.value == nil { + if len(node.children) > 0 { // TODO: print out members } else { output += "\n" } } else { - output += " " + section.defaultValue.ToString(0, false) + output += " " + node.defaultValue.ToString(0, false) output += "\n" } + return } diff --git a/parser/tree.go b/parser/tree.go index 30b4be5..63f885d 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -161,24 +161,19 @@ type DataSection struct { value Argument } -// TypeMember represents member data -type TypeMember struct { +// TypeNode represents a part of a type. +type TypeNode struct { location file.Location name string what Type permission types.Permission defaultValue Argument + children map[string] TypeNode } // TypeSection represents a type definition. type TypeSection struct { location file.Location - name string - - inherits Type - permission types.Permission - defaultValue Argument - // this should be 1 dimensional for now. - members map[string] TypeMember + root TypeNode } diff --git a/parser/type.go b/parser/type.go index 7f1b3f5..bc76c92 100644 --- a/parser/type.go +++ b/parser/type.go @@ -4,6 +4,7 @@ import "git.tebibyte.media/sashakoshka/arf/types" import "git.tebibyte.media/sashakoshka/arf/lexer" // import "git.tebibyte.media/sashakoshka/arf/infoerr" +// parseTypeSection parses a type definition. func (parser *ParsingOperation) parseTypeSection () ( section *TypeSection, err error, @@ -16,19 +17,19 @@ func (parser *ParsingOperation) parseTypeSection () ( // get permission err = parser.nextToken(lexer.TokenKindPermission) if err != nil { return } - section.permission = parser.token.Value().(types.Permission) + section.root.permission = parser.token.Value().(types.Permission) // get name err = parser.nextToken(lexer.TokenKindName) if err != nil { return } - section.name = parser.token.Value().(string) + section.root.name = parser.token.Value().(string) // get inherited type err = parser.nextToken(lexer.TokenKindColon) if err != nil { return } err = parser.nextToken() if err != nil { return } - section.inherits, err = parser.parseType() + section.root.what, err = parser.parseType() if err != nil { return } if parser.token.Is(lexer.TokenKindNewline) { @@ -38,7 +39,7 @@ func (parser *ParsingOperation) parseTypeSection () ( // section.value, err = parser.parseInitializationValues(0) // if err != nil { return } } else { - section.defaultValue, err = parser.parseArgument() + section.root.defaultValue, err = parser.parseArgument() if err != nil { return } err = parser.expect(lexer.TokenKindNewline)