e004c9f95b9b045266482f5c299d527c5b72eb26
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_process.h"
31 #include "sl_pp_public.h"
35 skip_whitespace(const struct sl_pp_token_info
*input
,
39 while (*first
< last
&& input
[*first
].token
== SL_PP_WHITESPACE
) {
46 _parse_formal_args(struct sl_pp_context
*context
,
47 const struct sl_pp_token_info
*input
,
50 struct sl_pp_macro
*macro
)
52 struct sl_pp_macro_formal_arg
**arg
;
56 skip_whitespace(input
, first
, last
);
58 if (input
[*first
].token
== SL_PP_RPAREN
) {
63 strcpy(context
->error_msg
, "expected either macro formal argument or `)'");
70 if (*first
< last
&& input
[*first
].token
!= SL_PP_IDENTIFIER
) {
71 strcpy(context
->error_msg
, "expected macro formal argument");
75 *arg
= malloc(sizeof(struct sl_pp_macro_formal_arg
));
77 strcpy(context
->error_msg
, "out of memory");
81 (**arg
).name
= input
[*first
].data
.identifier
;
89 skip_whitespace(input
, first
, last
);
91 if (input
[*first
].token
== SL_PP_COMMA
) {
93 skip_whitespace(input
, first
, last
);
94 } else if (input
[*first
].token
== SL_PP_RPAREN
) {
98 strcpy(context
->error_msg
, "expected either `,' or `)'");
102 strcpy(context
->error_msg
, "expected either `,' or `)'");
107 /* Should not gete here. */
112 sl_pp_process_define(struct sl_pp_context
*context
,
113 const struct sl_pp_token_info
*input
,
118 struct sl_pp_macro
*macro
;
120 unsigned int body_len
;
123 if (first
< last
&& input
[first
].token
== SL_PP_IDENTIFIER
) {
124 macro_name
= input
[first
].data
.identifier
;
127 if (macro_name
== -1) {
128 strcpy(context
->error_msg
, "expected macro name");
132 /* Check for reserved macro names */
134 const char *name
= sl_pp_context_cstr(context
, macro_name
);
136 if (strstr(name
, "__")) {
137 strcpy(context
->error_msg
, "macro names containing `__' are reserved");
140 if (name
[0] == 'G' && name
[1] == 'L' && name
[2] == '_') {
141 strcpy(context
->error_msg
, "macro names prefixed with `GL_' are reserved");
146 for (macro
= context
->macro
; macro
; macro
= macro
->next
) {
147 if (macro
->name
== macro_name
) {
153 macro
= sl_pp_macro_new();
155 strcpy(context
->error_msg
, "out of memory");
159 *context
->macro_tail
= macro
;
160 context
->macro_tail
= ¯o
->next
;
162 sl_pp_macro_reset(macro
);
165 macro
->name
= macro_name
;
168 * If there is no whitespace between macro name and left paren, a macro
169 * formal argument list follows. This is the only place where the presence
170 * of a whitespace matters and it's the only reason why we are dealing
171 * with whitespace at this level.
173 if (first
< last
&& input
[first
].token
== SL_PP_LPAREN
) {
175 if (_parse_formal_args(context
, input
, &first
, last
, macro
)) {
180 /* Calculate body size, trim out whitespace, make room for EOF. */
182 for (i
= first
; i
< last
; i
++) {
183 if (input
[i
].token
!= SL_PP_WHITESPACE
) {
188 macro
->body
= malloc(sizeof(struct sl_pp_token_info
) * body_len
);
190 strcpy(context
->error_msg
, "out of memory");
194 for (j
= 0, i
= first
; i
< last
; i
++) {
195 if (input
[i
].token
!= SL_PP_WHITESPACE
) {
196 macro
->body
[j
++] = input
[i
];
199 macro
->body
[j
++].token
= SL_PP_EOF
;
206 sl_pp_process_undef(struct sl_pp_context
*context
,
207 const struct sl_pp_token_info
*input
,
212 struct sl_pp_macro
**pmacro
;
213 struct sl_pp_macro
*macro
;
215 if (first
< last
&& input
[first
].token
== SL_PP_IDENTIFIER
) {
216 macro_name
= input
[first
].data
.identifier
;
218 if (macro_name
== -1) {
222 for (pmacro
= &context
->macro
; *pmacro
; pmacro
= &(**pmacro
).next
) {
223 if ((**pmacro
).name
== macro_name
) {
232 *pmacro
= macro
->next
;
234 sl_pp_macro_free(macro
);