mirror of
https://github.com/sigmasternchen/libargo
synced 2025-03-15 21:28:54 +00:00
better error messages
This commit is contained in:
parent
7590a1acd7
commit
6c097589ab
4 changed files with 24 additions and 11 deletions
|
@ -178,6 +178,8 @@ char* generateFreeFunction(FILE* output, struct structinfo* info, char* suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
void generateCodeStruct(FILE* output, struct structinfo* info) {
|
void generateCodeStruct(FILE* output, struct structinfo* info) {
|
||||||
|
fprintf(output, "// struct: %s\n\n", info->names[0]);
|
||||||
|
|
||||||
char* suffix = fixStructName(info->names[0]);
|
char* suffix = fixStructName(info->names[0]);
|
||||||
|
|
||||||
char* marshall = generateMarshallFunction(output, info, suffix);
|
char* marshall = generateMarshallFunction(output, info, suffix);
|
||||||
|
@ -200,7 +202,11 @@ void generateCodeStruct(FILE* output, struct structinfo* info) {
|
||||||
free(unmarshall);
|
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++) {
|
for (size_t i = 0; i < declarations->structno; i++) {
|
||||||
generateCodeStruct(output, declarations->structs[i]);
|
generateCodeStruct(output, declarations->structs[i]);
|
||||||
}
|
}
|
||||||
|
@ -255,7 +261,7 @@ int main(int argc, char** argv) {
|
||||||
for (int i = 0; i < fileno; i++) {
|
for (int i = 0; i < fileno; i++) {
|
||||||
yyin = input[i];
|
yyin = input[i];
|
||||||
|
|
||||||
int result = yyparse();
|
int result = yyparse(files[i]);
|
||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -266,7 +272,7 @@ int main(int argc, char** argv) {
|
||||||
generatePreamble(output, files, fileno);
|
generatePreamble(output, files, fileno);
|
||||||
|
|
||||||
for (int i = 0; i < fileno; i++) {
|
for (int i = 0; i < fileno; i++) {
|
||||||
generateCode(output, parsed[i]);
|
generateCode(output, parsed[i], files[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -4,4 +4,7 @@
|
||||||
void _panic(const char* f, const char* s);
|
void _panic(const char* f, const char* s);
|
||||||
#define panic(s) _panic(__func__, s)
|
#define panic(s) _panic(__func__, s)
|
||||||
|
|
||||||
|
#define WARN(msg) ("\033[33mwarning:\033[0m " msg)
|
||||||
|
#define ERROR(msg) ("\033[31mwerror:\033[0m " msg)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
#include <codegen.h>
|
#include <codegen.h>
|
||||||
|
|
||||||
int yylex();
|
int yylex();
|
||||||
void yyerror(char*);
|
void yyerror(const char*, char*);
|
||||||
|
|
||||||
struct typeinfo* newTypeInfo(bool isPointer, char* type);
|
struct typeinfo* newTypeInfo(bool isPointer, char* type);
|
||||||
struct memberinfo* newMemberInfo(struct typeinfo* type, char* name);
|
struct memberinfo* newMemberInfo(struct typeinfo* type, char* name);
|
||||||
|
@ -43,12 +43,14 @@ char* prefix(char* string, const char* prefix);
|
||||||
%type <declarsinfo> file
|
%type <declarsinfo> file
|
||||||
|
|
||||||
%token TYPEDEF STRUCT
|
%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 POINTER
|
||||||
%token SEMICOLON OPEN_BRACES CLOSE_BRACES OPEN_BRACKETS CLOSE_BRACKETS
|
%token SEMICOLON OPEN_BRACES CLOSE_BRACES OPEN_BRACKETS CLOSE_BRACKETS
|
||||||
%token <id> ID
|
%token <id> ID
|
||||||
%token NUM
|
%token NUM
|
||||||
|
|
||||||
|
%parse-param {const char* filename}
|
||||||
|
|
||||||
%start file
|
%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"); }
|
| CHAR { $$ = newTypeInfo(false, "char"); }
|
||||||
| SHORT { $$ = newTypeInfo(false, "short"); }
|
| SHORT { $$ = newTypeInfo(false, "short"); }
|
||||||
| INT { $$ = newTypeInfo(false, "int"); }
|
| INT { $$ = newTypeInfo(false, "int"); }
|
||||||
|
@ -126,12 +128,13 @@ type: STDINT { yyerror("stdint types are not yet supported"); YYERROR; }
|
||||||
| FLOAT { $$ = newTypeInfo(false, "float"); }
|
| FLOAT { $$ = newTypeInfo(false, "float"); }
|
||||||
| DOUBLE { $$ = newTypeInfo(false, "double"); }
|
| DOUBLE { $$ = newTypeInfo(false, "double"); }
|
||||||
| STRING { $$ = newTypeInfo(false, "string"); }
|
| 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 ")); }
|
| STRUCT ID { $$ = newTypeInfo(false, prefix($2, "struct ")); }
|
||||||
| ID { $$ = newTypeInfo(false, $1); }
|
| ID { $$ = newTypeInfo(false, $1); }
|
||||||
| type POINTER
|
| type POINTER
|
||||||
{
|
{
|
||||||
if ($1->isPointer || $1->type == "string") {
|
if ($1->isPointer || $1->type == "string") {
|
||||||
yyerror("multiple pointer types are not supported");
|
yyerror(filename, ERROR("multiple pointer types are not supported"));
|
||||||
YYERROR;
|
YYERROR;
|
||||||
} else {
|
} else {
|
||||||
$$ = newTypeInfo(true, $1->type);
|
$$ = 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;
|
extern int yylineno;
|
||||||
fprintf(stderr, "%s (line %d)\n", s, yylineno);
|
fprintf(stderr, "%s (%s, line %d)\n", s, filename, yylineno);
|
||||||
exit(2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct typeinfo* newTypeInfo(bool isPointer, char* type) {
|
struct typeinfo* newTypeInfo(bool isPointer, char* type) {
|
||||||
|
|
|
@ -17,7 +17,8 @@ long_long "long long"{whitespace}"int"?
|
||||||
float "float"
|
float "float"
|
||||||
double "double"
|
double "double"
|
||||||
|
|
||||||
string "const "?"char"{whitespace}*"*"
|
string "char"{whitespace}*"*"
|
||||||
|
const_string "const"{whitespace}*{string}
|
||||||
|
|
||||||
%option noyywrap
|
%option noyywrap
|
||||||
%option nodefault
|
%option nodefault
|
||||||
|
@ -56,6 +57,7 @@ string "const "?"char"{whitespace}*"*"
|
||||||
<INITIAL>{float} { return FLOAT; }
|
<INITIAL>{float} { return FLOAT; }
|
||||||
<INITIAL>{double} { return DOUBLE; }
|
<INITIAL>{double} { return DOUBLE; }
|
||||||
<INITIAL>{string} { return STRING; }
|
<INITIAL>{string} { return STRING; }
|
||||||
|
<INITIAL>{const_string} { return CONST_STRING; }
|
||||||
<INITIAL>"*" { return POINTER; }
|
<INITIAL>"*" { return POINTER; }
|
||||||
|
|
||||||
<INITIAL>";" { return SEMICOLON; }
|
<INITIAL>";" { return SEMICOLON; }
|
||||||
|
|
Loading…
Reference in a new issue