From b260d9424af3c6afedab81759b5f4771a64249e9 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 17 Sep 2022 00:58:08 -0400 Subject: [PATCH] Introducing new enum syntax --- parser/enum.go | 65 +++++++++++++++++++++++++++++--------- parser/enum_test.go | 46 ++++++++++++++------------- parser/tree-tostring.go | 12 +++---- parser/type-notation.go | 20 ++++++++---- tests/parser/enum/main.arf | 44 +++++++++++++++----------- 5 files changed, 117 insertions(+), 70 deletions(-) diff --git a/parser/enum.go b/parser/enum.go index 92f01ec..06e110b 100644 --- a/parser/enum.go +++ b/parser/enum.go @@ -61,23 +61,13 @@ 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 } + err = parser.nextToken(lexer.TokenKindMinus) + if err != nil { return } - member := EnumMember { } - - // get name - err = parser.nextToken(lexer.TokenKindName) + var member EnumMember + member, err = parser.parseEnumMember() + into.members = append(into.members, member) if err != nil { return } - member.location = parser.token.Location() - member.name = parser.token.Value().(string) - err = parser.nextToken() - if err != nil { return } - - // parse default value - if !parser.token.Is(lexer.TokenKindNewline) { - member.value, err = parser.parseArgument() - into.members = append(into.members, member) - if err != nil { return } - } err = parser.expect(lexer.TokenKindNewline) if err != nil { return } @@ -85,3 +75,48 @@ func (parser *ParsingOperation) parseEnumMembers ( if err != nil { return } } } + +// parseEnumMember parses a single enum member. Indenttion level is assumed. +func (parser *ParsingOperation) parseEnumMember () ( + member EnumMember, + err error, +) { + err = parser.expect(lexer.TokenKindMinus) + if err != nil { return } + + // get name + err = parser.nextToken(lexer.TokenKindName) + if err != nil { return } + member.location = parser.token.Location() + member.name = parser.token.Value().(string) + + // see if value exists + err = parser.nextToken ( + lexer.TokenKindColon, + lexer.TokenKindNewline) + if err != nil { return } + + if parser.token.Is(lexer.TokenKindColon) { + err = parser.nextToken() + if err != nil { return } + err = parser.skipWhitespace() + if err != nil { return } + err = parser.expect ( + lexer.TokenKindLessThan, + lexer.TokenKindGreaterThan) + if err != nil { return } + + if parser.token.Is(lexer.TokenKindLessThan) { + // parse value + member.value, err = parser.parseBasicDefaultValue() + if err != nil { return } + + } else if parser.token.Is(lexer.TokenKindLParen) { + // parse default values + member.value, err = parser.parseObjectDefaultValue() + if err != nil { return } + } + } + + return +} diff --git a/parser/enum_test.go b/parser/enum_test.go index bdc57e5..582084e 100644 --- a/parser/enum_test.go +++ b/parser/enum_test.go @@ -7,32 +7,34 @@ func TestEnum (test *testing.T) { `:arf --- enum ro AffrontToGod:Int:4 - bird0 - 28394 - 9328 + - bird0: + <28394 9328 + 398 9> + - bird1: + <23 932832 398 - 9 - bird1 - 23 - 932832 - 398 - 2349 - bird2 - 1 + 2349> + - bird2: + <1 2 3 - 4 + 4> enum ro NamedColor:U32 - red 16711680 - green 65280 - blue 255 + - red: <0xFF0000> + - green: <0x00FF00> + - blue: <0x0000FF> +enum ro ThisIsTerrible:Obj:(.rw x:Int .rw y:Int) + - up: (.x: 0 .y: -1) + - down: (.x: 0 .y: 1) + - left: (.x: -1 .y: 0) + - right: (.x: 1 .y: 0) enum ro Weekday:Int - sunday - monday - tuesday - wednesday - thursday - friday - saturday + - sunday + - monday + - tuesday + - wednesday + - thursday + - friday + - saturday `, test) } diff --git a/parser/tree-tostring.go b/parser/tree-tostring.go index 90e422c..f0cf4b4 100644 --- a/parser/tree-tostring.go +++ b/parser/tree-tostring.go @@ -354,24 +354,22 @@ func (section EnumSection) ToString (indent int) (output string) { "enum ", section.permission.ToString(), " ", section.name, ":", - section.what.ToString(indent, true), "\n") + section.what.ToString(indent + 1, true), "\n") for _, member := range section.members { - output += doIndent(indent + 1, member.name) + output += doIndent(indent + 1, "- ", member.name) isComplexInitialization := member.value.kind == ArgumentKindObjectDefaultValues || member.value.kind == ArgumentKindArrayDefaultValues - if member.value.value == nil { - output += "\n" - } else if isComplexInitialization { + if isComplexInitialization { output += "\n" output += member.value.ToString(indent + 2, true) - } else { + } else if member.value.kind != ArgumentKindNil { output += " " + member.value.ToString(0, false) - output += "\n" } + output += "\n" } return } diff --git a/parser/type-notation.go b/parser/type-notation.go index d428520..1c7b50c 100644 --- a/parser/type-notation.go +++ b/parser/type-notation.go @@ -71,6 +71,8 @@ func (parser *ParsingOperation) parseType () (what Type, err error) { } else if parser.token.Is(lexer.TokenKindUInt) { // parse fixed array length what.length = parser.token.Value().(uint64) + err = parser.nextToken() + if err != nil { return } } else if parser.token.Is(lexer.TokenKindLessThan) { // parse default value @@ -84,9 +86,6 @@ func (parser *ParsingOperation) parseType () (what Type, err error) { err = parser.parseObjectDefaultValueAndMembers() if err != nil { return } } - - err = parser.nextToken() - if err != nil { return } } return @@ -124,6 +123,10 @@ func (parser *ParsingOperation) parseBasicDefaultValue () ( if err != nil { return } attributes = append(attributes, value) } + + err = parser.nextToken() + if err != nil { return } + return } @@ -186,6 +189,9 @@ func (parser *ParsingOperation) parseObjectDefaultValueAndMembers () ( } } + err = parser.nextToken() + if err != nil { return } + return } @@ -228,6 +234,9 @@ func (parser *ParsingOperation) parseObjectDefaultValue () ( attributes[memberName] = memberValue } + err = parser.nextToken() + if err != nil { return } + return } @@ -262,9 +271,6 @@ func (parser *ParsingOperation) parseObjectInheritedMember () ( value, err = parser.parseObjectDefaultValue() if err != nil { return } } - - err = parser.nextToken() - if err != nil { return } return } @@ -296,7 +302,7 @@ func (parser *ParsingOperation) parseObjectNewMember () ( member.what, err = parser.parseType() if err != nil { return } - // TODO: get bit width + // get bit width if parser.token.Is(lexer.TokenKindBinaryAnd) { err = parser.nextToken(lexer.TokenKindUInt) if err != nil { return } diff --git a/tests/parser/enum/main.arf b/tests/parser/enum/main.arf index 6d2788e..90ab9be 100644 --- a/tests/parser/enum/main.arf +++ b/tests/parser/enum/main.arf @@ -2,29 +2,35 @@ --- enum ro Weekday:Int - sunday - monday - tuesday - wednesday - thursday - friday - saturday + - sunday + - monday + - tuesday + - wednesday + - thursday + - friday + - saturday enum ro NamedColor:U32 - red 0xFF0000 - green 0x00FF00 - blue 0x0000FF + - red: <0xFF0000> + - green: <0x00FF00> + - blue: <0x0000FF> enum ro AffrontToGod:Int:4 - bird0 - 28394 9328 - 398 9 - bird1 - 23 932832 + - bird0: + <28394 9328 + 398 9> + - bird1: + <23 932832 398 - 2349 - bird2 - 1 + 2349> + - bird2: + <1 2 3 - 4 + 4> + +enum ro ThisIsTerrible:Obj:(.rw x:Int .rw y:Int) + - up: (.x: 0 .y: -1) + - down: (.x: 0 .y: 1) + - left: (.x: -1 .y: 0) + - right: (.x: 1 .y: 0)