blob: 8e3ab661e6fb5a3498055876864e96f20fc9d049 [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 Worthaaa9acb2010-05-19 13:28:24 -070087 return NEWLINE;
Carl Worth1a295002010-05-17 13:19:04 -070088}
89
Carl Wortha807fb72010-05-18 22:10:04 -070090<ST_DEFINE_OBJ_OR_FUNC>{HSPACE}+ {
91 BEGIN ST_DEFINE_VALUE;
Carl Worthaaa9acb2010-05-19 13:28:24 -070092 return SPACE;
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
Carl Worthaaa9acb2010-05-19 13:28:24 -0700116<ST_DEFINE_VALUE>{TOKEN} {
Carl Worthb5693832010-05-20 08:01:44 -0700117 yylval.token.type = TOKEN;
118 yylval.token.value = xtalloc_strdup (yyextra, yytext);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700119 return TOKEN;
120}
121
122<ST_DEFINE_VALUE>[(),] {
Carl Worthb5693832010-05-20 08:01:44 -0700123 yylval.token.type = TOKEN;
124 yylval.token.value = xtalloc_strdup (yyextra, yytext);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700125 return TOKEN;
126}
127
128<ST_DEFINE_VALUE>{HSPACE}+
129
130<ST_DEFINE_VALUE>\n {
Carl Worth81f01432010-05-14 17:08:45 -0700131 BEGIN INITIAL;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700132 return NEWLINE;
Carl Worth81f01432010-05-14 17:08:45 -0700133}
134
Carl Worth0a93cbb2010-05-13 10:29:07 -0700135{IDENTIFIER} {
Carl Wortha807fb72010-05-18 22:10:04 -0700136 int parameter_index;
137 yylval.str = xtalloc_strdup (yyextra, yytext);
138 switch (glcpp_parser_classify_token (yyextra, yylval.str,
139 &parameter_index))
140 {
141 case TOKEN_CLASS_ARGUMENT:
142 talloc_free (yylval.str);
143 /* We don't return a value here since the
144 * current token will be replaced by new
145 * tokens. */
146 glcpp_parser_push_expansion_argument (yyextra,
147 parameter_index);
148 break;
149 case TOKEN_CLASS_IDENTIFIER:
150 return IDENTIFIER;
151 break;
Carl Worthb5693832010-05-20 08:01:44 -0700152 case TOKEN_CLASS_IDENTIFIER_FINALIZED:
153 return IDENTIFIER_FINALIZED;
154 break;
Carl Wortha807fb72010-05-18 22:10:04 -0700155 case TOKEN_CLASS_FUNC_MACRO:
156 return FUNC_MACRO;
157 break;
158 case TOKEN_CLASS_OBJ_MACRO:
159 return OBJ_MACRO;
160 break;
161
162 }
Carl Worthcd27e642010-05-12 13:11:50 -0700163}
164
Carl Worth0a93cbb2010-05-13 10:29:07 -0700165[(),] {
166 return yytext[0];
167}
Carl Worthfcbbb462010-05-13 09:36:23 -0700168
Carl Worth012295f2010-05-12 13:19:23 -0700169{TOKEN} {
Carl Worthb5693832010-05-20 08:01:44 -0700170 yylval.token.type = TOKEN;
171 yylval.token.value = xtalloc_strdup (yyextra, yytext);
Carl Worth33cc4002010-05-12 12:17:10 -0700172 return TOKEN;
173}
Carl Worth3a37b872010-05-10 11:44:09 -0700174
Carl Worth012295f2010-05-12 13:19:23 -0700175\n {
Carl Worth1a295002010-05-17 13:19:04 -0700176 printf ("\n");
Carl Worth012295f2010-05-12 13:19:23 -0700177}
178
Carl Worthe36a4d52010-05-14 17:29:24 -0700179{HSPACE}+
Carl Worth012295f2010-05-12 13:19:23 -0700180
Carl Worth3a37b872010-05-10 11:44:09 -0700181%%