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_macro.h"
32 #include "sl_pp_process.h"
33 #include "sl_pp_public.h"
34 #include "sl_pp_token.h"
38 skip_whitespace(const struct sl_pp_token_info
*input
,
42 while (*first
< last
&& input
[*first
].token
== SL_PP_WHITESPACE
) {
49 _parse_formal_args(struct sl_pp_context
*context
,
50 const struct sl_pp_token_info
*input
,
53 struct sl_pp_macro
*macro
)
55 struct sl_pp_macro_formal_arg
**arg
;
59 skip_whitespace(input
, first
, last
);
61 if (input
[*first
].token
== SL_PP_RPAREN
) {
66 strcpy(context
->error_msg
, "expected either macro formal argument or `)'");
73 if (*first
< last
&& input
[*first
].token
!= SL_PP_IDENTIFIER
) {
74 strcpy(context
->error_msg
, "expected macro formal argument");
78 *arg
= malloc(sizeof(struct sl_pp_macro_formal_arg
));
80 strcpy(context
->error_msg
, "out of memory");
84 (**arg
).name
= input
[*first
].data
.identifier
;
92 skip_whitespace(input
, first
, last
);
94 if (input
[*first
].token
== SL_PP_COMMA
) {
96 skip_whitespace(input
, first
, last
);
97 } else if (input
[*first
].token
== SL_PP_RPAREN
) {
101 strcpy(context
->error_msg
, "expected either `,' or `)'");
105 strcpy(context
->error_msg
, "expected either `,' or `)'");
110 /* Should not gete here. */
115 sl_pp_process_define(struct sl_pp_context
*context
,
116 const struct sl_pp_token_info
*input
,
121 struct sl_pp_macro
*macro
;
123 unsigned int body_len
;
126 if (first
< last
&& input
[first
].token
== SL_PP_IDENTIFIER
) {
127 macro_name
= input
[first
].data
.identifier
;
130 if (macro_name
== -1) {
131 strcpy(context
->error_msg
, "expected macro name");
135 /* Check for reserved macro names */
137 const char *name
= sl_pp_context_cstr(context
, macro_name
);
139 if (strstr(name
, "__")) {
140 strcpy(context
->error_msg
, "macro names containing `__' are reserved");
143 if (name
[0] == 'G' && name
[1] == 'L' && name
[2] == '_') {
144 strcpy(context
->error_msg
, "macro names prefixed with `GL_' are reserved");
149 for (macro
= context
->macro
; macro
; macro
= macro
->next
) {
150 if (macro
->name
== macro_name
) {
156 macro
= sl_pp_macro_new();
158 strcpy(context
->error_msg
, "out of memory");
162 *context
->macro_tail
= macro
;
163 context
->macro_tail
= ¯o
->next
;
165 sl_pp_macro_reset(macro
);
168 macro
->name
= macro_name
;
171 * If there is no whitespace between macro name and left paren, a macro
172 * formal argument list follows. This is the only place where the presence
173 * of a whitespace matters and it's the only reason why we are dealing
174 * with whitespace at this level.
176 if (first
< last
&& input
[first
].token
== SL_PP_LPAREN
) {
178 if (_parse_formal_args(context
, input
, &first
, last
, macro
)) {
183 /* Calculate body size, trim out whitespace, make room for EOF. */
185 for (i
= first
; i
< last
; i
++) {
186 if (input
[i
].token
!= SL_PP_WHITESPACE
) {
191 macro
->body
= malloc(sizeof(struct sl_pp_token_info
) * body_len
);
193 strcpy(context
->error_msg
, "out of memory");
197 for (j
= 0, i
= first
; i
< last
; i
++) {
198 if (input
[i
].token
!= SL_PP_WHITESPACE
) {
199 macro
->body
[j
++] = input
[i
];
202 macro
->body
[j
++].token
= SL_PP_EOF
;
209 sl_pp_process_undef(struct sl_pp_context
*context
,
210 const struct sl_pp_token_info
*input
,
215 struct sl_pp_macro
**pmacro
;
216 struct sl_pp_macro
*macro
;
218 if (first
< last
&& input
[first
].token
== SL_PP_IDENTIFIER
) {
219 macro_name
= input
[first
].data
.identifier
;
221 if (macro_name
== -1) {
225 for (pmacro
= &context
->macro
; *pmacro
; pmacro
= &(**pmacro
).next
) {
226 if ((**pmacro
).name
== macro_name
) {
235 *pmacro
= macro
->next
;
237 sl_pp_macro_free(macro
);