-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
GoParser.g4
531 lines (420 loc) · 9.8 KB
/
GoParser.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
[The "BSD licence"] Copyright (c) 2017 Sasa Coh, Michał Błotniak
Copyright (c) 2019 Ivan Kochurkin, kvanttt@gmail.com, Positive Technologies
Copyright (c) 2019 Dmitry Rassadin, flipparassa@gmail.com,Positive Technologies All rights reserved.
Copyright (c) 2021 Martin Mirchev, mirchevmartin2203@gmail.com
Copyright (c) 2023 Dmitry Litovchenko, i@dlitovchenko.ru
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met: 1. Redistributions of source code must retain the
above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in
binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution. 3. The name
of the author may not be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
* A Go grammar for ANTLR 4 derived from the Go Language Specification https://golang.org/ref/spec
*/
// $antlr-format alignTrailingComments true, columnLimit 150, minEmptyLines 1, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine false, allowShortBlocksOnASingleLine true, alignSemicolons hanging, alignColons hanging
parser grammar GoParser;
// Insert here @header for C++ parser.
options {
tokenVocab = GoLexer;
superClass = GoParserBase;
}
sourceFile
: packageClause eos (importDecl eos)* ((functionDecl | methodDecl | declaration) eos)* EOF
;
packageClause
: PACKAGE packageName = IDENTIFIER
;
importDecl
: IMPORT (importSpec | L_PAREN (importSpec eos)* R_PAREN)
;
importSpec
: alias = (DOT | IDENTIFIER)? importPath
;
importPath
: string_
;
declaration
: constDecl
| typeDecl
| varDecl
;
constDecl
: CONST (constSpec | L_PAREN (constSpec eos)* R_PAREN)
;
constSpec
: identifierList (type_? ASSIGN expressionList)?
;
identifierList
: IDENTIFIER (COMMA IDENTIFIER)*
;
expressionList
: expression (COMMA expression)*
;
typeDecl
: TYPE (typeSpec | L_PAREN (typeSpec eos)* R_PAREN)
;
typeSpec
: aliasDecl
| typeDef
;
aliasDecl
: IDENTIFIER ASSIGN type_
;
typeDef
: IDENTIFIER typeParameters? type_
;
typeParameters
: L_BRACKET typeParameterDecl (COMMA typeParameterDecl)* R_BRACKET
;
typeParameterDecl
: identifierList typeElement
;
typeElement
: typeTerm (OR typeTerm)*
;
typeTerm
: UNDERLYING? type_
;
// Function declarations
functionDecl
: FUNC IDENTIFIER typeParameters? signature block?
;
methodDecl
: FUNC receiver IDENTIFIER signature block?
;
receiver
: parameters
;
varDecl
: VAR (varSpec | L_PAREN (varSpec eos)* R_PAREN)
;
varSpec
: identifierList (type_ (ASSIGN expressionList)? | ASSIGN expressionList)
;
block
: L_CURLY statementList? R_CURLY
;
statementList
: ((SEMI? | EOS? | {this.closingBracket()}?) statement eos)+
;
statement
: declaration
| labeledStmt
| simpleStmt
| goStmt
| returnStmt
| breakStmt
| continueStmt
| gotoStmt
| fallthroughStmt
| block
| ifStmt
| switchStmt
| selectStmt
| forStmt
| deferStmt
;
simpleStmt
: sendStmt
| incDecStmt
| assignment
| expressionStmt
| shortVarDecl
;
expressionStmt
: expression
;
sendStmt
: channel = expression RECEIVE expression
;
incDecStmt
: expression (PLUS_PLUS | MINUS_MINUS)
;
assignment
: expressionList assign_op expressionList
;
assign_op
: (PLUS | MINUS | OR | CARET | STAR | DIV | MOD | LSHIFT | RSHIFT | AMPERSAND | BIT_CLEAR)? ASSIGN
;
shortVarDecl
: identifierList DECLARE_ASSIGN expressionList
;
labeledStmt
: IDENTIFIER COLON statement?
;
returnStmt
: RETURN expressionList?
;
breakStmt
: BREAK IDENTIFIER?
;
continueStmt
: CONTINUE IDENTIFIER?
;
gotoStmt
: GOTO IDENTIFIER
;
fallthroughStmt
: FALLTHROUGH
;
deferStmt
: DEFER expression
;
ifStmt
: IF (expression | eos expression | simpleStmt eos expression) block (ELSE (ifStmt | block))?
;
switchStmt
: exprSwitchStmt
| typeSwitchStmt
;
exprSwitchStmt
: SWITCH (expression? | simpleStmt? eos expression?) L_CURLY exprCaseClause* R_CURLY
;
exprCaseClause
: exprSwitchCase COLON statementList?
;
exprSwitchCase
: CASE expressionList
| DEFAULT
;
typeSwitchStmt
: SWITCH (typeSwitchGuard | eos typeSwitchGuard | simpleStmt eos typeSwitchGuard) L_CURLY typeCaseClause* R_CURLY
;
typeSwitchGuard
: (IDENTIFIER DECLARE_ASSIGN)? primaryExpr DOT L_PAREN TYPE R_PAREN
;
typeCaseClause
: typeSwitchCase COLON statementList?
;
typeSwitchCase
: CASE typeList
| DEFAULT
;
typeList
: (type_ | NIL_LIT) (COMMA (type_ | NIL_LIT))*
;
selectStmt
: SELECT L_CURLY commClause* R_CURLY
;
commClause
: commCase COLON statementList?
;
commCase
: CASE (sendStmt | recvStmt)
| DEFAULT
;
recvStmt
: (expressionList ASSIGN | identifierList DECLARE_ASSIGN)? recvExpr = expression
;
forStmt
: FOR (expression? | forClause | rangeClause?) block
;
forClause
: initStmt = simpleStmt? eos expression? eos postStmt = simpleStmt?
;
rangeClause
: (expressionList ASSIGN | identifierList DECLARE_ASSIGN)? RANGE expression
;
goStmt
: GO expression
;
type_
: typeName typeArgs?
| typeLit
| L_PAREN type_ R_PAREN
;
typeArgs
: L_BRACKET typeList COMMA? R_BRACKET
;
typeName
: qualifiedIdent
| IDENTIFIER
;
typeLit
: arrayType
| structType
| pointerType
| functionType
| interfaceType
| sliceType
| mapType
| channelType
;
arrayType
: L_BRACKET arrayLength R_BRACKET elementType
;
arrayLength
: expression
;
elementType
: type_
;
pointerType
: STAR type_
;
interfaceType
: INTERFACE L_CURLY ((methodSpec | typeElement) eos)* R_CURLY
;
sliceType
: L_BRACKET R_BRACKET elementType
;
// It's possible to replace `type` with more restricted typeLit list and also pay attention to nil maps
mapType
: MAP L_BRACKET type_ R_BRACKET elementType
;
channelType
: (CHAN | CHAN RECEIVE | RECEIVE CHAN) elementType
;
methodSpec
: IDENTIFIER parameters result
| IDENTIFIER parameters
;
functionType
: FUNC signature
;
signature
: parameters result?
;
result
: parameters
| type_
;
parameters
: L_PAREN (parameterDecl (COMMA parameterDecl)* COMMA?)? R_PAREN
;
parameterDecl
: identifierList? ELLIPSIS? type_
;
expression
: primaryExpr
| unary_op = (PLUS | MINUS | EXCLAMATION | CARET | STAR | AMPERSAND | RECEIVE) expression
| expression mul_op = (STAR | DIV | MOD | LSHIFT | RSHIFT | AMPERSAND | BIT_CLEAR) expression
| expression add_op = (PLUS | MINUS | OR | CARET) expression
| expression rel_op = (
EQUALS
| NOT_EQUALS
| LESS
| LESS_OR_EQUALS
| GREATER
| GREATER_OR_EQUALS
) expression
| expression LOGICAL_AND expression
| expression LOGICAL_OR expression
;
primaryExpr
: operand
| conversion
| methodExpr
| primaryExpr ( DOT IDENTIFIER | index | slice_ | typeAssertion | arguments)
;
conversion
: type_ L_PAREN expression COMMA? R_PAREN
;
operand
: literal
| operandName typeArgs?
| L_PAREN expression R_PAREN
;
literal
: basicLit
| compositeLit
| functionLit
;
basicLit
: NIL_LIT
| integer
| string_
| FLOAT_LIT
;
integer
: DECIMAL_LIT
| BINARY_LIT
| OCTAL_LIT
| HEX_LIT
| IMAGINARY_LIT
| RUNE_LIT
;
operandName
: IDENTIFIER
;
qualifiedIdent
: IDENTIFIER DOT IDENTIFIER
;
compositeLit
: literalType literalValue
;
literalType
: structType
| arrayType
| L_BRACKET ELLIPSIS R_BRACKET elementType
| sliceType
| mapType
| typeName typeArgs?
;
literalValue
: L_CURLY (elementList COMMA?)? R_CURLY
;
elementList
: keyedElement (COMMA keyedElement)*
;
keyedElement
: (key COLON)? element
;
key
: expression
| literalValue
;
element
: expression
| literalValue
;
structType
: STRUCT L_CURLY (fieldDecl eos)* R_CURLY
;
fieldDecl
: (identifierList type_ | embeddedField) tag = string_?
;
string_
: RAW_STRING_LIT
| INTERPRETED_STRING_LIT
;
embeddedField
: STAR? typeName typeArgs?
;
functionLit
: FUNC signature block
; // function
index
: L_BRACKET expression R_BRACKET
;
slice_
: L_BRACKET (expression? COLON expression? | expression? COLON expression COLON expression) R_BRACKET
;
typeAssertion
: DOT L_PAREN type_ R_PAREN
;
arguments
: L_PAREN ((expressionList | type_ (COMMA expressionList)?) ELLIPSIS? COMMA?)? R_PAREN
;
methodExpr
: type_ DOT IDENTIFIER
;
eos
: SEMI
| EOF
| EOS
| {this.closingBracket()}?
;