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 string_recover_error;
%}
/*
@ -83,7 +82,7 @@ TRUE t[Rr][Uu][Ee]
FALSE f[Aa][Ll][Ss][Ee]
%x NCOMMENT SCOMMENT STRING
%x NCOMMENT SCOMMENT STRING STRINGREC
%%
@ -193,38 +192,64 @@ FALSE f[Aa][Ll][Ss][Ee]
* \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);
string_buf_ptr = string_buf; /* reset string buf ptr*/
string_recover_error = 0; /* reset error flag*/
}
<STRING>[^\"\\\n] {
if (!string_recover_error){
*string_buf_ptr = yytext[0];
string_buf_ptr ++;
if (string_buf_ptr >= string_buf + MAX_STR_CONST) {
string_recover_error = 1; /* string too long */
}
<STRING>[^\"\\\n\0] {
/* it is necessary to exclude \0 here */
*string_buf_ptr = yytext[0];
string_buf_ptr ++;
if (string_buf_ptr > string_buf + MAX_STR_CONST) {
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) {
if (!string_recover_error){
/*
* Within a string, a sequence \c denotes the
* character c, except \b \t \n \f
*/
switch(yytext[1]) {
case 'b': *string_buf_ptr = '\b'; break;
case 't': *string_buf_ptr = '\t'; break;
case 'n': *string_buf_ptr = '\n'; 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;
}
string_buf_ptr ++;
if (string_buf_ptr >= string_buf + MAX_STR_CONST) {
string_recover_error = 1; /* string too long */
}
if (yytext[1] == '\n') {
curr_lineno += 1;
}
/*
* Within a string, a sequence \c denotes the
* character c, except \b \t \n \f
*/
switch(yytext[1]) {
case 'b': *string_buf_ptr = '\b'; break;
case 't': *string_buf_ptr = '\t'; break;
case 'n': *string_buf_ptr = '\n'; break;
case 'f': *string_buf_ptr = '\f'; break;
default: *string_buf_ptr = yytext[1]; break; /* \\n is included here*/
}
/*
* The two characters \0(0x5c30) is valid, but actually converted to '0'(0x30)
* this is handled in the default branch
*/
string_buf_ptr ++;
if (string_buf_ptr > string_buf + MAX_STR_CONST) {
BEGIN(STRINGREC);
cool_yylval.error_msg = "String constant too long";
return (ERROR);
}
}
/*
@ -242,52 +267,30 @@ FALSE f[Aa][Ll][Ss][Ee]
*/
}
<STRING>\0 {
string_recover_error = 2; /* null character */
BEGIN(STRINGREC);
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.
*/
}
/*
* 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
*/
<STRING>\"|\n {
<STRING>\n {
/* the escaped case should haved been captured in the escape rule */
BEGIN(INITIAL);
if (yytext[0] == '\n') {
curr_lineno += 1;
}
if (!string_recover_error) {
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 */
cool_yylval.error_msg = "Unterminated string constant";
return (ERROR);
/*
* If a string contains an unescaped newline,
* report that error as Unterminated string constant
* and resume lexing at the beginning of the next line
*/
}
}
else if (string_recover_error == 1) {
cool_yylval.error_msg = "EOF in string constant";
return (ERROR);
/*
* 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.
*/
}
curr_lineno += 1;
cool_yylval.error_msg = "Unterminated string constant";
return (ERROR);
/*
* If a string contains an unescaped newline,
* report that error as Unterminated string constant
* and resume lexing at the beginning of the next line
*/
}
<STRING>\" {
BEGIN(INITIAL);
cool_yylval.symbol = stringtable.add_string(string_buf, string_buf_ptr - string_buf);
return (STR_CONST);
}
/* Integer constants

View File

@ -95,19 +95,39 @@ class Main {
}
};
};
-- fuc
>fuck > ? *)
(* (* (*shit
fuck
scheisse*)sdsd*)*)
sds
inttest: Int <- 0011234; inttest2: Int <- 0x011234;
--
trUE <= True = true < fALse FaLSE;
err_string "This is
not ok"
err_string "This is \
also not ok"
ok_string "This is \
ok"
escape_string "\"\n\\n\b\f\t\K\*"
*)
class AdditionalTest {
-- Int test
valid_int: Int <- 0123123;
invalid_int: Int <- 0x1234;
invalid_int2: Int <- 000_000;
-- String test
valid_str: String <- "hello world!\n";
escape_str: String <- "\"\n\\n\b\ff\t\K\*\00";
err_newline_str: String <- "This is
not ok";
err_newline_str2: String <- "This is \
also not ok";
err_newline_str3: String <- "This is \
tricky lineno
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 ";