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