fix: newline escapes in char literal

This commit is contained in:
overflowerror 2024-04-27 17:08:12 +02:00
parent 8c3c1d1991
commit 6d7c58a512
3 changed files with 15 additions and 2 deletions

View file

@ -10,7 +10,7 @@ if "if"
else "else"
while "while"
char "'"."'"
char "'"([^\\]|[\\]n|[\\]t|[\\]{2})"'"
id [a-zA-Z_][a-zA-Z0-9_]*
%option noyywrap
@ -34,6 +34,17 @@ id [a-zA-Z_][a-zA-Z0-9_]*
#include "y.tab.h"
char char_escape(const char* yytext) {
if (yytext[1] == '\\') {
if (yytext[2] == '\\') return '\\';
else if (yytext[2] == 'n') return '\n';
else if (yytext[2] == 't') return '\t';
else lex_panic("unknown escape sequence: %s\n", yytext);
} else {
return yytext[1];
}
}
%}
%%
@ -106,7 +117,7 @@ strbuf_t strbuf = NULL;
<INITIAL>{dec} { yylval.number = strtol(yytext, NULL, 10); return NUM; }
<INITIAL>{oct} { yylval.number = strtol(yytext + 1, NULL, 8); return NUM; }
<INITIAL>{bin} { yylval.number = strtol(yytext + 2, NULL, 2); return NUM; }
<INITIAL>{char} { yylval.ch = yytext[1]; return CHAR; }
<INITIAL>{char} { yylval.ch = char_escape(yytext); return CHAR; }
<INITIAL>{id} { yylval.id = strdup(yytext); return ID; }
<INITIAL>{whitespace}+ { }

View file

@ -0,0 +1 @@
print('\n');

View file

@ -0,0 +1 @@
[-]++++++++++.