better error messages

This commit is contained in:
overflowerror 2021-05-04 17:24:58 +02:00
parent 7590a1acd7
commit 6c097589ab
4 changed files with 24 additions and 11 deletions

View file

@ -178,6 +178,8 @@ char* generateFreeFunction(FILE* output, struct structinfo* info, char* suffix)
}
void generateCodeStruct(FILE* output, struct structinfo* info) {
fprintf(output, "// struct: %s\n\n", info->names[0]);
char* suffix = fixStructName(info->names[0]);
char* marshall = generateMarshallFunction(output, info, suffix);
@ -200,7 +202,11 @@ void generateCodeStruct(FILE* output, struct structinfo* info) {
free(unmarshall);
}
void generateCode(FILE* output, struct declarsinfo* declarations) {
void generateCode(FILE* output, struct declarsinfo* declarations, const char* filename) {
fprintf(output, "/*\n");
fprintf(output, " * file: %s\n", filename);
fprintf(output, "*/\n\n");
for (size_t i = 0; i < declarations->structno; i++) {
generateCodeStruct(output, declarations->structs[i]);
}
@ -255,7 +261,7 @@ int main(int argc, char** argv) {
for (int i = 0; i < fileno; i++) {
yyin = input[i];
int result = yyparse();
int result = yyparse(files[i]);
if (result != 0) {
return result;
}
@ -266,7 +272,7 @@ int main(int argc, char** argv) {
generatePreamble(output, files, fileno);
for (int i = 0; i < fileno; i++) {
generateCode(output, parsed[i]);
generateCode(output, parsed[i], files[i]);
}
return 0;

View file

@ -4,4 +4,7 @@
void _panic(const char* f, const char* s);
#define panic(s) _panic(__func__, s)
#define WARN(msg) ("\033[33mwarning:\033[0m " msg)
#define ERROR(msg) ("\033[31mwerror:\033[0m " msg)
#endif

View file

@ -10,7 +10,7 @@
#include <codegen.h>
int yylex();
void yyerror(char*);
void yyerror(const char*, char*);
struct typeinfo* newTypeInfo(bool isPointer, char* type);
struct memberinfo* newMemberInfo(struct typeinfo* type, char* name);
@ -43,12 +43,14 @@ char* prefix(char* string, const char* prefix);
%type <declarsinfo> file
%token TYPEDEF STRUCT
%token CHAR SHORT INT LONG LONG_LONG FLOAT DOUBLE STRING STDINT
%token CHAR SHORT INT LONG LONG_LONG FLOAT DOUBLE STRING CONST_STRING STDINT
%token POINTER
%token SEMICOLON OPEN_BRACES CLOSE_BRACES OPEN_BRACKETS CLOSE_BRACKETS
%token <id> ID
%token NUM
%parse-param {const char* filename}
%start file
%%
@ -117,7 +119,7 @@ structmember: type ID
}
;
type: STDINT { yyerror("stdint types are not yet supported"); YYERROR; }
type: STDINT { yyerror(filename, ERROR("stdint types are not yet supported")); YYERROR; }
| CHAR { $$ = newTypeInfo(false, "char"); }
| SHORT { $$ = newTypeInfo(false, "short"); }
| INT { $$ = newTypeInfo(false, "int"); }
@ -126,12 +128,13 @@ type: STDINT { yyerror("stdint types are not yet supported"); YYERROR; }
| FLOAT { $$ = newTypeInfo(false, "float"); }
| DOUBLE { $$ = newTypeInfo(false, "double"); }
| STRING { $$ = newTypeInfo(false, "string"); }
| CONST_STRING { $$ = newTypeInfo(false, "string"); yyerror(filename, WARN("const char* struct members are discouraged")); }
| STRUCT ID { $$ = newTypeInfo(false, prefix($2, "struct ")); }
| ID { $$ = newTypeInfo(false, $1); }
| type POINTER
{
if ($1->isPointer || $1->type == "string") {
yyerror("multiple pointer types are not supported");
yyerror(filename, ERROR("multiple pointer types are not supported"));
YYERROR;
} else {
$$ = newTypeInfo(true, $1->type);
@ -141,10 +144,9 @@ type: STDINT { yyerror("stdint types are not yet supported"); YYERROR; }
%%
void yyerror(char* s) {
void yyerror(const char* filename, char* s) {
extern int yylineno;
fprintf(stderr, "%s (line %d)\n", s, yylineno);
exit(2);
fprintf(stderr, "%s (%s, line %d)\n", s, filename, yylineno);
}
struct typeinfo* newTypeInfo(bool isPointer, char* type) {

View file

@ -17,7 +17,8 @@ long_long "long long"{whitespace}"int"?
float "float"
double "double"
string "const "?"char"{whitespace}*"*"
string "char"{whitespace}*"*"
const_string "const"{whitespace}*{string}
%option noyywrap
%option nodefault
@ -56,6 +57,7 @@ string "const "?"char"{whitespace}*"*"
<INITIAL>{float} { return FLOAT; }
<INITIAL>{double} { return DOUBLE; }
<INITIAL>{string} { return STRING; }
<INITIAL>{const_string} { return CONST_STRING; }
<INITIAL>"*" { return POINTER; }
<INITIAL>";" { return SEMICOLON; }