almost bug free

This commit is contained in:
ridethepig 2023-03-20 17:06:15 +08:00
parent 88610ace11
commit 20d083bc58
6 changed files with 319 additions and 87 deletions

View File

@ -44,7 +44,6 @@ extern YYSTYPE cool_yylval;
*/ */
int comment_nest_level; int comment_nest_level;
int string_recover_error;
%} %}
/* /*
@ -83,7 +82,7 @@ TRUE t[Rr][Uu][Ee]
FALSE f[Aa][Ll][Ss][Ee] FALSE f[Aa][Ll][Ss][Ee]
%x NCOMMENT SCOMMENT STRING %x NCOMMENT SCOMMENT STRING STRINGREC
%% %%
@ -193,22 +192,44 @@ FALSE f[Aa][Ll][Ss][Ee]
* \n \t \b \f, the result is c. * \n \t \b \f, the result is c.
* *
*/ */
<STRINGREC>\\\n {
curr_lineno += 1;
}
/*
* In either case(null char | too long), lexing should resume after the end of the string.
* The end of the string is defined as either:
* the beginning of the next line if an unescaped newline occurs after these errors are encountered
* after the closing ” otherwise
*/
<STRINGREC>\"|\n {
BEGIN(INITIAL);
if (yytext[0] == '\n') curr_lineno += 1;
}
<STRINGREC>.
\" { \" {
BEGIN(STRING); BEGIN(STRING);
string_buf_ptr = string_buf; /* reset string buf ptr*/ string_buf_ptr = string_buf; /* reset string buf ptr*/
string_recover_error = 0; /* reset error flag*/
} }
<STRING>[^\"\\\n] { <STRING>[^\"\\\n\0] {
if (!string_recover_error){ /* it is necessary to exclude \0 here */
*string_buf_ptr = yytext[0]; *string_buf_ptr = yytext[0];
string_buf_ptr ++; string_buf_ptr ++;
if (string_buf_ptr >= string_buf + MAX_STR_CONST) { if (string_buf_ptr > string_buf + MAX_STR_CONST) {
string_recover_error = 1; /* string too long */ BEGIN(STRINGREC);
} cool_yylval.error_msg = "String constant too long";
return (ERROR);
/*
* When a string is too long,
* report the error as String constant too long
* in the error string in the ERROR token.
*/
} }
} }
<STRING>\\(.|\n) { <STRING>\\(.|\n) {
if (!string_recover_error){ if (yytext[1] == '\n') {
curr_lineno += 1;
}
/* /*
* Within a string, a sequence \c denotes the * Within a string, a sequence \c denotes the
* character c, except \b \t \n \f * character c, except \b \t \n \f
@ -218,13 +239,17 @@ FALSE f[Aa][Ll][Ss][Ee]
case 't': *string_buf_ptr = '\t'; break; case 't': *string_buf_ptr = '\t'; break;
case 'n': *string_buf_ptr = '\n'; break; case 'n': *string_buf_ptr = '\n'; break;
case 'f': *string_buf_ptr = '\f'; break; case 'f': *string_buf_ptr = '\f'; break;
case '\n': *string_buf_ptr = '\n'; curr_lineno += 1; break; default: *string_buf_ptr = yytext[1]; break; /* \\n is included here*/
default: *string_buf_ptr = yytext[1]; break;
} }
/*
* The two characters \0(0x5c30) is valid, but actually converted to '0'(0x30)
* this is handled in the default branch
*/
string_buf_ptr ++; string_buf_ptr ++;
if (string_buf_ptr >= string_buf + MAX_STR_CONST) { if (string_buf_ptr > string_buf + MAX_STR_CONST) {
string_recover_error = 1; /* string too long */ BEGIN(STRINGREC);
} cool_yylval.error_msg = "String constant too long";
return (ERROR);
} }
} }
/* /*
@ -242,26 +267,18 @@ FALSE f[Aa][Ll][Ss][Ee]
*/ */
} }
<STRING>\0 { <STRING>\0 {
string_recover_error = 2; /* null character */ BEGIN(STRINGREC);
} cool_yylval.error_msg = "String contains null character.";
return (ERROR);
/* /*
* In either case(null char | too long), lexing should resume after the end of the string. * If the string contains invalid characters (i.e., the null character),
* The end of the string is defined as either: * report this as String contains null character.
* the beginning of the next line if an unescaped newline occurs after these errors are encountered
* after the closing ” otherwise
*/ */
<STRING>\"|\n {
BEGIN(INITIAL);
if (yytext[0] == '\n') {
curr_lineno += 1;
} }
if (!string_recover_error) { <STRING>\n {
if (yytext[0] == '\"') {
cool_yylval.symbol = stringtable.add_string(string_buf, string_buf_ptr - string_buf);
return (STR_CONST);
}
else if (yytext[0] == '\n') {
/* the escaped case should haved been captured in the escape rule */ /* the escaped case should haved been captured in the escape rule */
BEGIN(INITIAL);
curr_lineno += 1;
cool_yylval.error_msg = "Unterminated string constant"; cool_yylval.error_msg = "Unterminated string constant";
return (ERROR); return (ERROR);
/* /*
@ -270,24 +287,10 @@ FALSE f[Aa][Ll][Ss][Ee]
* and resume lexing at the beginning of the next line * and resume lexing at the beginning of the next line
*/ */
} }
} <STRING>\" {
else if (string_recover_error == 1) { BEGIN(INITIAL);
cool_yylval.error_msg = "EOF in string constant"; cool_yylval.symbol = stringtable.add_string(string_buf, string_buf_ptr - string_buf);
return (ERROR); return (STR_CONST);
/*
* When a string is too long,
* report the error as String constant too long
* in the error string in the ERROR token.
*/
}
else if (string_recover_error == 2){
cool_yylval.error_msg = "String contains null character";
return (ERROR);
/*
* If the string contains invalid characters (i.e., the null character),
* report this as String contains null character.
*/
}
} }
/* Integer constants /* Integer constants

View File

@ -95,19 +95,39 @@ class Main {
} }
}; };
}; };
-- fuc *)
>fuck > ? *) class AdditionalTest {
(* (* (*shit -- Int test
fuck valid_int: Int <- 0123123;
scheisse*)sdsd*)*) invalid_int: Int <- 0x1234;
sds invalid_int2: Int <- 000_000;
inttest: Int <- 0011234; inttest2: Int <- 0x011234; -- String test
-- valid_str: String <- "hello world!\n";
trUE <= True = true < fALse FaLSE; escape_str: String <- "\"\n\\n\b\ff\t\K\*\00";
err_string "This is err_newline_str: String <- "This is
not ok" not ok";
err_string "This is \ err_newline_str2: String <- "This is \
also not ok" also not ok";
ok_string "This is \ err_newline_str3: String <- "This is \
ok" tricky lineno
escape_string "\"\n\\n\b\f\t\K\*" not ok";
ok_newline_string: String <- "This is \
ok";
-- Bool test
valid_bool_true: Bool <- true = tRUE = trUe;
valid_bool_false: Bool <- false = fALsE = fALSE;
invalid_bool_true: Bool <- True = TRUe;
invalid_bool_false: Bool <- False = FALSE;
-- Invalid character test
_hello: Bool <- 1 != 2 <> 3_3?;
-- Comment test
hello: Type <- 0;
-- Valid nested comment
(* (* feigling (* versagar
scheisse
scheisse*) verpfeif *)
*)
-- Invalid nested comment
(* *) (( lost left close, Unmatched * ) expected *)
(* (* lost right close, EOF in comment expected) *)
};

View File

@ -0,0 +1,2 @@
-- String EOF
"haha

View File

@ -0,0 +1 @@
-- Single line comment EOF

Binary file not shown.

View File

@ -0,0 +1,206 @@
(*
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
Long comment test
*)
long_string: String <- " long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string";
long_string_resume: String <- " long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string
long string long string long string long string long string long string long string long string long string long string";
long_string_1026: String <- " long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long strin ";
long_string_1025: String <- " long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long strin ";
long_string_1024: String <- " long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long string long string long string long string long string \
long string long string long string long string long string long strin ";