File Index
Files and directories
Sign in
FILE

dtc-lexer.l

File preview with syntax highlighting.

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