OSDN Git Service

Implement comment handling in the lexer (with test).
[android-x86/external-mesa.git] / glcpp-lex.l
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
28 #include "glcpp.h"
29 #include "glcpp-parse.h"
30 %}
31
32 %option reentrant noyywrap
33 %option extra-type="glcpp_parser_t *"
34
35 SPACE           [[:space:]]
36 NONSPACE        [^[:space:]]
37 NEWLINE         [\n]
38 HSPACE          [ \t]
39 HASH            ^{HSPACE}*#{HSPACE}*
40 IDENTIFIER      [_a-zA-Z][_a-zA-Z0-9]*
41 PUNCTUATION     [][(){}.&*~!/%<>^|;,=+-]
42 OTHER           [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
43
44 DECIMAL_INTEGER         [1-9][0-9]*[uU]?
45 OCTAL_INTEGER           0[0-7]*[uU]?
46 HEXADECIMAL_INTEGER     0[xX][0-9a-fA-F]+[uU]?
47
48 %%
49
50         /* Single-line comments */
51 "//"[^\n]+\n {
52         return NEWLINE;
53 }
54
55         /* Multi-line comments */
56 [/][*]([^*]*[*]+[^/])*[^*]*[*]*[/] {
57         if (yyextra->space_tokens)
58                 return SPACE;
59 }
60
61 {HASH}if/.*\n {
62         yyextra->lexing_if = 1;
63         yyextra->space_tokens = 0;
64         return HASH_IF;
65 }
66
67 {HASH}elif/.*\n {
68         yyextra->lexing_if = 1;
69         yyextra->space_tokens = 0;
70         return HASH_ELIF;
71 }
72
73 {HASH}else/.*\n {
74         yyextra->space_tokens = 0;
75         return HASH_ELSE;
76 }
77
78 {HASH}endif/.*\n {
79         yyextra->space_tokens = 0;
80         return HASH_ENDIF;
81 }
82
83         /* When skipping (due to an #if 0 or similar) consume anything
84          * up to a newline. We do this less priroty than any
85          * #if-related directive (#if, #elif, #else, #endif), but with
86          * more priority than any other directive or token to avoid
87          * any side-effects from skipped content.
88          *
89          * We use the lexing_if flag to avoid skipping any part of an
90          * if conditional expression. */
91 [^\n]+/\n {
92         if (yyextra->lexing_if ||
93             yyextra->skip_stack == NULL ||
94             yyextra->skip_stack->type == SKIP_NO_SKIP)
95         {
96                 REJECT;
97         }
98 }
99
100 {HASH}define{HSPACE}+/{IDENTIFIER}"(" {
101         yyextra->space_tokens = 0;
102         return HASH_DEFINE_FUNC;
103 }
104
105 {HASH}define {
106         yyextra->space_tokens = 0;
107         return HASH_DEFINE_OBJ;
108 }
109
110 {HASH}undef {
111         yyextra->space_tokens = 0;
112         return HASH_UNDEF;
113 }
114
115 {HASH} {
116         yyextra->space_tokens = 0;
117         return HASH;
118 }
119
120 {DECIMAL_INTEGER} {
121         yylval.str = xtalloc_strdup (yyextra, yytext);
122         return INTEGER_STRING;
123 }
124
125 {OCTAL_INTEGER} {
126         yylval.str = xtalloc_strdup (yyextra, yytext);
127         return INTEGER_STRING;
128 }
129
130 {HEXADECIMAL_INTEGER} {
131         yylval.str = xtalloc_strdup (yyextra, yytext);
132         return INTEGER_STRING;
133 }
134
135 "<<"  {
136         return LEFT_SHIFT;
137 }
138
139 ">>" {
140         return RIGHT_SHIFT;
141 }
142
143 "<=" {
144         return LESS_OR_EQUAL;
145 }
146
147 ">=" {
148         return GREATER_OR_EQUAL;
149 }
150
151 "==" {
152         return EQUAL;
153 }
154
155 "!=" {
156         return NOT_EQUAL;
157 }
158
159 "&&" {
160         return AND;
161 }
162
163 "||" {
164         return OR;
165 }
166
167 "##" {
168         return PASTE;
169 }
170
171 "defined" {
172         return DEFINED;
173 }
174
175 {IDENTIFIER} {
176         yylval.str = xtalloc_strdup (yyextra, yytext);
177         return IDENTIFIER;
178 }
179
180 {PUNCTUATION} {
181         return yytext[0];
182 }
183
184 {OTHER}+ {
185         yylval.str = xtalloc_strdup (yyextra, yytext);
186         return OTHER;
187 }
188
189 {HSPACE}+ {
190         if (yyextra->space_tokens) {
191                 return SPACE;
192         }
193 }
194
195 \n {
196         yyextra->lexing_if = 0;
197         return NEWLINE;
198 }
199
200 %%