blob: 3142507b7dca2fcef6981caf78d5ec21f146ed40 [file] [log] [blame]
George Mountc09acd42015-01-07 16:34:06 -08001/*
George Mount1143e9f2015-03-31 14:13:05 -07002 [The "BSD licence"]
3 Copyright (c) 2013 Terence Parr, Sam Harwell
4 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
15 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*/
George Mountc09acd42015-01-07 16:34:06 -080026grammar BindingExpression;
27
28bindingSyntax
George Mountc09acd42015-01-07 16:34:06 -080029 : expression defaults?
30 ;
31
32defaults
33 : ',' 'default' '=' constantValue
34 ;
35constantValue
36 : literal
37 | ResourceReference
George Mountc09acd42015-01-07 16:34:06 -080038 | identifier
39 ;
40
41expression
42 : '(' expression ')' # Grouping
43// this isn't allowed yet.
44// | THIS # Primary
45 | literal # Primary
46 | identifier # Primary
47 | classExtraction # Primary
George Mountae7cb822015-02-23 15:43:53 -080048 | resources # Resource
George Mountd0717692015-01-26 16:57:04 -080049// | typeArguments (explicitGenericInvocationSuffix | 'this' arguments) # GenericCall
George Mountc09acd42015-01-07 16:34:06 -080050 | expression '.' Identifier # DotOp
51// | expression '.' 'this' # ThisReference
George Mountd0717692015-01-26 16:57:04 -080052// | expression '.' explicitGenericInvocation # ExplicitGenericInvocationOp
George Mountc09acd42015-01-07 16:34:06 -080053 | expression '[' expression ']' # BracketOp
Yigit Boyard7af42b2015-01-09 14:23:33 -080054 | target=expression '.' methodName=Identifier '(' args=expressionList? ')' # MethodInvocation
George Mountc09acd42015-01-07 16:34:06 -080055 | '(' type ')' expression # CastOp
George Mountd0717692015-01-26 16:57:04 -080056 | op=('+'|'-') expression # UnaryOp
57 | op=('~'|'!') expression # UnaryOp
Yigit Boyar35e303e2015-01-09 10:43:24 -080058 | left=expression op=('*'|'/'|'%') right=expression # MathOp
59 | left=expression op=('+'|'-') right=expression # MathOp
60 | left=expression op=('<<' | '>>>' | '>>') right=expression # BitShiftOp
61 | left=expression op=('<=' | '>=' | '>' | '<') right=expression # ComparisonOp
George Mountc09acd42015-01-07 16:34:06 -080062 | expression 'instanceof' type # InstanceOfOp
Yigit Boyar35e303e2015-01-09 10:43:24 -080063 | left=expression op=('==' | '!=') right=expression # ComparisonOp
64 | left=expression op='&' right=expression # BinaryOp
65 | left=expression op='^' right=expression # BinaryOp
66 | left=expression op='|' right=expression # BinaryOp
67 | left=expression op='&&' right=expression # AndOrOp
68 | left=expression op='||' right=expression # AndOrOp
69 | left=expression op='?' iftrue=expression ':' iffalse=expression # TernaryOp
70 | left=expression op='??' right=expression # QuestionQuestionOp
George Mountc09acd42015-01-07 16:34:06 -080071 ;
72
73THIS
74 : 'this'
75 ;
76
77classExtraction
78 : type '.' 'class'
79 | 'void' '.' 'class'
80 ;
81
82expressionList
83 : expression (',' expression)*
84 ;
85
86literal
87 : javaLiteral
88 | stringLiteral
89 ;
90
91identifier
92 : Identifier
93 ;
94
95javaLiteral
96 : IntegerLiteral
97 | FloatingPointLiteral
98 | BooleanLiteral
99 | NullLiteral
100 | CharacterLiteral
101 ;
102
103stringLiteral
104 : SingleQuoteString
105 | DoubleQuoteString
106 ;
107
108explicitGenericInvocation
109 : typeArguments explicitGenericInvocationSuffix
110 ;
111
112typeArguments
113 : '<' type (',' type)* '>'
114 ;
115
116type
117 : classOrInterfaceType ('[' ']')*
118 | primitiveType ('[' ']')*
119 ;
120
121explicitGenericInvocationSuffix
122 : Identifier arguments
123 ;
124
125arguments
126 : '(' expressionList? ')'
127 ;
128
129classOrInterfaceType
130 : identifier typeArguments? ('.' Identifier typeArguments? )*
131 ;
132
133primitiveType
134 : 'boolean'
135 | 'char'
136 | 'byte'
137 | 'short'
138 | 'int'
139 | 'long'
140 | 'float'
141 | 'double'
142 ;
143
George Mountae7cb822015-02-23 15:43:53 -0800144resources
145 : ResourceReference resourceParameters?
146 ;
147
148resourceParameters
149 : '(' expressionList ')'
150 ;
151
George Mountc09acd42015-01-07 16:34:06 -0800152// LEXER
153
154// §3.10.1 Integer Literals
155
156IntegerLiteral
157 : DecimalIntegerLiteral
158 | HexIntegerLiteral
159 | OctalIntegerLiteral
160 | BinaryIntegerLiteral
161 ;
162
163fragment
164DecimalIntegerLiteral
165 : DecimalNumeral IntegerTypeSuffix?
166 ;
167
168fragment
169HexIntegerLiteral
170 : HexNumeral IntegerTypeSuffix?
171 ;
172
173fragment
174OctalIntegerLiteral
175 : OctalNumeral IntegerTypeSuffix?
176 ;
177
178fragment
179BinaryIntegerLiteral
180 : BinaryNumeral IntegerTypeSuffix?
181 ;
182
183fragment
184IntegerTypeSuffix
185 : [lL]
186 ;
187
188fragment
189DecimalNumeral
190 : '0'
191 | NonZeroDigit (Digits? | Underscores Digits)
192 ;
193
194fragment
195Digits
196 : Digit (DigitOrUnderscore* Digit)?
197 ;
198
199fragment
200Digit
201 : '0'
202 | NonZeroDigit
203 ;
204
205fragment
206NonZeroDigit
207 : [1-9]
208 ;
209
210fragment
211DigitOrUnderscore
212 : Digit
213 | '_'
214 ;
215
216fragment
217Underscores
218 : '_'+
219 ;
220
221fragment
222HexNumeral
223 : '0' [xX] HexDigits
224 ;
225
226fragment
227HexDigits
228 : HexDigit (HexDigitOrUnderscore* HexDigit)?
229 ;
230
231fragment
232HexDigit
233 : [0-9a-fA-F]
234 ;
235
236fragment
237HexDigitOrUnderscore
238 : HexDigit
239 | '_'
240 ;
241
242fragment
243OctalNumeral
244 : '0' Underscores? OctalDigits
245 ;
246
247fragment
248OctalDigits
249 : OctalDigit (OctalDigitOrUnderscore* OctalDigit)?
250 ;
251
252fragment
253OctalDigit
254 : [0-7]
255 ;
256
257fragment
258OctalDigitOrUnderscore
259 : OctalDigit
260 | '_'
261 ;
262
263fragment
264BinaryNumeral
265 : '0' [bB] BinaryDigits
266 ;
267
268fragment
269BinaryDigits
270 : BinaryDigit (BinaryDigitOrUnderscore* BinaryDigit)?
271 ;
272
273fragment
274BinaryDigit
275 : [01]
276 ;
277
278fragment
279BinaryDigitOrUnderscore
280 : BinaryDigit
281 | '_'
282 ;
283
284// §3.10.2 Floating-Point Literals
285
286FloatingPointLiteral
287 : DecimalFloatingPointLiteral
288 | HexadecimalFloatingPointLiteral
289 ;
290
291fragment
292DecimalFloatingPointLiteral
293 : Digits '.' Digits? ExponentPart? FloatTypeSuffix?
294 | '.' Digits ExponentPart? FloatTypeSuffix?
295 | Digits ExponentPart FloatTypeSuffix?
296 | Digits FloatTypeSuffix
297 ;
298
299fragment
300ExponentPart
301 : ExponentIndicator SignedInteger
302 ;
303
304fragment
305ExponentIndicator
306 : [eE]
307 ;
308
309fragment
310SignedInteger
311 : Sign? Digits
312 ;
313
314fragment
315Sign
316 : [+-]
317 ;
318
319fragment
320FloatTypeSuffix
321 : [fFdD]
322 ;
323
324fragment
325HexadecimalFloatingPointLiteral
326 : HexSignificand BinaryExponent FloatTypeSuffix?
327 ;
328
329fragment
330HexSignificand
331 : HexNumeral '.'?
332 | '0' [xX] HexDigits? '.' HexDigits
333 ;
334
335fragment
336BinaryExponent
337 : BinaryExponentIndicator SignedInteger
338 ;
339
340fragment
341BinaryExponentIndicator
342 : [pP]
343 ;
344
345// §3.10.3 Boolean Literals
346
347BooleanLiteral
348 : 'true'
349 | 'false'
350 ;
351
352// §3.10.4 Character Literals
353
354CharacterLiteral
355 : '\'' SingleCharacter '\''
356 | '\'' EscapeSequence '\''
357 ;
358
359fragment
360SingleCharacter
361 : ~['\\]
362 ;
363// §3.10.5 String Literals
364SingleQuoteString
George Mount7920e172015-02-05 16:02:46 -0800365 : '`' SingleQuoteStringCharacter* '`'
George Mountc09acd42015-01-07 16:34:06 -0800366 ;
367
368DoubleQuoteString
369 : '"' StringCharacters? '"'
370 ;
371
372fragment
373StringCharacters
374 : StringCharacter+
375 ;
376fragment
377StringCharacter
378 : ~["\\]
379 | EscapeSequence
380 ;
381fragment
382SingleQuoteStringCharacter
383 : ~[`\\]
384 | EscapeSequence
385 ;
386
387// §3.10.6 Escape Sequences for Character and String Literals
388fragment
389EscapeSequence
390 : '\\' [btnfr"'`\\]
391 | OctalEscape
392 | UnicodeEscape
393 ;
394
395fragment
396OctalEscape
397 : '\\' OctalDigit
398 | '\\' OctalDigit OctalDigit
399 | '\\' ZeroToThree OctalDigit OctalDigit
400 ;
401
402fragment
403UnicodeEscape
404 : '\\' 'u' HexDigit HexDigit HexDigit HexDigit
405 ;
406
407fragment
408ZeroToThree
409 : [0-3]
410 ;
411
412// §3.10.7 The Null Literal
413
414NullLiteral
415 : 'null'
416 ;
417
418// §3.8 Identifiers (must appear after all keywords in the grammar)
419
420Identifier
421 : JavaLetter JavaLetterOrDigit*
422 ;
423
424fragment
425JavaLetter
426 : [a-zA-Z$_] // these are the "java letters" below 0xFF
427 | // covers all characters above 0xFF which are not a surrogate
428 ~[\u0000-\u00FF\uD800-\uDBFF]
429 {Character.isJavaIdentifierStart(_input.LA(-1))}?
430 | // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
431 [\uD800-\uDBFF] [\uDC00-\uDFFF]
432 {Character.isJavaIdentifierStart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
433 ;
434
435fragment
436JavaLetterOrDigit
437 : [a-zA-Z0-9$_] // these are the "java letters or digits" below 0xFF
438 | // covers all characters above 0xFF which are not a surrogate
439 ~[\u0000-\u00FF\uD800-\uDBFF]
440 {Character.isJavaIdentifierPart(_input.LA(-1))}?
441 | // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
442 [\uD800-\uDBFF] [\uDC00-\uDFFF]
443 {Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
444 ;
445
446//
447// Whitespace and comments
448//
449
450WS : [ \t\r\n\u000C]+ -> skip
451 ;
452
453//
454// Resource references
455//
456
457ResourceReference
George Mountae7cb822015-02-23 15:43:53 -0800458 : '@' (PackageName ':')? ResourceType '/' Identifier
George Mountc09acd42015-01-07 16:34:06 -0800459 ;
460
George Mountc09acd42015-01-07 16:34:06 -0800461PackageName
462 : 'android'
463 | Identifier
464 ;
465
George Mountc09acd42015-01-07 16:34:06 -0800466ResourceType
467 : 'anim'
468 | 'animator'
George Mountc09acd42015-01-07 16:34:06 -0800469 | 'bool'
470 | 'color'
George Mountc752a5f2015-01-21 16:24:43 -0800471 | 'colorStateList'
George Mountc09acd42015-01-07 16:34:06 -0800472 | 'dimen'
George Mountc752a5f2015-01-21 16:24:43 -0800473 | 'dimenOffset'
474 | 'dimenSize'
George Mountc09acd42015-01-07 16:34:06 -0800475 | 'drawable'
476 | 'fraction'
477 | 'id'
478 | 'integer'
George Mountc752a5f2015-01-21 16:24:43 -0800479 | 'intArray'
George Mountc09acd42015-01-07 16:34:06 -0800480 | 'interpolator'
481 | 'layout'
George Mountc09acd42015-01-07 16:34:06 -0800482 | 'plurals'
George Mountc752a5f2015-01-21 16:24:43 -0800483 | 'stateListAnimator'
George Mountc09acd42015-01-07 16:34:06 -0800484 | 'string'
George Mountc752a5f2015-01-21 16:24:43 -0800485 | 'stringArray'
George Mountc09acd42015-01-07 16:34:06 -0800486 | 'transition'
George Mountc752a5f2015-01-21 16:24:43 -0800487 | 'typedArray'
George Mountc09acd42015-01-07 16:34:06 -0800488 ;