Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [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 | |
| 21 | %option noyywrap nounput noinput never-interactive |
| 22 | |
| 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_]* |
| 30 | STRING \"([^\\"]|\\.)*\" |
| 31 | CHAR_LITERAL '([^']|\\')*' |
| 32 | WS [[:space:]] |
| 33 | COMMENT "/*"([^*]|\*+[^*/])*\*+"/" |
| 34 | LINECOMMENT "//".*\n |
| 35 | |
| 36 | %{ |
| 37 | #include "dtc.h" |
| 38 | #include "srcpos.h" |
| 39 | #include "dtc-parser.tab.h" |
| 40 | |
| 41 | YYLTYPE yylloc; |
| 42 | extern bool treesource_error; |
| 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 | } |
| 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 | |
| 58 | static int dts_version = 1; |
| 59 | |
| 60 | #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \ |
| 61 | BEGIN(V1); \ |
| 62 | |
| 63 | static void push_input_file(const char *filename); |
| 64 | static bool pop_input_file(void); |
| 65 | #ifdef __GNUC__ |
| 66 | static void lexical_error(const char *fmt, ...) |
| 67 | __attribute__((format (printf, 1, 2))); |
| 68 | #else |
| 69 | static void lexical_error(const char *fmt, ...); |
| 70 | #endif |
| 71 | |
| 72 | %} |
| 73 | |
| 74 | %% |
| 75 | <*>"/include/"{WS}*{STRING} { |
| 76 | char *name = strchr(yytext, '\"') + 1; |
| 77 | yytext[yyleng-1] = '\0'; |
| 78 | push_input_file(name); |
| 79 | } |
| 80 | |
| 81 | <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? { |
| 82 | char *line, *fnstart, *fnend; |
| 83 | struct data fn; |
| 84 | /* skip text before line # */ |
| 85 | line = yytext; |
| 86 | while (!isdigit((unsigned char)*line)) |
| 87 | line++; |
| 88 | |
| 89 | /* regexp ensures that first and list " |
| 90 | * in the whole yytext are those at |
| 91 | * beginning and end of the filename string */ |
| 92 | fnstart = memchr(yytext, '"', yyleng); |
| 93 | for (fnend = yytext + yyleng - 1; |
| 94 | *fnend != '"'; fnend--) |
| 95 | ; |
| 96 | assert(fnstart && fnend && (fnend > fnstart)); |
| 97 | |
| 98 | fn = data_copy_escape_string(fnstart + 1, |
| 99 | fnend - fnstart - 1); |
| 100 | |
| 101 | /* Don't allow nuls in filenames */ |
| 102 | if (memchr(fn.val, '\0', fn.len - 1)) |
| 103 | lexical_error("nul in line number directive"); |
| 104 | |
| 105 | /* -1 since #line is the number of the next line */ |
| 106 | srcpos_set_line(xstrdup(fn.val), atoi(line) - 1); |
| 107 | data_free(fn); |
| 108 | } |
| 109 | |
| 110 | <*><<EOF>> { |
| 111 | if (!pop_input_file()) { |
| 112 | yyterminate(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | <*>{STRING} { |
| 117 | DPRINT("String: %s\n", yytext); |
| 118 | yylval.data = data_copy_escape_string(yytext+1, |
| 119 | yyleng-2); |
| 120 | return DT_STRING; |
| 121 | } |
| 122 | |
| 123 | <*>"/dts-v1/" { |
| 124 | DPRINT("Keyword: /dts-v1/\n"); |
| 125 | dts_version = 1; |
| 126 | BEGIN_DEFAULT(); |
| 127 | return DT_V1; |
| 128 | } |
| 129 | |
| 130 | <*>"/plugin/" { |
| 131 | DPRINT("Keyword: /plugin/\n"); |
| 132 | return DT_PLUGIN; |
| 133 | } |
| 134 | |
| 135 | <*>"/memreserve/" { |
| 136 | DPRINT("Keyword: /memreserve/\n"); |
| 137 | BEGIN_DEFAULT(); |
| 138 | return DT_MEMRESERVE; |
| 139 | } |
| 140 | |
| 141 | <*>"/bits/" { |
| 142 | DPRINT("Keyword: /bits/\n"); |
| 143 | BEGIN_DEFAULT(); |
| 144 | return DT_BITS; |
| 145 | } |
| 146 | |
| 147 | <*>"/delete-property/" { |
| 148 | DPRINT("Keyword: /delete-property/\n"); |
| 149 | DPRINT("<PROPNODENAME>\n"); |
| 150 | BEGIN(PROPNODENAME); |
| 151 | return DT_DEL_PROP; |
| 152 | } |
| 153 | |
| 154 | <*>"/delete-node/" { |
| 155 | DPRINT("Keyword: /delete-node/\n"); |
| 156 | DPRINT("<PROPNODENAME>\n"); |
| 157 | BEGIN(PROPNODENAME); |
| 158 | return DT_DEL_NODE; |
| 159 | } |
| 160 | |
| 161 | <*>{LABEL}: { |
| 162 | DPRINT("Label: %s\n", yytext); |
| 163 | yylval.labelref = xstrdup(yytext); |
| 164 | yylval.labelref[yyleng-1] = '\0'; |
| 165 | return DT_LABEL; |
| 166 | } |
| 167 | |
| 168 | <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? { |
| 169 | char *e; |
| 170 | DPRINT("Integer Literal: '%s'\n", yytext); |
| 171 | |
| 172 | errno = 0; |
| 173 | yylval.integer = strtoull(yytext, &e, 0); |
| 174 | |
| 175 | if (*e && e[strspn(e, "UL")]) { |
| 176 | lexical_error("Bad integer literal '%s'", |
| 177 | yytext); |
| 178 | } |
| 179 | |
| 180 | if (errno == ERANGE) |
| 181 | lexical_error("Integer literal '%s' out of range", |
| 182 | yytext); |
| 183 | else |
| 184 | /* ERANGE is the only strtoull error triggerable |
| 185 | * by strings matching the pattern */ |
| 186 | assert(errno == 0); |
| 187 | return DT_LITERAL; |
| 188 | } |
| 189 | |
| 190 | <*>{CHAR_LITERAL} { |
| 191 | struct data d; |
| 192 | DPRINT("Character literal: %s\n", yytext); |
| 193 | |
| 194 | d = data_copy_escape_string(yytext+1, yyleng-2); |
| 195 | if (d.len == 1) { |
| 196 | lexical_error("Empty character literal"); |
| 197 | yylval.integer = 0; |
| 198 | } else { |
| 199 | yylval.integer = (unsigned char)d.val[0]; |
| 200 | |
| 201 | if (d.len > 2) |
| 202 | lexical_error("Character literal has %d" |
| 203 | " characters instead of 1", |
| 204 | d.len - 1); |
| 205 | } |
| 206 | |
| 207 | data_free(d); |
| 208 | return DT_CHAR_LITERAL; |
| 209 | } |
| 210 | |
| 211 | <*>\&{LABEL} { /* label reference */ |
| 212 | DPRINT("Ref: %s\n", yytext+1); |
| 213 | yylval.labelref = xstrdup(yytext+1); |
| 214 | return DT_REF; |
| 215 | } |
| 216 | |
| 217 | <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */ |
| 218 | yytext[yyleng-1] = '\0'; |
| 219 | DPRINT("Ref: %s\n", yytext+2); |
| 220 | yylval.labelref = xstrdup(yytext+2); |
| 221 | return DT_REF; |
| 222 | } |
| 223 | |
| 224 | <BYTESTRING>[0-9a-fA-F]{2} { |
| 225 | yylval.byte = strtol(yytext, NULL, 16); |
| 226 | DPRINT("Byte: %02x\n", (int)yylval.byte); |
| 227 | return DT_BYTE; |
| 228 | } |
| 229 | |
| 230 | <BYTESTRING>"]" { |
| 231 | DPRINT("/BYTESTRING\n"); |
| 232 | BEGIN_DEFAULT(); |
| 233 | return ']'; |
| 234 | } |
| 235 | |
| 236 | <PROPNODENAME>\\?{PROPNODECHAR}+ { |
| 237 | DPRINT("PropNodeName: %s\n", yytext); |
| 238 | yylval.propnodename = xstrdup((yytext[0] == '\\') ? |
| 239 | yytext + 1 : yytext); |
| 240 | BEGIN_DEFAULT(); |
| 241 | return DT_PROPNODENAME; |
| 242 | } |
| 243 | |
| 244 | "/incbin/" { |
| 245 | DPRINT("Binary Include\n"); |
| 246 | return DT_INCBIN; |
| 247 | } |
| 248 | |
| 249 | <*>{WS}+ /* eat whitespace */ |
| 250 | <*>{COMMENT}+ /* eat C-style comments */ |
| 251 | <*>{LINECOMMENT}+ /* eat C++-style comments */ |
| 252 | |
| 253 | <*>"<<" { return DT_LSHIFT; }; |
| 254 | <*>">>" { return DT_RSHIFT; }; |
| 255 | <*>"<=" { return DT_LE; }; |
| 256 | <*>">=" { return DT_GE; }; |
| 257 | <*>"==" { return DT_EQ; }; |
| 258 | <*>"!=" { return DT_NE; }; |
| 259 | <*>"&&" { return DT_AND; }; |
| 260 | <*>"||" { return DT_OR; }; |
| 261 | |
| 262 | <*>. { |
| 263 | DPRINT("Char: %c (\\x%02x)\n", yytext[0], |
| 264 | (unsigned)yytext[0]); |
| 265 | if (yytext[0] == '[') { |
| 266 | DPRINT("<BYTESTRING>\n"); |
| 267 | BEGIN(BYTESTRING); |
| 268 | } |
| 269 | if ((yytext[0] == '{') |
| 270 | || (yytext[0] == ';')) { |
| 271 | DPRINT("<PROPNODENAME>\n"); |
| 272 | BEGIN(PROPNODENAME); |
| 273 | } |
| 274 | return yytext[0]; |
| 275 | } |
| 276 | |
| 277 | %% |
| 278 | |
| 279 | static void push_input_file(const char *filename) |
| 280 | { |
| 281 | assert(filename); |
| 282 | |
| 283 | srcfile_push(filename); |
| 284 | |
| 285 | yyin = current_srcfile->f; |
| 286 | |
| 287 | yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE)); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | static bool pop_input_file(void) |
| 292 | { |
| 293 | if (srcfile_pop() == 0) |
| 294 | return false; |
| 295 | |
| 296 | yypop_buffer_state(); |
| 297 | yyin = current_srcfile->f; |
| 298 | |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | static void lexical_error(const char *fmt, ...) |
| 303 | { |
| 304 | va_list ap; |
| 305 | |
| 306 | va_start(ap, fmt); |
| 307 | srcpos_verror(&yylloc, "Lexical error", fmt, ap); |
| 308 | va_end(ap); |
| 309 | |
| 310 | treesource_error = true; |
| 311 | } |