| 1 |
/* |
| 2 |
[The "BSD licence"] |
| 3 |
Copyright (c) 2013 Terence Parr, Sam Harwell |
| 4 |
All rights reserved. |
| 5 |
|
| 6 |
Redistribution and use in source and binary forms, with or without |
| 7 |
modification, are permitted provided that the following conditions |
| 8 |
are met: |
| 9 |
1. Redistributions of source code must retain the above copyright |
| 10 |
notice, this list of conditions and the following disclaimer. |
| 11 |
2. Redistributions in binary form must reproduce the above copyright |
| 12 |
notice, this list of conditions and the following disclaimer in the |
| 13 |
documentation and/or other materials provided with the distribution. |
| 14 |
3. The name of the author may not be used to endorse or promote products |
| 15 |
derived from this software without specific prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 |
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 |
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
lexer grammar Java7Lexer; |
| 30 |
|
| 31 |
channels { |
| 32 |
CHANNEL_COMMENT |
| 33 |
} |
| 34 |
|
| 35 |
// §3.9 Keywords |
| 36 |
|
| 37 |
ABSTRACT : 'abstract'; |
| 38 |
ASSERT : 'assert'; |
| 39 |
BOOLEAN : 'boolean'; |
| 40 |
BREAK : 'break'; |
| 41 |
BYTE : 'byte'; |
| 42 |
CASE : 'case'; |
| 43 |
CATCH : 'catch'; |
| 44 |
CHAR : 'char'; |
| 45 |
CLASS : 'class'; |
| 46 |
CONST : 'const'; |
| 47 |
CONTINUE : 'continue'; |
| 48 |
DEFAULT : 'default'; |
| 49 |
DO : 'do'; |
| 50 |
DOUBLE : 'double'; |
| 51 |
ELSE : 'else'; |
| 52 |
ENUM : 'enum'; |
| 53 |
EXTENDS : 'extends'; |
| 54 |
FINAL : 'final'; |
| 55 |
FINALLY : 'finally'; |
| 56 |
FLOAT : 'float'; |
| 57 |
FOR : 'for'; |
| 58 |
IF : 'if'; |
| 59 |
GOTO : 'goto'; |
| 60 |
IMPLEMENTS : 'implements'; |
| 61 |
IMPORT : 'import'; |
| 62 |
INSTANCEOF : 'instanceof'; |
| 63 |
INT : 'int'; |
| 64 |
INTERFACE : 'interface'; |
| 65 |
LONG : 'long'; |
| 66 |
NATIVE : 'native'; |
| 67 |
NEW : 'new'; |
| 68 |
PACKAGE : 'package'; |
| 69 |
PRIVATE : 'private'; |
| 70 |
PROTECTED : 'protected'; |
| 71 |
PUBLIC : 'public'; |
| 72 |
RETURN : 'return'; |
| 73 |
SHORT : 'short'; |
| 74 |
STATIC : 'static'; |
| 75 |
STRICTFP : 'strictfp'; |
| 76 |
SUPER : 'super'; |
| 77 |
SWITCH : 'switch'; |
| 78 |
SYNCHRONIZED : 'synchronized'; |
| 79 |
THIS : 'this'; |
| 80 |
THROW : 'throw'; |
| 81 |
THROWS : 'throws'; |
| 82 |
TRANSIENT : 'transient'; |
| 83 |
TRY : 'try'; |
| 84 |
VOID : 'void'; |
| 85 |
VOLATILE : 'volatile'; |
| 86 |
WHILE : 'while'; |
| 87 |
|
| 88 |
// §3.10.1 Integer Literals |
| 89 |
|
| 90 |
IntegerLiteral |
| 91 |
: DecimalIntegerLiteral |
| 92 |
| HexIntegerLiteral |
| 93 |
| OctalIntegerLiteral |
| 94 |
| BinaryIntegerLiteral |
| 95 |
; |
| 96 |
|
| 97 |
fragment |
| 98 |
DecimalIntegerLiteral |
| 99 |
: DecimalNumeral IntegerTypeSuffix? |
| 100 |
; |
| 101 |
|
| 102 |
fragment |
| 103 |
HexIntegerLiteral |
| 104 |
: HexNumeral IntegerTypeSuffix? |
| 105 |
; |
| 106 |
|
| 107 |
fragment |
| 108 |
OctalIntegerLiteral |
| 109 |
: OctalNumeral IntegerTypeSuffix? |
| 110 |
; |
| 111 |
|
| 112 |
fragment |
| 113 |
BinaryIntegerLiteral |
| 114 |
: BinaryNumeral IntegerTypeSuffix? |
| 115 |
; |
| 116 |
|
| 117 |
fragment |
| 118 |
IntegerTypeSuffix |
| 119 |
: [lL] |
| 120 |
; |
| 121 |
|
| 122 |
fragment |
| 123 |
DecimalNumeral |
| 124 |
: '0' |
| 125 |
| NonZeroDigit (Digits? | Underscores Digits) |
| 126 |
; |
| 127 |
|
| 128 |
fragment |
| 129 |
Digits |
| 130 |
: Digit (DigitOrUnderscore* Digit)? |
| 131 |
; |
| 132 |
|
| 133 |
fragment |
| 134 |
Digit |
| 135 |
: '0' |
| 136 |
| NonZeroDigit |
| 137 |
; |
| 138 |
|
| 139 |
fragment |
| 140 |
NonZeroDigit |
| 141 |
: [1-9] |
| 142 |
; |
| 143 |
|
| 144 |
fragment |
| 145 |
DigitOrUnderscore |
| 146 |
: Digit |
| 147 |
| '_' |
| 148 |
; |
| 149 |
|
| 150 |
fragment |
| 151 |
Underscores |
| 152 |
: '_'+ |
| 153 |
; |
| 154 |
|
| 155 |
fragment |
| 156 |
HexNumeral |
| 157 |
: '0' [xX] HexDigits |
| 158 |
; |
| 159 |
|
| 160 |
fragment |
| 161 |
HexDigits |
| 162 |
: HexDigit (HexDigitOrUnderscore* HexDigit)? |
| 163 |
; |
| 164 |
|
| 165 |
fragment |
| 166 |
HexDigit |
| 167 |
: [0-9a-fA-F] |
| 168 |
; |
| 169 |
|
| 170 |
fragment |
| 171 |
HexDigitOrUnderscore |
| 172 |
: HexDigit |
| 173 |
| '_' |
| 174 |
; |
| 175 |
|
| 176 |
fragment |
| 177 |
OctalNumeral |
| 178 |
: '0' Underscores? OctalDigits |
| 179 |
; |
| 180 |
|
| 181 |
fragment |
| 182 |
OctalDigits |
| 183 |
: OctalDigit (OctalDigitOrUnderscore* OctalDigit)? |
| 184 |
; |
| 185 |
|
| 186 |
fragment |
| 187 |
OctalDigit |
| 188 |
: [0-7] |
| 189 |
; |
| 190 |
|
| 191 |
fragment |
| 192 |
OctalDigitOrUnderscore |
| 193 |
: OctalDigit |
| 194 |
| '_' |
| 195 |
; |
| 196 |
|
| 197 |
fragment |
| 198 |
BinaryNumeral |
| 199 |
: '0' [bB] BinaryDigits |
| 200 |
; |
| 201 |
|
| 202 |
fragment |
| 203 |
BinaryDigits |
| 204 |
: BinaryDigit (BinaryDigitOrUnderscore* BinaryDigit)? |
| 205 |
; |
| 206 |
|
| 207 |
fragment |
| 208 |
BinaryDigit |
| 209 |
: [01] |
| 210 |
; |
| 211 |
|
| 212 |
fragment |
| 213 |
BinaryDigitOrUnderscore |
| 214 |
: BinaryDigit |
| 215 |
| '_' |
| 216 |
; |
| 217 |
|
| 218 |
// §3.10.2 Floating-Point Literals |
| 219 |
|
| 220 |
FloatingPointLiteral |
| 221 |
: DecimalFloatingPointLiteral |
| 222 |
| HexadecimalFloatingPointLiteral |
| 223 |
; |
| 224 |
|
| 225 |
fragment |
| 226 |
DecimalFloatingPointLiteral |
| 227 |
: Digits '.' Digits? ExponentPart? FloatTypeSuffix? |
| 228 |
| '.' Digits ExponentPart? FloatTypeSuffix? |
| 229 |
| Digits ExponentPart FloatTypeSuffix? |
| 230 |
| Digits FloatTypeSuffix |
| 231 |
; |
| 232 |
|
| 233 |
fragment |
| 234 |
ExponentPart |
| 235 |
: ExponentIndicator SignedInteger |
| 236 |
; |
| 237 |
|
| 238 |
fragment |
| 239 |
ExponentIndicator |
| 240 |
: [eE] |
| 241 |
; |
| 242 |
|
| 243 |
fragment |
| 244 |
SignedInteger |
| 245 |
: Sign? Digits |
| 246 |
; |
| 247 |
|
| 248 |
fragment |
| 249 |
Sign |
| 250 |
: [+-] |
| 251 |
; |
| 252 |
|
| 253 |
fragment |
| 254 |
FloatTypeSuffix |
| 255 |
: [fFdD] |
| 256 |
; |
| 257 |
|
| 258 |
fragment |
| 259 |
HexadecimalFloatingPointLiteral |
| 260 |
: HexSignificand BinaryExponent FloatTypeSuffix? |
| 261 |
; |
| 262 |
|
| 263 |
fragment |
| 264 |
HexSignificand |
| 265 |
: HexNumeral '.'? |
| 266 |
| '0' [xX] HexDigits? '.' HexDigits |
| 267 |
; |
| 268 |
|
| 269 |
fragment |
| 270 |
BinaryExponent |
| 271 |
: BinaryExponentIndicator SignedInteger |
| 272 |
; |
| 273 |
|
| 274 |
fragment |
| 275 |
BinaryExponentIndicator |
| 276 |
: [pP] |
| 277 |
; |
| 278 |
|
| 279 |
// §3.10.3 Boolean Literals |
| 280 |
|
| 281 |
BooleanLiteral |
| 282 |
: 'true' |
| 283 |
| 'false' |
| 284 |
; |
| 285 |
|
| 286 |
// §3.10.4 Character Literals |
| 287 |
|
| 288 |
CharacterLiteral |
| 289 |
: '\'' SingleCharacter '\'' |
| 290 |
| '\'' EscapeSequence '\'' |
| 291 |
; |
| 292 |
|
| 293 |
fragment |
| 294 |
SingleCharacter |
| 295 |
: ~['\\] |
| 296 |
; |
| 297 |
|
| 298 |
// §3.10.5 String Literals |
| 299 |
|
| 300 |
StringLiteral |
| 301 |
: '"' StringCharacters? '"' |
| 302 |
; |
| 303 |
|
| 304 |
fragment |
| 305 |
StringCharacters |
| 306 |
: StringCharacter+ |
| 307 |
; |
| 308 |
|
| 309 |
fragment |
| 310 |
StringCharacter |
| 311 |
: ~["\\] |
| 312 |
| EscapeSequence |
| 313 |
; |
| 314 |
|
| 315 |
// §3.10.6 Escape Sequences for Character and String Literals |
| 316 |
|
| 317 |
fragment |
| 318 |
EscapeSequence |
| 319 |
: '\\' [btnfr"'\\] |
| 320 |
| OctalEscape |
| 321 |
| UnicodeEscape |
| 322 |
; |
| 323 |
|
| 324 |
fragment |
| 325 |
OctalEscape |
| 326 |
: '\\' OctalDigit |
| 327 |
| '\\' OctalDigit OctalDigit |
| 328 |
| '\\' ZeroToThree OctalDigit OctalDigit |
| 329 |
; |
| 330 |
|
| 331 |
fragment |
| 332 |
UnicodeEscape |
| 333 |
: '\\' 'u' HexDigit HexDigit HexDigit HexDigit |
| 334 |
; |
| 335 |
|
| 336 |
fragment |
| 337 |
ZeroToThree |
| 338 |
: [0-3] |
| 339 |
; |
| 340 |
|
| 341 |
// §3.10.7 The Null Literal |
| 342 |
|
| 343 |
NullLiteral |
| 344 |
: 'null' |
| 345 |
; |
| 346 |
|
| 347 |
// §3.11 Separators |
| 348 |
|
| 349 |
LPAREN : '('; |
| 350 |
RPAREN : ')'; |
| 351 |
LBRACE : '{'; |
| 352 |
RBRACE : '}'; |
| 353 |
LBRACK : '['; |
| 354 |
RBRACK : ']'; |
| 355 |
SEMI : ';'; |
| 356 |
COMMA : ','; |
| 357 |
DOT : '.'; |
| 358 |
|
| 359 |
// §3.12 Operators |
| 360 |
|
| 361 |
ASSIGN : '='; |
| 362 |
GT : '>'; |
| 363 |
LT : '<'; |
| 364 |
BANG : '!'; |
| 365 |
TILDE : '~'; |
| 366 |
QUESTION : '?'; |
| 367 |
COLON : ':'; |
| 368 |
EQUAL : '=='; |
| 369 |
LE : '<='; |
| 370 |
GE : '>='; |
| 371 |
NOTEQUAL : '!='; |
| 372 |
AND : '&&'; |
| 373 |
OR : '||'; |
| 374 |
INC : '++'; |
| 375 |
DEC : '--'; |
| 376 |
ADD : '+'; |
| 377 |
SUB : '-'; |
| 378 |
MUL : '*'; |
| 379 |
DIV : '/'; |
| 380 |
BITAND : '&'; |
| 381 |
BITOR : '|'; |
| 382 |
CARET : '^'; |
| 383 |
MOD : '%'; |
| 384 |
|
| 385 |
ADD_ASSIGN : '+='; |
| 386 |
SUB_ASSIGN : '-='; |
| 387 |
MUL_ASSIGN : '*='; |
| 388 |
DIV_ASSIGN : '/='; |
| 389 |
AND_ASSIGN : '&='; |
| 390 |
OR_ASSIGN : '|='; |
| 391 |
XOR_ASSIGN : '^='; |
| 392 |
MOD_ASSIGN : '%='; |
| 393 |
LSHIFT_ASSIGN : '<<='; |
| 394 |
RSHIFT_ASSIGN : '>>='; |
| 395 |
URSHIFT_ASSIGN : '>>>='; |
| 396 |
|
| 397 |
// §3.8 Identifiers (must appear after all keywords in the grammar) |
| 398 |
|
| 399 |
Identifier |
| 400 |
: JavaLetter JavaLetterOrDigit* |
| 401 |
; |
| 402 |
|
| 403 |
fragment |
| 404 |
JavaLetter |
| 405 |
: [a-zA-Z$_] // these are the "java letters" below 0xFF |
| 406 |
| // covers all characters above 0xFF which are not a surrogate |
| 407 |
~[\u0000-\u00FF\uD800-\uDBFF] |
| 408 |
{Character.isJavaIdentifierStart(_input.LA(-1))}? |
| 409 |
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF |
| 410 |
[\uD800-\uDBFF] [\uDC00-\uDFFF] |
| 411 |
{Character.isJavaIdentifierStart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}? |
| 412 |
; |
| 413 |
|
| 414 |
fragment |
| 415 |
JavaLetterOrDigit |
| 416 |
: [a-zA-Z0-9$_] // these are the "java letters or digits" below 0xFF |
| 417 |
| // covers all characters above 0xFF which are not a surrogate |
| 418 |
~[\u0000-\u00FF\uD800-\uDBFF] |
| 419 |
{Character.isJavaIdentifierPart(_input.LA(-1))}? |
| 420 |
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF |
| 421 |
[\uD800-\uDBFF] [\uDC00-\uDFFF] |
| 422 |
{Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}? |
| 423 |
; |
| 424 |
|
| 425 |
// |
| 426 |
// Additional symbols not defined in the lexical specification |
| 427 |
// |
| 428 |
|
| 429 |
AT : '@'; |
| 430 |
ELLIPSIS : '...'; |
| 431 |
|
| 432 |
// |
| 433 |
// Whitespace and comments |
| 434 |
// |
| 435 |
|
| 436 |
WHITESPACE |
| 437 |
: [ \t\r\n\u000C]+ -> channel(HIDDEN) |
| 438 |
; |
| 439 |
|
| 440 |
JAVADOC |
| 441 |
: '/**' .*? '**/' -> channel(CHANNEL_COMMENT) |
| 442 |
; |
| 443 |
|
| 444 |
COMMENT |
| 445 |
: '/*' .*? '*/' -> channel(CHANNEL_COMMENT) |
| 446 |
; |
| 447 |
|
| 448 |
LINE_COMMENT |
| 449 |
: '//' ~[\r\n]* -> channel(CHANNEL_COMMENT) |
| 450 |
; |