aboutsummaryrefslogtreecommitdiff
path: root/dtc-lexer.l
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2014-01-03 20:00:01 +1100
committerDavid Gibson <david@gibson.dropbear.id.au>2014-01-03 20:00:01 +1100
commitb82b9776140a077db723f13832afd7e279a45184 (patch)
tree5d7f9a3ba5b9f17af44be6a4893a0dcbade48086 /dtc-lexer.l
parent0e2d3992258ff4129a8c0f79b660e92994411684 (diff)
downloaddtc-b82b9776140a077db723f13832afd7e279a45184.zip
dtc-b82b9776140a077db723f13832afd7e279a45184.tar.gz
dtc-b82b9776140a077db723f13832afd7e279a45184.tar.bz2
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>
Diffstat (limited to 'dtc-lexer.l')
-rw-r--r--dtc-lexer.l30
1 files changed, 28 insertions, 2 deletions
diff --git a/dtc-lexer.l b/dtc-lexer.l
index 0cd7e67..ba5d150 100644
--- a/dtc-lexer.l
+++ b/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;
+}