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 !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 }
member.location = parser.token.Location()
member.name = parser.token.Value().(string)
err = parser.nextToken()
err = parser.nextToken(lexer.TokenKindMinus)
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 }
}
var member EnumMember
member, err = parser.parseEnumMember()
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
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}
@ -263,9 +272,6 @@ func (parser *ParsingOperation) parseObjectInheritedMember () (
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 }

View File

@ -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)