Browse Source

Use flex's YY_USER_ACTION feature to avoid code duplication

Current, every lexer rule starts with some boiler plate to update the
yylloc value for use by the parser.  One of the rules, even mistakenly
has a redundant allocation to one of the members.

This patch uses the flex YY_USER_ACTION macro hook, which is executed
before every rule to avoid this duplication.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
Jon Loeliger 16 years ago
parent
commit
83ac55d9c4
  1. 36
      dtc-lexer.l

36
dtc-lexer.l

@ -38,6 +38,11 @@ LINECOMMENT "//".*\n
#include "srcpos.h" #include "srcpos.h"
#include "dtc-parser.tab.h" #include "dtc-parser.tab.h"


#define YY_USER_ACTION \
{ \
yylloc.file = srcpos_file; \
yylloc.first_line = yylineno; \
}


/*#define LEXDEBUG 1*/ /*#define LEXDEBUG 1*/


@ -75,18 +80,13 @@ static int pop_input_file(void);
} }


<*>{STRING} { <*>{STRING} {
yylloc.file = srcpos_file;
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;
} }


<*>"/dts-v1/" { <*>"/dts-v1/" {
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("Keyword: /dts-v1/\n"); DPRINT("Keyword: /dts-v1/\n");
dts_version = 1; dts_version = 1;
BEGIN_DEFAULT(); BEGIN_DEFAULT();
@ -94,16 +94,12 @@ static int pop_input_file(void);
} }


<*>"/memreserve/" { <*>"/memreserve/" {
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("Keyword: /memreserve/\n"); DPRINT("Keyword: /memreserve/\n");
BEGIN_DEFAULT(); BEGIN_DEFAULT();
return DT_MEMRESERVE; return DT_MEMRESERVE;
} }


<*>{LABEL}: { <*>{LABEL}: {
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("Label: %s\n", yytext); DPRINT("Label: %s\n", yytext);
yylval.labelref = xstrdup(yytext); yylval.labelref = xstrdup(yytext);
yylval.labelref[yyleng-1] = '\0'; yylval.labelref[yyleng-1] = '\0';
@ -111,8 +107,6 @@ static int pop_input_file(void);
} }


<INITIAL>[bodh]# { <INITIAL>[bodh]# {
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
if (*yytext == 'b') if (*yytext == 'b')
yylval.cbase = 2; yylval.cbase = 2;
else if (*yytext == 'o') else if (*yytext == 'o')
@ -126,32 +120,24 @@ static int pop_input_file(void);
} }


<INITIAL>[0-9a-fA-F]+ { <INITIAL>[0-9a-fA-F]+ {
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
yylval.literal = xstrdup(yytext); yylval.literal = xstrdup(yytext);
DPRINT("Literal: '%s'\n", yylval.literal); DPRINT("Literal: '%s'\n", yylval.literal);
return DT_LEGACYLITERAL; return DT_LEGACYLITERAL;
} }


<V1>[0-9]+|0[xX][0-9a-fA-F]+ { <V1>[0-9]+|0[xX][0-9a-fA-F]+ {
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
yylval.literal = xstrdup(yytext); yylval.literal = xstrdup(yytext);
DPRINT("Literal: '%s'\n", yylval.literal); DPRINT("Literal: '%s'\n", yylval.literal);
return DT_LITERAL; return DT_LITERAL;
} }


\&{LABEL} { /* label reference */ \&{LABEL} { /* label reference */
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("Ref: %s\n", yytext+1); DPRINT("Ref: %s\n", yytext+1);
yylval.labelref = xstrdup(yytext+1); yylval.labelref = xstrdup(yytext+1);
return DT_REF; return DT_REF;
} }


"&{/"{PATHCHAR}+\} { /* new-style path reference */ "&{/"{PATHCHAR}+\} { /* new-style path reference */
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
yytext[yyleng-1] = '\0'; yytext[yyleng-1] = '\0';
DPRINT("Ref: %s\n", yytext+2); DPRINT("Ref: %s\n", yytext+2);
yylval.labelref = xstrdup(yytext+2); yylval.labelref = xstrdup(yytext+2);
@ -159,32 +145,24 @@ static int pop_input_file(void);
} }


<INITIAL>"&/"{PATHCHAR}+ { /* old-style path reference */ <INITIAL>"&/"{PATHCHAR}+ { /* old-style path reference */
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("Ref: %s\n", yytext+1); DPRINT("Ref: %s\n", yytext+1);
yylval.labelref = xstrdup(yytext+1); yylval.labelref = xstrdup(yytext+1);
return DT_REF; return DT_REF;
} }


<BYTESTRING>[0-9a-fA-F]{2} { <BYTESTRING>[0-9a-fA-F]{2} {
yylloc.file = srcpos_file;
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.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("/BYTESTRING\n"); DPRINT("/BYTESTRING\n");
BEGIN_DEFAULT(); BEGIN_DEFAULT();
return ']'; return ']';
} }


<PROPNODENAME>{PROPNODECHAR}+ { <PROPNODENAME>{PROPNODECHAR}+ {
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("PropNodeName: %s\n", yytext); DPRINT("PropNodeName: %s\n", yytext);
yylval.propnodename = xstrdup(yytext); yylval.propnodename = xstrdup(yytext);
BEGIN_DEFAULT(); BEGIN_DEFAULT();
@ -192,8 +170,6 @@ static int pop_input_file(void);
} }


"/incbin/" { "/incbin/" {
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("Binary Include\n"); DPRINT("Binary Include\n");
return DT_INCBIN; return DT_INCBIN;
} }
@ -203,8 +179,6 @@ static int pop_input_file(void);
<*>{LINECOMMENT}+ /* eat C++-style comments */ <*>{LINECOMMENT}+ /* eat C++-style comments */


<*>. { <*>. {
yylloc.file = srcpos_file;
yylloc.first_line = yylineno;
DPRINT("Char: %c (\\x%02x)\n", yytext[0], DPRINT("Char: %c (\\x%02x)\n", yytext[0],
(unsigned)yytext[0]); (unsigned)yytext[0]);
if (yytext[0] == '[') { if (yytext[0] == '[') {

Loading…
Cancel
Save