blob: 52be1b1ea437441bd35353ce6f56ebb3f8e6ba93 [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 *"
Carl Worth3a37b872010-05-10 11:44:09 -070034
Carl Worth81f01432010-05-14 17:08:45 -070035%x ST_DEFINE
Carl Wortha807fb72010-05-18 22:10:04 -070036%x ST_DEFINE_OBJ_OR_FUNC
37%x ST_DEFINE_PARAMETER
38%x ST_DEFINE_VALUE
Carl Worth1a295002010-05-17 13:19:04 -070039%x ST_UNDEF
40%x ST_UNDEF_END
Carl Worth81f01432010-05-14 17:08:45 -070041
Carl Worth0b27b5f2010-05-10 16:16:06 -070042SPACE [[:space:]]
43NONSPACE [^[:space:]]
Carl Worth33cc4002010-05-12 12:17:10 -070044NEWLINE [\n]
Carl Worth0b27b5f2010-05-10 16:16:06 -070045HSPACE [ \t]
Carl Worthe36a4d52010-05-14 17:29:24 -070046HASH ^{HSPACE}*#{HSPACE}*
Carl Worth0b27b5f2010-05-10 16:16:06 -070047IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
Carl Worthfcbbb462010-05-13 09:36:23 -070048TOKEN [^[:space:](),]+
Carl Worth33cc4002010-05-12 12:17:10 -070049
Carl Worth3a37b872010-05-10 11:44:09 -070050%%
51
Carl Worth0a93cbb2010-05-13 10:29:07 -070052{HASH}undef{HSPACE}* {
Carl Worth1a295002010-05-17 13:19:04 -070053 BEGIN ST_UNDEF;
Carl Worthcd27e642010-05-12 13:11:50 -070054 return UNDEF;
55}
56
Carl Worth1a295002010-05-17 13:19:04 -070057<ST_UNDEF>{IDENTIFIER} {
58 BEGIN ST_UNDEF_END;
Carl Wortha807fb72010-05-18 22:10:04 -070059 yylval.str = xtalloc_strdup (yyextra, yytext);
60 return IDENTIFIER;
Carl Worth1a295002010-05-17 13:19:04 -070061}
62
Carl Wortha807fb72010-05-18 22:10:04 -070063<ST_UNDEF_END>{HSPACE}*
64
Carl Worth1a295002010-05-17 13:19:04 -070065<ST_UNDEF_END>\n {
66 BEGIN INITIAL;
Carl Worth1a295002010-05-17 13:19:04 -070067}
68
Carl Worth0a93cbb2010-05-13 10:29:07 -070069 /* We use the ST_DEFINE and ST_DEFVAL states so that we can
70 * pass a space token, (yes, a token for whitespace!), since
71 * the preprocessor specification requires distinguishing
72 * "#define foo()" from "#define foo ()".
73 */
74{HASH}define{HSPACE}* {
Carl Worth81f01432010-05-14 17:08:45 -070075 BEGIN ST_DEFINE;
Carl Worth0a93cbb2010-05-13 10:29:07 -070076 return DEFINE;
77}
Carl Worth012295f2010-05-12 13:19:23 -070078
Carl Worth81f01432010-05-14 17:08:45 -070079<ST_DEFINE>{IDENTIFIER} {
Carl Wortha807fb72010-05-18 22:10:04 -070080 BEGIN ST_DEFINE_OBJ_OR_FUNC;
Carl Worth81f01432010-05-14 17:08:45 -070081 yylval.str = xtalloc_strdup (yyextra, yytext);
82 return IDENTIFIER;
83}
84
Carl Wortha807fb72010-05-18 22:10:04 -070085<ST_DEFINE_OBJ_OR_FUNC>\n {
Carl Worth1a295002010-05-17 13:19:04 -070086 BEGIN INITIAL;
Carl Wortha807fb72010-05-18 22:10:04 -070087 yylval.str = xtalloc_strdup (yyextra, "");
88 return REPLACEMENT;
Carl Worth1a295002010-05-17 13:19:04 -070089}
90
Carl Wortha807fb72010-05-18 22:10:04 -070091<ST_DEFINE_OBJ_OR_FUNC>{HSPACE}+ {
92 BEGIN ST_DEFINE_VALUE;
Carl Worth1a295002010-05-17 13:19:04 -070093}
94
Carl Wortha807fb72010-05-18 22:10:04 -070095<ST_DEFINE_OBJ_OR_FUNC>"(" {
96 BEGIN ST_DEFINE_PARAMETER;
Carl Worth1a295002010-05-17 13:19:04 -070097 return '(';
98}
99
Carl Wortha807fb72010-05-18 22:10:04 -0700100<ST_DEFINE_PARAMETER>{IDENTIFIER} {
Carl Worth1a295002010-05-17 13:19:04 -0700101 yylval.str = xtalloc_strdup (yyextra, yytext);
Carl Wortha807fb72010-05-18 22:10:04 -0700102 return IDENTIFIER;
Carl Worth1a295002010-05-17 13:19:04 -0700103}
104
Carl Wortha807fb72010-05-18 22:10:04 -0700105<ST_DEFINE_PARAMETER>"," {
106 return ',';
107}
108
109<ST_DEFINE_PARAMETER>")" {
110 BEGIN ST_DEFINE_VALUE;
111 return ')';
112}
113
114<ST_DEFINE_PARAMETER>{HSPACE}+
115
116<ST_DEFINE_VALUE>.*\n {
Carl Worth81f01432010-05-14 17:08:45 -0700117 BEGIN INITIAL;
Carl Wortha807fb72010-05-18 22:10:04 -0700118 yylval.str = xtalloc_strndup (yyextra, yytext, strlen (yytext) - 1);
119 return REPLACEMENT;
Carl Worth81f01432010-05-14 17:08:45 -0700120}
121
Carl Worth0a93cbb2010-05-13 10:29:07 -0700122{IDENTIFIER} {
Carl Wortha807fb72010-05-18 22:10:04 -0700123 int parameter_index;
124 yylval.str = xtalloc_strdup (yyextra, yytext);
125 switch (glcpp_parser_classify_token (yyextra, yylval.str,
126 &parameter_index))
127 {
128 case TOKEN_CLASS_ARGUMENT:
129 talloc_free (yylval.str);
130 /* We don't return a value here since the
131 * current token will be replaced by new
132 * tokens. */
133 glcpp_parser_push_expansion_argument (yyextra,
134 parameter_index);
135 break;
136 case TOKEN_CLASS_IDENTIFIER:
137 return IDENTIFIER;
138 break;
139 case TOKEN_CLASS_FUNC_MACRO:
140 return FUNC_MACRO;
141 break;
142 case TOKEN_CLASS_OBJ_MACRO:
143 return OBJ_MACRO;
144 break;
145
146 }
Carl Worthcd27e642010-05-12 13:11:50 -0700147}
148
Carl Worth0a93cbb2010-05-13 10:29:07 -0700149[(),] {
150 return yytext[0];
151}
Carl Worthfcbbb462010-05-13 09:36:23 -0700152
Carl Worth012295f2010-05-12 13:19:23 -0700153{TOKEN} {
Carl Worth5070a202010-05-12 12:45:33 -0700154 yylval.str = xtalloc_strdup (yyextra, yytext);
Carl Worth33cc4002010-05-12 12:17:10 -0700155 return TOKEN;
156}
Carl Worth3a37b872010-05-10 11:44:09 -0700157
Carl Worth012295f2010-05-12 13:19:23 -0700158\n {
Carl Worth1a295002010-05-17 13:19:04 -0700159 printf ("\n");
Carl Worth012295f2010-05-12 13:19:23 -0700160}
161
Carl Worthe36a4d52010-05-14 17:29:24 -0700162{HSPACE}+
Carl Worth012295f2010-05-12 13:19:23 -0700163
Carl Wortha807fb72010-05-18 22:10:04 -0700164<<EOF>> {
165 int done;
166
167 done = glcpp_lex_stack_pop (yyextra->lex_stack);
168
169 if (done)
170 yyterminate ();
171
172 glcpp_parser_pop_expansion (yyextra);
173}
174
Carl Worth3a37b872010-05-10 11:44:09 -0700175%%
Carl Wortha807fb72010-05-18 22:10:04 -0700176
177void
178glcpp_lex_stack_push (glcpp_lex_stack_t *stack, const char *string)
179{
180 struct yyguts_t *yyg = (struct yyguts_t*) stack->parser->scanner;
181 glcpp_lex_node_t *node;
182
183 /* Save the current buffer on the top of the stack. */
184 node = xtalloc (stack, glcpp_lex_node_t);
185 node->buffer = YY_CURRENT_BUFFER;
186
187 node->next = stack->head;
188 stack->head = node;
189
190 /* Then switch to a new scan buffer for string. */
191 yy_scan_string (string, stack->parser->scanner);
192}
193
194int
195glcpp_lex_stack_pop (glcpp_lex_stack_t *stack)
196{
197 struct yyguts_t *yyg = (struct yyguts_t*) stack->parser->scanner;
198 glcpp_lex_node_t *node;
199
200 node = stack->head;
201
202 if (node == NULL)
203 return 1;
204
205 stack->head = node->next;
206
207 yy_delete_buffer (YY_CURRENT_BUFFER, stack->parser->scanner);
208 yy_switch_to_buffer ((YY_BUFFER_STATE) node->buffer,
209 stack->parser->scanner);
210
211 talloc_free (node);
212
213 return 0;
214}