From 0d366964ca30aa004f522b246bc0727e68ec906a Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 24 Aug 2022 01:16:44 -0400 Subject: [PATCH] Enum members are now ordered --- parser/enum.go | 19 +++++++++---------- parser/parser_test.go | 12 ++++++------ parser/tree-tostring.go | 16 +++++++--------- parser/tree.go | 11 +++++++++-- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/parser/enum.go b/parser/enum.go index 139cd9f..f302242 100644 --- a/parser/enum.go +++ b/parser/enum.go @@ -11,10 +11,7 @@ func (parser *ParsingOperation) parseEnumSection () ( err = parser.expect(lexer.TokenKindName) if err != nil { return } - section = &EnumSection { - location: parser.token.Location(), - members: make(map[string] Argument), - } + section = &EnumSection { location: parser.token.Location() } // get permission err = parser.nextToken(lexer.TokenKindPermission) @@ -63,26 +60,28 @@ func (parser *ParsingOperation) parseEnumMembers ( // if we've left the block, stop parsing if !parser.token.Is(lexer.TokenKindIndent) { return } if parser.token.Value().(int) != 1 { return } + + member := EnumMember { } // get name err = parser.nextToken(lexer.TokenKindName) if err != nil { return } - name := parser.token.Value().(string) + member.location = parser.token.Location() + member.name = parser.token.Value().(string) err = parser.nextToken() if err != nil { return } // parse default value - var argument Argument if parser.token.Is(lexer.TokenKindNewline) { err = parser.nextToken() if err != nil { return } - argument, err = parser.parseInitializationValues(1) - into.members[name] = argument + member.value, err = parser.parseInitializationValues(1) + into.members = append(into.members, member) if err != nil { return } } else { - argument, err = parser.parseArgument() - into.members[name] = argument + member.value, err = parser.parseArgument() + into.members = append(into.members, member) if err != nil { return } err = parser.expect(lexer.TokenKindNewline) diff --git a/parser/parser_test.go b/parser/parser_test.go index cf25d54..133fbd2 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -179,17 +179,17 @@ enum ro AffrontToGod:{Int 4} 3 4 enum ro NamedColor:U32 - blue 255 - green 65280 red 16711680 + green 65280 + blue 255 enum ro Weekday:Int - friday - monday - saturday sunday - thursday + monday tuesday wednesday + thursday + friday + saturday `, test) } diff --git a/parser/tree-tostring.go b/parser/tree-tostring.go index df8a9b1..adbfcbd 100644 --- a/parser/tree-tostring.go +++ b/parser/tree-tostring.go @@ -337,22 +337,20 @@ func (section *EnumSection) ToString (indent int) (output string) { section.name, ":", section.what.ToString(), "\n") - for _, name := range sortMapKeysAlphabetically(section.members) { - output += doIndent(indent + 1, name) - - defaultValue := section.members[name] + for _, member := range section.members { + output += doIndent(indent + 1, member.name) isComplexInitialization := - defaultValue.kind == ArgumentKindObjectInitializationValues || - defaultValue.kind == ArgumentKindArrayInitializationValues + member.value.kind == ArgumentKindObjectInitializationValues || + member.value.kind == ArgumentKindArrayInitializationValues - if defaultValue.value == nil { + if member.value.value == nil { output += "\n" } else if isComplexInitialization { output += "\n" - output += defaultValue.ToString(indent + 2, true) + output += member.value.ToString(indent + 2, true) } else { - output += " " + defaultValue.ToString(0, false) + output += " " + member.value.ToString(0, false) output += "\n" } } diff --git a/parser/tree.go b/parser/tree.go index f6e6e55..a0394ed 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -191,9 +191,17 @@ type ObjtSection struct { inherits Type permission types.Permission + // TODO: order matters here we need to store these in an array + // TODO: add bitfield support (:n) members map[string] ObjtMember } +type EnumMember struct { + location file.Location + name string + value Argument +} + // EnumSection represents an enumerated type section. type EnumSection struct { location file.Location @@ -201,8 +209,7 @@ type EnumSection struct { what Type permission types.Permission - // TODO: order matters here we need to store these in an array - members map[string] Argument + members []EnumMember } // FaceBehavior represents a behavior of an interface section.