blob: 06bde3f1957e003ca6c995dc63fdcc8d351db095 [file] [log] [blame]
Carl Worth3a37b872010-05-10 11:44:09 -07001%{
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
Carl Wortha1e32bc2010-05-10 13:17:25 -070028#include "glcpp.h"
Carl Worth3a37b872010-05-10 11:44:09 -070029#include "glcpp-parse.h"
30%}
31
Kenneth Graunkee0e429f2010-06-16 16:26:28 -070032%option bison-bridge reentrant noyywrap
Carl Worth5070a202010-05-12 12:45:33 -070033%option extra-type="glcpp_parser_t *"
Kenneth Graunke254a4852010-06-16 11:51:43 -070034%option prefix="glcpp_"
Carl Worth3a37b872010-05-10 11:44:09 -070035
Kenneth Graunkef82d6732010-06-16 12:53:19 -070036%x DONE
37
Carl Worth0b27b5f2010-05-10 16:16:06 -070038SPACE [[:space:]]
39NONSPACE [^[:space:]]
Carl Worth33cc4002010-05-12 12:17:10 -070040NEWLINE [\n]
Carl Worth0b27b5f2010-05-10 16:16:06 -070041HSPACE [ \t]
Carl Worthe36a4d52010-05-14 17:29:24 -070042HASH ^{HSPACE}*#{HSPACE}*
Carl Worth0b27b5f2010-05-10 16:16:06 -070043IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
Carl Worth63101692010-05-29 05:07:24 -070044PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
Carl Worth3ff81672010-05-25 13:09:03 -070045OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
Carl Worth33cc4002010-05-12 12:17:10 -070046
Carl Worth03f6d5d2010-05-24 11:29:02 -070047DECIMAL_INTEGER [1-9][0-9]*[uU]?
48OCTAL_INTEGER 0[0-7]*[uU]?
49HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
50
Carl Worth111e25b2010-06-02 12:54:15 -070051NON_STARS_THEN_STARS [^*]*[*]+
52
Carl Worth3a37b872010-05-10 11:44:09 -070053%%
54
Carl Worth25714152010-06-01 12:18:43 -070055 /* Single-line comments */
56"//"[^\n]+\n {
57 return NEWLINE;
58}
59
60 /* Multi-line comments */
Carl Worth111e25b2010-06-02 12:54:15 -070061"/*"({NON_STARS_THEN_STARS}[^*/])*{NON_STARS_THEN_STARS}"/" {
Carl Worth25714152010-06-01 12:18:43 -070062 if (yyextra->space_tokens)
63 return SPACE;
64}
65
Kenneth Graunke3b73ea32010-06-16 12:21:17 -070066 /* glcpp doesn't handle #extension, #version, or #pragma directives.
67 * Simply pass them through to the main compiler's lexer/parser. */
68{HASH}(extension|version|pragma).*\n {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -070069 yylval->str = xtalloc_strdup (yyextra, yytext);
Kenneth Graunke3b73ea32010-06-16 12:21:17 -070070 return OTHER;
71}
72
Carl Wortha771a402010-06-01 11:20:18 -070073{HASH}if/.*\n {
74 yyextra->lexing_if = 1;
75 yyextra->space_tokens = 0;
76 return HASH_IF;
77}
78
79{HASH}elif/.*\n {
80 yyextra->lexing_if = 1;
81 yyextra->space_tokens = 0;
82 return HASH_ELIF;
83}
84
85{HASH}else/.*\n {
86 yyextra->space_tokens = 0;
87 return HASH_ELSE;
88}
89
90{HASH}endif/.*\n {
91 yyextra->space_tokens = 0;
92 return HASH_ENDIF;
93}
94
95 /* When skipping (due to an #if 0 or similar) consume anything
96 * up to a newline. We do this less priroty than any
97 * #if-related directive (#if, #elif, #else, #endif), but with
98 * more priority than any other directive or token to avoid
99 * any side-effects from skipped content.
100 *
101 * We use the lexing_if flag to avoid skipping any part of an
102 * if conditional expression. */
103[^\n]+/\n {
104 if (yyextra->lexing_if ||
105 yyextra->skip_stack == NULL ||
106 yyextra->skip_stack->type == SKIP_NO_SKIP)
107 {
108 REJECT;
109 }
110}
111
Carl Worth3ff81672010-05-25 13:09:03 -0700112{HASH}define{HSPACE}+/{IDENTIFIER}"(" {
Carl Worthf34a0002010-05-25 16:59:02 -0700113 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700114 return HASH_DEFINE_FUNC;
Carl Worthb20d33c2010-05-20 22:27:07 -0700115}
116
Carl Worth3ff81672010-05-25 13:09:03 -0700117{HASH}define {
Carl Worthf34a0002010-05-25 16:59:02 -0700118 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700119 return HASH_DEFINE_OBJ;
Carl Worthb20d33c2010-05-20 22:27:07 -0700120}
121
Carl Worth3ff81672010-05-25 13:09:03 -0700122{HASH}undef {
Carl Worthf34a0002010-05-25 16:59:02 -0700123 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700124 return HASH_UNDEF;
Carl Worth03f6d5d2010-05-24 11:29:02 -0700125}
126
Carl Worth3ff81672010-05-25 13:09:03 -0700127{HASH} {
Carl Worthf34a0002010-05-25 16:59:02 -0700128 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700129 return HASH;
Carl Worth81f01432010-05-14 17:08:45 -0700130}
131
Carl Worth8fed1cd2010-05-26 09:32:12 -0700132{DECIMAL_INTEGER} {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700133 yylval->str = xtalloc_strdup (yyextra, yytext);
Carl Worth050e3de2010-05-27 14:36:29 -0700134 return INTEGER_STRING;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700135}
136
137{OCTAL_INTEGER} {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700138 yylval->str = xtalloc_strdup (yyextra, yytext);
Carl Worth050e3de2010-05-27 14:36:29 -0700139 return INTEGER_STRING;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700140}
141
142{HEXADECIMAL_INTEGER} {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700143 yylval->str = xtalloc_strdup (yyextra, yytext);
Carl Worth050e3de2010-05-27 14:36:29 -0700144 return INTEGER_STRING;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700145}
146
Carl Worthf34a0002010-05-25 16:59:02 -0700147"<<" {
148 return LEFT_SHIFT;
Carl Worthb1854fd2010-05-25 16:28:26 -0700149}
150
Carl Worthf34a0002010-05-25 16:59:02 -0700151">>" {
152 return RIGHT_SHIFT;
Carl Worthb1854fd2010-05-25 16:28:26 -0700153}
154
Carl Worthf34a0002010-05-25 16:59:02 -0700155"<=" {
156 return LESS_OR_EQUAL;
157}
158
159">=" {
160 return GREATER_OR_EQUAL;
161}
162
163"==" {
164 return EQUAL;
165}
166
167"!=" {
168 return NOT_EQUAL;
169}
170
171"&&" {
172 return AND;
173}
174
175"||" {
176 return OR;
177}
178
179"##" {
180 return PASTE;
181}
182
Carl Worth8fed1cd2010-05-26 09:32:12 -0700183"defined" {
184 return DEFINED;
185}
186
Carl Worth16c1e982010-05-26 09:35:34 -0700187{IDENTIFIER} {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700188 yylval->str = xtalloc_strdup (yyextra, yytext);
Carl Worth16c1e982010-05-26 09:35:34 -0700189 return IDENTIFIER;
190}
191
Carl Worthf34a0002010-05-25 16:59:02 -0700192{PUNCTUATION} {
193 return yytext[0];
Carl Worthb1854fd2010-05-25 16:28:26 -0700194}
195
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700196{OTHER}+ {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700197 yylval->str = xtalloc_strdup (yyextra, yytext);
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700198 return OTHER;
Carl Worth3ff81672010-05-25 13:09:03 -0700199}
200
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700201{HSPACE}+ {
Carl Worthf34a0002010-05-25 16:59:02 -0700202 if (yyextra->space_tokens) {
Carl Worthf34a0002010-05-25 16:59:02 -0700203 return SPACE;
204 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700205}
Carl Worthfcbbb462010-05-13 09:36:23 -0700206
Carl Worth3ff81672010-05-25 13:09:03 -0700207\n {
Carl Wortha771a402010-06-01 11:20:18 -0700208 yyextra->lexing_if = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700209 return NEWLINE;
Carl Worth33cc4002010-05-12 12:17:10 -0700210}
Carl Worth3a37b872010-05-10 11:44:09 -0700211
Kenneth Graunkef82d6732010-06-16 12:53:19 -0700212 /* Handle missing newline at EOF. */
213<INITIAL><<EOF>> {
214 BEGIN DONE; /* Don't keep matching this rule forever. */
215 yyextra->lexing_if = 0;
216 return NEWLINE;
217}
218
Carl Worth3a37b872010-05-10 11:44:09 -0700219%%
Kenneth Graunke1b1f43e2010-06-16 12:01:17 -0700220
221void
222glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
223{
224 yy_scan_string(shader, parser->scanner);
225}