blob: 4cb73c5d715f61744d3fc2c75b69ed64300e5f7d [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"
Carl Worth1a295002010-05-17 13:19:04 -070030
31/* Yes, a macro with a return statement in it is evil. But surely no
32 * more evil than all the code generation happening with flex in the
33 * first place. */
34#define LEXIFY_IDENTIFIER do { \
35 yylval.str = xtalloc_strdup (yyextra, yytext); \
36 switch (glcpp_parser_macro_type (yyextra, yylval.str)) \
37 { \
38 case MACRO_TYPE_UNDEFINED: \
39 return IDENTIFIER; \
40 break; \
41 case MACRO_TYPE_OBJECT: \
42 return OBJ_MACRO; \
43 break; \
44 case MACRO_TYPE_FUNCTION: \
45 return FUNC_MACRO; \
46 break; \
47 } \
48 } while (0)
49
Carl Worth3a37b872010-05-10 11:44:09 -070050%}
51
Carl Worth38aa8352010-05-10 11:52:29 -070052%option reentrant noyywrap
Carl Worth5070a202010-05-12 12:45:33 -070053%option extra-type="glcpp_parser_t *"
Carl Worth3a37b872010-05-10 11:44:09 -070054
Carl Worth81f01432010-05-14 17:08:45 -070055%x ST_DEFINE
Carl Worth1a295002010-05-17 13:19:04 -070056%x ST_DEFVAL_START
Carl Worth81f01432010-05-14 17:08:45 -070057%x ST_DEFVAL
Carl Worth1a295002010-05-17 13:19:04 -070058%x ST_UNDEF
59%x ST_UNDEF_END
Carl Worth81f01432010-05-14 17:08:45 -070060
Carl Worth0b27b5f2010-05-10 16:16:06 -070061SPACE [[:space:]]
62NONSPACE [^[:space:]]
Carl Worth33cc4002010-05-12 12:17:10 -070063NEWLINE [\n]
Carl Worth0b27b5f2010-05-10 16:16:06 -070064HSPACE [ \t]
Carl Worthe36a4d52010-05-14 17:29:24 -070065HASH ^{HSPACE}*#{HSPACE}*
Carl Worth0b27b5f2010-05-10 16:16:06 -070066IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
Carl Worthfcbbb462010-05-13 09:36:23 -070067TOKEN [^[:space:](),]+
Carl Worth33cc4002010-05-12 12:17:10 -070068
Carl Worth3a37b872010-05-10 11:44:09 -070069%%
70
Carl Worth0a93cbb2010-05-13 10:29:07 -070071{HASH}undef{HSPACE}* {
Carl Worth1a295002010-05-17 13:19:04 -070072 BEGIN ST_UNDEF;
Carl Worthcd27e642010-05-12 13:11:50 -070073 return UNDEF;
74}
75
Carl Worth1a295002010-05-17 13:19:04 -070076<ST_UNDEF>{IDENTIFIER} {
77 BEGIN ST_UNDEF_END;
78 LEXIFY_IDENTIFIER;
79}
80
81<ST_UNDEF_END>\n {
82 BEGIN INITIAL;
83 return NEWLINE;
84}
85
Carl Worth0a93cbb2010-05-13 10:29:07 -070086 /* We use the ST_DEFINE and ST_DEFVAL states so that we can
87 * pass a space token, (yes, a token for whitespace!), since
88 * the preprocessor specification requires distinguishing
89 * "#define foo()" from "#define foo ()".
90 */
91{HASH}define{HSPACE}* {
Carl Worth81f01432010-05-14 17:08:45 -070092 BEGIN ST_DEFINE;
Carl Worth0a93cbb2010-05-13 10:29:07 -070093 return DEFINE;
94}
Carl Worth012295f2010-05-12 13:19:23 -070095
Carl Worth81f01432010-05-14 17:08:45 -070096<ST_DEFINE>{IDENTIFIER} {
Carl Worth1a295002010-05-17 13:19:04 -070097 BEGIN ST_DEFVAL_START;
Carl Worth81f01432010-05-14 17:08:45 -070098 yylval.str = xtalloc_strdup (yyextra, yytext);
99 return IDENTIFIER;
100}
101
Carl Worth1a295002010-05-17 13:19:04 -0700102<ST_DEFVAL_START>\n {
103 BEGIN INITIAL;
104 return NEWLINE;
105}
106
107<ST_DEFVAL_START>{HSPACE}+ {
108 BEGIN ST_DEFVAL;
109 return SPACE;
110}
111
112<ST_DEFVAL_START>"(" {
113 BEGIN ST_DEFVAL;
114 return '(';
115}
116
117<ST_DEFVAL>{IDENTIFIER} {
118 LEXIFY_IDENTIFIER;
119}
120
121<ST_DEFVAL>[(),] {
122 return yytext[0];
123}
124
125<ST_DEFVAL>{TOKEN} {
126 yylval.str = xtalloc_strdup (yyextra, yytext);
127 return TOKEN;
128}
129
Carl Worth81f01432010-05-14 17:08:45 -0700130<ST_DEFVAL>\n {
131 BEGIN INITIAL;
132 return NEWLINE;
133}
134
Carl Worth1a295002010-05-17 13:19:04 -0700135<ST_DEFVAL>{HSPACE}+
Carl Worth81f01432010-05-14 17:08:45 -0700136
Carl Worth0a93cbb2010-05-13 10:29:07 -0700137{IDENTIFIER} {
Carl Worth1a295002010-05-17 13:19:04 -0700138 LEXIFY_IDENTIFIER;
Carl Worthcd27e642010-05-12 13:11:50 -0700139}
140
Carl Worth0a93cbb2010-05-13 10:29:07 -0700141[(),] {
142 return yytext[0];
143}
Carl Worthfcbbb462010-05-13 09:36:23 -0700144
Carl Worth012295f2010-05-12 13:19:23 -0700145{TOKEN} {
Carl Worth5070a202010-05-12 12:45:33 -0700146 yylval.str = xtalloc_strdup (yyextra, yytext);
Carl Worth33cc4002010-05-12 12:17:10 -0700147 return TOKEN;
148}
Carl Worth3a37b872010-05-10 11:44:09 -0700149
Carl Worth012295f2010-05-12 13:19:23 -0700150\n {
Carl Worth1a295002010-05-17 13:19:04 -0700151 printf ("\n");
Carl Worth012295f2010-05-12 13:19:23 -0700152}
153
Carl Worthe36a4d52010-05-14 17:29:24 -0700154{HSPACE}+
Carl Worth012295f2010-05-12 13:19:23 -0700155
Carl Worth3a37b872010-05-10 11:44:09 -0700156%%