dtc-lexer.l 4.0 KB
Newer Older
D
David Gibson 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *                                                                   USA
 */

21
%option noyywrap nounput noinput never-interactive
D
David Gibson 已提交
22 23 24 25 26 27 28 29 30

%x INCLUDE
%x BYTESTRING
%x PROPNODENAME
%s V1

PROPNODECHAR	[a-zA-Z0-9,._+*#?@-]
PATHCHAR	({PROPNODECHAR}|[/])
LABEL		[a-zA-Z_][a-zA-Z0-9_]*
31 32 33 34
STRING		\"([^\\"]|\\.)*\"
WS		[[:space:]]
COMMENT		"/*"([^*]|\*+[^*/])*\*+"/"
LINECOMMENT	"//".*\n
D
David Gibson 已提交
35 36 37 38 39 40

%{
#include "dtc.h"
#include "srcpos.h"
#include "dtc-parser.tab.h"

41 42 43 44 45 46 47
YYLTYPE yylloc;

/* CAUTION: this will stop working if we ever use yyless() or yyunput() */
#define	YY_USER_ACTION \
	{ \
		srcpos_update(&yylloc, yytext, yyleng); \
	}
D
David Gibson 已提交
48 49 50 51 52 53 54 55 56

/*#define LEXDEBUG	1*/

#ifdef LEXDEBUG
#define DPRINT(fmt, ...)	fprintf(stderr, fmt, ##__VA_ARGS__)
#else
#define DPRINT(fmt, ...)	do { } while (0)
#endif

57
static int dts_version = 1;
D
David Gibson 已提交
58

59
#define BEGIN_DEFAULT()		DPRINT("<V1>\n"); \
D
David Gibson 已提交
60
				BEGIN(V1); \
61 62 63

static void push_input_file(const char *filename);
static int pop_input_file(void);
D
David Gibson 已提交
64 65 66
%}

%%
67 68 69 70
<*>"/include/"{WS}*{STRING} {
			char *name = strchr(yytext, '\"') + 1;
			yytext[yyleng-1] = '\0';
			push_input_file(name);
D
David Gibson 已提交
71 72 73 74 75 76 77 78
		}

<*><<EOF>>		{
			if (!pop_input_file()) {
				yyterminate();
			}
		}

79
<*>{STRING}	{
D
David Gibson 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
			DPRINT("String: %s\n", yytext);
			yylval.data = data_copy_escape_string(yytext+1,
					yyleng-2);
			return DT_STRING;
		}

<*>"/dts-v1/"	{
			DPRINT("Keyword: /dts-v1/\n");
			dts_version = 1;
			BEGIN_DEFAULT();
			return DT_V1;
		}

<*>"/memreserve/"	{
			DPRINT("Keyword: /memreserve/\n");
			BEGIN_DEFAULT();
			return DT_MEMRESERVE;
		}

<*>{LABEL}:	{
			DPRINT("Label: %s\n", yytext);
101
			yylval.labelref = xstrdup(yytext);
D
David Gibson 已提交
102 103 104 105 106
			yylval.labelref[yyleng-1] = '\0';
			return DT_LABEL;
		}

<V1>[0-9]+|0[xX][0-9a-fA-F]+      {
107
			yylval.literal = xstrdup(yytext);
D
David Gibson 已提交
108 109 110 111
			DPRINT("Literal: '%s'\n", yylval.literal);
			return DT_LITERAL;
		}

112
<*>\&{LABEL}	{	/* label reference */
D
David Gibson 已提交
113
			DPRINT("Ref: %s\n", yytext+1);
114
			yylval.labelref = xstrdup(yytext+1);
D
David Gibson 已提交
115 116 117
			return DT_REF;
		}

118
<*>"&{/"{PATHCHAR}+\}	{	/* new-style path reference */
D
David Gibson 已提交
119 120
			yytext[yyleng-1] = '\0';
			DPRINT("Ref: %s\n", yytext+2);
121
			yylval.labelref = xstrdup(yytext+2);
D
David Gibson 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
			return DT_REF;
		}

<BYTESTRING>[0-9a-fA-F]{2} {
			yylval.byte = strtol(yytext, NULL, 16);
			DPRINT("Byte: %02x\n", (int)yylval.byte);
			return DT_BYTE;
		}

<BYTESTRING>"]"	{
			DPRINT("/BYTESTRING\n");
			BEGIN_DEFAULT();
			return ']';
		}

<PROPNODENAME>{PROPNODECHAR}+ {
			DPRINT("PropNodeName: %s\n", yytext);
139
			yylval.propnodename = xstrdup(yytext);
D
David Gibson 已提交
140 141 142 143
			BEGIN_DEFAULT();
			return DT_PROPNODENAME;
		}

144 145 146
"/incbin/"	{
			DPRINT("Binary Include\n");
			return DT_INCBIN;
D
David Gibson 已提交
147 148
		}

149 150 151
<*>{WS}+	/* eat whitespace */
<*>{COMMENT}+	/* eat C-style comments */
<*>{LINECOMMENT}+ /* eat C++-style comments */
D
David Gibson 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

<*>.		{
			DPRINT("Char: %c (\\x%02x)\n", yytext[0],
				(unsigned)yytext[0]);
			if (yytext[0] == '[') {
				DPRINT("<BYTESTRING>\n");
				BEGIN(BYTESTRING);
			}
			if ((yytext[0] == '{')
			    || (yytext[0] == ';')) {
				DPRINT("<PROPNODENAME>\n");
				BEGIN(PROPNODENAME);
			}
			return yytext[0];
		}

%%

170
static void push_input_file(const char *filename)
D
David Gibson 已提交
171
{
172
	assert(filename);
D
David Gibson 已提交
173

174
	srcfile_push(filename);
D
David Gibson 已提交
175

176
	yyin = current_srcfile->f;
D
David Gibson 已提交
177

178
	yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
D
David Gibson 已提交
179 180 181
}


182
static int pop_input_file(void)
D
David Gibson 已提交
183
{
184
	if (srcfile_pop() == 0)
D
David Gibson 已提交
185 186
		return 0;

187 188
	yypop_buffer_state();
	yyin = current_srcfile->f;
D
David Gibson 已提交
189 190 191

	return 1;
}