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