@@ -118,7 +118,7 @@ public static Set<String> getKeywords() {
118118 private final int length ;
119119
120120 private final List <Token > tokens ;
121- private final StringBuilder buffer ;
121+ private final StringBuilder globalBuffer ;
122122
123123 private int pos ;
124124 private int row , col ;
@@ -128,7 +128,7 @@ public Lexer(String input) {
128128 length = input .length ();
129129
130130 tokens = new ArrayList <>();
131- buffer = new StringBuilder (40 );
131+ globalBuffer = new StringBuilder (40 );
132132 row = col = 1 ;
133133 }
134134
@@ -184,19 +184,19 @@ private void tokenizeNumber() {
184184 if (decimal ) {
185185 addToken (TokenType .DECIMAL_NUMBER , buffer .toString (), startPos );
186186 } else if (current == 'L' ) {
187- next ();
187+ skip ();
188188 addToken (TokenType .LONG_NUMBER , buffer .toString (), startPos );
189189 } else {
190190 addToken (TokenType .NUMBER , buffer .toString (), startPos );
191191 }
192192 }
193193
194194 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+ };
200200
201201 boolean hasValue = false ;
202202 char current = peek (0 );
@@ -237,7 +237,7 @@ private void tokenizeHexNumber(int skipChars) {
237237 if (buffer .isEmpty ()) throw error ("Empty HEX value" , startPos );
238238 if (peek (-1 ) == '_' ) throw error ("HEX value cannot end with _" , startPos , markEndPos ());
239239 if (current == 'L' ) {
240- next ();
240+ skip ();
241241 addToken (TokenType .HEX_LONG_NUMBER , buffer .toString (), startPos );
242242 } else {
243243 addToken (TokenType .HEX_NUMBER , buffer .toString (), startPos );
@@ -320,35 +320,37 @@ private void tokenizeText() {
320320 while (true ) {
321321 if (current == '\\' ) {
322322 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 );
346341 } else {
347- // rollback
348- buffer .append ("\\ u" );
349- pos = rollbackPosition ;
342+ escapedValue = -1 ;
350343 }
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 ;
352354 }
353355 buffer .append ('\\' );
354356 continue ;
@@ -396,8 +398,8 @@ private boolean isOwnLangIdentifierPart(char current) {
396398 }
397399
398400 private StringBuilder createBuffer () {
399- buffer .setLength (0 );
400- return buffer ;
401+ globalBuffer .setLength (0 );
402+ return globalBuffer ;
401403 }
402404
403405 private Pos markPos () {
0 commit comments