@@ -118,7 +118,7 @@ public static Set<String> getKeywords() {
118
118
private final int length ;
119
119
120
120
private final List <Token > tokens ;
121
- private final StringBuilder buffer ;
121
+ private final StringBuilder globalBuffer ;
122
122
123
123
private int pos ;
124
124
private int row , col ;
@@ -128,7 +128,7 @@ public Lexer(String input) {
128
128
length = input .length ();
129
129
130
130
tokens = new ArrayList <>();
131
- buffer = new StringBuilder (40 );
131
+ globalBuffer = new StringBuilder (40 );
132
132
row = col = 1 ;
133
133
}
134
134
@@ -184,19 +184,19 @@ private void tokenizeNumber() {
184
184
if (decimal ) {
185
185
addToken (TokenType .DECIMAL_NUMBER , buffer .toString (), startPos );
186
186
} else if (current == 'L' ) {
187
- next ();
187
+ skip ();
188
188
addToken (TokenType .LONG_NUMBER , buffer .toString (), startPos );
189
189
} else {
190
190
addToken (TokenType .NUMBER , buffer .toString (), startPos );
191
191
}
192
192
}
193
193
194
194
private int subTokenizeScientificNumber (Pos startPos ) {
195
- int sign = 1 ;
196
- switch ( next ()) {
197
- case '-' : sign = - 1 ;
198
- case '+' : skip (); break ;
199
- }
195
+ int sign = switch ( next ()) {
196
+ case '-' -> { skip (); yield - 1 ; }
197
+ case '+' -> { skip (); yield 1 ; }
198
+ default -> 1 ;
199
+ };
200
200
201
201
boolean hasValue = false ;
202
202
char current = peek (0 );
@@ -237,7 +237,7 @@ private void tokenizeHexNumber(int skipChars) {
237
237
if (buffer .isEmpty ()) throw error ("Empty HEX value" , startPos );
238
238
if (peek (-1 ) == '_' ) throw error ("HEX value cannot end with _" , startPos , markEndPos ());
239
239
if (current == 'L' ) {
240
- next ();
240
+ skip ();
241
241
addToken (TokenType .HEX_LONG_NUMBER , buffer .toString (), startPos );
242
242
} else {
243
243
addToken (TokenType .HEX_NUMBER , buffer .toString (), startPos );
@@ -320,35 +320,37 @@ private void tokenizeText() {
320
320
while (true ) {
321
321
if (current == '\\' ) {
322
322
current = next ();
323
- switch (current ) {
324
- case '\\' : current = next (); buffer .append ('\\' ); continue ;
325
- case '"' : current = next (); buffer .append ('"' ); continue ;
326
- case '0' : current = next (); buffer .append ('\0' ); continue ;
327
- case 'b' : current = next (); buffer .append ('\b' ); continue ;
328
- case 'f' : current = next (); buffer .append ('\f' ); continue ;
329
- case 'n' : current = next (); buffer .append ('\n' ); continue ;
330
- case 'r' : current = next (); buffer .append ('\r' ); continue ;
331
- case 't' : current = next (); buffer .append ('\t' ); continue ;
332
- case 'u' : // http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.3
333
- int rollbackPosition = pos ;
334
- while (current == 'u' ) current = next ();
335
- int escapedValue = 0 ;
336
- for (int i = 12 ; i >= 0 && escapedValue != -1 ; i -= 4 ) {
337
- if (isHexNumber (current )) {
338
- escapedValue |= (Character .digit (current , 16 ) << i );
339
- } else {
340
- escapedValue = -1 ;
341
- }
342
- current = next ();
343
- }
344
- if (escapedValue >= 0 ) {
345
- buffer .append ((char ) escapedValue );
323
+ if ("\r \n \0 " .indexOf (current ) != -1 ) {
324
+ throw error ("Reached end of line while parsing extended word." , startPos , markEndPos ());
325
+ }
326
+
327
+ int idx = "\\ 0\" bfnrt" .indexOf (current );
328
+ if (idx != -1 ) {
329
+ current = next ();
330
+ buffer .append ("\\ \0 \" \b \f \n \r \t " .charAt (idx ));
331
+ continue ;
332
+ }
333
+ if (current == 'u' ) {
334
+ // http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.3
335
+ int rollbackPosition = pos ;
336
+ while (current == 'u' ) current = next ();
337
+ int escapedValue = 0 ;
338
+ for (int i = 12 ; i >= 0 && escapedValue != -1 ; i -= 4 ) {
339
+ if (isHexNumber (current )) {
340
+ escapedValue |= (Character .digit (current , 16 ) << i );
346
341
} else {
347
- // rollback
348
- buffer .append ("\\ u" );
349
- pos = rollbackPosition ;
342
+ escapedValue = -1 ;
350
343
}
351
- continue ;
344
+ current = next ();
345
+ }
346
+ if (escapedValue >= 0 ) {
347
+ buffer .append ((char ) escapedValue );
348
+ } else {
349
+ // rollback
350
+ buffer .append ("\\ u" );
351
+ pos = rollbackPosition ;
352
+ }
353
+ continue ;
352
354
}
353
355
buffer .append ('\\' );
354
356
continue ;
@@ -396,8 +398,8 @@ private boolean isOwnLangIdentifierPart(char current) {
396
398
}
397
399
398
400
private StringBuilder createBuffer () {
399
- buffer .setLength (0 );
400
- return buffer ;
401
+ globalBuffer .setLength (0 );
402
+ return globalBuffer ;
401
403
}
402
404
403
405
private Pos markPos () {
0 commit comments