-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubbler.g4
337 lines (275 loc) · 6.27 KB
/
bubbler.g4
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
grammar bubbler;
proto:
(
importStatement
| packageStatement
| optionStatement
| topLevelDef
| emptyStatement_
)* EOF;
// Import Statement
importStatement: IMPORT strLit SEMI;
// Top Level definitions
topLevelDef: enumDef | structDef;
// Size
size_:
LB (
( byteSize ( HASH bitSize )? )
| ( HASH bitSize )
) RB;
byteSize: intLit;
bitSize: intLit;
// Package
packageStatement: PACKAGE fullIdent SEMI;
// Option
optionStatement: OPTION optionName ASSIGN constant SEMI;
optionName: ident;
// optionName:
// fullIdent
// | LP fullIdent RP ( DOT fullIdent )?;
// Field
field:
fieldVoid
| fieldConstant
| fieldEmbedded
| fieldNormal
;
fieldVoid:
VOID size_ fieldOptions? SEMI;
fieldConstant:
basicType fieldName? size_? ASSIGN constant fieldOptions? SEMI;
fieldEmbedded:
type_ fieldOptions? SEMI;
fieldNormal:
type_ fieldName size_? fieldOptions? fieldMethods? SEMI;
fieldOptions: LB fieldOption ( COMMA fieldOption )* RB;
fieldOption: optionName ASSIGN constant;
fieldMethods : LC fieldMethod* RC;
fieldMethod: op = ( GET | SET ) methodName? LP basicType RP COLON expr SEMI;
// field types
type_:
basicType
| STRING
| BYTES
| arrayType
| structType // old fashion: 'struct' ident
| enumType // old fashion: 'enum' ident
| ident // unknown type ident
;
basicType:
BOOL
| INT8
| INT16
| INT32
| INT64
| UINT8
| UINT16
| UINT32
| UINT64
| FLOAT32
| FLOAT64
;
// array
arrayElementType: basicType | STRING | BYTES | structType | enumType | ident;
arrayType: arrayElementType LT intLit GT;
// enum
enumDef: enumName size_ enumBody;
enumBody: LC enumElement* RC;
enumElement: enumValue | emptyStatement_;
enumValue: enumValueName ( ASSIGN constant )? enumValueOptions? ( SEMI | COMMA );
enumValueOptions:
LB enumValueOption ( COMMA enumValueOption )* RB;
enumValueOption: optionName ASSIGN constant;
// struct
structDef: structName size_? structBody;
structBody: LC structElement* RC;
structElement: field | emptyStatement_;
// expr
expr:
value # ExprValue
| constant # ExprConstant
| LP expr RP # ExprParens
| expr POW expr # ExprPower
| op = ( ADD | SUB | BNOT | NOT ) expr # ExprUnary
| LP basicType RP expr # ExprCast
| expr op = ( MUL | DIV | MOD ) expr # ExprMulDivMod
| expr op = ( ADD | SUB ) expr # ExprAddSub
| expr op = ( SHL | SHR ) expr # ExprShift
| expr op = ( LT | LE | GT | GE ) expr # ExprRelational
| expr op = ( EQ | NE ) expr # ExprEquality
| expr BAND expr # ExprBitAnd
| expr BXOR expr # ExprBitXor
| expr BOR expr # ExprBitOr
| expr AND expr # ExprLogicalAnd
| expr OR expr # ExprLogicalOr
| expr QUESTION expr COLON expr # ExprTernary
;
// lexical
value: VALUE;
constant:
// fullIdent
( SUB | ADD )? intLit
| ( SUB | ADD )? floatLit
| strLit
| boolLit
// | blockLit
;
// not specified in specification but used in tests
// blockLit: LC ( ident COLON constant )* RC;
emptyStatement_: SEMI;
// Lexical elements
ident: IDENTIFIER;
fullIdent: ident ( DOT ident )*;
fieldName: ident;
methodName: ident;
structName: STRUCT ident;
enumName: ENUM ident;
enumValueName: ident;
// structType: ( DOT )? ( ident DOT )* structName;
// enumType: ( DOT )? ( ident DOT )* enumName;
structType: structName | structDef;
enumType: enumName;
intLit: INT_LIT;
strLit: STR_LIT;
boolLit: BOOL_LIT;
floatLit: FLOAT_LIT;
// keywords
SYNTAX: 'syntax';
IMPORT: 'import';
GET: 'get';
SET: 'set';
VALUE: 'value';
// WEAK: 'weak';
// PUBLIC: 'public';
PACKAGE: 'package';
OPTION: 'option';
// OPTIONAL: 'optional';
// REPEATED: 'repeated';
// ONEOF: 'oneof';
// MAP: 'map';
VOID: 'void';
INT8: 'int8';
INT16: 'int16';
INT32: 'int32';
INT64: 'int64';
UINT8: 'uint8';
UINT16: 'uint16';
UINT32: 'uint32';
UINT64: 'uint64';
FLOAT32: 'float32';
FLOAT64: 'float64';
BOOL: 'bool';
STRING: 'string';
BYTES: 'bytes';
ENUM: 'enum';
STRUCT: 'struct';
// SERVICE: 'service';
// EXTEND: 'extend';
// RPC: 'rpc';
// STREAM: 'stream';
// RETURNS: 'returns';
// symbols
HASH: '#';
SEMI: ';';
ASSIGN: '=';
QUESTION: '?';
LP: '(';
RP: ')';
LB: '[';
RB: ']';
LC: '{';
RC: '}';
LT: '<';
LE: '<=';
GT: '>';
GE: '>=';
EQ: '==';
NE: '!=';
DOT: '.';
COMMA: ',';
COLON: ':';
ADD: '+';
SUB: '-';
MUL: '*';
DIV: '/';
MOD: '%';
POW: '**';
SHL: '<<';
SHR: '>>';
BAND: '&';
BOR: '|';
BXOR: '^';
BNOT: '~';
AND: '&&';
OR: '||';
NOT: '!';
STR_LIT: ( '\'' ( CHAR_VALUE )*? '\'' ) | ( '"' ( CHAR_VALUE )*? '"' );
fragment CHAR_VALUE:
HEX_ESCAPE
| OCT_ESCAPE
| CHAR_ESCAPE
| ~[\u0000\n\\];
fragment HEX_ESCAPE: '\\' ( 'x' | 'X' ) HEX_DIGIT HEX_DIGIT;
fragment OCT_ESCAPE: '\\' OCTAL_DIGIT OCTAL_DIGIT OCTAL_DIGIT;
fragment CHAR_ESCAPE:
'\\' (
'a'
| 'b'
| 'f'
| 'n'
| 'r'
| 't'
| 'v'
| '\\'
| '\''
| '"'
);
BOOL_LIT: 'true' | 'false';
FLOAT_LIT: (
DECIMALS DOT DECIMALS? EXPONENT?
| DECIMALS EXPONENT
| DOT DECIMALS EXPONENT?
)
| 'inf'
| 'nan';
fragment EXPONENT: ( 'e' | 'E' ) ( ADD | SUB )? DECIMALS;
fragment DECIMALS: DECIMAL_DIGIT+;
INT_LIT: DECIMAL_LIT | OCTAL_LIT | HEX_LIT;
fragment DECIMAL_LIT: ( [1-9] ) DECIMAL_DIGIT*;
fragment OCTAL_LIT: '0' OCTAL_DIGIT*;
fragment HEX_LIT: '0' ( 'x' | 'X' ) HEX_DIGIT+;
IDENTIFIER: LETTER ( LETTER | DECIMAL_DIGIT )*;
fragment LETTER: [A-Za-z_];
fragment DECIMAL_DIGIT: [0-9];
fragment OCTAL_DIGIT: [0-7];
fragment HEX_DIGIT: [0-9A-Fa-f];
// comments
WS: [ \t\r\n\u000C]+ -> skip;
LINE_COMMENT: '//' ~[\r\n]* -> channel(HIDDEN);
COMMENT: '/*' .*? '*/' -> channel(HIDDEN);
KEYWORDS:
IMPORT
| SYNTAX
| PACKAGE
| OPTION
| GET
| SET
| VALUE
| VOID
| INT8
| INT16
| INT32
| INT64
| UINT8
| UINT16
| UINT32
| UINT64
| FLOAT32
| FLOAT64
| BOOL
| STRING
| BYTES
| ENUM
| STRUCT
| BOOL_LIT
;