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 extern bool treesource_error;
43 /* CAUTION: this will stop working if we ever use yyless() or yyunput() */
44 #define YY_USER_ACTION \
46 srcpos_update(&yylloc, yytext, yyleng); \
49 /*#define LEXDEBUG 1*/
52 #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
54 #define DPRINT(fmt, ...) do { } while (0)
57 static int dts_version = 1;
59 #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \
62 static void push_input_file(const char *filename);
63 static bool pop_input_file(void);
64 static void lexical_error(const char *fmt, ...);
68 <*>"/include/"{WS}*{STRING} {
69 char *name = strchr(yytext, '\"') + 1;
70 yytext[yyleng-1] = '\0';
71 push_input_file(name);
74 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? {
75 char *line, *fnstart, *fnend;
77 /* skip text before line # */
79 while (!isdigit((unsigned char)*line))
82 /* regexp ensures that first and list "
83 * in the whole yytext are those at
84 * beginning and end of the filename string */
85 fnstart = memchr(yytext, '"', yyleng);
86 for (fnend = yytext + yyleng - 1;
87 *fnend != '"'; fnend--)
89 assert(fnstart && fnend && (fnend > fnstart));
91 fn = data_copy_escape_string(fnstart + 1,
94 /* Don't allow nuls in filenames */
95 if (memchr(fn.val, '\0', fn.len - 1))
96 lexical_error("nul in line number directive");
98 /* -1 since #line is the number of the next line */
99 srcpos_set_line(xstrdup(fn.val), atoi(line) - 1);
104 if (!pop_input_file()) {
110 DPRINT("String: %s\n", yytext);
111 yylval.data = data_copy_escape_string(yytext+1,
117 DPRINT("Keyword: /dts-v1/\n");
124 DPRINT("Keyword: /memreserve/\n");
126 return DT_MEMRESERVE;
130 DPRINT("Keyword: /bits/\n");
135 <*>"/delete-property/" {
136 DPRINT("Keyword: /delete-property/\n");
137 DPRINT("<PROPNODENAME>\n");
143 DPRINT("Keyword: /delete-node/\n");
144 DPRINT("<PROPNODENAME>\n");
150 DPRINT("Label: %s\n", yytext);
151 yylval.labelref = xstrdup(yytext);
152 yylval.labelref[yyleng-1] = '\0';
156 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
158 DPRINT("Integer Literal: '%s'\n", yytext);
161 yylval.integer = strtoull(yytext, &e, 0);
163 if (*e && e[strspn(e, "UL")]) {
164 lexical_error("Bad integer literal '%s'",
169 lexical_error("Integer literal '%s' out of range",
172 /* ERANGE is the only strtoull error triggerable
173 * by strings matching the pattern */
180 DPRINT("Character literal: %s\n", yytext);
182 d = data_copy_escape_string(yytext+1, yyleng-2);
184 lexical_error("Empty character literal");
186 return DT_CHAR_LITERAL;
189 yylval.integer = (unsigned char)d.val[0];
192 lexical_error("Character literal has %d"
193 " characters instead of 1",
196 return DT_CHAR_LITERAL;
199 <*>\&{LABEL} { /* label reference */
200 DPRINT("Ref: %s\n", yytext+1);
201 yylval.labelref = xstrdup(yytext+1);
205 <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */
206 yytext[yyleng-1] = '\0';
207 DPRINT("Ref: %s\n", yytext+2);
208 yylval.labelref = xstrdup(yytext+2);
212 <BYTESTRING>[0-9a-fA-F]{2} {
213 yylval.byte = strtol(yytext, NULL, 16);
214 DPRINT("Byte: %02x\n", (int)yylval.byte);
219 DPRINT("/BYTESTRING\n");
224 <PROPNODENAME>\\?{PROPNODECHAR}+ {
225 DPRINT("PropNodeName: %s\n", yytext);
226 yylval.propnodename = xstrdup((yytext[0] == '\\') ?
227 yytext + 1 : yytext);
229 return DT_PROPNODENAME;
233 DPRINT("Binary Include\n");
237 <*>{WS}+ /* eat whitespace */
238 <*>{COMMENT}+ /* eat C-style comments */
239 <*>{LINECOMMENT}+ /* eat C++-style comments */
241 <*>"<<" { return DT_LSHIFT; };
242 <*>">>" { return DT_RSHIFT; };
243 <*>"<=" { return DT_LE; };
244 <*>">=" { return DT_GE; };
245 <*>"==" { return DT_EQ; };
246 <*>"!=" { return DT_NE; };
247 <*>"&&" { return DT_AND; };
248 <*>"||" { return DT_OR; };
251 DPRINT("Char: %c (\\x%02x)\n", yytext[0],
252 (unsigned)yytext[0]);
253 if (yytext[0] == '[') {
254 DPRINT("<BYTESTRING>\n");
257 if ((yytext[0] == '{')
258 || (yytext[0] == ';')) {
259 DPRINT("<PROPNODENAME>\n");
267 static void push_input_file(const char *filename)
271 srcfile_push(filename);
273 yyin = current_srcfile->f;
275 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
279 static bool pop_input_file(void)
281 if (srcfile_pop() == 0)
284 yypop_buffer_state();
285 yyin = current_srcfile->f;
290 static void lexical_error(const char *fmt, ...)
295 srcpos_verror(&yylloc, "Lexical error", fmt, ap);
298 treesource_error = true;