Introducing new enum syntax

This commit is contained in:
Sasha Koshka 2022-09-17 00:58:08 -04:00
parent 2f26e63354
commit b260d9424a
5 changed files with 117 additions and 70 deletions

View File

@ -61,23 +61,13 @@ func (parser *ParsingOperation) parseEnumMembers (
// if we've left the block, stop parsing // if we've left the block, stop parsing
if !parser.token.Is(lexer.TokenKindIndent) { return } if !parser.token.Is(lexer.TokenKindIndent) { return }
if parser.token.Value().(int) != 1 { return } if parser.token.Value().(int) != 1 { return }
err = parser.nextToken(lexer.TokenKindMinus)
member := EnumMember { }
// get name
err = parser.nextToken(lexer.TokenKindName)
if err != nil { return }
member.location = parser.token.Location()
member.name = parser.token.Value().(string)
err = parser.nextToken()
if err != nil { return } if err != nil { return }
// parse default value var member EnumMember
if !parser.token.Is(lexer.TokenKindNewline) { member, err = parser.parseEnumMember()
member.value, err = parser.parseArgument()
into.members = append(into.members, member) into.members = append(into.members, member)
if err != nil { return } if err != nil { return }
}
err = parser.expect(lexer.TokenKindNewline) err = parser.expect(lexer.TokenKindNewline)
if err != nil { return } if err != nil { return }
@ -85,3 +75,48 @@ func (parser *ParsingOperation) parseEnumMembers (
if err != nil { return } 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
}

View File

@ -7,32 +7,34 @@ func TestEnum (test *testing.T) {
`:arf `:arf
--- ---
enum ro AffrontToGod:Int:4 enum ro AffrontToGod:Int:4
bird0 - bird0:
28394 <28394 9328
9328 398 9>
- bird1:
<23 932832
398 398
9 2349>
bird1 - bird2:
23 <1
932832
398
2349
bird2
1
2 2
3 3
4 4>
enum ro NamedColor:U32 enum ro NamedColor:U32
red 16711680 - red: <0xFF0000>
green 65280 - green: <0x00FF00>
blue 255 - 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 enum ro Weekday:Int
sunday - sunday
monday - monday
tuesday - tuesday
wednesday - wednesday
thursday - thursday
friday - friday
saturday - saturday
`, test) `, test)
} }

View File

@ -354,24 +354,22 @@ func (section EnumSection) ToString (indent int) (output string) {
"enum ", "enum ",
section.permission.ToString(), " ", section.permission.ToString(), " ",
section.name, ":", section.name, ":",
section.what.ToString(indent, true), "\n") section.what.ToString(indent + 1, true), "\n")
for _, member := range section.members { for _, member := range section.members {
output += doIndent(indent + 1, member.name) output += doIndent(indent + 1, "- ", member.name)
isComplexInitialization := isComplexInitialization :=
member.value.kind == ArgumentKindObjectDefaultValues || member.value.kind == ArgumentKindObjectDefaultValues ||
member.value.kind == ArgumentKindArrayDefaultValues member.value.kind == ArgumentKindArrayDefaultValues
if member.value.value == nil { if isComplexInitialization {
output += "\n"
} else if isComplexInitialization {
output += "\n" output += "\n"
output += member.value.ToString(indent + 2, true) output += member.value.ToString(indent + 2, true)
} else { } else if member.value.kind != ArgumentKindNil {
output += " " + member.value.ToString(0, false) output += " " + member.value.ToString(0, false)
output += "\n"
} }
output += "\n"
} }
return return
} }

View File

@ -71,6 +71,8 @@ func (parser *ParsingOperation) parseType () (what Type, err error) {
} else if parser.token.Is(lexer.TokenKindUInt) { } else if parser.token.Is(lexer.TokenKindUInt) {
// parse fixed array length // parse fixed array length
what.length = parser.token.Value().(uint64) what.length = parser.token.Value().(uint64)
err = parser.nextToken()
if err != nil { return }
} else if parser.token.Is(lexer.TokenKindLessThan) { } else if parser.token.Is(lexer.TokenKindLessThan) {
// parse default value // parse default value
@ -84,9 +86,6 @@ func (parser *ParsingOperation) parseType () (what Type, err error) {
err = parser.parseObjectDefaultValueAndMembers() err = parser.parseObjectDefaultValueAndMembers()
if err != nil { return } if err != nil { return }
} }
err = parser.nextToken()
if err != nil { return }
} }
return return
@ -124,6 +123,10 @@ func (parser *ParsingOperation) parseBasicDefaultValue () (
if err != nil { return } if err != nil { return }
attributes = append(attributes, value) attributes = append(attributes, value)
} }
err = parser.nextToken()
if err != nil { return }
return return
} }
@ -186,6 +189,9 @@ func (parser *ParsingOperation) parseObjectDefaultValueAndMembers () (
} }
} }
err = parser.nextToken()
if err != nil { return }
return return
} }
@ -228,6 +234,9 @@ func (parser *ParsingOperation) parseObjectDefaultValue () (
attributes[memberName] = memberValue attributes[memberName] = memberValue
} }
err = parser.nextToken()
if err != nil { return }
return return
} }
@ -263,9 +272,6 @@ func (parser *ParsingOperation) parseObjectInheritedMember () (
if err != nil { return } if err != nil { return }
} }
err = parser.nextToken()
if err != nil { return }
return return
} }
@ -296,7 +302,7 @@ func (parser *ParsingOperation) parseObjectNewMember () (
member.what, err = parser.parseType() member.what, err = parser.parseType()
if err != nil { return } if err != nil { return }
// TODO: get bit width // get bit width
if parser.token.Is(lexer.TokenKindBinaryAnd) { if parser.token.Is(lexer.TokenKindBinaryAnd) {
err = parser.nextToken(lexer.TokenKindUInt) err = parser.nextToken(lexer.TokenKindUInt)
if err != nil { return } if err != nil { return }

View File

@ -2,29 +2,35 @@
--- ---
enum ro Weekday:Int enum ro Weekday:Int
sunday - sunday
monday - monday
tuesday - tuesday
wednesday - wednesday
thursday - thursday
friday - friday
saturday - saturday
enum ro NamedColor:U32 enum ro NamedColor:U32
red 0xFF0000 - red: <0xFF0000>
green 0x00FF00 - green: <0x00FF00>
blue 0x0000FF - blue: <0x0000FF>
enum ro AffrontToGod:Int:4 enum ro AffrontToGod:Int:4
bird0 - bird0:
28394 9328 <28394 9328
398 9 398 9>
bird1 - bird1:
23 932832 <23 932832
398 398
2349 2349>
bird2 - bird2:
1 <1
2 2
3 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)