1 /* SPDX-License-Identifier: GPL-2.0-or-later * << 2 /* 1 /* 3 * (C) Copyright David Gibson <dwg@au1.ibm.com> 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 4 */ 19 */ 5 20 6 %option noyywrap nounput noinput never-interac 21 %option noyywrap nounput noinput never-interactive 7 22 8 %x BYTESTRING 23 %x BYTESTRING 9 %x PROPNODENAME 24 %x PROPNODENAME 10 %s V1 25 %s V1 11 26 12 PROPNODECHAR [a-zA-Z0-9,._+*#?@-] 27 PROPNODECHAR [a-zA-Z0-9,._+*#?@-] 13 PATHCHAR ({PROPNODECHAR}|[/]) 28 PATHCHAR ({PROPNODECHAR}|[/]) 14 LABEL [a-zA-Z_][a-zA-Z0-9_]* 29 LABEL [a-zA-Z_][a-zA-Z0-9_]* 15 STRING \"([^\\"]|\\.)*\" 30 STRING \"([^\\"]|\\.)*\" 16 CHAR_LITERAL '([^']|\\')*' 31 CHAR_LITERAL '([^']|\\')*' 17 WS [[:space:]] 32 WS [[:space:]] 18 COMMENT "/*"([^*]|\*+[^*/])*\*+"/" 33 COMMENT "/*"([^*]|\*+[^*/])*\*+"/" 19 LINECOMMENT "//".*\n 34 LINECOMMENT "//".*\n 20 35 21 %{ 36 %{ 22 #include "dtc.h" 37 #include "dtc.h" 23 #include "srcpos.h" 38 #include "srcpos.h" 24 #include "dtc-parser.tab.h" 39 #include "dtc-parser.tab.h" 25 40 26 extern bool treesource_error; 41 extern bool treesource_error; 27 42 28 /* CAUTION: this will stop working if we ever 43 /* CAUTION: this will stop working if we ever use yyless() or yyunput() */ 29 #define YY_USER_ACTION \ 44 #define YY_USER_ACTION \ 30 { \ 45 { \ 31 srcpos_update(&yylloc, yytext, 46 srcpos_update(&yylloc, yytext, yyleng); \ 32 } 47 } 33 48 34 /*#define LEXDEBUG 1*/ 49 /*#define LEXDEBUG 1*/ 35 50 36 #ifdef LEXDEBUG 51 #ifdef LEXDEBUG 37 #define DPRINT(fmt, ...) fprintf(stderr 52 #define DPRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) 38 #else 53 #else 39 #define DPRINT(fmt, ...) do { } while ( 54 #define DPRINT(fmt, ...) do { } while (0) 40 #endif 55 #endif 41 56 42 static int dts_version = 1; 57 static int dts_version = 1; 43 58 44 #define BEGIN_DEFAULT() DPRINT("<V1>\n 59 #define BEGIN_DEFAULT() DPRINT("<V1>\n"); \ 45 BEGIN(V1); \ 60 BEGIN(V1); \ 46 61 47 static void push_input_file(const char *filena 62 static void push_input_file(const char *filename); 48 static bool pop_input_file(void); 63 static bool pop_input_file(void); 49 static void PRINTF(1, 2) lexical_error(const c !! 64 static void lexical_error(const char *fmt, ...); 50 << 51 %} 65 %} 52 66 53 %% 67 %% 54 <*>"/include/"{WS}*{STRING} { 68 <*>"/include/"{WS}*{STRING} { 55 char *name = strchr(yy 69 char *name = strchr(yytext, '\"') + 1; 56 yytext[yyleng-1] = '\0 70 yytext[yyleng-1] = '\0'; 57 push_input_file(name); 71 push_input_file(name); 58 } 72 } 59 73 60 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t] !! 74 <*>^"#"(line)?[ \t]+[0-9]+[ \t]+{STRING}([ \t]+[0-9]+)? { 61 char *line, *fnstart, 75 char *line, *fnstart, *fnend; 62 struct data fn; 76 struct data fn; 63 /* skip text before li 77 /* skip text before line # */ 64 line = yytext; 78 line = yytext; 65 while (!isdigit((unsig 79 while (!isdigit((unsigned char)*line)) 66 line++; 80 line++; 67 81 68 /* regexp ensures that 82 /* regexp ensures that first and list " 69 * in the whole yytext 83 * in the whole yytext are those at 70 * beginning and end o 84 * beginning and end of the filename string */ 71 fnstart = memchr(yytex 85 fnstart = memchr(yytext, '"', yyleng); 72 for (fnend = yytext + 86 for (fnend = yytext + yyleng - 1; 73 *fnend != '"'; fn 87 *fnend != '"'; fnend--) 74 ; 88 ; 75 assert(fnstart && fnen 89 assert(fnstart && fnend && (fnend > fnstart)); 76 90 77 fn = data_copy_escape_ 91 fn = data_copy_escape_string(fnstart + 1, 78 92 fnend - fnstart - 1); 79 93 80 /* Don't allow nuls in 94 /* Don't allow nuls in filenames */ 81 if (memchr(fn.val, '\0 95 if (memchr(fn.val, '\0', fn.len - 1)) 82 lexical_error( 96 lexical_error("nul in line number directive"); 83 97 84 /* -1 since #line is t 98 /* -1 since #line is the number of the next line */ 85 srcpos_set_line(xstrdu 99 srcpos_set_line(xstrdup(fn.val), atoi(line) - 1); 86 data_free(fn); 100 data_free(fn); 87 } 101 } 88 102 89 <*><<EOF>> { 103 <*><<EOF>> { 90 if (!pop_input_file()) 104 if (!pop_input_file()) { 91 yyterminate(); 105 yyterminate(); 92 } 106 } 93 } 107 } 94 108 95 <*>{STRING} { 109 <*>{STRING} { 96 DPRINT("String: %s\n", 110 DPRINT("String: %s\n", yytext); 97 yylval.data = data_cop 111 yylval.data = data_copy_escape_string(yytext+1, 98 yyleng 112 yyleng-2); 99 return DT_STRING; 113 return DT_STRING; 100 } 114 } 101 115 102 <*>"/dts-v1/" { 116 <*>"/dts-v1/" { 103 DPRINT("Keyword: /dts- 117 DPRINT("Keyword: /dts-v1/\n"); 104 dts_version = 1; 118 dts_version = 1; 105 BEGIN_DEFAULT(); 119 BEGIN_DEFAULT(); 106 return DT_V1; 120 return DT_V1; 107 } 121 } 108 122 109 <*>"/plugin/" { << 110 DPRINT("Keyword: /plug << 111 return DT_PLUGIN; << 112 } << 113 << 114 <*>"/memreserve/" { 123 <*>"/memreserve/" { 115 DPRINT("Keyword: /memr 124 DPRINT("Keyword: /memreserve/\n"); 116 BEGIN_DEFAULT(); 125 BEGIN_DEFAULT(); 117 return DT_MEMRESERVE; 126 return DT_MEMRESERVE; 118 } 127 } 119 128 120 <*>"/bits/" { 129 <*>"/bits/" { 121 DPRINT("Keyword: /bits 130 DPRINT("Keyword: /bits/\n"); 122 BEGIN_DEFAULT(); 131 BEGIN_DEFAULT(); 123 return DT_BITS; 132 return DT_BITS; 124 } 133 } 125 134 126 <*>"/delete-property/" { 135 <*>"/delete-property/" { 127 DPRINT("Keyword: /dele 136 DPRINT("Keyword: /delete-property/\n"); 128 DPRINT("<PROPNODENAME> 137 DPRINT("<PROPNODENAME>\n"); 129 BEGIN(PROPNODENAME); 138 BEGIN(PROPNODENAME); 130 return DT_DEL_PROP; 139 return DT_DEL_PROP; 131 } 140 } 132 141 133 <*>"/delete-node/" { 142 <*>"/delete-node/" { 134 DPRINT("Keyword: /dele 143 DPRINT("Keyword: /delete-node/\n"); 135 DPRINT("<PROPNODENAME> 144 DPRINT("<PROPNODENAME>\n"); 136 BEGIN(PROPNODENAME); 145 BEGIN(PROPNODENAME); 137 return DT_DEL_NODE; 146 return DT_DEL_NODE; 138 } 147 } 139 148 140 <*>"/omit-if-no-ref/" { << 141 DPRINT("Keyword: /omit << 142 DPRINT("<PROPNODENAME> << 143 BEGIN(PROPNODENAME); << 144 return DT_OMIT_NO_REF; << 145 } << 146 << 147 <*>{LABEL}: { 149 <*>{LABEL}: { 148 DPRINT("Label: %s\n", 150 DPRINT("Label: %s\n", yytext); 149 yylval.labelref = xstr 151 yylval.labelref = xstrdup(yytext); 150 yylval.labelref[yyleng 152 yylval.labelref[yyleng-1] = '\0'; 151 return DT_LABEL; 153 return DT_LABEL; 152 } 154 } 153 155 154 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? 156 <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? { 155 char *e; 157 char *e; 156 DPRINT("Integer Litera 158 DPRINT("Integer Literal: '%s'\n", yytext); 157 159 158 errno = 0; 160 errno = 0; 159 yylval.integer = strto 161 yylval.integer = strtoull(yytext, &e, 0); 160 162 161 if (*e && e[strspn(e, 163 if (*e && e[strspn(e, "UL")]) { 162 lexical_error( 164 lexical_error("Bad integer literal '%s'", 163 165 yytext); 164 } 166 } 165 167 166 if (errno == ERANGE) 168 if (errno == ERANGE) 167 lexical_error( 169 lexical_error("Integer literal '%s' out of range", 168 170 yytext); 169 else 171 else 170 /* ERANGE is t 172 /* ERANGE is the only strtoull error triggerable 171 * by strings 173 * by strings matching the pattern */ 172 assert(errno = 174 assert(errno == 0); 173 return DT_LITERAL; 175 return DT_LITERAL; 174 } 176 } 175 177 176 <*>{CHAR_LITERAL} { 178 <*>{CHAR_LITERAL} { 177 struct data d; 179 struct data d; 178 DPRINT("Character lite 180 DPRINT("Character literal: %s\n", yytext); 179 181 180 d = data_copy_escape_s 182 d = data_copy_escape_string(yytext+1, yyleng-2); 181 if (d.len == 1) { 183 if (d.len == 1) { 182 lexical_error( 184 lexical_error("Empty character literal"); 183 yylval.integer 185 yylval.integer = 0; 184 } else { !! 186 return DT_CHAR_LITERAL; 185 yylval.integer << 186 << 187 if (d.len > 2) << 188 lexica << 189 << 190 << 191 } 187 } 192 188 193 data_free(d); !! 189 yylval.integer = (unsigned char)d.val[0]; >> 190 >> 191 if (d.len > 2) >> 192 lexical_error("Character literal has %d" >> 193 " characters instead of 1", >> 194 d.len - 1); >> 195 194 return DT_CHAR_LITERAL 196 return DT_CHAR_LITERAL; 195 } 197 } 196 198 197 <*>\&{LABEL} { /* label reference */ 199 <*>\&{LABEL} { /* label reference */ 198 DPRINT("Ref: %s\n", yy 200 DPRINT("Ref: %s\n", yytext+1); 199 yylval.labelref = xstr 201 yylval.labelref = xstrdup(yytext+1); 200 return DT_LABEL_REF; !! 202 return DT_REF; 201 } 203 } 202 204 203 <*>"&{"{PATHCHAR}*\} { /* new-style p !! 205 <*>"&{/"{PATHCHAR}*\} { /* new-style path reference */ 204 yytext[yyleng-1] = '\0 206 yytext[yyleng-1] = '\0'; 205 DPRINT("Ref: %s\n", yy 207 DPRINT("Ref: %s\n", yytext+2); 206 yylval.labelref = xstr 208 yylval.labelref = xstrdup(yytext+2); 207 return DT_PATH_REF; !! 209 return DT_REF; 208 } 210 } 209 211 210 <BYTESTRING>[0-9a-fA-F]{2} { 212 <BYTESTRING>[0-9a-fA-F]{2} { 211 yylval.byte = strtol(y 213 yylval.byte = strtol(yytext, NULL, 16); 212 DPRINT("Byte: %02x\n", 214 DPRINT("Byte: %02x\n", (int)yylval.byte); 213 return DT_BYTE; 215 return DT_BYTE; 214 } 216 } 215 217 216 <BYTESTRING>"]" { 218 <BYTESTRING>"]" { 217 DPRINT("/BYTESTRING\n" 219 DPRINT("/BYTESTRING\n"); 218 BEGIN_DEFAULT(); 220 BEGIN_DEFAULT(); 219 return ']'; 221 return ']'; 220 } 222 } 221 223 222 <PROPNODENAME>\\?{PROPNODECHAR}+ { 224 <PROPNODENAME>\\?{PROPNODECHAR}+ { 223 DPRINT("PropNodeName: 225 DPRINT("PropNodeName: %s\n", yytext); 224 yylval.propnodename = 226 yylval.propnodename = xstrdup((yytext[0] == '\\') ? 225 227 yytext + 1 : yytext); 226 BEGIN_DEFAULT(); 228 BEGIN_DEFAULT(); 227 return DT_PROPNODENAME 229 return DT_PROPNODENAME; 228 } 230 } 229 231 230 "/incbin/" { 232 "/incbin/" { 231 DPRINT("Binary Include 233 DPRINT("Binary Include\n"); 232 return DT_INCBIN; 234 return DT_INCBIN; 233 } 235 } 234 236 235 <*>{WS}+ /* eat whitespace */ 237 <*>{WS}+ /* eat whitespace */ 236 <*>{COMMENT}+ /* eat C-style comments */ 238 <*>{COMMENT}+ /* eat C-style comments */ 237 <*>{LINECOMMENT}+ /* eat C++-style comments */ 239 <*>{LINECOMMENT}+ /* eat C++-style comments */ 238 240 239 <*>"<<" { return DT_LSHIFT; }; 241 <*>"<<" { return DT_LSHIFT; }; 240 <*>">>" { return DT_RSHIFT; }; 242 <*>">>" { return DT_RSHIFT; }; 241 <*>"<=" { return DT_LE; }; 243 <*>"<=" { return DT_LE; }; 242 <*>">=" { return DT_GE; }; 244 <*>">=" { return DT_GE; }; 243 <*>"==" { return DT_EQ; }; 245 <*>"==" { return DT_EQ; }; 244 <*>"!=" { return DT_NE; }; 246 <*>"!=" { return DT_NE; }; 245 <*>"&&" { return DT_AND; }; 247 <*>"&&" { return DT_AND; }; 246 <*>"||" { return DT_OR; }; 248 <*>"||" { return DT_OR; }; 247 249 248 <*>. { 250 <*>. { 249 DPRINT("Char: %c (\\x% 251 DPRINT("Char: %c (\\x%02x)\n", yytext[0], 250 (unsigned)yyte 252 (unsigned)yytext[0]); 251 if (yytext[0] == '[') 253 if (yytext[0] == '[') { 252 DPRINT("<BYTES 254 DPRINT("<BYTESTRING>\n"); 253 BEGIN(BYTESTRI 255 BEGIN(BYTESTRING); 254 } 256 } 255 if ((yytext[0] == '{') 257 if ((yytext[0] == '{') 256 || (yytext[0] == ' 258 || (yytext[0] == ';')) { 257 DPRINT("<PROPN 259 DPRINT("<PROPNODENAME>\n"); 258 BEGIN(PROPNODE 260 BEGIN(PROPNODENAME); 259 } 261 } 260 return yytext[0]; 262 return yytext[0]; 261 } 263 } 262 264 263 %% 265 %% 264 266 265 static void push_input_file(const char *filena 267 static void push_input_file(const char *filename) 266 { 268 { 267 assert(filename); 269 assert(filename); 268 270 269 srcfile_push(filename); 271 srcfile_push(filename); 270 272 271 yyin = current_srcfile->f; 273 yyin = current_srcfile->f; 272 274 273 yypush_buffer_state(yy_create_buffer(y 275 yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE)); 274 } 276 } 275 277 276 278 277 static bool pop_input_file(void) 279 static bool pop_input_file(void) 278 { 280 { 279 if (srcfile_pop() == 0) 281 if (srcfile_pop() == 0) 280 return false; 282 return false; 281 283 282 yypop_buffer_state(); 284 yypop_buffer_state(); 283 yyin = current_srcfile->f; 285 yyin = current_srcfile->f; 284 286 285 return true; 287 return true; 286 } 288 } 287 289 288 static void lexical_error(const char *fmt, ... 290 static void lexical_error(const char *fmt, ...) 289 { 291 { 290 va_list ap; 292 va_list ap; 291 293 292 va_start(ap, fmt); 294 va_start(ap, fmt); 293 srcpos_verror(&yylloc, "Lexical error" 295 srcpos_verror(&yylloc, "Lexical error", fmt, ap); 294 va_end(ap); 296 va_end(ap); 295 297 296 treesource_error = true; 298 treesource_error = true; 297 } 299 }
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.