blob: cde6ce934a955882b9492ad6ba16c04bf93df855 (
plain)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
namespace host
lex
rl NL / '\n' /
token escape
/ '@' any /
literal `={ `}= `${ `}$ `@{ `}@
token host_any / any /
end
def tok
[`${ StmtList: stmt* `}$] :Stmt
| [`={ Expr: expr `}=] :Expr
| [escape] :Escape
| [host_any] :Any
end
lex
rl NL / '\n' /
rl s_literal
/ "'" ([^'\\\n] | '\\' (any | NL))* "'" /
rl d_literal
/ '"' ([^"\\] | NL | '\\' (any | NL))* '"' /
rl c_comment
/ '/*' ( any | NL )* :>> '*/' /
rl cpp_comment
/ '//' [^\n]* NL /
literal `array `value `TRUE `FALSE
`while `switch `case
`if `else `offset `index
`goto `deref `entry `label `default
`host `cast `match `pat
literal `uint `const
`s8 `s16 `s32 `s64
`s128 `nil `export
`fallthrough `u `c `break `continue
token ident
/( alpha | '_' ) ( alpha | digit | '_' )*/
token uint
/ digit+ /
token hex_number
/ '0x' [0-9a-fA-F]+ /
ignore
/ c_comment | cpp_comment /
token string
/ s_literal | d_literal /
ignore / ( [ \t] | NL )+ /
literal `$ `{ `} `= `[ `]
`- `, `. `; `( `) `:
`? `* `+ `> `< `&
`~ `! `!= `== `<< `>>
`+= `&& `|| `<= `>=
`@ `-= `-> `={ `${ `@{
end
def embedded_host
[`host `( string `, uint `) `={ TL: host::tok* host::`}=] :Expr
| [`host `( string `, uint `) `${ TL: host::tok* host::`}$] :Stmt
| [`host `( string `, uint `) `@{ TL: host::tok* host::`}@] :Bare
def type
[ident] :Ident
| [ident ident] :Ident2
| [`uint] :Uint
| [`s8] :S8
| [`s16] :S16
| [`s32] :S32
| [`s64] :S64
| [`s128] :S128
def expr_factor
[embedded_host] :EmbeddedHost
| [ident] :Ident
| [ident `[ expr `]] :ArraySub
| [ident `[ expr `] `. Field: ident] :ArraySubField
| [`offset `( ident `, expr `)] :Offset
| [`deref `( ident `, expr `)] :Deref
| [number] :Number
| [`TRUE] :True
| [`FALSE] :False
| [`nil] :Nil
| [hex_number] :HexNumber
| [string] :String
| [embedded_host `-> expr_factor] :Access
| [`( expr `)] :Paren
| [`cast `( type `) expr_factor] :Cast
def lvalue
[embedded_host]
| [ident]
| [ident `[ expr `]]
| [ident `[ expr `] `. ident]
| [embedded_host `-> lvalue]
def expr_factor_op
[`! expr_factor_op]
| [`~ expr_factor_op]
| [expr_factor]
def expr_bitwise
[expr_bitwise `& expr_factor_op]
| [expr_factor_op]
def expr_mult
[expr_mult `* expr_bitwise]
| [expr_bitwise]
def add_op
[`+] | [`-]
def expr_add
[expr_add add_op expr_mult]
| [expr_mult]
def shift_op
[`<<] | [`>>]
def expr_shift
[expr_shift shift_op expr_add]
| [expr_add]
def test_op
[`<] | [`>] | [`<=] | [`>=] |
[`==] | [`!=] | [`&&] | [`||]
def expr_test
[expr_test test_op expr_shift]
| [expr_shift]
def expr
[expr_test]
def sint
[uint]
| [`- uint]
def number
[`u `( uint `)] :Unsigned
| [`c `( uint `)] :Char
| [sint] :Number
def comma_num
[`, number]
def num_list
[number comma_num*]
| []
def static_array
[`array type ident `( number `, number `) `= `{ num_list `} `;]
def static_value
[`value type ident `= number `;]
def break_label
[ident `: `:]
def while_stmt
[break_label? `while `( expr `) stmt]
def else_if_clause
[`else `if `( expr `) stmt]
def else_clause
[`else stmt]
def if_stmt [
`if `( expr `) stmt
else_if_clause* else_clause?
]
def match_stmt
[`match `( E: expr `) `{ P: pat_block* D: default_block? `}]
def pat_block
[`pat expr `{ stmt* `}]
def switch_stmt
[`switch `( expr `) `{ stmt* `}]
def case_block
[`case expr `{ stmt* `}]
def default_block
[`default `{ stmt* `}]
def case_label
[`case expr `:]
def goto_label
[ident `:]
def opt_init
[`= expr]
| []
def opt_ptr
[`*]
| []
def opt_const
[`const]
| []
def declaration
[opt_const type ident opt_init `;]
def index_stmt
[`index type ident opt_init`;]
def export_stmt
[`export type ident number `;]
def goto_stmt
Id: int
[`goto ident `;]
def fallthrough
[`fallthrough `;]
def break_stmt
[`break ident? `;]
def continue_stmt
[`continue ident? `;]
def block
[`{ StmtList: stmt* `}]
def expr_stmt
[expr `;]
def assign_op
[`=] | [`+=] | [`-=]
def assign_stmt
[LValue: lvalue assign_op expr `;]
def stmt
[embedded_host]
| [static_array]
| [static_value]
| [declaration]
| [index_stmt]
| [export_stmt]
| [assign_stmt]
| [expr_stmt]
| [while_stmt]
| [if_stmt]
| [match_stmt]
| [switch_stmt]
| [case_block]
| [default_block]
| [case_label]
| [goto_label]
| [goto_stmt]
| [fallthrough]
| [break_stmt]
| [continue_stmt]
| [block]
token bom / 0xEF 0xBB 0xBF /
def opt_bom
[bom] :Bom
| []
def start
[opt_bom stmt*]
|