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