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