| 1 |
/* |
| 2 |
[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 |
*/ |
| 26 |
grammar BindingExpression; |
| 27 |
|
| 28 |
bindingSyntax |
| 29 |
: expression defaults? # RootExpr |
| 30 |
| lambdaExpression # RootLambda |
| 31 |
; |
| 32 |
|
| 33 |
defaults |
| 34 |
: ',' 'default' '=' constantValue |
| 35 |
; |
| 36 |
|
| 37 |
constantValue |
| 38 |
: literal |
| 39 |
| ResourceReference |
| 40 |
| identifier |
| 41 |
; |
| 42 |
|
| 43 |
lambdaExpression |
| 44 |
: args=lambdaParameters '->' expr=expression |
| 45 |
; |
| 46 |
|
| 47 |
lambdaParameters |
| 48 |
: Identifier # SingleLambdaParameter |
| 49 |
| '(' params=inferredFormalParameterList? ')' #LambdaParameterList |
| 50 |
; |
| 51 |
|
| 52 |
inferredFormalParameterList |
| 53 |
: Identifier (',' Identifier)* |
| 54 |
; |
| 55 |
|
| 56 |
expression |
| 57 |
: '(' expression ')' # Grouping |
| 58 |
// this isn't allowed yet. |
| 59 |
// | THIS # Primary |
| 60 |
| literal # Primary |
| 61 |
| VoidLiteral # Primary |
| 62 |
| identifier # Primary |
| 63 |
| classExtraction # Primary |
| 64 |
| resources # Resource |
| 65 |
// | typeArguments (explicitGenericInvocationSuffix | 'this' arguments) # GenericCall |
| 66 |
| expression '.' Identifier # DotOp |
| 67 |
| expression '::' Identifier # FunctionRef |
| 68 |
// | expression '.' 'this' # ThisReference |
| 69 |
// | expression '.' explicitGenericInvocation # ExplicitGenericInvocationOp |
| 70 |
| expression '[' expression ']' # BracketOp |
| 71 |
| target=expression '.' methodName=Identifier '(' args=expressionList? ')' # MethodInvocation |
| 72 |
| '(' type ')' expression # CastOp |
| 73 |
| op=('+'|'-') expression # UnaryOp |
| 74 |
| op=('~'|'!') expression # UnaryOp |
| 75 |
| left=expression op=('*'|'/'|'%') right=expression # MathOp |
| 76 |
| left=expression op=('+'|'-') right=expression # MathOp |
| 77 |
| left=expression op=('<<' | '>>>' | '>>') right=expression # BitShiftOp |
| 78 |
| left=expression op=('<=' | '>=' | '>' | '<') right=expression # ComparisonOp |
| 79 |
| expression 'instanceof' type # InstanceOfOp |
| 80 |
| left=expression op=('==' | '!=') right=expression # ComparisonOp |
| 81 |
| left=expression op='&' right=expression # BinaryOp |
| 82 |
| left=expression op='^' right=expression # BinaryOp |
| 83 |
| left=expression op='|' right=expression # BinaryOp |
| 84 |
| left=expression op='&&' right=expression # AndOrOp |
| 85 |
| left=expression op='||' right=expression # AndOrOp |
| 86 |
| <assoc=right>left=expression op='?' iftrue=expression ':' iffalse=expression # TernaryOp |
| 87 |
| left=expression op='??' right=expression # QuestionQuestionOp |
| 88 |
; |
| 89 |
|
| 90 |
THIS |
| 91 |
: 'this' |
| 92 |
; |
| 93 |
|
| 94 |
classExtraction |
| 95 |
: type '.' 'class' |
| 96 |
; |
| 97 |
|
| 98 |
expressionList |
| 99 |
: expression (',' expression)* |
| 100 |
; |
| 101 |
|
| 102 |
literal |
| 103 |
: javaLiteral |
| 104 |
| stringLiteral |
| 105 |
; |
| 106 |
|
| 107 |
identifier |
| 108 |
: Identifier |
| 109 |
; |
| 110 |
|
| 111 |
javaLiteral |
| 112 |
: IntegerLiteral |
| 113 |
| FloatingPointLiteral |
| 114 |
| BooleanLiteral |
| 115 |
| NullLiteral |
| 116 |
| CharacterLiteral |
| 117 |
; |
| 118 |
|
| 119 |
VoidLiteral |
| 120 |
: 'Void' |
| 121 |
| 'void' |
| 122 |
| '¯\\_(ツ)_/¯' |
| 123 |
; |
| 124 |
|
| 125 |
stringLiteral |
| 126 |
: SingleQuoteString |
| 127 |
| DoubleQuoteString |
| 128 |
; |
| 129 |
|
| 130 |
explicitGenericInvocation |
| 131 |
: typeArguments explicitGenericInvocationSuffix |
| 132 |
; |
| 133 |
|
| 134 |
typeArguments |
| 135 |
: '<' type (',' type)* '>' |
| 136 |
; |
| 137 |
|
| 138 |
type |
| 139 |
: classOrInterfaceType ('[' ']')* |
| 140 |
| primitiveType ('[' ']')* |
| 141 |
; |
| 142 |
|
| 143 |
explicitGenericInvocationSuffix |
| 144 |
: Identifier arguments |
| 145 |
; |
| 146 |
|
| 147 |
arguments |
| 148 |
: '(' expressionList? ')' |
| 149 |
; |
| 150 |
|
| 151 |
classOrInterfaceType |
| 152 |
: identifier typeArguments? ('.' Identifier typeArguments? )* |
| 153 |
; |
| 154 |
|
| 155 |
primitiveType |
| 156 |
: 'boolean' |
| 157 |
| 'char' |
| 158 |
| 'byte' |
| 159 |
| 'short' |
| 160 |
| 'int' |
| 161 |
| 'long' |
| 162 |
| 'float' |
| 163 |
| 'double' |
| 164 |
; |
| 165 |
|
| 166 |
resources |
| 167 |
: ResourceReference resourceParameters? |
| 168 |
; |
| 169 |
|
| 170 |
resourceParameters |
| 171 |
: '(' expressionList ')' |
| 172 |
; |
| 173 |
|
| 174 |
// LEXER |
| 175 |
|
| 176 |
// §3.10.1 Integer Literals |
| 177 |
|
| 178 |
IntegerLiteral |
| 179 |
: DecimalIntegerLiteral |
| 180 |
| HexIntegerLiteral |
| 181 |
| OctalIntegerLiteral |
| 182 |
| BinaryIntegerLiteral |
| 183 |
; |
| 184 |
|
| 185 |
fragment |
| 186 |
DecimalIntegerLiteral |
| 187 |
: DecimalNumeral IntegerTypeSuffix? |
| 188 |
; |
| 189 |
|
| 190 |
fragment |
| 191 |
HexIntegerLiteral |
| 192 |
: HexNumeral IntegerTypeSuffix? |
| 193 |
; |
| 194 |
|
| 195 |
fragment |
| 196 |
OctalIntegerLiteral |
| 197 |
: OctalNumeral IntegerTypeSuffix? |
| 198 |
; |
| 199 |
|
| 200 |
fragment |
| 201 |
BinaryIntegerLiteral |
| 202 |
: BinaryNumeral IntegerTypeSuffix? |
| 203 |
; |
| 204 |
|
| 205 |
fragment |
| 206 |
IntegerTypeSuffix |
| 207 |
: [lL] |
| 208 |
; |
| 209 |
|
| 210 |
fragment |
| 211 |
DecimalNumeral |
| 212 |
: '0' |
| 213 |
| NonZeroDigit (Digits? | Underscores Digits) |
| 214 |
; |
| 215 |
|
| 216 |
fragment |
| 217 |
Digits |
| 218 |
: Digit (DigitOrUnderscore* Digit)? |
| 219 |
; |
| 220 |
|
| 221 |
fragment |
| 222 |
Digit |
| 223 |
: '0' |
| 224 |
| NonZeroDigit |
| 225 |
; |
| 226 |
|
| 227 |
fragment |
| 228 |
NonZeroDigit |
| 229 |
: [1-9] |
| 230 |
; |
| 231 |
|
| 232 |
fragment |
| 233 |
DigitOrUnderscore |
| 234 |
: Digit |
| 235 |
| '_' |
| 236 |
; |
| 237 |
|
| 238 |
fragment |
| 239 |
Underscores |
| 240 |
: '_'+ |
| 241 |
; |
| 242 |
|
| 243 |
fragment |
| 244 |
HexNumeral |
| 245 |
: '0' [xX] HexDigits |
| 246 |
; |
| 247 |
|
| 248 |
fragment |
| 249 |
HexDigits |
| 250 |
: HexDigit (HexDigitOrUnderscore* HexDigit)? |
| 251 |
; |
| 252 |
|
| 253 |
fragment |
| 254 |
HexDigit |
| 255 |
: [0-9a-fA-F] |
| 256 |
; |
| 257 |
|
| 258 |
fragment |
| 259 |
HexDigitOrUnderscore |
| 260 |
: HexDigit |
| 261 |
| '_' |
| 262 |
; |
| 263 |
|
| 264 |
fragment |
| 265 |
OctalNumeral |
| 266 |
: '0' Underscores? OctalDigits |
| 267 |
; |
| 268 |
|
| 269 |
fragment |
| 270 |
OctalDigits |
| 271 |
: OctalDigit (OctalDigitOrUnderscore* OctalDigit)? |
| 272 |
; |
| 273 |
|
| 274 |
fragment |
| 275 |
OctalDigit |
| 276 |
: [0-7] |
| 277 |
; |
| 278 |
|
| 279 |
fragment |
| 280 |
OctalDigitOrUnderscore |
| 281 |
: OctalDigit |
| 282 |
| '_' |
| 283 |
; |
| 284 |
|
| 285 |
fragment |
| 286 |
BinaryNumeral |
| 287 |
: '0' [bB] BinaryDigits |
| 288 |
; |
| 289 |
|
| 290 |
fragment |
| 291 |
BinaryDigits |
| 292 |
: BinaryDigit (BinaryDigitOrUnderscore* BinaryDigit)? |
| 293 |
; |
| 294 |
|
| 295 |
fragment |
| 296 |
BinaryDigit |
| 297 |
: [01] |
| 298 |
; |
| 299 |
|
| 300 |
fragment |
| 301 |
BinaryDigitOrUnderscore |
| 302 |
: BinaryDigit |
| 303 |
| '_' |
| 304 |
; |
| 305 |
|
| 306 |
// §3.10.2 Floating-Point Literals |
| 307 |
|
| 308 |
FloatingPointLiteral |
| 309 |
: DecimalFloatingPointLiteral |
| 310 |
| HexadecimalFloatingPointLiteral |
| 311 |
; |
| 312 |
|
| 313 |
fragment |
| 314 |
DecimalFloatingPointLiteral |
| 315 |
: Digits '.' Digits? ExponentPart? FloatTypeSuffix? |
| 316 |
| '.' Digits ExponentPart? FloatTypeSuffix? |
| 317 |
| Digits ExponentPart FloatTypeSuffix? |
| 318 |
| Digits FloatTypeSuffix |
| 319 |
; |
| 320 |
|
| 321 |
fragment |
| 322 |
ExponentPart |
| 323 |
: ExponentIndicator SignedInteger |
| 324 |
; |
| 325 |
|
| 326 |
fragment |
| 327 |
ExponentIndicator |
| 328 |
: [eE] |
| 329 |
; |
| 330 |
|
| 331 |
fragment |
| 332 |
SignedInteger |
| 333 |
: Sign? Digits |
| 334 |
; |
| 335 |
|
| 336 |
fragment |
| 337 |
Sign |
| 338 |
: [+-] |
| 339 |
; |
| 340 |
|
| 341 |
fragment |
| 342 |
FloatTypeSuffix |
| 343 |
: [fFdD] |
| 344 |
; |
| 345 |
|
| 346 |
fragment |
| 347 |
HexadecimalFloatingPointLiteral |
| 348 |
: HexSignificand BinaryExponent FloatTypeSuffix? |
| 349 |
; |
| 350 |
|
| 351 |
fragment |
| 352 |
HexSignificand |
| 353 |
: HexNumeral '.'? |
| 354 |
| '0' [xX] HexDigits? '.' HexDigits |
| 355 |
; |
| 356 |
|
| 357 |
fragment |
| 358 |
BinaryExponent |
| 359 |
: BinaryExponentIndicator SignedInteger |
| 360 |
; |
| 361 |
|
| 362 |
fragment |
| 363 |
BinaryExponentIndicator |
| 364 |
: [pP] |
| 365 |
; |
| 366 |
|
| 367 |
// §3.10.3 Boolean Literals |
| 368 |
|
| 369 |
BooleanLiteral |
| 370 |
: 'true' |
| 371 |
| 'false' |
| 372 |
; |
| 373 |
|
| 374 |
// §3.10.4 Character Literals |
| 375 |
|
| 376 |
CharacterLiteral |
| 377 |
: '\'' SingleCharacter '\'' |
| 378 |
| '\'' EscapeSequence '\'' |
| 379 |
; |
| 380 |
|
| 381 |
fragment |
| 382 |
SingleCharacter |
| 383 |
: ~['\\] |
| 384 |
; |
| 385 |
// §3.10.5 String Literals |
| 386 |
SingleQuoteString |
| 387 |
: '`' SingleQuoteStringCharacter* '`' |
| 388 |
; |
| 389 |
|
| 390 |
DoubleQuoteString |
| 391 |
: '"' StringCharacters? '"' |
| 392 |
; |
| 393 |
|
| 394 |
fragment |
| 395 |
StringCharacters |
| 396 |
: StringCharacter+ |
| 397 |
; |
| 398 |
fragment |
| 399 |
StringCharacter |
| 400 |
: ~["\\] |
| 401 |
| EscapeSequence |
| 402 |
; |
| 403 |
fragment |
| 404 |
SingleQuoteStringCharacter |
| 405 |
: ~[`\\] |
| 406 |
| EscapeSequence |
| 407 |
; |
| 408 |
|
| 409 |
// §3.10.6 Escape Sequences for Character and String Literals |
| 410 |
fragment |
| 411 |
EscapeSequence |
| 412 |
: '\\' [btnfr"'`\\] |
| 413 |
| OctalEscape |
| 414 |
| UnicodeEscape |
| 415 |
; |
| 416 |
|
| 417 |
fragment |
| 418 |
OctalEscape |
| 419 |
: '\\' OctalDigit |
| 420 |
| '\\' OctalDigit OctalDigit |
| 421 |
| '\\' ZeroToThree OctalDigit OctalDigit |
| 422 |
; |
| 423 |
|
| 424 |
fragment |
| 425 |
UnicodeEscape |
| 426 |
: '\\' 'u' HexDigit HexDigit HexDigit HexDigit |
| 427 |
; |
| 428 |
|
| 429 |
fragment |
| 430 |
ZeroToThree |
| 431 |
: [0-3] |
| 432 |
; |
| 433 |
|
| 434 |
// §3.10.7 The Null Literal |
| 435 |
|
| 436 |
NullLiteral |
| 437 |
: 'null' |
| 438 |
; |
| 439 |
|
| 440 |
// §3.8 Identifiers (must appear after all keywords in the grammar) |
| 441 |
|
| 442 |
Identifier |
| 443 |
: JavaLetter JavaLetterOrDigit* |
| 444 |
; |
| 445 |
|
| 446 |
fragment |
| 447 |
JavaLetter |
| 448 |
: [a-zA-Z$_] // these are the "java letters" below 0xFF |
| 449 |
| // covers all characters above 0xFF which are not a surrogate |
| 450 |
~[\u0000-\u00FF\uD800-\uDBFF] |
| 451 |
{Character.isJavaIdentifierStart(_input.LA(-1))}? |
| 452 |
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF |
| 453 |
[\uD800-\uDBFF] [\uDC00-\uDFFF] |
| 454 |
{Character.isJavaIdentifierStart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}? |
| 455 |
; |
| 456 |
|
| 457 |
fragment |
| 458 |
JavaLetterOrDigit |
| 459 |
: [a-zA-Z0-9$_] // these are the "java letters or digits" below 0xFF |
| 460 |
| // covers all characters above 0xFF which are not a surrogate |
| 461 |
~[\u0000-\u00FF\uD800-\uDBFF] |
| 462 |
{Character.isJavaIdentifierPart(_input.LA(-1))}? |
| 463 |
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF |
| 464 |
[\uD800-\uDBFF] [\uDC00-\uDFFF] |
| 465 |
{Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}? |
| 466 |
; |
| 467 |
|
| 468 |
// |
| 469 |
// Whitespace and comments |
| 470 |
// |
| 471 |
|
| 472 |
WS : [ \t\r\n\u000C]+ -> skip |
| 473 |
; |
| 474 |
|
| 475 |
// |
| 476 |
// Resource references |
| 477 |
// |
| 478 |
|
| 479 |
ResourceReference |
| 480 |
: '@' (PackageName ':')? ResourceType '/' Identifier |
| 481 |
; |
| 482 |
|
| 483 |
PackageName |
| 484 |
: 'android' |
| 485 |
| Identifier |
| 486 |
; |
| 487 |
|
| 488 |
ResourceType |
| 489 |
: 'anim' |
| 490 |
| 'animator' |
| 491 |
| 'bool' |
| 492 |
| 'color' |
| 493 |
| 'colorStateList' |
| 494 |
| 'dimen' |
| 495 |
| 'dimenOffset' |
| 496 |
| 'dimenSize' |
| 497 |
| 'drawable' |
| 498 |
| 'fraction' |
| 499 |
| 'id' |
| 500 |
| 'integer' |
| 501 |
| 'intArray' |
| 502 |
| 'interpolator' |
| 503 |
| 'layout' |
| 504 |
| 'plurals' |
| 505 |
| 'stateListAnimator' |
| 506 |
| 'string' |
| 507 |
| 'stringArray' |
| 508 |
| 'transition' |
| 509 |
| 'typedArray' |
| 510 |
; |