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"
42 extern bool treesource_error;
44 /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
45 #define YY_USER_ACTION \
47 srcpos_update(&yylloc, yytext, yyleng); \
50 /*#define LEXDEBUG 1*/
53 #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
55 #define DPRINT(fmt, ...) do { } while (0)
58 static int dts_version = 1;
60 #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
63 static void push_input_file(const char *filename);
64 static bool pop_input_file(void);
65 static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
70 <*>"/include/"{WS}*{STRING} {
71 char *name = strchr(yytext, '\"') + 1;
72 yytext[yyleng-1] = '\0';
73 push_input_file(name);
76 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
77 char *line, *fnstart, *fnend;
79 /* skip text before line # */
81 while (!isdigit((unsigned char)*line))
84 /* regexp ensures that first and list "
85 * in the whole yytext are those at
86 * beginning and end of the filename string */
87 fnstart = memchr(yytext, '"', yyleng);
88 for (fnend = yytext + yyleng - 1;
89 *fnend != '"'; fnend--)
91 assert(fnstart && fnend && (fnend > fnstart));
93 fn = data_copy_escape_string(fnstart + 1,
96 /* Don't allow nuls in filenames */
97 if (memchr(fn.val, '\0', fn.len - 1))
98 lexical_error("nul in line number directive");
100 /* -1 since #line is the number of the next line */
101 srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
106 if (!pop_input_file()) {
112 DPRINT("String: %s\n", yytext);
113 yylval.data = data_copy_escape_string(yytext+1,
119 DPRINT("Keyword: /dts-v1/\n");
126 DPRINT("Keyword: /plugin/\n");
131 DPRINT("Keyword: /memreserve/\n");
133 return DT_MEMRESERVE;
137 DPRINT("Keyword: /bits/\n");
142 <*>"/delete-property/" {
143 DPRINT("Keyword: /delete-property/\n");
144 DPRINT("<PROPNODENAME>\n");
150 DPRINT("Keyword: /delete-node/\n");
151 DPRINT("<PROPNODENAME>\n");
157 DPRINT("Label: %s\n", yytext);
158 yylval.labelref = xstrdup(yytext);
159 yylval.labelref[yyleng-1] = '\0';
163 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
165 DPRINT("Integer Literal: '%s'\n", yytext);
168 yylval.integer = strtoull(yytext, &e, 0);
170 if (*e && e[strspn(e, "UL")]) {
171 lexical_error("Bad integer literal '%s'",
176 lexical_error("Integer literal '%s' out of range",
179 /* ERANGE is the only strtoull error triggerable
180 * by strings matching the pattern */
187 DPRINT("Character literal: %s\n", yytext);
189 d = data_copy_escape_string(yytext+1, yyleng-2);
191 lexical_error("Empty character literal");
194 yylval.integer = (unsigned char)d.val[0];
197 lexical_error("Character literal has %d"
198 " characters instead of 1",
203 return DT_CHAR_LITERAL;
206 <*>\&{LABEL} { /* label reference */
207 DPRINT("Ref: %s\n", yytext+1);
208 yylval.labelref = xstrdup(yytext+1);
212 <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
213 yytext[yyleng-1] = '\0';
214 DPRINT("Ref: %s\n", yytext+2);
215 yylval.labelref = xstrdup(yytext+2);
219 <BYTESTRING>[0-9a-fA-F]{2} {
220 yylval.byte = strtol(yytext, NULL, 16);
221 DPRINT("Byte: %02x\n", (int)yylval.byte);
226 DPRINT("/BYTESTRING\n");
231 <PROPNODENAME>\\?{PROPNODECHAR}+ {
232 DPRINT("PropNodeName: %s\n", yytext);
233 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
234 yytext + 1 : yytext);
236 return DT_PROPNODENAME;
240 DPRINT("Binary Include\n");
244 <*>{WS}+ /* eat whitespace */
245 <*>{COMMENT}+ /* eat C-style comments */
246 <*>{LINECOMMENT}+ /* eat C++-style comments */
248 <*>"<<" { return DT_LSHIFT; };
249 <*>">>" { return DT_RSHIFT; };
250 <*>"<=" { return DT_LE; };
251 <*>">=" { return DT_GE; };
252 <*>"==" { return DT_EQ; };
253 <*>"!=" { return DT_NE; };
254 <*>"&&" { return DT_AND; };
255 <*>"||" { return DT_OR; };
258 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
259 (unsigned)yytext[0]);
260 if (yytext[0] == '[') {
261 DPRINT("<BYTESTRING>\n");
264 if ((yytext[0] == '{')
265 || (yytext[0] == ';')) {
266 DPRINT("<PROPNODENAME>\n");
274 static void push_input_file(const char *filename)
278 srcfile_push(filename);
280 yyin = current_srcfile->f;
282 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
286 static bool pop_input_file(void)
288 if (srcfile_pop() == 0)
291 yypop_buffer_state();
292 yyin = current_srcfile->f;
297 static void lexical_error(const char *fmt, ...)
302 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
305 treesource_error = true;