2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 %option noyywrap nounput noinput never-interactive
27 PROPNODECHAR [a-zA-Z0-9,._+*#?@-]
28 PATHCHAR ({PROPNODECHAR}|[/])
29 LABEL [a-zA-Z_][a-zA-Z0-9_]*
30 STRING \"([^\\"]|\\.)*\"
31 CHAR_LITERAL '([^']|\\')*'
33 COMMENT "/*"([^*]|\*+[^*/])*\*+"/"
39 #include "dtc-parser.tab.h"
41 #define MAX_INCLUDE_NESTING 100
42 YY_BUFFER_STATE include_stack[MAX_INCLUDE_NESTING];
43 int include_stack_pointer = 0;
46 extern bool treesource_error;
48 /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
49 #define YY_USER_ACTION \
51 srcpos_update(&yylloc, yytext, yyleng); \
54 /*#define LEXDEBUG 1*/
57 #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
59 #define DPRINT(fmt, ...) do { } while (0)
62 static int dts_version = 1;
64 #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
67 static void push_input_file(const char *filename);
68 static bool pop_input_file(void);
70 static void lexical_error(const char *fmt, ...)
71 __attribute__((format (printf, 1, 2)));
73 static void lexical_error(const char *fmt, ...);
79 <*>"/include/"{WS}*{STRING} {
80 char *name = strchr(yytext, '\"') + 1;
81 yytext[yyleng-1] = '\0';
82 push_input_file(name);
85 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
86 char *line, *fnstart, *fnend;
88 /* skip text before line # */
90 while (!isdigit((unsigned char)*line))
93 /* regexp ensures that first and list "
94 * in the whole yytext are those at
95 * beginning and end of the filename string */
96 fnstart = memchr(yytext, '"', yyleng);
97 for (fnend = yytext + yyleng - 1;
98 *fnend != '"'; fnend--)
100 assert(fnstart && fnend && (fnend > fnstart));
102 fn = data_copy_escape_string(fnstart + 1,
103 fnend - fnstart - 1);
105 /* Don't allow nuls in filenames */
106 if (memchr(fn.val, '\0', fn.len - 1))
107 lexical_error("nul in line number directive");
109 /* -1 since #line is the number of the next line */
110 srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
115 if (!pop_input_file()) {
121 DPRINT("String: %s\n", yytext);
122 yylval.data = data_copy_escape_string(yytext+1,
128 DPRINT("Keyword: /dts-v1/\n");
135 DPRINT("Keyword: /plugin/\n");
140 DPRINT("Keyword: /memreserve/\n");
142 return DT_MEMRESERVE;
146 DPRINT("Keyword: /bits/\n");
151 <*>"/delete-property/" {
152 DPRINT("Keyword: /delete-property/\n");
153 DPRINT("<PROPNODENAME>\n");
159 DPRINT("Keyword: /delete-node/\n");
160 DPRINT("<PROPNODENAME>\n");
166 DPRINT("Label: %s\n", yytext);
167 yylval.labelref = xstrdup(yytext);
168 yylval.labelref[yyleng-1] = '\0';
172 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
174 DPRINT("Integer Literal: '%s'\n", yytext);
177 yylval.integer = strtoull(yytext, &e, 0);
179 if (*e && e[strspn(e, "UL")]) {
180 lexical_error("Bad integer literal '%s'",
185 lexical_error("Integer literal '%s' out of range",
188 /* ERANGE is the only strtoull error triggerable
189 * by strings matching the pattern */
196 DPRINT("Character literal: %s\n", yytext);
198 d = data_copy_escape_string(yytext+1, yyleng-2);
200 lexical_error("Empty character literal");
203 yylval.integer = (unsigned char)d.val[0];
206 lexical_error("Character literal has %d"
207 " characters instead of 1",
212 return DT_CHAR_LITERAL;
215 <*>\&{LABEL} { /* label reference */
216 DPRINT("Ref: %s\n", yytext+1);
217 yylval.labelref = xstrdup(yytext+1);
221 <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
222 yytext[yyleng-1] = '\0';
223 DPRINT("Ref: %s\n", yytext+2);
224 yylval.labelref = xstrdup(yytext+2);
228 <BYTESTRING>[0-9a-fA-F]{2} {
229 yylval.byte = strtol(yytext, NULL, 16);
230 DPRINT("Byte: %02x\n", (int)yylval.byte);
235 DPRINT("/BYTESTRING\n");
240 <PROPNODENAME>\\?{PROPNODECHAR}+ {
241 DPRINT("PropNodeName: %s\n", yytext);
242 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
243 yytext + 1 : yytext);
245 return DT_PROPNODENAME;
249 DPRINT("Binary Include\n");
253 <*>{WS}+ /* eat whitespace */
254 <*>{COMMENT}+ /* eat C-style comments */
255 <*>{LINECOMMENT}+ /* eat C++-style comments */
257 <*>"<<" { return DT_LSHIFT; };
258 <*>">>" { return DT_RSHIFT; };
259 <*>"<=" { return DT_LE; };
260 <*>">=" { return DT_GE; };
261 <*>"==" { return DT_EQ; };
262 <*>"!=" { return DT_NE; };
263 <*>"&&" { return DT_AND; };
264 <*>"||" { return DT_OR; };
267 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
268 (unsigned)yytext[0]);
269 if (yytext[0] == '[') {
270 DPRINT("<BYTESTRING>\n");
273 if ((yytext[0] == '{')
274 || (yytext[0] == ';')) {
275 DPRINT("<PROPNODENAME>\n");
283 static void push_input_file(const char *filename)
287 assert(include_stack_pointer < MAX_INCLUDE_NESTING);
289 srcfile_push(filename);
291 yyin = current_srcfile->f;
293 include_stack[include_stack_pointer++] = YY_CURRENT_BUFFER;
295 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
299 static bool pop_input_file(void)
301 if (srcfile_pop() == 0)
304 assert(include_stack_pointer > 0);
306 yy_delete_buffer( YY_CURRENT_BUFFER );
308 yy_switch_to_buffer( include_stack[--include_stack_pointer] );
310 yyin = current_srcfile->f;
315 static void lexical_error(const char *fmt, ...)
320 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
323 treesource_error = true;