forked from luoyesiqiu/Lexer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
java.flex
284 lines (233 loc) · 12.3 KB
/
java.flex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright (C) 1998-2015 Gerwin Klein <[email protected]> *
* All rights reserved. *
* *
* License: BSD *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Java 1.2 language lexer specification */
/* Use together with unicode.flex for Unicode preprocesssing */
/* and java12.cup for a Java 1.2 parser */
/* Note that this lexer specification is not tuned for speed.
It is in fact quite slow on integer and floating point literals,
because the input is read twice and the methods used to parse
the numbers are not very fast.
For a production quality application (e.g. a Java compiler)
this could be optimized */
package lexer;
%%
%public
%class JavaLexer
%unicode
%type JavaType
%line
%column
%{
private long parseLong(int start, int end, int radix) {
long result = 0;
long digit;
for (int i = start; i < end; i++) {
digit = Character.digit(yycharat(i),radix);
result*= radix;
result+= digit;
}
return result;
}
%}
/* main character classes */
LineTerminator = \r|\n|\r\n
InputCharacter = [^\r\n]
WhiteSpace = {LineTerminator} | [ \t\f]
/* comments */
Comment = {TraditionalComment} | {EndOfLineComment} |
{DocumentationComment}
TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/"
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}?
DocumentationComment = "/*" "*"+ [^/*] ~"*/"
/* identifiers */
Identifier = [:jletter:][:jletterdigit:]*
/* integer literals */
DecIntegerLiteral = 0 | [1-9][0-9]*
DecLongLiteral = {DecIntegerLiteral} [lL]
HexIntegerLiteral = 0 [xX] 0* {HexDigit} {1,8}
HexLongLiteral = 0 [xX] 0* {HexDigit} {1,16} [lL]
HexDigit = [0-9a-fA-F]
OctIntegerLiteral = 0+ [1-3]? {OctDigit} {1,15}
OctLongLiteral = 0+ 1? {OctDigit} {1,21} [lL]
OctDigit = [0-7]
/* floating point literals */
FloatLiteral = ({FLit1}|{FLit2}|{FLit3}) {Exponent}? [fF]
DoubleLiteral = ({FLit1}|{FLit2}|{FLit3}) {Exponent}?
FLit1 = [0-9]+ \. [0-9]*
FLit2 = \. [0-9]+
FLit3 = [0-9]+
Exponent = [eE] [+-]? [0-9]+
/* string and character literals */
StringCharacter = [^\r\n\"\\]
SingleCharacter = [^\r\n\'\\]
%state STRING, CHARLITERAL
%%
<YYINITIAL> {
/* keywords */
"abstract" { return JavaType.KEYWORD; }
"boolean" { return JavaType.KEYWORD; }
"break" { return JavaType.KEYWORD;}
"byte" { return JavaType.KEYWORD; }
"case" { return JavaType.KEYWORD; }
"catch" { return JavaType.KEYWORD; }
"char" { return JavaType.KEYWORD; }
"class" { return JavaType.KEYWORD; }
"const" { return JavaType.KEYWORD; }
"continue" { return JavaType.KEYWORD; }
"do" { return JavaType.KEYWORD; }
"double" { return JavaType.KEYWORD; }
"else" { return JavaType.KEYWORD; }
"extends" { return JavaType.KEYWORD; }
"final" { return JavaType.KEYWORD; }
"finally" { return JavaType.KEYWORD; }
"float" { return JavaType.KEYWORD; }
"for" { return JavaType.KEYWORD; }
"default" { return JavaType.KEYWORD; }
"implements" { return JavaType.KEYWORD; }
"import" { return JavaType.KEYWORD; }
"instanceof" { return JavaType.KEYWORD; }
"int" { return JavaType.KEYWORD; }
"interface" { return JavaType.KEYWORD; }
"long" { return JavaType.KEYWORD; }
"native" { return JavaType.KEYWORD; }
"new" { return JavaType.KEYWORD; }
"goto" { return JavaType.KEYWORD; }
"if" { return JavaType.KEYWORD; }
"public" { return JavaType.KEYWORD; }
"short" { return JavaType.KEYWORD; }
"super" { return JavaType.KEYWORD; }
"switch" { return JavaType.KEYWORD; }
"synchronized" { return JavaType.KEYWORD; }
"package" { return JavaType.KEYWORD; }
"private" { return JavaType.KEYWORD; }
"protected" { return JavaType.KEYWORD; }
"transient" { return JavaType.KEYWORD; }
"return" { return JavaType.KEYWORD; }
"void" { return JavaType.KEYWORD; }
"static" { return JavaType.KEYWORD; }
"while" { return JavaType.KEYWORD; }
"this" { return JavaType.KEYWORD; }
"throw" { return JavaType.KEYWORD; }
"throws" { return JavaType.KEYWORD; }
"try" { return JavaType.KEYWORD; }
"volatile" { return JavaType.KEYWORD; }
"strictfp" { return JavaType.KEYWORD; }
/* boolean literals */
"true" { return JavaType.BOOLEAN_LITERAL; }
"false" { return JavaType.BOOLEAN_LITERAL; }
/* null literal */
"null" { return JavaType.NULL_LITERAL; }
/* separators */
"(" { return JavaType.LPAREN; }
")" { return JavaType.RPAREN; }
"{" { return JavaType.LBRACE; }
"}" { return JavaType.RBRACE; }
"[" { return JavaType.LBRACK; }
"]" { return JavaType.RBRACK; }
";" { return JavaType.SEMICOLON; }
"," { return JavaType.COMMA; }
"." { return JavaType.DOT; }
/* operators */
"=" { return JavaType.EQ; }
">" { return JavaType.GT; }
"<" { return JavaType.LT; }
"!" { return JavaType.NOT; }
"~" { return JavaType.COMP; }
"?" { return JavaType.QUESTION; }
":" { return JavaType.COLON; }
"==" { return JavaType.EQEQ; }
"<=" { return JavaType.LTEQ; }
">=" { return JavaType.GTEQ; }
"!=" { return JavaType.NOTEQ; }
"&&" { return JavaType.ANDAND; }
"||" { return JavaType.OROR; }
"++" { return JavaType.PLUSPLUS; }
"--" { return JavaType.MINUSMINUS; }
"+" { return JavaType.PLUS; }
"-" { return JavaType.MINUS; }
"*" { return JavaType.MULT; }
"/" { return JavaType.DIV; }
"&" { return JavaType.AND; }
"|" { return JavaType.OR; }
"^" { return JavaType.XOR; }
"%" { return JavaType.MOD; }
"<<" { return JavaType.LSHIFT; }
">>" { return JavaType.RSHIFT; }
">>>" { return JavaType.URSHIFT; }
"+=" { return JavaType.PLUSEQ; }
"-=" { return JavaType.MINUSEQ; }
"*=" { return JavaType.MULTEQ; }
"/=" { return JavaType.DIVEQ; }
"&=" { return JavaType.ANDEQ; }
"|=" { return JavaType.OREQ; }
"^=" { return JavaType.XOREQ; }
"%=" { return JavaType.MODEQ; }
"<<=" { return JavaType.LSHIFTEQ; }
">>=" { return JavaType.RSHIFTEQ; }
">>>=" { return JavaType.URSHIFTEQ; }
/* string literal */
\" { yybegin(STRING);return JavaType.STRING; }
/* character literal */
\' { yybegin(CHARLITERAL);return JavaType.CHARACTER_LITERAL; }
/* numeric literals */
/* This is matched together with the minus, because the number is too big to
be represented by a positive integer. */
"-2147483648" { return JavaType.INTEGER_LITERAL; }
{DecIntegerLiteral} { return JavaType.INTEGER_LITERAL; }
{DecLongLiteral} { return JavaType.INTEGER_LITERAL; }
{HexIntegerLiteral} { return JavaType.INTEGER_LITERAL; }
{HexLongLiteral} { return JavaType.INTEGER_LITERAL; }
{OctIntegerLiteral} { return JavaType.INTEGER_LITERAL; }
{OctLongLiteral} { return JavaType.INTEGER_LITERAL; }
{FloatLiteral} { return JavaType.FLOATING_POINT_LITERAL; }
{DoubleLiteral} { return JavaType.FLOATING_POINT_LITERAL; }
{DoubleLiteral}[dD] { return JavaType.FLOATING_POINT_LITERAL; }
/* comments */
{Comment} { return JavaType.COMMENT; }
/* whitespace */
{WhiteSpace} { return JavaType.WHITESPACE; }
/* identifiers */
{Identifier} { return JavaType.IDENTIFIER; }
}
<STRING> {
\" { yybegin(YYINITIAL);return JavaType.STRING; }
{StringCharacter}+ { return JavaType.STRING; }
/* escape sequences */
"\\b" { return JavaType.STRING; }
"\\t" { return JavaType.STRING; }
"\\n" { return JavaType.STRING; }
"\\f" { return JavaType.STRING; }
"\\r" { return JavaType.STRING; }
"\\\"" { return JavaType.STRING; }
"\\'" { return JavaType.STRING; }
"\\\\" { return JavaType.STRING; }
\\[0-3]?{OctDigit}?{OctDigit} { return JavaType.STRING; }
/* error cases */
\\. { throw new RuntimeException("Illegal escape sequence \""+yytext()+"\""); }
{LineTerminator} { throw new RuntimeException("Unterminated string at end of line"); }
}
<CHARLITERAL> {
{SingleCharacter}\' { yybegin(YYINITIAL); return JavaType.CHARACTER_LITERAL; }
/* escape sequences */
"\\b"\' { yybegin(YYINITIAL); return JavaType.CHARACTER_LITERAL;}
"\\t"\' { yybegin(YYINITIAL); return JavaType.CHARACTER_LITERAL;}
"\\n"\' { yybegin(YYINITIAL); return JavaType.CHARACTER_LITERAL;}
"\\f"\' { yybegin(YYINITIAL); return JavaType.CHARACTER_LITERAL;}
"\\r"\' { yybegin(YYINITIAL); return JavaType.CHARACTER_LITERAL;}
"\\\""\' { yybegin(YYINITIAL); return JavaType.CHARACTER_LITERAL;}
"\\'"\' { yybegin(YYINITIAL); return JavaType.CHARACTER_LITERAL;}
"\\\\"\' { yybegin(YYINITIAL); return JavaType.CHARACTER_LITERAL; }
\\[0-3]?{OctDigit}?{OctDigit}\' { yybegin(YYINITIAL); return JavaType.CHARACTER_LITERAL; }
/* error cases */
\\. { throw new RuntimeException("Illegal escape sequence \""+yytext()+"\""); }
{LineTerminator} { throw new RuntimeException("Unterminated character literal at end of line"); }
}
/* error fallback */
[^] { throw new RuntimeException("Illegal character \""+yytext()+
"\" at line "+yyline+", column "+yycolumn); }
<<EOF>> { return JavaType.EOF; }