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