blob: a51d9e185fc6f7d834ae6f5d4bb5d610829efb98 [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 Worth0b27b5f2010-05-10 16:16:06 -070035SPACE [[:space:]]
36NONSPACE [^[:space:]]
Carl Worth33cc4002010-05-12 12:17:10 -070037NEWLINE [\n]
Carl Worth0b27b5f2010-05-10 16:16:06 -070038HSPACE [ \t]
Carl Worthe36a4d52010-05-14 17:29:24 -070039HASH ^{HSPACE}*#{HSPACE}*
Carl Worth0b27b5f2010-05-10 16:16:06 -070040IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
Carl Worth63101692010-05-29 05:07:24 -070041PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
Carl Worth3ff81672010-05-25 13:09:03 -070042OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
Carl Worth33cc4002010-05-12 12:17:10 -070043
Carl Worth03f6d5d2010-05-24 11:29:02 -070044DECIMAL_INTEGER [1-9][0-9]*[uU]?
45OCTAL_INTEGER 0[0-7]*[uU]?
46HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
47
Carl Worth3a37b872010-05-10 11:44:09 -070048%%
49
Carl Wortha771a402010-06-01 11:20:18 -070050{HASH}if/.*\n {
51 yyextra->lexing_if = 1;
52 yyextra->space_tokens = 0;
53 return HASH_IF;
54}
55
56{HASH}elif/.*\n {
57 yyextra->lexing_if = 1;
58 yyextra->space_tokens = 0;
59 return HASH_ELIF;
60}
61
62{HASH}else/.*\n {
63 yyextra->space_tokens = 0;
64 return HASH_ELSE;
65}
66
67{HASH}endif/.*\n {
68 yyextra->space_tokens = 0;
69 return HASH_ENDIF;
70}
71
72 /* When skipping (due to an #if 0 or similar) consume anything
73 * up to a newline. We do this less priroty than any
74 * #if-related directive (#if, #elif, #else, #endif), but with
75 * more priority than any other directive or token to avoid
76 * any side-effects from skipped content.
77 *
78 * We use the lexing_if flag to avoid skipping any part of an
79 * if conditional expression. */
80[^\n]+/\n {
81 if (yyextra->lexing_if ||
82 yyextra->skip_stack == NULL ||
83 yyextra->skip_stack->type == SKIP_NO_SKIP)
84 {
85 REJECT;
86 }
87}
88
Carl Worth3ff81672010-05-25 13:09:03 -070089{HASH}define{HSPACE}+/{IDENTIFIER}"(" {
Carl Worthf34a0002010-05-25 16:59:02 -070090 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -070091 return HASH_DEFINE_FUNC;
Carl Worthb20d33c2010-05-20 22:27:07 -070092}
93
Carl Worth3ff81672010-05-25 13:09:03 -070094{HASH}define {
Carl Worthf34a0002010-05-25 16:59:02 -070095 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -070096 return HASH_DEFINE_OBJ;
Carl Worthb20d33c2010-05-20 22:27:07 -070097}
98
Carl Worth3ff81672010-05-25 13:09:03 -070099{HASH}undef {
Carl Worthf34a0002010-05-25 16:59:02 -0700100 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700101 return HASH_UNDEF;
Carl Worth03f6d5d2010-05-24 11:29:02 -0700102}
103
Carl Worth3ff81672010-05-25 13:09:03 -0700104{HASH} {
Carl Worthf34a0002010-05-25 16:59:02 -0700105 yyextra->space_tokens = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700106 return HASH;
Carl Worth81f01432010-05-14 17:08:45 -0700107}
108
Carl Worth8fed1cd2010-05-26 09:32:12 -0700109{DECIMAL_INTEGER} {
Carl Worth050e3de2010-05-27 14:36:29 -0700110 yylval.str = xtalloc_strdup (yyextra, yytext);
111 return INTEGER_STRING;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700112}
113
114{OCTAL_INTEGER} {
Carl Worth050e3de2010-05-27 14:36:29 -0700115 yylval.str = xtalloc_strdup (yyextra, yytext);
116 return INTEGER_STRING;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700117}
118
119{HEXADECIMAL_INTEGER} {
Carl Worth050e3de2010-05-27 14:36:29 -0700120 yylval.str = xtalloc_strdup (yyextra, yytext);
121 return INTEGER_STRING;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700122}
123
Carl Worthf34a0002010-05-25 16:59:02 -0700124"<<" {
125 return LEFT_SHIFT;
Carl Worthb1854fd2010-05-25 16:28:26 -0700126}
127
Carl Worthf34a0002010-05-25 16:59:02 -0700128">>" {
129 return RIGHT_SHIFT;
Carl Worthb1854fd2010-05-25 16:28:26 -0700130}
131
Carl Worthf34a0002010-05-25 16:59:02 -0700132"<=" {
133 return LESS_OR_EQUAL;
134}
135
136">=" {
137 return GREATER_OR_EQUAL;
138}
139
140"==" {
141 return EQUAL;
142}
143
144"!=" {
145 return NOT_EQUAL;
146}
147
148"&&" {
149 return AND;
150}
151
152"||" {
153 return OR;
154}
155
156"##" {
157 return PASTE;
158}
159
Carl Worth8fed1cd2010-05-26 09:32:12 -0700160"defined" {
161 return DEFINED;
162}
163
Carl Worth16c1e982010-05-26 09:35:34 -0700164{IDENTIFIER} {
165 yylval.str = xtalloc_strdup (yyextra, yytext);
166 return IDENTIFIER;
167}
168
Carl Worthf34a0002010-05-25 16:59:02 -0700169{PUNCTUATION} {
170 return yytext[0];
Carl Worthb1854fd2010-05-25 16:28:26 -0700171}
172
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700173{OTHER}+ {
174 yylval.str = xtalloc_strdup (yyextra, yytext);
175 return OTHER;
Carl Worth3ff81672010-05-25 13:09:03 -0700176}
177
Carl Worth9fb8b7a2010-05-25 15:04:32 -0700178{HSPACE}+ {
Carl Worthf34a0002010-05-25 16:59:02 -0700179 if (yyextra->space_tokens) {
Carl Worthf34a0002010-05-25 16:59:02 -0700180 return SPACE;
181 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700182}
Carl Worthfcbbb462010-05-13 09:36:23 -0700183
Carl Worth3ff81672010-05-25 13:09:03 -0700184\n {
Carl Wortha771a402010-06-01 11:20:18 -0700185 yyextra->lexing_if = 0;
Carl Worth3ff81672010-05-25 13:09:03 -0700186 return NEWLINE;
Carl Worth33cc4002010-05-12 12:17:10 -0700187}
Carl Worth3a37b872010-05-10 11:44:09 -0700188
Carl Worth3a37b872010-05-10 11:44:09 -0700189%%