|
| 1 | +/* |
| 2 | + [The "BSD licence"] |
| 3 | + Copyright (c) 2013 Terence Parr, Sam Harwell |
| 4 | + Copyright (c) 2017 Ivan Kochurkin (upgrade to Java 8) |
| 5 | + Copyright (c) 2021 Michał Lorek (upgrade to Java 11) |
| 6 | + Copyright (c) 2022 Michał Lorek (upgrade to Java 17) |
| 7 | + All rights reserved. |
| 8 | +
|
| 9 | + Redistribution and use in source and binary forms, with or without |
| 10 | + modification, are permitted provided that the following conditions |
| 11 | + are met: |
| 12 | + 1. Redistributions of source code must retain the above copyright |
| 13 | + notice, this list of conditions and the following disclaimer. |
| 14 | + 2. Redistributions in binary form must reproduce the above copyright |
| 15 | + notice, this list of conditions and the following disclaimer in the |
| 16 | + documentation and/or other materials provided with the distribution. |
| 17 | + 3. The name of the author may not be used to endorse or promote products |
| 18 | + derived from this software without specific prior written permission. |
| 19 | +
|
| 20 | + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 21 | + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 22 | + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 23 | + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 29 | + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | +*/ |
| 31 | + |
| 32 | +lexer grammar JavaLexer; |
| 33 | + |
| 34 | +// Keywords |
| 35 | + |
| 36 | +ABSTRACT: 'abstract'; |
| 37 | +ASSERT: 'assert'; |
| 38 | +BOOLEAN: 'boolean'; |
| 39 | +BREAK: 'break'; |
| 40 | +BYTE: 'byte'; |
| 41 | +CASE: 'case'; |
| 42 | +CATCH: 'catch'; |
| 43 | +CHAR: 'char'; |
| 44 | +CLASS: 'class'; |
| 45 | +CONST: 'const'; |
| 46 | +CONTINUE: 'continue'; |
| 47 | +DEFAULT: 'default'; |
| 48 | +DO: 'do'; |
| 49 | +DOUBLE: 'double'; |
| 50 | +ELSE: 'else'; |
| 51 | +ENUM: 'enum'; |
| 52 | +EXTENDS: 'extends'; |
| 53 | +FINAL: 'final'; |
| 54 | +FINALLY: 'finally'; |
| 55 | +FLOAT: 'float'; |
| 56 | +FOR: 'for'; |
| 57 | +IF: 'if'; |
| 58 | +GOTO: 'goto'; |
| 59 | +IMPLEMENTS: 'implements'; |
| 60 | +IMPORT: 'import'; |
| 61 | +INSTANCEOF: 'instanceof'; |
| 62 | +INT: 'int'; |
| 63 | +INTERFACE: 'interface'; |
| 64 | +LONG: 'long'; |
| 65 | +NATIVE: 'native'; |
| 66 | +NEW: 'new'; |
| 67 | +PACKAGE: 'package'; |
| 68 | +PRIVATE: 'private'; |
| 69 | +PROTECTED: 'protected'; |
| 70 | +PUBLIC: 'public'; |
| 71 | +RETURN: 'return'; |
| 72 | +SHORT: 'short'; |
| 73 | +STATIC: 'static'; |
| 74 | +STRICTFP: 'strictfp'; |
| 75 | +SUPER: 'super'; |
| 76 | +SWITCH: 'switch'; |
| 77 | +SYNCHRONIZED: 'synchronized'; |
| 78 | +THIS: 'this'; |
| 79 | +THROW: 'throw'; |
| 80 | +THROWS: 'throws'; |
| 81 | +TRANSIENT: 'transient'; |
| 82 | +TRY: 'try'; |
| 83 | +VOID: 'void'; |
| 84 | +VOLATILE: 'volatile'; |
| 85 | +WHILE: 'while'; |
| 86 | + |
| 87 | +// Module related keywords |
| 88 | +MODULE: 'module'; |
| 89 | +OPEN: 'open'; |
| 90 | +REQUIRES: 'requires'; |
| 91 | +EXPORTS: 'exports'; |
| 92 | +OPENS: 'opens'; |
| 93 | +TO: 'to'; |
| 94 | +USES: 'uses'; |
| 95 | +PROVIDES: 'provides'; |
| 96 | +WITH: 'with'; |
| 97 | +TRANSITIVE: 'transitive'; |
| 98 | + |
| 99 | +// Local Variable Type Inference |
| 100 | +VAR: 'var'; // reserved type name |
| 101 | + |
| 102 | +// Switch Expressions |
| 103 | +YIELD: 'yield'; // reserved type name from Java 14 |
| 104 | + |
| 105 | +// Records |
| 106 | +RECORD: 'record'; |
| 107 | + |
| 108 | +// Sealed Classes |
| 109 | +SEALED: 'sealed'; |
| 110 | +PERMITS: 'permits'; |
| 111 | +NON_SEALED: 'non-sealed'; |
| 112 | + |
| 113 | +// Literals |
| 114 | + |
| 115 | +DECIMAL_LITERAL: ('0' | [1-9] (Digits? | '_'+ Digits)) [lL]?; |
| 116 | +HEX_LITERAL: '0' [xX] [0-9a-fA-F] ([0-9a-fA-F_]* [0-9a-fA-F])? [lL]?; |
| 117 | +OCT_LITERAL: '0' '_'* [0-7] ([0-7_]* [0-7])? [lL]?; |
| 118 | +BINARY_LITERAL: '0' [bB] [01] ([01_]* [01])? [lL]?; |
| 119 | + |
| 120 | +FLOAT_LITERAL: (Digits '.' Digits? | '.' Digits) ExponentPart? [fFdD]? |
| 121 | + | Digits (ExponentPart [fFdD]? | [fFdD]) |
| 122 | + ; |
| 123 | + |
| 124 | +HEX_FLOAT_LITERAL: '0' [xX] (HexDigits '.'? | HexDigits? '.' HexDigits) [pP] [+-]? Digits [fFdD]?; |
| 125 | + |
| 126 | +BOOL_LITERAL: 'true' |
| 127 | + | 'false' |
| 128 | + ; |
| 129 | + |
| 130 | +CHAR_LITERAL: '\'' (~['\\\r\n] | EscapeSequence) '\''; |
| 131 | + |
| 132 | +STRING_LITERAL: '"' (~["\\\r\n] | EscapeSequence)* '"'; |
| 133 | +
|
| 134 | +TEXT_BLOCK: '"""' [ \t]* [\r\n] (. | EscapeSequence)*? '"""'; |
| 135 | +
|
| 136 | +NULL_LITERAL: 'null'; |
| 137 | +
|
| 138 | +// Separators |
| 139 | +
|
| 140 | +LPAREN: '('; |
| 141 | +RPAREN: ')'; |
| 142 | +LBRACE: '{'; |
| 143 | +RBRACE: '}'; |
| 144 | +LBRACK: '['; |
| 145 | +RBRACK: ']'; |
| 146 | +SEMI: ';'; |
| 147 | +COMMA: ','; |
| 148 | +DOT: '.'; |
| 149 | +
|
| 150 | +// Operators |
| 151 | +
|
| 152 | +ASSIGN: '='; |
| 153 | +GT: '>'; |
| 154 | +LT: '<'; |
| 155 | +BANG: '!'; |
| 156 | +TILDE: '~'; |
| 157 | +QUESTION: '?'; |
| 158 | +COLON: ':'; |
| 159 | +EQUAL: '=='; |
| 160 | +LE: '<='; |
| 161 | +GE: '>='; |
| 162 | +NOTEQUAL: '!='; |
| 163 | +AND: '&&'; |
| 164 | +OR: '||'; |
| 165 | +INC: '++'; |
| 166 | +DEC: '--'; |
| 167 | +ADD: '+'; |
| 168 | +SUB: '-'; |
| 169 | +MUL: '*'; |
| 170 | +DIV: '/'; |
| 171 | +BITAND: '&'; |
| 172 | +BITOR: '|'; |
| 173 | +CARET: '^'; |
| 174 | +MOD: '%'; |
| 175 | +
|
| 176 | +ADD_ASSIGN: '+='; |
| 177 | +SUB_ASSIGN: '-='; |
| 178 | +MUL_ASSIGN: '*='; |
| 179 | +DIV_ASSIGN: '/='; |
| 180 | +AND_ASSIGN: '&='; |
| 181 | +OR_ASSIGN: '|='; |
| 182 | +XOR_ASSIGN: '^='; |
| 183 | +MOD_ASSIGN: '%='; |
| 184 | +LSHIFT_ASSIGN: '<<='; |
| 185 | +RSHIFT_ASSIGN: '>>='; |
| 186 | +URSHIFT_ASSIGN: '>>>='; |
| 187 | +
|
| 188 | +// Java 8 tokens |
| 189 | +
|
| 190 | +ARROW: '->'; |
| 191 | +COLONCOLON: '::'; |
| 192 | +
|
| 193 | +// Additional symbols not defined in the lexical specification |
| 194 | +
|
| 195 | +AT: '@'; |
| 196 | +ELLIPSIS: '...'; |
| 197 | +
|
| 198 | +// Whitespace and comments |
| 199 | +
|
| 200 | +WS: [ \t\r\n\u000C]+ -> channel(HIDDEN); |
| 201 | +COMMENT: '/*' .*? '*/' -> channel(HIDDEN); |
| 202 | +LINE_COMMENT: '//' ~[\r\n]* -> channel(HIDDEN); |
| 203 | + |
| 204 | +// Identifiers |
| 205 | + |
| 206 | +IDENTIFIER: Letter LetterOrDigit*; |
| 207 | + |
| 208 | +// Fragment rules |
| 209 | + |
| 210 | +fragment ExponentPart |
| 211 | + : [eE] [+-]? Digits |
| 212 | + ; |
| 213 | + |
| 214 | +fragment EscapeSequence |
| 215 | + : '\\' 'u005c'? [btnfr"'\\] |
| 216 | + | '\\' 'u005c'? ([0-3]? [0-7])? [0-7] |
| 217 | + | '\\' 'u'+ HexDigit HexDigit HexDigit HexDigit |
| 218 | + ; |
| 219 | +
|
| 220 | +fragment HexDigits |
| 221 | + : HexDigit ((HexDigit | '_')* HexDigit)? |
| 222 | + ; |
| 223 | +
|
| 224 | +fragment HexDigit |
| 225 | + : [0-9a-fA-F] |
| 226 | + ; |
| 227 | +
|
| 228 | +fragment Digits |
| 229 | + : [0-9] ([0-9_]* [0-9])? |
| 230 | + ; |
| 231 | +
|
| 232 | +fragment LetterOrDigit |
| 233 | + : Letter |
| 234 | + | [0-9] |
| 235 | + ; |
| 236 | +
|
| 237 | +fragment Letter |
| 238 | + : [a-zA-Z$_] // these are the "java letters" below 0x7F |
| 239 | + | ~[\u0000-\u007F\uD800-\uDBFF] // covers all characters above 0x7F which are not a surrogate |
| 240 | + | [\uD800-\uDBFF] [\uDC00-\uDFFF] // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF |
| 241 | + ; |
0 commit comments