1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 #include "sl_pp_context.h"
31 #include "sl_pp_process.h"
32 #include "sl_pp_public.h"
36 skip_whitespace(const struct sl_pp_token_info
*input
,
40 while (*first
< last
&& input
[*first
].token
== SL_PP_WHITESPACE
) {
47 _parse_formal_args(struct sl_pp_context
*context
,
48 const struct sl_pp_token_info
*input
,
51 struct sl_pp_macro
*macro
)
53 struct sl_pp_macro_formal_arg
**arg
;
57 skip_whitespace(input
, first
, last
);
59 if (input
[*first
].token
== SL_PP_RPAREN
) {
64 strcpy(context
->error_msg
, "expected either macro formal argument or `)'");
71 if (*first
< last
&& input
[*first
].token
!= SL_PP_IDENTIFIER
) {
72 strcpy(context
->error_msg
, "expected macro formal argument");
76 *arg
= malloc(sizeof(struct sl_pp_macro_formal_arg
));
78 strcpy(context
->error_msg
, "out of memory");
82 (**arg
).name
= input
[*first
].data
.identifier
;
90 skip_whitespace(input
, first
, last
);
92 if (input
[*first
].token
== SL_PP_COMMA
) {
94 skip_whitespace(input
, first
, last
);
95 } else if (input
[*first
].token
== SL_PP_RPAREN
) {
99 strcpy(context
->error_msg
, "expected either `,' or `)'");
103 strcpy(context
->error_msg
, "expected either `,' or `)'");
108 /* Should not gete here. */
113 sl_pp_process_define(struct sl_pp_context
*context
,
114 const struct sl_pp_token_info
*input
,
119 struct sl_pp_macro
*macro
;
121 unsigned int body_len
;
124 if (first
< last
&& input
[first
].token
== SL_PP_IDENTIFIER
) {
125 macro_name
= input
[first
].data
.identifier
;
128 if (macro_name
== -1) {
129 strcpy(context
->error_msg
, "expected macro name");
133 /* Check for reserved macro names */
135 const char *name
= sl_pp_context_cstr(context
, macro_name
);
137 if (strstr(name
, "__")) {
138 strcpy(context
->error_msg
, "macro names containing `__' are reserved");
141 if (name
[0] == 'G' && name
[1] == 'L' && name
[2] == '_') {
142 strcpy(context
->error_msg
, "macro names prefixed with `GL_' are reserved");
147 for (macro
= context
->macro
; macro
; macro
= macro
->next
) {
148 if (macro
->name
== macro_name
) {
154 macro
= sl_pp_macro_new();
156 strcpy(context
->error_msg
, "out of memory");
160 *context
->macro_tail
= macro
;
161 context
->macro_tail
= ¯o
->next
;
163 sl_pp_macro_reset(macro
);
166 macro
->name
= macro_name
;
169 * If there is no whitespace between macro name and left paren, a macro
170 * formal argument list follows. This is the only place where the presence
171 * of a whitespace matters and it's the only reason why we are dealing
172 * with whitespace at this level.
174 if (first
< last
&& input
[first
].token
== SL_PP_LPAREN
) {
176 if (_parse_formal_args(context
, input
, &first
, last
, macro
)) {
181 /* Calculate body size, trim out whitespace, make room for EOF. */
183 for (i
= first
; i
< last
; i
++) {
184 if (input
[i
].token
!= SL_PP_WHITESPACE
) {
189 macro
->body
= malloc(sizeof(struct sl_pp_token_info
) * body_len
);
191 strcpy(context
->error_msg
, "out of memory");
195 for (j
= 0, i
= first
; i
< last
; i
++) {
196 if (input
[i
].token
!= SL_PP_WHITESPACE
) {
197 macro
->body
[j
++] = input
[i
];
200 macro
->body
[j
++].token
= SL_PP_EOF
;
207 sl_pp_process_undef(struct sl_pp_context
*context
,
208 const struct sl_pp_token_info
*input
,
213 struct sl_pp_macro
**pmacro
;
214 struct sl_pp_macro
*macro
;
216 if (first
< last
&& input
[first
].token
== SL_PP_IDENTIFIER
) {
217 macro_name
= input
[first
].data
.identifier
;
219 if (macro_name
== -1) {
223 for (pmacro
= &context
->macro
; *pmacro
; pmacro
= &(**pmacro
).next
) {
224 if ((**pmacro
).name
== macro_name
) {
233 *pmacro
= macro
->next
;
235 sl_pp_macro_free(macro
);