Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
Add support for \r inside strings and chars, fix col counting in CHAR
Browse files Browse the repository at this point in the history
  • Loading branch information
anmolkabra committed Feb 3, 2019
1 parent 5ec94dc commit b547887
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/main/java/lexer/XiToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ private String format(Object o) {
if (o instanceof String) {
String s = (String) o;
s = s.replace("\n", "\\n");
s = s.replace("\r", "\\r");
s = s.replace("\t", "\\t");
return s;
} else if (o instanceof Character) {
if (o.equals('\n')) return "\\n";
if (o.equals('\r')) return "\\r";
if (o.equals('\t')) return "\\t";
return o.toString();
} else {
Expand Down
17 changes: 10 additions & 7 deletions src/test.flex
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ INTEGER = 0 | [1-9][0-9]*
\" { yybegin(YYINITIAL);
return new XiToken(TokenType.STRING_LIT, yyline,
stringLiteralStartCol, stringLiteral.toString()); }
[^\n\"\\]+ { stringLiteral.append( yytext() ); }//TODO exclude HEX?
[^\n\r\"\\]+ { stringLiteral.append( yytext() ); }
{HEX} { stringLiteral.append( (char) Integer.parseInt(
yytext().substring(2, yylength()), 16
)); }
\\t { stringLiteral.append( '\t' ); }
\\n { stringLiteral.append( '\n' ); }
\\r { stringLiteral.append( '\r' ); }
\\\" { stringLiteral.append( '\"' ); }
\\ { stringLiteral.append( '\\' ); }
}
Expand All @@ -115,22 +116,24 @@ INTEGER = 0 | [1-9][0-9]*
\' { yybegin(YYINITIAL);
return new XiToken(TokenType.ERROR, yyline,
charLiteralStartCol, "empty character literal"); }
[^\n\'\\]\' { yybegin(YYINITIAL); //TODO exclude HEX?
[^\n\r\'\\]\' { yybegin(YYINITIAL);
return new XiToken(TokenType.CHAR_LIT, yyline,
charLiteralStartCol, yytext().charAt(0)); }
{HEX}\' {yybegin(YYINITIAL); return new XiToken(TokenType.CHAR_LIT, yyline,
yycolumn,
charLiteralStartCol,
Character.forDigit(
Integer.parseInt(yytext().substring(2,yylength()) ,16), 10
));}
\\t\' { yybegin(YYINITIAL); return new XiToken(TokenType
.CHAR_LIT, yyline, yycolumn, '\t'); }
.CHAR_LIT, yyline, charLiteralStartCol, '\t'); }
\\n\' { yybegin(YYINITIAL); return new XiToken(TokenType
.CHAR_LIT, yyline, yycolumn, '\n'); }
.CHAR_LIT, yyline, charLiteralStartCol, '\n'); }
\\r\' { yybegin(YYINITIAL); return new XiToken(TokenType
.CHAR_LIT, yyline, charLiteralStartCol, '\r'); }
\\\' { yybegin(YYINITIAL); return new XiToken(TokenType
.CHAR_LIT, yyline, yycolumn, '\\'); }
.CHAR_LIT, yyline, charLiteralStartCol, '\\'); }
\\\'\' { yybegin(YYINITIAL); return new XiToken(TokenType
.CHAR_LIT, yyline, yycolumn, '\''); }
.CHAR_LIT, yyline, charLiteralStartCol, '\''); }
}

/* error fallback */
Expand Down

0 comments on commit b547887

Please sign in to comment.