From 17f4b004c5a9efab1961b8175990c23ee545d037 Mon Sep 17 00:00:00 2001 From: anon Date: Fri, 5 Jul 2024 21:28:53 +0200 Subject: [PATCH] fancier errors --- Makefile | 5 ++++- debug/error_test.sh | 5 +++++ debug/multi_error.eax | 11 +++++++++++ debug/unclosed_artimetric.eax | 3 +++ debug/unclosed_comment.eax | 4 ++++ debug/unclosed_program.eax | 4 ++++ debug/unknown_instruction.eax | 8 ++++++++ source/eaxhla.l | 17 ++++++++++++----- source/eaxhla.y | 19 +++++++++++++++---- source/main.c | 6 +++++- 10 files changed, 71 insertions(+), 11 deletions(-) create mode 100755 debug/error_test.sh create mode 100644 debug/multi_error.eax create mode 100644 debug/unclosed_artimetric.eax create mode 100644 debug/unclosed_comment.eax create mode 100644 debug/unclosed_program.eax create mode 100644 debug/unknown_instruction.eax diff --git a/Makefile b/Makefile index 40d6843..a28a99c 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,10 @@ bootstrap: test: ${OUT} #fcpp -C -LL debug/xop.eax > debug/xop.eax.pp - ${WRAP} ./${OUT} debug/xop.eax + #${WRAP} ./${OUT} debug/xop.eax + ${WRAP} ./${OUT} debug/artimetrics.eax + @echo " --- ERROR TESTING BEGINS BELOW ---" + debug/error_test.sh clean: -rm ${OUT} ${OBJECT} ${GENOBJECT} ${GENSOURCE} diff --git a/debug/error_test.sh b/debug/error_test.sh new file mode 100755 index 0000000..42604e2 --- /dev/null +++ b/debug/error_test.sh @@ -0,0 +1,5 @@ +./eaxhla debug/unclosed_program.eax +./eaxhla debug/unknown_instruction.eax +./eaxhla debug/multi_error.eax +./eaxhla debug/unclosed_comment.eax +./eaxhla debug/unclosed_artimetric.eax diff --git a/debug/multi_error.eax b/debug/multi_error.eax new file mode 100644 index 0000000..128f259 --- /dev/null +++ b/debug/multi_error.eax @@ -0,0 +1,11 @@ +program main + k8 kek +begin + wigglecall func + xor $rsp $rsp + xor $rsp $rsp + poke $rsp + xor $rsp $rsp + xor $rsp $rsp + xor $rsp $rsp +end program diff --git a/debug/unclosed_artimetric.eax b/debug/unclosed_artimetric.eax new file mode 100644 index 0000000..03dd5ce --- /dev/null +++ b/debug/unclosed_artimetric.eax @@ -0,0 +1,3 @@ +program a +begin +[ diff --git a/debug/unclosed_comment.eax b/debug/unclosed_comment.eax new file mode 100644 index 0000000..441923c --- /dev/null +++ b/debug/unclosed_comment.eax @@ -0,0 +1,4 @@ +/* + reeeeeeee + reeeeeeeee + reeeeee diff --git a/debug/unclosed_program.eax b/debug/unclosed_program.eax new file mode 100644 index 0000000..62ad896 --- /dev/null +++ b/debug/unclosed_program.eax @@ -0,0 +1,4 @@ +unix +program main + exit 1 +end rpogram diff --git a/debug/unknown_instruction.eax b/debug/unknown_instruction.eax new file mode 100644 index 0000000..9ec4ee8 --- /dev/null +++ b/debug/unknown_instruction.eax @@ -0,0 +1,8 @@ +procedure a + nop +end procedure + +unix +program main + wigglecall a +end program diff --git a/source/eaxhla.l b/source/eaxhla.l index d86bd49..12dee63 100644 --- a/source/eaxhla.l +++ b/source/eaxhla.l @@ -11,7 +11,7 @@ string_literal_buffer = malloc(128); void yyfree_leftovers(void); - void yyerror(void); + void yyerror(const char * errmsg); %} %option noyywrap @@ -23,7 +23,7 @@ uhex [0123456789ABCDEF] %x IN_COMMENT IN_MULTILINE_COMMENT %x IN_STRING -%x IN_END +%x IN_END IN_UNKNOWN_END %x IN_ARTIMETRIC_BLOCK %option nodefault @@ -111,11 +111,15 @@ loop { BEGIN INITIAL; return END_LOOP; } if { BEGIN INITIAL; return END_IF; } machine { BEGIN INITIAL; return END_MACHINE; } {wsnl} { ; } -. { yyerror(); } +. { yyless(0); BEGIN IN_UNKNOWN_END; } +} + +{ +.* { yyerror("unknown end sequence"); BEGIN INITIAL; return 0; } } { - /* XXX: escapes */ + /* XXX: escapes; multiline strings will die */ \" { BEGIN INITIAL; yylval.strval = strdup(string_literal_buffer); @@ -136,11 +140,13 @@ machine { BEGIN INITIAL; return END_MACHINE; } { \n { BEGIN INITIAL; } .* { ; } +<> { yytext = strdup(""); yyerror("unterminated comment"); yyterminate(); } } { \*\/ { BEGIN INITIAL; } .|\n { ; } +<> { yytext = strdup(""); yyerror("unterminated comment"); yyterminate(); } } { @@ -163,12 +169,13 @@ machine { BEGIN INITIAL; return END_MACHINE; } yylval.intval = strtol(yytext + 2, NULL, 16); return LITERAL; } +<> { yytext = strdup(""); yyerror("unterminated artimetric block"); yyterminate(); } } {identifier} { yylval.strval = strdup(yytext); return IDENTIFIER; } {identifier}: { yylval.strval = strdup(yytext); return LABEL; } -. { yyerror(); } +. { yyerror("scanner jammed"); } %% diff --git a/source/eaxhla.y b/source/eaxhla.y index e56b5ce..ac2f616 100644 --- a/source/eaxhla.y +++ b/source/eaxhla.y @@ -26,10 +26,15 @@ #include "assembler.h" extern void yyfree_leftovers(void); + extern char * yyfilename; - void yyerror() { - printf("\033[31mError: syntax error at line %d near '%s'.\033[0m\n", yylineno, yytext); - yyfree_leftovers(); + void yyerror(const char * errmsg) { + printf("\033[1m%s:%d:\033[0m \033[31mError\033[0m: %s near \033[1m'%s'\033[0m.\n", + yyfilename, + yylineno, + errmsg, + yytext + ); } long new_static(int size) { @@ -38,6 +43,8 @@ } %} + // %define parse.error detailed + %union{ long intval; char * strval; @@ -121,6 +128,7 @@ function_specifier: %empty declaration_section: %empty | declaration declaration_section + | error declaration_section { yyerrok; } ; declaration: origin type IDENTIFIER { $2.name = $3; /* add_var($1); */ free($3); } @@ -143,6 +151,7 @@ type: S8 { $$ = (static_variable){ .is_signed = 1, .size = 8, .address = new ; code: %empty + | error code { yyerrok; } | loop code | if code | call code @@ -239,7 +248,7 @@ immediate: LITERAL artimetric_block: '[' artimetric_expression ']' { $$ = $2; } ; -artimetric_expression: %empty { yyerror(); } +artimetric_expression: %empty { YYERROR; } | artimetric_operand | artimetric_expression '+' artimetric_operand { $$ = $1 + $3; } | artimetric_expression '-' artimetric_operand { $$ = $1 - $3; } @@ -257,3 +266,5 @@ exit: EXIT immediate | EXIT IDENTIFIER ; %% + + diff --git a/source/main.c b/source/main.c index dd012ac..b5de7a0 100644 --- a/source/main.c +++ b/source/main.c @@ -5,6 +5,8 @@ extern void yyfree_leftovers(void); +char * yyfilename; + signed main(int argc, char * argv[]) { if (argc < 2) { printf("%s: \n", argv[0]); @@ -14,7 +16,9 @@ signed main(int argc, char * argv[]) { yydebug = 1; #endif - yyin = fopen(argv[1], "r"); + yyfilename = argv[1]; + + yyin = fopen(yyfilename, "r"); yyparse(); yyfree_leftovers();