Rudimentary support for reporting the line number of syntax errors.

This commit is contained in:
David Gibson 2005-10-19 16:00:31 +10:00
parent b4ac04952a
commit 86dbcbd1e4
2 changed files with 22 additions and 4 deletions

View file

@ -18,7 +18,7 @@
* USA * USA
*/ */
%option noyywrap nounput %option noyywrap nounput yylineno
%x CELLDATA %x CELLDATA
%x BYTESTRING %x BYTESTRING
@ -43,24 +43,30 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])
#define DPRINT(fmt, ...) do { } while (0) #define DPRINT(fmt, ...) do { } while (0)
#endif #endif
%} %}
%% %%
\"[^"]*\" { \"[^"]*\" {
yylloc.first_line = yylineno;
DPRINT("String: %s\n", yytext); DPRINT("String: %s\n", yytext);
yylval.data = data_copy_escape_string(yytext+1, yylval.data = data_copy_escape_string(yytext+1,
yyleng-2); yyleng-2);
yylloc.first_line = yylineno;
return DT_STRING; return DT_STRING;
} }
"/memreserve/" { "/memreserve/" {
yylloc.first_line = yylineno;
DPRINT("Keyword: /memreserve/\n"); DPRINT("Keyword: /memreserve/\n");
BEGIN(MEMRESERVE); BEGIN(MEMRESERVE);
return DT_MEMRESERVE; return DT_MEMRESERVE;
} }
<MEMRESERVE>[0-9a-fA-F]+ { <MEMRESERVE>[0-9a-fA-F]+ {
yylloc.first_line = yylineno;
if (yyleng > 2*sizeof(yylval.addr)) { if (yyleng > 2*sizeof(yylval.addr)) {
fprintf(stderr, "Address value %s too large\n", fprintf(stderr, "Address value %s too large\n",
yytext); yytext);
@ -72,12 +78,14 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])
} }
<MEMRESERVE>";" { <MEMRESERVE>";" {
yylloc.first_line = yylineno;
DPRINT("/MEMRESERVE\n"); DPRINT("/MEMRESERVE\n");
BEGIN(INITIAL); BEGIN(INITIAL);
return ';'; return ';';
} }
<CELLDATA>[0-9a-fA-F]+ { <CELLDATA>[0-9a-fA-F]+ {
yylloc.first_line = yylineno;
if (yyleng > 2*sizeof(yylval.cval)) { if (yyleng > 2*sizeof(yylval.cval)) {
fprintf(stderr, fprintf(stderr,
"Cell value %s too long\n", yytext); "Cell value %s too long\n", yytext);
@ -88,36 +96,42 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])
} }
<CELLDATA>">" { <CELLDATA>">" {
yylloc.first_line = yylineno;
DPRINT("/CELLDATA\n"); DPRINT("/CELLDATA\n");
BEGIN(INITIAL); BEGIN(INITIAL);
return '>'; return '>';
} }
<CELLDATA>\&{REFCHAR}* { <CELLDATA>\&{REFCHAR}* {
yylloc.first_line = yylineno;
DPRINT("Ref: %s\n", yytext+1); DPRINT("Ref: %s\n", yytext+1);
yylval.str = strdup(yytext+1); yylval.str = strdup(yytext+1);
return DT_REF; return DT_REF;
} }
<BYTESTRING>[0-9a-fA-F]{2} { <BYTESTRING>[0-9a-fA-F]{2} {
yylloc.first_line = yylineno;
yylval.byte = strtol(yytext, NULL, 16); yylval.byte = strtol(yytext, NULL, 16);
DPRINT("Byte: %02x\n", (int)yylval.byte); DPRINT("Byte: %02x\n", (int)yylval.byte);
return DT_BYTE; return DT_BYTE;
} }
<BYTESTRING>"]" { <BYTESTRING>"]" {
yylloc.first_line = yylineno;
DPRINT("/BYTESTRING\n"); DPRINT("/BYTESTRING\n");
BEGIN(INITIAL); BEGIN(INITIAL);
return ']'; return ']';
} }
{PROPCHAR}+ { {PROPCHAR}+ {
yylloc.first_line = yylineno;
DPRINT("PropName: %s\n", yytext); DPRINT("PropName: %s\n", yytext);
yylval.str = strdup(yytext); yylval.str = strdup(yytext);
return DT_PROPNAME; return DT_PROPNAME;
} }
{PROPCHAR}+(@{UNITCHAR}+)? { {PROPCHAR}+(@{UNITCHAR}+)? {
yylloc.first_line = yylineno;
DPRINT("NodeName: %s\n", yytext); DPRINT("NodeName: %s\n", yytext);
yylval.str = strdup(yytext); yylval.str = strdup(yytext);
return DT_NODENAME; return DT_NODENAME;
@ -125,6 +139,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])
[a-zA-Z_][a-zA-Z0-9_]*: { [a-zA-Z_][a-zA-Z0-9_]*: {
yylloc.first_line = yylineno;
DPRINT("Label: %s\n", yytext); DPRINT("Label: %s\n", yytext);
yylval.str = strdup(yytext); yylval.str = strdup(yytext);
yylval.str[yyleng-1] = '\0'; yylval.str[yyleng-1] = '\0';
@ -134,6 +149,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])
<*>{WS}+ /* eat whitespace */ <*>{WS}+ /* eat whitespace */
<*>"/*"([^*]|\*+[^*/])*\*+"/" { <*>"/*"([^*]|\*+[^*/])*\*+"/" {
yylloc.first_line = yylineno;
DPRINT("Comment: %s\n", yytext); DPRINT("Comment: %s\n", yytext);
/* eat comments */ /* eat comments */
} }
@ -141,6 +157,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])
<*>"//".*\n /* eat line comments */ <*>"//".*\n /* eat line comments */
<*>. { <*>. {
yylloc.first_line = yylineno;
switch (yytext[0]) { switch (yytext[0]) {
case '<': case '<':
DPRINT("CELLDATA\n"); DPRINT("CELLDATA\n");

View file

@ -18,6 +18,9 @@
* USA * USA
*/ */
%glr-parser
%locations
%{ %{
#include "dtc.h" #include "dtc.h"
@ -69,8 +72,6 @@ extern struct boot_info *the_boot_info;
%type <str> label %type <str> label
%type <str> nodename %type <str> nodename
%glr-parser
%% %%
sourcefile: memreserves devicetree { sourcefile: memreserves devicetree {
@ -160,5 +161,5 @@ label: DT_LABEL { $$ = $1; }
void yyerror (char const *s) void yyerror (char const *s)
{ {
fprintf (stderr, "%s\n", s); fprintf (stderr, "%s at line %d\n", s, yylloc.first_line);
} }