-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarpGrammar.g4
278 lines (238 loc) · 7.83 KB
/
CarpGrammar.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
grammar CarpGrammar;
options {
contextSuperClass = Carp.interpreter.ScopedParserRuleContext;
}
ELIPSIS : '..' ;
PERIOD : '.' ;
COMMA : ',' ;
LPAREN : '(' ;
RPAREN : ')' ;
LBRACE : '{' ;
RBRACE : '}' ;
LBRACKET : '[' ;
RBRACKET : ']' ;
EQUALS : '=' ;
EQUALS_EQUALS : '==' ;
NOT_EQUALS : '!=' | '<>' ;
GREATER_THAN : '>' ;
LESS_THAN : '<' ;
LESS_THAN_EQUALS : '<=' ;
GREATER_THAN_EQUALS : '>=' ;
PIPE : '|' ;
AMPERSAND : '&' ;
TILDE : '~' ;
TILDE_TILDE : '~~' ;
AT : '@' ;
BANG : '!' ;
PLUS_EQUALS : '+=' ;
MINUS_EQUALS : '-=' ;
ASTERISK_EQUALS : '*=' ;
SLASH_EQUALS : '/=' ;
CARET_EQUALS : '^=' ;
PERCENT_EQUALS : '%=' ;
PLUS_PLUS : '++' ;
MINUS_MINUS : '--' ;
PLUS : '+' ;
MINUS : '-' ;
SLASH : '/' ;
PERCENT : '%' ;
ASTERISK_BSPACE : ' * ' ;
ASTERISK_LSPACE : ' *' ;
ASTERISK_RSPACE : '* ' ;
ASTERISK_NSPC : '*' ;
BACKSLASH : '\\' ;
CARET : '^' ;
QUESTION_MARK : '?' ;
UNDERSCORE : '_' ;
COLON : ':' ;
SEMICOLON : ';' ;
COLON_COLON : '::' ;
SEMICOLON_SEMICOLON : ';;' ;
ARROW : '->' ;
HASH : '#' ;
TRUE : 'true' ;
FALSE : 'false' ;
NULL : 'null' ;
TRY: 'try' ;
CATCH: 'catch' ;
FINALLY: 'finally' ;
IF : 'if' ;
ELSE : 'else' ;
ELSE_IF : 'else if' ;
WHILE : 'while' ;
ITER : 'for' ;
RETURN : 'return' ;
BREAK : 'break' ;
CONTINUE : 'continue' ;
YIELD : 'yield' ;
CLASS : 'class' ;
STRUCT : 'struct' ;
LET : 'let' ;
FIXED : 'fixed' ;
//IMPORT : 'import' ;
ID : [a-zA-Z][a-zA-Z0-9_]* ;
//STRING : '\'' (~['\\])* '\'' ;
INT : '-'? ( [0-9]+ | [0-9]+ '.' [0-9]+ | '.' [0-9]+ ) ;
WS : [ \t\r\n]+ -> skip ;
COMMENT : '#' .*? [\n] -> skip ;
STRING : '\'' SHORT_STRING_ITEM_FOR_SINGLE_QUOTE* '\'' ;
CHAR : '`' . ;
fragment SHORT_STRING_ITEM_FOR_SINGLE_QUOTE : SHORT_STRING_CHAR_NO_SINGLE_QUOTE | ('\\' .);
fragment SHORT_STRING_CHAR_NO_SINGLE_QUOTE : ~[\\'];
//PATH : [a-zA-Z0-9_\-.]+ ;
program : (statements+=statement)* EOF ;
block : (statements+=statement)* ;
generic_block
: '{' block '}' # enclosedBlock
| '->' expression # lambdaExpressionBlock
| '->' statement # lambdaBlock
;
statement
// : IMPORT loc+=(ID | PERIOD | MINUS | SLASH | UNDERSCORE | INT)* (':' ver+=(ID | PERIOD | MINUS | SLASH | UNDERSCORE | INT | COLON)+)? ';' # importStatement
: definition_with_attr # definitionStatement
| expression # expressionStatement
| flow_control # flowControlStatement
;
flow_control
: if_statement
| while_statement
| iter_statement
| return_statement
| try_statement
| break_statement
| continue_statement
| yield_statement
;
if_statement
: IF cond=expression body=generic_block (ELSE_IF elif_expressions+=expression elif_blocks+=generic_block)* (ELSE else_block=generic_block)?
;
while_statement
: WHILE cond=expression body=generic_block
;
try_statement
: TRY try_block=generic_block (CATCH '(' catch_types+=type catch_names+=name ')' catch_blocks+=generic_block)* (FINALLY finally_block=generic_block)?
| TRY try_block=generic_block (CATCH catch_types+=type catch_names+=name catch_blocks+=generic_block)* (FINALLY finally_block=generic_block)?
;
iter_statement
: ITER iter=expression body=generic_block # iterStatement
| ITER type name ':' iter=expression body=generic_block # iterAsStatement
| ITER '(' type name ':' iter=expression ')' body=generic_block # iterAsStatement
| ITER type_name_list ':' iter=expression body=generic_block # iterAsUnpackedStatement
| ITER '(' type_name_list ':' iter=expression ')' body=generic_block # iterAsUnpackedStatement
;
return_statement : RETURN value=expression? ;
break_statement : BREAK ;
continue_statement : CONTINUE ;
yield_statement : YIELD value=expression? ;
attribute
: '@' obj=expression
| '[' obj=expression ']'
;
definition_with_attr
: attrs+=attribute* def=definition
;
definition
: rtype=type key=name '(' values=type_name_list ')' body=generic_block # functionDefinition
| rtype=type key=name '(' values=type_name_list ')' # emptyFunctionDefinition
| type key=name '=' value=expression # initializedVariableDefinition
| type key=name # variableDefinition
| CLASS key=name (':' inherits+=type (',' inherits+=type)*)? '{' definitions+=definition_with_attr* '}' # classDefinition
| STRUCT key=name (':' inherits+=type (',' inherits+=type)*)? '{' definitions+=definition_with_attr* '}' # structDefinition
| FIXED key=name '{' values+=name* '}' # enumDefinition
;
expression
: constant # constantExpression
| obj=expression '~' dest=type # castExpression
| obj=expression TILDE_TILDE dest=type # compareTypeExpression
| token=('++'|'--') expr=expression # infixExpression
| expr=expression token=('++'|'--') # postfixExpression
| obj=expression '(' parameters=expression_list ')' # callExpression // Side effects
| obj=expression '[' parameters=expression_list ']' # indexExpression
| obj=expression '.' value=name # propertyExpression
| left=expression op=binary right=expression # binaryExpression
| op=unary left=expression # unaryExpression
| left=expression op=comparison right=expression # comparisonExpression
| left=expression op=logical right=expression # logicalExpression
| condition=expression '?' left=expression ':' right=expression # ternaryExpression // Side effects
| map # mapExpression
| array # arrayExpression
| name # variableExpression
| left=expression ELIPSIS right=expression # rangeExpression
| ELIPSIS right=expression # endRangeExpression
| '(' obj=expression ')' # parenthesizedExpression
| inner=expression '::' # windExpression
| inner=expression ';;' # filterExpression
| left=expression '=' right=expression # assignmentExpression // Side effects
// add += -= *= /= %= ^=
| left=expression op=compoundAssignment right=expression # compoundAssignmentExpression // Side effects
| '(' values=type_name_list ')' body=generic_block # lambdaExpression
;
expression_list
: (expressions+=expression (',' expressions+=expression)*)?
;
compoundAssignment
: PLUS_EQUALS # addCompound
| MINUS_EQUALS # subtractCompound
| ASTERISK_EQUALS # multiplyCompound
| SLASH_EQUALS # divideCompound
| ASTERISK_LSPACE EQUALS # multiplyCompound
| CARET_EQUALS # powerCompound
| PERCENT_EQUALS # modulusCompound
;
constant
: INT # intConstant
| STRING # stringConstant
| CHAR # charConstant
| TRUE # trueConstant
| FALSE # falseConstant
| NULL # nullConstant
;
unary
: MINUS # negateUnary
| BANG # notUnary
;
logical
: AMPERSAND # andLogical
| PIPE # orLogical
;
comparison
: EQUALS_EQUALS # matchComparison
| NOT_EQUALS # notMatchComparison
| GREATER_THAN # greaterThanComparison
| LESS_THAN # lessThanComparison
| GREATER_THAN_EQUALS # greaterThanEqualsComparison
| LESS_THAN_EQUALS # lessThanEqualsComparison
;
binary
: PLUS # addBinary
| MINUS # subtractBinary
| (ASTERISK_LSPACE | ASTERISK_BSPACE | ASTERISK_NSPC) # multiplicationBinary
| SLASH # divideBinary
| CARET # powerBinary
| PERCENT # modulusBinary
;
array : '[' expression_list ']' ;
map : '[' keys+=expression ':' values+=expression (',' keys+=expression ':' values+=expression)* ']' ;
// variables that end with * are lists
// variables that start with _ are private
// variables that start with :: are static
// variables that start with # are constants
// int* myarr = [1, 2, 3]
// :mystatic = f -> 5
modifier
: '_' # privateModifier
// | '::' # staticModifier
// | '#' # constantModifier
;
type
: key=type ':' value=type # mapType
| element=type (ASTERISK_RSPACE | ASTERISK_NSPC) # listType
| main=type '<' subs+=type (',' subs+=type)* '>' # genericType
| main=ID ('.' parts+=ID)+ # propertyType
| ID # namedType
| LET # autoType
;
type_name_list
: (types+=type names+=name (',' types+=type names+=name)*)?
;
name : modifiers+=modifier* ID ;