tools: improve error messages

Add line numbers to error messages
This commit is contained in:
Nunuhara Cabbage
2020-04-28 21:07:40 -07:00
parent 38edfe8a98
commit cb085f6c5c
6 changed files with 43 additions and 15 deletions
+8 -3
View File
@@ -28,6 +28,9 @@
char string_buf[65536];
char *string_buf_ptr;
unsigned long asm_line = 1;
extern void yyerror(const char *s);
%}
%option noyywrap
@@ -39,8 +42,8 @@ id_char [^ \t\r\n:;\"]
%%
[ \t] ;
;[^\n]*\n return NEWLINE;
\n return NEWLINE;
;[^\n]*\n asm_line++; return NEWLINE;
\n asm_line++; return NEWLINE;
[a-zA-Z0-9_-]+: yylval.string = make_string(yytext, yyleng-1); return LABEL;
({id_char}|:)*{id_char}+ yylval.string = make_string(yytext, yyleng); return IDENTIFIER;
@@ -55,7 +58,7 @@ id_char [^ \t\r\n:;\"]
return IDENTIFIER;
}
\n ERROR("Unterminated string literal");
\n yyerror("Unterminated string literal");
\\n *string_buf_ptr++ = '\n';
\\t *string_buf_ptr++ = '\t';
@@ -72,4 +75,6 @@ id_char [^ \t\r\n:;\"]
}
}
. return INVALID_TOKEN;
%%
+13 -5
View File
@@ -37,7 +37,15 @@
#include "system4/string.h"
extern int yylex();
void yyerror(const char *s) { ERROR("%s", s); }
extern unsigned long asm_line;
#define PARSE_ERROR(fmt, ...) \
sys_error("ERROR: At line %d: " fmt "\n", asm_line-1, ##__VA_ARGS__)
void yyerror(const char *s)
{
sys_error("ERROR: At line %d: %s\n", asm_line, s);
}
static uint32_t instr_ptr;
@@ -63,7 +71,7 @@ static struct parse_instruction *make_instruction(struct string *name, parse_arg
// check opcode
const struct instruction *info = asm_get_instruction(name->text);
if (!info)
ERROR("Invalid instruction: %s", name->text);
PARSE_ERROR("Invalid instruction: %s", name->text);
// check argument count
size_t nr_args = args ? kv_size(*args) : 0;
if (nr_args != (size_t)info->nr_args) {
@@ -72,8 +80,8 @@ static struct parse_instruction *make_instruction(struct string *name, parse_arg
fprintf(stderr, " %s", kv_A(*args, i)->text);
}
fprintf(stderr, "'\n");
ERROR("Wrong number of arguments for instruction '%s' (expected %d; got %lu)",
name->text, info->nr_args, nr_args);
PARSE_ERROR("Wrong number of arguments for instruction '%s' (expected %d; got %lu)",
name->text, info->nr_args, nr_args);
}
// NOTE: argument values checked on second pass
@@ -112,7 +120,7 @@ static void push_label(char *name)
%}
%token <string> IDENTIFIER LABEL
%token <token> NEWLINE
%token <token> NEWLINE INVALID_TOKEN
%type <program> lines
%type <instr> line
+8 -3
View File
@@ -28,6 +28,9 @@
char string_buf[65536];
char *string_buf_ptr;
unsigned long text_line = 1;
extern void text_error(const char *s);
%}
%option noyywrap
@@ -38,8 +41,8 @@ char *string_buf_ptr;
%%
[ \t] ;
;[^\n]*\n return NEWLINE;
\n return NEWLINE;
;[^\n]*\n text_line++; return NEWLINE;
\n text_line++; return NEWLINE;
\[ return LBRACKET;
\] return RBRACKET;
= return EQUAL;
@@ -59,7 +62,7 @@ s return STRINGS;
return STRING;
}
\n ERROR("Unterminated string literal");
\n text_error("Unterminated string literal");
\\n *string_buf_ptr++ = '\n';
\\t *string_buf_ptr++ = '\t';
@@ -76,4 +79,6 @@ s return STRINGS;
}
}
. return INVALID_TOKEN;
%%
+7 -2
View File
@@ -30,7 +30,12 @@
#include "system4/string.h"
extern int text_lex();
void text_error(const char *s) { ERROR("%s", s); }
extern unsigned long text_line;
void text_error(const char *s)
{
sys_error("ERROR: At line %lu: %s\n", text_line, s);
}
static assignment_list *make_program(void)
{
@@ -55,7 +60,7 @@ static struct text_assignment *make_assignment(int type, int index, struct strin
%}
%token <token> NEWLINE LBRACKET RBRACKET EQUAL MESSAGES STRINGS
%token <token> NEWLINE LBRACKET RBRACKET EQUAL MESSAGES STRINGS INVALID_TOKEN
%token <integer> NUMBER
%token <string> STRING
+2 -1
View File
@@ -41,6 +41,7 @@ struct string *make_string_from_utf8(const char *str, size_t len)
}
unsigned long yex_line = 1;
extern void yex_error(const char *s);
%}
@@ -90,7 +91,7 @@ tree return TREE;
return CONST_STRING;
}
\n ERROR("Unterminated string literal");
\n yex_error("Unterminated string literal");
\\n *string_buf_ptr++ = '\n';
\\t *string_buf_ptr++ = '\t';
+5 -1
View File
@@ -43,7 +43,11 @@
extern int yex_lex();
extern unsigned long yex_line;
void yex_error(const char *s) { ERROR("at line %lu: %s", yex_line, s); }
void yex_error(const char *s)
{
sys_error("ERROR: at line %lu: %s\n", yex_line, s);
}
%}