blob: c28f2f6269eea9ceba3c4b409cdfe83643166b46 [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
Carl Worth38aa8352010-05-10 11:52:29 -070032%option 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
Carl Worth0b27b5f2010-05-10 16:16:06 -070036SPACE [[:space:]]
37NONSPACE [^[:space:]]
Carl Worth33cc4002010-05-12 12:17:10 -070038NEWLINE [\n]
Carl Worth0b27b5f2010-05-10 16:16:06 -070039HSPACE [ \t]
Carl Worthe36a4d52010-05-14 17:29:24 -070040HASH ^{HSPACE}*#{HSPACE}*
Carl Worth0b27b5f2010-05-10 16:16:06 -070041IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
Carl Worth63101692010-05-29 05:07:24 -070042PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
Carl Worth3ff81672010-05-25 13:09:03 -070043OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
Carl Worth33cc4002010-05-12 12:17:10 -070044
Carl Worth03f6d5d2010-05-24 11:29:02 -070045DECIMAL_INTEGER [1-9][0-9]*[uU]?
46OCTAL_INTEGER 0[0-7]*[uU]?
47HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
48
Carl Worth111e25b2010-06-02 12:54:15 -070049NON_STARS_THEN_STARS [^*]*[*]+
50
Carl Worth3a37b872010-05-10 11:44:09 -070051%%
52
Carl Worth25714152010-06-01 12:18:43 -070053 /* Single-line comments */
54"//"[^\n]+\n {
55 return NEWLINE;
56}
57
58 /* Multi-line comments */
Carl Worth111e25b2010-06-02 12:54:15 -070059"/*"({NON_STARS_THEN_STARS}[^*/])*{NON_STARS_THEN_STARS}"/" {
Carl Worth25714152010-06-01 12:18:43 -070060 if (yyextra->space_tokens)
61 return SPACE;
62}
63
Kenneth Graunke3b73ea32010-06-16 12:21:17 -070064 /* glcpp doesn't handle #extension, #version, or #pragma directives.
65 * Simply pass them through to the main compiler's lexer/parser. */
66{HASH}(extension|version|pragma).*\n {
67 yylval.str = xtalloc_strdup (yyextra, yytext);
68 return OTHER;
69}
70
Carl Wortha771a402010-06-01 11:20:18 -070071{HASH}if/.*\n {
72 yyextra->lexing_if = 1;
73 yyextra->space_tokens = 0;
74 return HASH_IF;
75}
76
77{HASH}elif/.*\n {
78 yyextra->lexing_if = 1;
79 yyextra->space_tokens = 0;
80 return HASH_ELIF;
81}
82
83{HASH}else/.*\n {
84 yyextra->space_tokens = 0;
85 return HASH_ELSE;
86}
87
88{HASH}endif/.*\n {
89 yyextra->space_tokens = 0;
90 return HASH_ENDIF;
91}
92
93 /* When skipping (due to an #if 0 or similar) consume anything
94 * up to a newline. We do this less priroty than any
95 * #if-related directive (#if, #elif, #else, #endif), but with
96 * more priority than any other directive or token to avoid
97 * any side-effects from skipped content.
98 *
99 * We use the lexing_if flag to avoid skipping any part of an
100 * if conditional expression. */
101[^\n]+/\n {
102 if (yyextra->lexing_if ||
103 yyextra->skip_stack == NULL ||
104 yyextra->skip_stack->type == SKIP_NO_SKIP)
105 {
106 REJECT;
107 }
108}
109
Carl Worth3ff81672010-05-25 13:09:03 -0700110{HASH}define{HSPACE}+/{IDENTIFIER}"(" {
Carl Worthf34a0002010-05-25 16:59:02 -0700111 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700112 return HASH_DEFINE_FUNC;
Carl Worthb20d33c2010-05-20 22:27:07 -0700113}
114
Carl Worth3ff81672010-05-25 13:09:03 -0700115{HASH}define {
Carl Worthf34a0002010-05-25 16:59:02 -0700116 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700117 return HASH_DEFINE_OBJ;
Carl Worthb20d33c2010-05-20 22:27:07 -0700118}
119
Carl Worth3ff81672010-05-25 13:09:03 -0700120{HASH}undef {
Carl Worthf34a0002010-05-25 16:59:02 -0700121 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700122 return HASH_UNDEF;
Carl Worth03f6d5d2010-05-24 11:29:02 -0700123}
124
Carl Worth3ff81672010-05-25 13:09:03 -0700125{HASH} {
Carl Worthf34a0002010-05-25 16:59:02 -0700126 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700127 return HASH;
Carl Worth81f01432010-05-14 17:08:45 -0700128}
129
Carl Worth8fed1cd2010-05-26 09:32:12 -0700130{DECIMAL_INTEGER} {
Carl Worth050e3de2010-05-27 14:36:29 -0700131 yylval.str = xtalloc_strdup (yyextra, yytext);
132 return INTEGER_STRING;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700133}
134
135{OCTAL_INTEGER} {
Carl Worth050e3de2010-05-27 14:36:29 -0700136 yylval.str = xtalloc_strdup (yyextra, yytext);
137 return INTEGER_STRING;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700138}
139
140{HEXADECIMAL_INTEGER} {
Carl Worth050e3de2010-05-27 14:36:29 -0700141 yylval.str = xtalloc_strdup (yyextra, yytext);
142 return INTEGER_STRING;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700143}
144
Carl Worthf34a0002010-05-25 16:59:02 -0700145"<<" {
146 return LEFT_SHIFT;
Carl Worthb1854fd2010-05-25 16:28:26 -0700147}
148
Carl Worthf34a0002010-05-25 16:59:02 -0700149">>" {
150 return RIGHT_SHIFT;
Carl Worthb1854fd2010-05-25 16:28:26 -0700151}
152
Carl Worthf34a0002010-05-25 16:59:02 -0700153"<=" {
154 return LESS_OR_EQUAL;
155}
156
157">=" {
158 return GREATER_OR_EQUAL;
159}
160
161"==" {
162 return EQUAL;
163}
164
165"!=" {
166 return NOT_EQUAL;
167}
168
169"&&" {
170 return AND;
171}
172
173"||" {
174 return OR;
175}
176
177"##" {
178 return PASTE;
179}
180
Carl Worth8fed1cd2010-05-26 09:32:12 -0700181"defined" {
182 return DEFINED;
183}
184
Carl Worth16c1e982010-05-26 09:35:34 -0700185{IDENTIFIER} {
186 yylval.str = xtalloc_strdup (yyextra, yytext);
187 return IDENTIFIER;
188}
189
Carl Worthf34a0002010-05-25 16:59:02 -0700190{PUNCTUATION} {
191 return yytext[0];
Carl Worthb1854fd2010-05-25 16:28:26 -0700192}
193
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700194{OTHER}+ {
195 yylval.str = xtalloc_strdup (yyextra, yytext);
196 return OTHER;
Carl Worth3ff81672010-05-25 13:09:03 -0700197}
198
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700199{HSPACE}+ {
Carl Worthf34a0002010-05-25 16:59:02 -0700200 if (yyextra->space_tokens) {
Carl Worthf34a0002010-05-25 16:59:02 -0700201 return SPACE;
202 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700203}
Carl Worthfcbbb462010-05-13 09:36:23 -0700204
Carl Worth3ff81672010-05-25 13:09:03 -0700205\n {
Carl Wortha771a402010-06-01 11:20:18 -0700206 yyextra->lexing_if = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700207 return NEWLINE;
Carl Worth33cc4002010-05-12 12:17:10 -0700208}
Carl Worth3a37b872010-05-10 11:44:09 -0700209
Carl Worth3a37b872010-05-10 11:44:09 -0700210%%
Kenneth Graunke1b1f43e2010-06-16 12:01:17 -0700211
212void
213glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
214{
215 yy_scan_string(shader, parser->scanner);
216}