blob: 7b5cdd57a0fb0646f41fb1dd4b7ab062e52f7b86 [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 Worth9fb8b7a2010-05-25 15:04:32 -070035 /* This lexer has two states:
36 *
37 * The CONTROL state is for control lines (directives)
38 * It lexes exactly as specified in the C99 specification.
39 *
40 * The INITIAL state is for input lines. In this state, we
41 * make the OTHER token much more broad in that it now
42 * includes tokens consisting entirely of whitespace. This
43 * allows us to pass text through verbatim. It avoids the
44 * "inadvertent token pasting" problem that would occur if we
45 * just printed tokens, while also avoiding excess whitespace
46 * insertion in the output.*/
47
48%x CONTROL
49
Carl Worth0b27b5f2010-05-10 16:16:06 -070050SPACE [[:space:]]
51NONSPACE [^[:space:]]
Carl Worth33cc4002010-05-12 12:17:10 -070052NEWLINE [\n]
Carl Worth0b27b5f2010-05-10 16:16:06 -070053HSPACE [ \t]
Carl Worthe36a4d52010-05-14 17:29:24 -070054HASH ^{HSPACE}*#{HSPACE}*
Carl Worth0b27b5f2010-05-10 16:16:06 -070055IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
Carl Worth3ff81672010-05-25 13:09:03 -070056PUNCTUATION [][(){}.&*~!/%<>^|;,+-]
57OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
Carl Worth33cc4002010-05-12 12:17:10 -070058
Carl Worth03f6d5d2010-05-24 11:29:02 -070059DECIMAL_INTEGER [1-9][0-9]*[uU]?
60OCTAL_INTEGER 0[0-7]*[uU]?
61HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
62
Carl Worth3a37b872010-05-10 11:44:09 -070063%%
64
Carl Worth3ff81672010-05-25 13:09:03 -070065{HASH}define{HSPACE}+/{IDENTIFIER}"(" {
Carl Worth9fb8b7a2010-05-25 15:04:32 -070066 BEGIN CONTROL;
Carl Worth3ff81672010-05-25 13:09:03 -070067 return HASH_DEFINE_FUNC;
Carl Worthb20d33c2010-05-20 22:27:07 -070068}
69
Carl Worth3ff81672010-05-25 13:09:03 -070070{HASH}define {
Carl Worth9fb8b7a2010-05-25 15:04:32 -070071 BEGIN CONTROL;
Carl Worth3ff81672010-05-25 13:09:03 -070072 return HASH_DEFINE_OBJ;
Carl Worthb20d33c2010-05-20 22:27:07 -070073}
74
Carl Worth3ff81672010-05-25 13:09:03 -070075{HASH}undef {
Carl Worth9fb8b7a2010-05-25 15:04:32 -070076 BEGIN CONTROL;
Carl Worth3ff81672010-05-25 13:09:03 -070077 return HASH_UNDEF;
Carl Worth03f6d5d2010-05-24 11:29:02 -070078}
79
Carl Worth3ff81672010-05-25 13:09:03 -070080{HASH} {
Carl Worth9fb8b7a2010-05-25 15:04:32 -070081 BEGIN CONTROL;
Carl Worth3ff81672010-05-25 13:09:03 -070082 return HASH;
Carl Worth81f01432010-05-14 17:08:45 -070083}
84
Carl Worth9fb8b7a2010-05-25 15:04:32 -070085<CONTROL>{IDENTIFIER} {
86 yylval.str = xtalloc_strdup (yyextra, yytext);
87 return IDENTIFIER;
88}
89
90<CONTROL>"<<" {
91 return LEFT_SHIFT;
92}
93
94<CONTROL>">>" {
95 return RIGHT_SHIFT;
96}
97
98<CONTROL>"<=" {
99 return LESS_OR_EQUAL;
100}
101
102<CONTROL>">=" {
103 return GREATER_OR_EQUAL;
104}
105
106<CONTROL>"==" {
107 return EQUAL;
108}
109
110<CONTROL>"!=" {
111 return NOT_EQUAL;
112}
113
114<CONTROL>"&&" {
115 return AND;
116}
117
118<CONTROL>"||" {
119 return OR;
120}
121
122<CONTROL>"##" {
123 return PASTE;
124}
125
126<CONTROL>{PUNCTUATION} {
127 return yytext[0];
128}
129
130<CONTROL>{OTHER} {
131 yylval.str = xtalloc_strdup (yyextra, yytext);
132 return OTHER;
133}
134
135<CONTROL>{HSPACE}+
136
137<CONTROL>\n {
138 BEGIN INITIAL;
139 return NEWLINE;
140}
141
Carl Worth0a93cbb2010-05-13 10:29:07 -0700142{IDENTIFIER} {
Carl Wortha807fb72010-05-18 22:10:04 -0700143 yylval.str = xtalloc_strdup (yyextra, yytext);
Carl Worth3ff81672010-05-25 13:09:03 -0700144 return IDENTIFIER;
Carl Worthcd27e642010-05-12 13:11:50 -0700145}
146
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700147{OTHER}+ {
148 yylval.str = xtalloc_strdup (yyextra, yytext);
149 return OTHER;
Carl Worth3ff81672010-05-25 13:09:03 -0700150}
151
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700152{HSPACE}+ {
153 yylval.str = xtalloc_strdup (yyextra, yytext);
154 return OTHER;
Carl Worth0a93cbb2010-05-13 10:29:07 -0700155}
Carl Worthfcbbb462010-05-13 09:36:23 -0700156
Carl Worth3ff81672010-05-25 13:09:03 -0700157\n {
158 return NEWLINE;
Carl Worth33cc4002010-05-12 12:17:10 -0700159}
Carl Worth3a37b872010-05-10 11:44:09 -0700160
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700161. {
Carl Worth3ff81672010-05-25 13:09:03 -0700162 yylval.str = xtalloc_strdup (yyextra, yytext);
163 return OTHER;
Carl Worth012295f2010-05-12 13:19:23 -0700164}
165
Carl Worth3a37b872010-05-10 11:44:09 -0700166%%