blob: f17336974e6ed1972994e91be6b2b9b0a7faca89 [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 Graunke465e03e2010-06-16 16:35:57 -070032%option bison-bridge bison-locations 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
Kenneth Graunke77260fc2010-06-17 14:36:34 -070073{HASH}ifdef/.*\n {
74 yyextra->space_tokens = 0;
75 return HASH_IFDEF;
76}
77
78{HASH}ifndef/.*\n {
79 yyextra->space_tokens = 0;
80 return HASH_IFNDEF;
81}
82
Carl Wortha771a402010-06-01 11:20:18 -070083{HASH}if/.*\n {
84 yyextra->lexing_if = 1;
85 yyextra->space_tokens = 0;
86 return HASH_IF;
87}
88
89{HASH}elif/.*\n {
90 yyextra->lexing_if = 1;
91 yyextra->space_tokens = 0;
92 return HASH_ELIF;
93}
94
95{HASH}else/.*\n {
96 yyextra->space_tokens = 0;
97 return HASH_ELSE;
98}
99
100{HASH}endif/.*\n {
101 yyextra->space_tokens = 0;
102 return HASH_ENDIF;
103}
104
105 /* When skipping (due to an #if 0 or similar) consume anything
106 * up to a newline. We do this less priroty than any
107 * #if-related directive (#if, #elif, #else, #endif), but with
108 * more priority than any other directive or token to avoid
109 * any side-effects from skipped content.
110 *
111 * We use the lexing_if flag to avoid skipping any part of an
112 * if conditional expression. */
113[^\n]+/\n {
114 if (yyextra->lexing_if ||
115 yyextra->skip_stack == NULL ||
116 yyextra->skip_stack->type == SKIP_NO_SKIP)
117 {
118 REJECT;
119 }
120}
121
Carl Worth3ff81672010-05-25 13:09:03 -0700122{HASH}define{HSPACE}+/{IDENTIFIER}"(" {
Carl Worthf34a0002010-05-25 16:59:02 -0700123 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700124 return HASH_DEFINE_FUNC;
Carl Worthb20d33c2010-05-20 22:27:07 -0700125}
126
Carl Worth3ff81672010-05-25 13:09:03 -0700127{HASH}define {
Carl Worthf34a0002010-05-25 16:59:02 -0700128 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700129 return HASH_DEFINE_OBJ;
Carl Worthb20d33c2010-05-20 22:27:07 -0700130}
131
Carl Worth3ff81672010-05-25 13:09:03 -0700132{HASH}undef {
Carl Worthf34a0002010-05-25 16:59:02 -0700133 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700134 return HASH_UNDEF;
Carl Worth03f6d5d2010-05-24 11:29:02 -0700135}
136
Carl Worth3ff81672010-05-25 13:09:03 -0700137{HASH} {
Carl Worthf34a0002010-05-25 16:59:02 -0700138 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700139 return HASH;
Carl Worth81f01432010-05-14 17:08:45 -0700140}
141
Carl Worth8fed1cd2010-05-26 09:32:12 -0700142{DECIMAL_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
147{OCTAL_INTEGER} {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700148 yylval->str = xtalloc_strdup (yyextra, yytext);
Carl Worth050e3de2010-05-27 14:36:29 -0700149 return INTEGER_STRING;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700150}
151
152{HEXADECIMAL_INTEGER} {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700153 yylval->str = xtalloc_strdup (yyextra, yytext);
Carl Worth050e3de2010-05-27 14:36:29 -0700154 return INTEGER_STRING;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700155}
156
Carl Worthf34a0002010-05-25 16:59:02 -0700157"<<" {
158 return LEFT_SHIFT;
Carl Worthb1854fd2010-05-25 16:28:26 -0700159}
160
Carl Worthf34a0002010-05-25 16:59:02 -0700161">>" {
162 return RIGHT_SHIFT;
Carl Worthb1854fd2010-05-25 16:28:26 -0700163}
164
Carl Worthf34a0002010-05-25 16:59:02 -0700165"<=" {
166 return LESS_OR_EQUAL;
167}
168
169">=" {
170 return GREATER_OR_EQUAL;
171}
172
173"==" {
174 return EQUAL;
175}
176
177"!=" {
178 return NOT_EQUAL;
179}
180
181"&&" {
182 return AND;
183}
184
185"||" {
186 return OR;
187}
188
189"##" {
190 return PASTE;
191}
192
Carl Worth8fed1cd2010-05-26 09:32:12 -0700193"defined" {
194 return DEFINED;
195}
196
Carl Worth16c1e982010-05-26 09:35:34 -0700197{IDENTIFIER} {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700198 yylval->str = xtalloc_strdup (yyextra, yytext);
Carl Worth16c1e982010-05-26 09:35:34 -0700199 return IDENTIFIER;
200}
201
Carl Worthf34a0002010-05-25 16:59:02 -0700202{PUNCTUATION} {
203 return yytext[0];
Carl Worthb1854fd2010-05-25 16:28:26 -0700204}
205
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700206{OTHER}+ {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700207 yylval->str = xtalloc_strdup (yyextra, yytext);
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700208 return OTHER;
Carl Worth3ff81672010-05-25 13:09:03 -0700209}
210
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700211{HSPACE}+ {
Carl Worthf34a0002010-05-25 16:59:02 -0700212 if (yyextra->space_tokens) {
Carl Worthf34a0002010-05-25 16:59:02 -0700213 return SPACE;
214 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700215}
Carl Worthfcbbb462010-05-13 09:36:23 -0700216
Carl Worth3ff81672010-05-25 13:09:03 -0700217\n {
Carl Wortha771a402010-06-01 11:20:18 -0700218 yyextra->lexing_if = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700219 return NEWLINE;
Carl Worth33cc4002010-05-12 12:17:10 -0700220}
Carl Worth3a37b872010-05-10 11:44:09 -0700221
Kenneth Graunkef82d6732010-06-16 12:53:19 -0700222 /* Handle missing newline at EOF. */
223<INITIAL><<EOF>> {
224 BEGIN DONE; /* Don't keep matching this rule forever. */
225 yyextra->lexing_if = 0;
226 return NEWLINE;
227}
228
Carl Worth3a37b872010-05-10 11:44:09 -0700229%%
Kenneth Graunke1b1f43e2010-06-16 12:01:17 -0700230
231void
232glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
233{
234 yy_scan_string(shader, parser->scanner);
235}