mirror of
https://github.com/dgibson/dtc.git
synced 2025-10-13 16:27:39 -04:00
Move integer literal processing back to the lexer
At the moment integer literals are passed from the lexer to the parser as a string, where it's evaluated into an integer by eval_literal(). That strange approach happened because we needed to know whether we were processing dts-v0 or dts-v1 - only known at the parser level - to know how to interpret the literal properly. dts-v0 support has been gone for some time now, and the base and bits parameters to eval_literal() are essentially useless. So, clean things up by moving the literal interpretation back to the lexer. This also introduces a new lexical_error() function to report malformed literals and set the treesource_error flag so that they'll cause a parse failure at the top level. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
parent
0e2d399225
commit
b82b977614
2 changed files with 38 additions and 34 deletions
30
dtc-lexer.l
30
dtc-lexer.l
|
@ -40,6 +40,7 @@ LINECOMMENT "//".*\n
|
|||
#include "dtc-parser.tab.h"
|
||||
|
||||
YYLTYPE yylloc;
|
||||
extern bool treesource_error;
|
||||
|
||||
/* CAUTION: this will stop working if we ever use yyless() or yyunput() */
|
||||
#define YY_USER_ACTION \
|
||||
|
@ -62,6 +63,7 @@ static int dts_version = 1;
|
|||
|
||||
static void push_input_file(const char *filename);
|
||||
static bool pop_input_file(void);
|
||||
static void lexical_error(const char *fmt, ...);
|
||||
%}
|
||||
|
||||
%%
|
||||
|
@ -146,8 +148,21 @@ static bool pop_input_file(void);
|
|||
}
|
||||
|
||||
<V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
|
||||
yylval.literal = xstrdup(yytext);
|
||||
DPRINT("Literal: '%s'\n", yylval.literal);
|
||||
char *e;
|
||||
DPRINT("Integer Literal: '%s'\n", yytext);
|
||||
|
||||
errno = 0;
|
||||
yylval.integer = strtoull(yytext, &e, 0);
|
||||
|
||||
assert(!(*e) || !e[strspn(e, "UL")]);
|
||||
|
||||
if (errno == ERANGE)
|
||||
lexical_error("Integer literal '%s' out of range",
|
||||
yytext);
|
||||
else
|
||||
/* ERANGE is the only strtoull error triggerable
|
||||
* by strings matching the pattern */
|
||||
assert(errno == 0);
|
||||
return DT_LITERAL;
|
||||
}
|
||||
|
||||
|
@ -248,3 +263,14 @@ static bool pop_input_file(void)
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void lexical_error(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
srcpos_verror(&yylloc, "Lexical error", fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
treesource_error = true;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue