/
lexer.go
130 lines (109 loc) · 2.95 KB
/
lexer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package parser
import (
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
var (
iniLexer = lexer.MustSimple([]lexer.SimpleRule{
{`Ident`, `[a-zA-Z_][0-9a-zA-Z0-9_]*`},
{`HexTag`, `#([0-9a-f]+_?|_)`},
{`BinTag`, `\$[01]*_?`},
{"BuiltIn", `(#<=|#<|##|#)`},
{"Comparison", `(<=|<|>=|>|!=|=)`},
{"NUMBER", `[\d]+`},
{`Punct`, `[][={};<>^~:.?)(]`},
{"comment", `//[^\n]*`},
{"whitespace", `\s+`},
})
tlbParser = participle.MustBuild[TLB](
participle.Lexer(iniLexer),
)
)
type TLB struct {
Declarations []CombinatorDeclaration `@@*`
}
type CombinatorDeclaration struct {
Constructor Constructor `@@`
FieldDefinitions []FieldDefinition `@@*`
Equal string `"="`
Combinator Combinator `@@`
End string `";"`
}
type Constructor struct {
Name string `@Ident`
Prefix string `@(HexTag|BinTag)?`
}
type Combinator struct {
Name string `@Ident`
TypeExpressions []TypeExpression `@@*`
}
type FieldDefinition struct {
Implicit *CurlyExpression `"{" @@ "}"`
NamedField *NamedField `| @@`
Anon *ParenExpression `| @@`
CellRef *CellRef `| @@`
TypeRef *TypeRef `| @@`
}
func (fd FieldDefinition) IsEmpty() bool {
return fd.NamedField == nil && fd.CellRef == nil && fd.Implicit == nil && fd.Anon == nil && fd.TypeRef == nil
}
type NamedField struct {
Name string `@(Ident|"_")`
Sep string `":"`
Expression TypeExpression `@@`
}
type TypeRef struct {
Name string `@Ident`
}
type CurlyExpression struct {
CompareExpr *CompareOperatorExpr `@@`
TypeDefinition *TypeDefinition `| @@`
}
type TypeDefinition struct {
Implicit *ImplicitField `(@@`
Expression *TypeExpression `| @@)`
}
type ImplicitField struct {
Name string `@Ident`
Sep string `":"`
Type string `@("#"|"Type")`
}
type ParenExpression struct {
Name TypeExpression `"(" @@ `
Parameter []TypeExpression `@@* ")"`
}
type CellRef struct {
TypeExpression TypeExpression `"^" @@`
}
type Anon struct {
Values []FieldDefinition `"[" @@* "]"`
}
type Optional struct {
Modificator string `@Ident`
Dot string `"."`
Int int `@NUMBER`
QMark string `"?"`
Ident string `@Ident`
}
type TypeExpression struct {
Tilda string `@"~"?`
ParenExpression *ParenExpression `(@@`
AnonymousConstructor *Anon `| @@`
CellRef *CellRef `| @@`
Optional *Optional `| @@`
BuiltIn *string `| @BuiltIn`
Number *int `| @NUMBER`
NamedRef *string `| @Ident)`
}
type CompareOperatorExpr struct {
Left *RefInner `@@`
Operation string `@Comparison`
Right *RefInner `@@`
}
type RefInner struct {
Ident *string `@Ident`
Number *int `| @NUMBER`
}
func Parse(tlb string) (*TLB, error) {
return tlbParser.ParseString("", tlb)
}