David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
| 3 | * |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | * USA |
| 19 | */ |
| 20 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 21 | %option noyywrap nounput noinput never-interactive |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 22 | |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 23 | %x BYTESTRING |
| 24 | %x PROPNODENAME |
| 25 | %s V1 |
| 26 | |
| 27 | PROPNODECHAR [a-zA-Z0-9,._+*#?@-] |
| 28 | PATHCHAR ({PROPNODECHAR}|[/]) |
| 29 | LABEL [a-zA-Z_][a-zA-Z0-9_]* |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 30 | STRING \"([^\\"]|\\.)*\" |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 31 | CHAR_LITERAL '([^']|\\')*' |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 32 | WS [[:space:]] |
| 33 | COMMENT "/*"([^*]|\*+[^*/])*\*+"/" |
| 34 | LINECOMMENT "//".*\n |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 35 | |
| 36 | %{ |
| 37 | #include "dtc.h" |
| 38 | #include "srcpos.h" |
| 39 | #include "dtc-parser.tab.h" |
| 40 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 41 | YYLTYPE yylloc; |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 42 | extern bool treesource_error; |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 43 | |
| 44 | /* CAUTION: this will stop working if we ever use yyless() or yyunput() */ |
| 45 | #define YY_USER_ACTION \ |
| 46 | { \ |
| 47 | srcpos_update(&yylloc, yytext, yyleng); \ |
| 48 | } |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 49 | |
| 50 | /*#define LEXDEBUG 1*/ |
| 51 | |
| 52 | #ifdef LEXDEBUG |
| 53 | #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) |
| 54 | #else |
| 55 | #define DPRINT(fmt, ...) do { } while (0) |
| 56 | #endif |
| 57 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 58 | static int dts_version = 1; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 59 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 60 | #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \ |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 61 | BEGIN(V1); \ |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 62 | |
| 63 | static void push_input_file(const char *filename); |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 64 | static bool pop_input_file(void); |
| 65 | static void lexical_error(const char *fmt, ...); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 66 | %} |
| 67 | |
| 68 | %% |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 69 | <*>"/include/"{WS}*{STRING} { |
| 70 | char *name = strchr(yytext, '\"') + 1; |
| 71 | yytext[yyleng-1] = '\0'; |
| 72 | push_input_file(name); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 73 | } |
| 74 | |
Grant Likely | 706b78f | 2013-06-13 12:57:44 +0100 | [diff] [blame] | 75 | <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? { |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 76 | char *line, *tmp, *fn; |
| 77 | /* skip text before line # */ |
| 78 | line = yytext; |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 79 | while (!isdigit((unsigned char)*line)) |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 80 | line++; |
| 81 | /* skip digits in line # */ |
| 82 | tmp = line; |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 83 | while (!isspace((unsigned char)*tmp)) |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 84 | tmp++; |
| 85 | /* "NULL"-terminate line # */ |
| 86 | *tmp = '\0'; |
| 87 | /* start of filename */ |
| 88 | fn = strchr(tmp + 1, '"') + 1; |
| 89 | /* strip trailing " from filename */ |
| 90 | tmp = strchr(fn, '"'); |
| 91 | *tmp = 0; |
| 92 | /* -1 since #line is the number of the next line */ |
| 93 | srcpos_set_line(xstrdup(fn), atoi(line) - 1); |
| 94 | } |
| 95 | |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 96 | <*><<EOF>> { |
| 97 | if (!pop_input_file()) { |
| 98 | yyterminate(); |
| 99 | } |
| 100 | } |
| 101 | |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 102 | <*>{STRING} { |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 103 | DPRINT("String: %s\n", yytext); |
| 104 | yylval.data = data_copy_escape_string(yytext+1, |
| 105 | yyleng-2); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 106 | return DT_STRING; |
| 107 | } |
| 108 | |
| 109 | <*>"/dts-v1/" { |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 110 | DPRINT("Keyword: /dts-v1/\n"); |
| 111 | dts_version = 1; |
| 112 | BEGIN_DEFAULT(); |
| 113 | return DT_V1; |
| 114 | } |
| 115 | |
| 116 | <*>"/memreserve/" { |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 117 | DPRINT("Keyword: /memreserve/\n"); |
| 118 | BEGIN_DEFAULT(); |
| 119 | return DT_MEMRESERVE; |
| 120 | } |
| 121 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 122 | <*>"/bits/" { |
| 123 | DPRINT("Keyword: /bits/\n"); |
| 124 | BEGIN_DEFAULT(); |
| 125 | return DT_BITS; |
| 126 | } |
| 127 | |
| 128 | <*>"/delete-property/" { |
| 129 | DPRINT("Keyword: /delete-property/\n"); |
| 130 | DPRINT("<PROPNODENAME>\n"); |
| 131 | BEGIN(PROPNODENAME); |
| 132 | return DT_DEL_PROP; |
| 133 | } |
| 134 | |
| 135 | <*>"/delete-node/" { |
| 136 | DPRINT("Keyword: /delete-node/\n"); |
| 137 | DPRINT("<PROPNODENAME>\n"); |
| 138 | BEGIN(PROPNODENAME); |
| 139 | return DT_DEL_NODE; |
| 140 | } |
| 141 | |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 142 | <*>{LABEL}: { |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 143 | DPRINT("Label: %s\n", yytext); |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 144 | yylval.labelref = xstrdup(yytext); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 145 | yylval.labelref[yyleng-1] = '\0'; |
| 146 | return DT_LABEL; |
| 147 | } |
| 148 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 149 | <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? { |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 150 | char *e; |
| 151 | DPRINT("Integer Literal: '%s'\n", yytext); |
| 152 | |
| 153 | errno = 0; |
| 154 | yylval.integer = strtoull(yytext, &e, 0); |
| 155 | |
| 156 | assert(!(*e) || !e[strspn(e, "UL")]); |
| 157 | |
| 158 | if (errno == ERANGE) |
| 159 | lexical_error("Integer literal '%s' out of range", |
| 160 | yytext); |
| 161 | else |
| 162 | /* ERANGE is the only strtoull error triggerable |
| 163 | * by strings matching the pattern */ |
| 164 | assert(errno == 0); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 165 | return DT_LITERAL; |
| 166 | } |
| 167 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 168 | <*>{CHAR_LITERAL} { |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 169 | struct data d; |
| 170 | DPRINT("Character literal: %s\n", yytext); |
| 171 | |
| 172 | d = data_copy_escape_string(yytext+1, yyleng-2); |
| 173 | if (d.len == 1) { |
| 174 | lexical_error("Empty character literal"); |
| 175 | yylval.integer = 0; |
| 176 | return DT_CHAR_LITERAL; |
| 177 | } |
| 178 | |
| 179 | yylval.integer = (unsigned char)d.val[0]; |
| 180 | |
| 181 | if (d.len > 2) |
| 182 | lexical_error("Character literal has %d" |
| 183 | " characters instead of 1", |
| 184 | d.len - 1); |
| 185 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 186 | return DT_CHAR_LITERAL; |
| 187 | } |
| 188 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 189 | <*>\&{LABEL} { /* label reference */ |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 190 | DPRINT("Ref: %s\n", yytext+1); |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 191 | yylval.labelref = xstrdup(yytext+1); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 192 | return DT_REF; |
| 193 | } |
| 194 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 195 | <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */ |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 196 | yytext[yyleng-1] = '\0'; |
| 197 | DPRINT("Ref: %s\n", yytext+2); |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 198 | yylval.labelref = xstrdup(yytext+2); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 199 | return DT_REF; |
| 200 | } |
| 201 | |
| 202 | <BYTESTRING>[0-9a-fA-F]{2} { |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 203 | yylval.byte = strtol(yytext, NULL, 16); |
| 204 | DPRINT("Byte: %02x\n", (int)yylval.byte); |
| 205 | return DT_BYTE; |
| 206 | } |
| 207 | |
| 208 | <BYTESTRING>"]" { |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 209 | DPRINT("/BYTESTRING\n"); |
| 210 | BEGIN_DEFAULT(); |
| 211 | return ']'; |
| 212 | } |
| 213 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 214 | <PROPNODENAME>\\?{PROPNODECHAR}+ { |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 215 | DPRINT("PropNodeName: %s\n", yytext); |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 216 | yylval.propnodename = xstrdup((yytext[0] == '\\') ? |
| 217 | yytext + 1 : yytext); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 218 | BEGIN_DEFAULT(); |
| 219 | return DT_PROPNODENAME; |
| 220 | } |
| 221 | |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 222 | "/incbin/" { |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 223 | DPRINT("Binary Include\n"); |
| 224 | return DT_INCBIN; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 225 | } |
| 226 | |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 227 | <*>{WS}+ /* eat whitespace */ |
| 228 | <*>{COMMENT}+ /* eat C-style comments */ |
| 229 | <*>{LINECOMMENT}+ /* eat C++-style comments */ |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 230 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 231 | <*>"<<" { return DT_LSHIFT; }; |
| 232 | <*>">>" { return DT_RSHIFT; }; |
| 233 | <*>"<=" { return DT_LE; }; |
| 234 | <*>">=" { return DT_GE; }; |
| 235 | <*>"==" { return DT_EQ; }; |
| 236 | <*>"!=" { return DT_NE; }; |
| 237 | <*>"&&" { return DT_AND; }; |
| 238 | <*>"||" { return DT_OR; }; |
| 239 | |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 240 | <*>. { |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 241 | DPRINT("Char: %c (\\x%02x)\n", yytext[0], |
| 242 | (unsigned)yytext[0]); |
| 243 | if (yytext[0] == '[') { |
| 244 | DPRINT("<BYTESTRING>\n"); |
| 245 | BEGIN(BYTESTRING); |
| 246 | } |
| 247 | if ((yytext[0] == '{') |
| 248 | || (yytext[0] == ';')) { |
| 249 | DPRINT("<PROPNODENAME>\n"); |
| 250 | BEGIN(PROPNODENAME); |
| 251 | } |
| 252 | return yytext[0]; |
| 253 | } |
| 254 | |
| 255 | %% |
| 256 | |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 257 | static void push_input_file(const char *filename) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 258 | { |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 259 | assert(filename); |
| 260 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 261 | srcfile_push(filename); |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 262 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 263 | yyin = current_srcfile->f; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 264 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 265 | yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE)); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 269 | static bool pop_input_file(void) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 270 | { |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 271 | if (srcfile_pop() == 0) |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 272 | return false; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 273 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 274 | yypop_buffer_state(); |
| 275 | yyin = current_srcfile->f; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 276 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 277 | return true; |
| 278 | } |
| 279 | |
| 280 | static void lexical_error(const char *fmt, ...) |
| 281 | { |
| 282 | va_list ap; |
| 283 | |
| 284 | va_start(ap, fmt); |
| 285 | srcpos_verror(&yylloc, "Lexical error", fmt, ap); |
| 286 | va_end(ap); |
| 287 | |
| 288 | treesource_error = true; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 289 | } |