glcpp-lex.l revision 1ffc1cd86186ae5d03bb28a1e041c4a57761515e
1%{
2/*
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include <stdio.h>
26#include <string.h>
27#include <ctype.h>
28
29#include "glcpp.h"
30#include "glcpp-parse.h"
31
32/* Flex annoyingly generates some functions without making them
33 * static. Let's declare them here. */
34int glcpp_get_column  (yyscan_t yyscanner);
35void glcpp_set_column (int  column_no , yyscan_t yyscanner);
36
37#define YY_NO_INPUT
38
39#define YY_USER_ACTION                                          \
40   do {                                                         \
41      yylloc->source = 0;                                       \
42      yylloc->first_column = yycolumn + 1;                      \
43      yylloc->first_line = yylineno;                            \
44      yycolumn += yyleng;                                       \
45   } while(0);
46#define YY_USER_INIT yylineno = 0; yycolumn = 0;
47%}
48
49%option bison-bridge bison-locations reentrant noyywrap
50%option extra-type="glcpp_parser_t *"
51%option prefix="glcpp_"
52%option stack
53
54%x DONE COMMENT UNREACHABLE
55
56SPACE		[[:space:]]
57NONSPACE	[^[:space:]]
58NEWLINE		[\n]
59HSPACE		[ \t]
60HASH		^{HSPACE}*#{HSPACE}*
61IDENTIFIER	[_a-zA-Z][_a-zA-Z0-9]*
62PUNCTUATION	[][(){}.&*~!/%<>^|;,=+-]
63OTHER		[^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
64
65DECIMAL_INTEGER		[1-9][0-9]*[uU]?
66OCTAL_INTEGER		0[0-7]*[uU]?
67HEXADECIMAL_INTEGER	0[xX][0-9a-fA-F]+[uU]?
68
69%%
70
71	/* Single-line comments */
72"//"[^\n]*\n {
73	yylineno++;
74	yycolumn = 0;
75	return NEWLINE;
76}
77
78	/* Multi-line comments */
79"/*"                    { yy_push_state(COMMENT, yyscanner); }
80<COMMENT>[^*\n]*
81<COMMENT>[^*\n]*\n      { yylineno++; yycolumn = 0; }
82<COMMENT>"*"+[^*/\n]*
83<COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; }
84<COMMENT>"*"+"/"        {
85	yy_pop_state(yyscanner);
86	if (yyextra->space_tokens)
87		return SPACE;
88}
89
90{HASH}(version) {
91	yylval->str = talloc_strdup (yyextra, yytext);
92	yylineno++;
93	yycolumn = 0;
94	yyextra->space_tokens = 0;
95	return HASH_VERSION;
96}
97
98	/* glcpp doesn't handle #extension, #version, or #pragma directives.
99	 * Simply pass them through to the main compiler's lexer/parser. */
100{HASH}(extension|pragma)[^\n]+ {
101	yylval->str = talloc_strdup (yyextra, yytext);
102	yylineno++;
103	yycolumn = 0;
104	return OTHER;
105}
106
107{HASH}ifdef/.*\n {
108	yyextra->lexing_if = 1;
109	yyextra->space_tokens = 0;
110	return HASH_IFDEF;
111}
112
113{HASH}ifndef/.*\n {
114	yyextra->lexing_if = 1;
115	yyextra->space_tokens = 0;
116	return HASH_IFNDEF;
117}
118
119{HASH}if/[^_a-zA-Z0-9].*\n {
120	yyextra->lexing_if = 1;
121	yyextra->space_tokens = 0;
122	return HASH_IF;
123}
124
125{HASH}elif/.*\n {
126	yyextra->lexing_if = 1;
127	yyextra->space_tokens = 0;
128	return HASH_ELIF;
129}
130
131{HASH}else/.*\n {
132	yyextra->space_tokens = 0;
133	return HASH_ELSE;
134}
135
136{HASH}endif/.*\n {
137	yyextra->space_tokens = 0;
138	return HASH_ENDIF;
139}
140
141	/* When skipping (due to an #if 0 or similar) consume anything
142	 * up to a newline. We do this with less priority than any
143	 * #if-related directive (#if, #elif, #else, #endif), but with
144	 * more priority than any other directive or token to avoid
145	 * any side-effects from skipped content.
146	 *
147	 * We use the lexing_if flag to avoid skipping any part of an
148	 * if conditional expression. */
149[^\n]+/\n {
150	/* Since this rule always matches, YY_USER_ACTION gets called for it,
151	 * wrongly incrementing yycolumn.  We undo that effect here. */
152	yycolumn -= yyleng;
153	if (yyextra->lexing_if ||
154	    yyextra->skip_stack == NULL ||
155	    yyextra->skip_stack->type == SKIP_NO_SKIP)
156	{
157		REJECT;
158	}
159}
160
161{HASH}error.* {
162	char *p;
163	for (p = yytext; !isalpha(p[0]); p++); /* skip "  #   " */
164	p += 5; /* skip "error" */
165	glcpp_error(yylloc, yyextra, "#error%s", p);
166}
167
168{HASH}define{HSPACE}+/{IDENTIFIER}"(" {
169	yyextra->space_tokens = 0;
170	return HASH_DEFINE_FUNC;
171}
172
173{HASH}define {
174	yyextra->space_tokens = 0;
175	return HASH_DEFINE_OBJ;
176}
177
178{HASH}undef {
179	yyextra->space_tokens = 0;
180	return HASH_UNDEF;
181}
182
183{HASH} {
184	yyextra->space_tokens = 0;
185	return HASH;
186}
187
188{DECIMAL_INTEGER} {
189	yylval->str = talloc_strdup (yyextra, yytext);
190	return INTEGER_STRING;
191}
192
193{OCTAL_INTEGER} {
194	yylval->str = talloc_strdup (yyextra, yytext);
195	return INTEGER_STRING;
196}
197
198{HEXADECIMAL_INTEGER} {
199	yylval->str = talloc_strdup (yyextra, yytext);
200	return INTEGER_STRING;
201}
202
203"<<"  {
204	return LEFT_SHIFT;
205}
206
207">>" {
208	return RIGHT_SHIFT;
209}
210
211"<=" {
212	return LESS_OR_EQUAL;
213}
214
215">=" {
216	return GREATER_OR_EQUAL;
217}
218
219"==" {
220	return EQUAL;
221}
222
223"!=" {
224	return NOT_EQUAL;
225}
226
227"&&" {
228	return AND;
229}
230
231"||" {
232	return OR;
233}
234
235"##" {
236	return PASTE;
237}
238
239"defined" {
240	return DEFINED;
241}
242
243{IDENTIFIER} {
244	yylval->str = talloc_strdup (yyextra, yytext);
245	return IDENTIFIER;
246}
247
248{PUNCTUATION} {
249	return yytext[0];
250}
251
252{OTHER}+ {
253	yylval->str = talloc_strdup (yyextra, yytext);
254	return OTHER;
255}
256
257{HSPACE}+ {
258	if (yyextra->space_tokens) {
259		return SPACE;
260	}
261}
262
263\n {
264	yyextra->lexing_if = 0;
265	yylineno++;
266	yycolumn = 0;
267	return NEWLINE;
268}
269
270	/* Handle missing newline at EOF. */
271<INITIAL><<EOF>> {
272	BEGIN DONE; /* Don't keep matching this rule forever. */
273	yyextra->lexing_if = 0;
274	return NEWLINE;
275}
276
277	/* We don't actually use the UNREACHABLE start condition. We
278	only have this action here so that we can pretend to call some
279	generated functions, (to avoid "defined but not used"
280	warnings. */
281<UNREACHABLE>. {
282	unput('.');
283	yy_top_state(yyextra);
284}
285
286%%
287
288void
289glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
290{
291	yy_scan_string(shader, parser->scanner);
292}
293