From 248aeeef6638c00123762952c3aff30d50290689 Mon Sep 17 00:00:00 2001 From: Cary R Date: Wed, 30 Dec 2020 18:00:00 -0800 Subject: [PATCH] Add support for \\\n being ignored in string constants --- lexor.lex | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lexor.lex b/lexor.lex index 7f1134b6c..b0fc37110 100644 --- a/lexor.lex +++ b/lexor.lex @@ -55,15 +55,23 @@ char* yytext_string_filter(const char*str, size_t str_len) { if (str == 0) return 0; char*buf = new char[str_len+1]; - for (size_t idx = 0 ; idx < str_len ; idx += 1) { - if (str[idx] == 0) { + size_t didx = 0; + for (size_t sidx = 0 ; sidx < str_len ; sidx += 1, didx += 1) { + if (str[sidx] == 0) { VLerror(yylloc, "error: Found nil (\\000) in string literal, replacing with space (\\015) character."); - buf[idx] = ' '; + buf[didx] = ' '; + } else if (str[sidx] == '\\') { /* Skip \\\n */ + if ((sidx+1 < str_len) && (str[sidx+1] == '\n')) { + sidx += 1; + didx -= 1; + } else { + buf[didx] = str[sidx]; + } } else { - buf[idx] = str[idx]; + buf[didx] = str[sidx]; } } - buf[str_len] = 0; + buf[didx] = 0; return buf; } @@ -246,14 +254,16 @@ TU [munpf] \" { BEGIN(CSTRING); } \\\\ { yymore(); /* Catch \\, which is a \ escaping itself */ } \\\" { yymore(); /* Catch \", which is an escaped quote */ } +\\\n { yymore(); /* Catch \\n, which will be filtered out */ + yylloc.first_line += 1; } \n { BEGIN(0); - yylval.text = yytext_string_filter(yytext, yyleng); - VLerror(yylloc, "Missing close quote of string."); + yylval.text = yytext_string_filter(yytext, yyleng); + VLerror(yylloc, "Missing closing quote for string."); yylloc.first_line += 1; return STRING; } \" { BEGIN(0); - yylval.text = yytext_string_filter(yytext, yyleng); - yylval.text[strlen(yytext)-1] = 0; + yylval.text = yytext_string_filter(yytext, yyleng); + yylval.text[strlen(yylval.text)-1] = 0; return STRING; } . { yymore(); }