-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse.go
More file actions
257 lines (200 loc) · 5.5 KB
/
parse.go
File metadata and controls
257 lines (200 loc) · 5.5 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package golfcart
import (
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
"github.com/alecthomas/participle/v2/lexer/stateful"
)
type ExpressionList struct {
Pos lexer.Position
Expressions []*Expression `@@*`
}
type Expression struct {
Pos lexer.Position
Assignment *Assignment `@@`
NativeFunctionValue *NativeFunctionValue
}
type Assignment struct {
Pos lexer.Position
LogicAnd *LogicAnd `@@`
Op string `( @"="`
Next *LogicAnd ` @@ )?`
}
type LogicAnd struct {
Pos lexer.Position
LogicOr *LogicOr `@@`
Op string `( @( "and" )`
Next *LogicAnd ` @@ )?`
}
type LogicOr struct {
Pos lexer.Position
Equality *Equality `@@`
Op string `( @( "or" )`
Next *LogicOr ` @@ )?`
}
type Equality struct {
Pos lexer.Position
Comparison *Comparison `@@`
Op string `( @( "!" "=" | "=" "=" )`
Next *Equality ` @@ )?`
}
type Comparison struct {
Pos lexer.Position
Addition *Addition `@@`
Op string `( @( ">" "=" | ">" | "<" "=" | "<" )`
Next *Comparison ` @@ )?`
}
type Addition struct {
Pos lexer.Position
Multiplication *Multiplication `@@`
Op string `( @( "-" | "+" )`
Next *Addition ` @@ )?`
}
type Multiplication struct {
Pos lexer.Position
Unary *Unary `@@`
Op string `( @( "/" | "*" | "%")`
Next *Multiplication ` @@ )?`
}
type Unary struct {
Pos lexer.Position
Op string `( @( "!" | "-" )`
Unary *Unary ` @@ )`
Primary *Primary `| @@`
}
type Primary struct {
Pos lexer.Position
If *If `@@`
DataLiteral *DataLiteral `| @@`
SubExpression *Expression `| "(" @@ ")"`
Call *Call `| @@`
// TODO: `for {}` is a parser error
ForKeyValue *ForKeyValue `| @@`
ForValue *ForValue `| @@`
For *For `| @@`
ForWhile *ForWhile `| @@`
Return *Return `| @@`
Break *Break `| @@`
Continue *Continue `| @@`
Number *float64 `| @Float | @Int`
Str *string `| @String`
True *bool `| @"true"`
False *bool `| @"false"`
Nil *bool `| @"nil"`
Ident *string `| @Ident`
}
type DataLiteral struct {
FunctionLiteral *FunctionLiteral `@@`
ListLiteral *ListLiteral `| @@`
DictLiteral *DictLiteral `| @@`
}
type If struct {
Pos lexer.Position
Condition *Expression `"if" @@`
IfBody []*Expression `"{" @@* "}"`
ElseIf *ElseIf `@@*`
ElseBody []*Expression `( "else" "{" @@* "}" )?`
}
type ElseIf struct {
Condition *Expression `"else" "if" @@`
IfBody []*Expression `"{" @@* "}"`
Next *ElseIf `@@*`
}
type FunctionLiteral struct {
Pos lexer.Position
Parameters []string `( "(" ( @Ident ( "," @Ident )* )? ")" | @Ident )`
Body []*Expression `"=" ">" ( "{" @@* "}" | @@ )`
}
type ListLiteral struct {
Pos lexer.Position
Expressions *[]Expression `"[" ( @@ ( "," @@ )* )? "]"`
}
type DictLiteral struct {
Pos lexer.Position
DictEntry *[]DictEntry `"{" ( @@ ("," @@)* ","? )? "}"`
}
type DictEntry struct {
Pos lexer.Position
Ident *string `( @Ident`
Key *Expression `| @@ ) ":" `
Value *Expression `@@`
}
type Call struct {
Pos lexer.Position
Ident *string `( @Ident`
SubExpression *Expression `| "(" @@ ")" )`
CallChain *CallChain `@@`
}
type CallChain struct {
Parameters *[]Expression `( "(" ( @@ ( "," @@ )* )? ")" `
Access *string ` | "." @Ident`
ComputedAccess *Expression ` | "[" @@ "]" )`
Next *CallChain `@@?`
}
type Break struct {
Pos lexer.Position
Break *string `"break"`
}
type Continue struct {
Pos lexer.Position
Continue *string `"continue"`
}
type Return struct {
Pos lexer.Position
Return *string `( "return" `
Expression *Expression `@@ )`
}
type For struct {
Pos lexer.Position
Init []*Assignment `"for" ( @@ ";"`
Condition *Expression `@@ ";"`
Post *Expression `@@`
Body []*Expression `"{" @@* "}" )`
}
type ForValue struct {
Pos lexer.Position
Value *string `( "for" @Ident "in"`
Collection *string `( @Ident`
CollectionExpression *Expression `| @@) `
Body []*Expression `"{" @@* "}" )`
}
type ForKeyValue struct {
Pos lexer.Position
Key *string `( "for" @Ident ","`
Value *string `@Ident "in"`
Collection *string `( @Ident`
CollectionExpression *Expression `| @@) `
Body []*Expression `"{" @@* "}" )`
}
type ForWhile struct {
Pos lexer.Position
Init []*Assignment `"for" (`
Condition *Expression `@@?`
Post *Expression ``
Body []*Expression `"{" @@* "}" )`
}
var (
_lexer = lexer.Must(stateful.New(stateful.Rules{
"Root": {
{"comment", `//.*|/\*.*?\*/`, nil},
{"whitespace", `[\n\r\t ]+`, nil},
{"Float", `[+-]?([0-9]*[.])?[0-9]+`, nil},
{"Int", `[\d]+`, nil},
{"String", `"([^"]*)"`, nil},
{"Ident", `[\w]+`, nil},
{"Punct", `[-[!*%()+_={}\|:;"<,>./]|]`, nil},
},
}))
parser = participle.MustBuild(&ExpressionList{}, participle.Lexer(_lexer),
participle.Elide("whitespace", "comment"), participle.UseLookahead(2))
)
func GetGrammer() string {
return parser.String()
}
func GenerateAST(source string) (*ExpressionList, error) {
expressionList := &ExpressionList{}
err := parser.ParseString("", source, expressionList)
if err != nil {
return nil, err
}
return expressionList, nil
}