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_token_util.h"
34 sl_pp_token_buffer_init(struct sl_pp_token_buffer
*buffer
,
35 struct sl_pp_context
*context
)
37 buffer
->context
= context
;
39 buffer
->capacity
= 64;
40 buffer
->tokens
= malloc(buffer
->capacity
* sizeof(struct sl_pp_token_info
));
41 if (!buffer
->tokens
) {
48 sl_pp_token_buffer_destroy(struct sl_pp_token_buffer
*buffer
)
54 sl_pp_token_buffer_get(struct sl_pp_token_buffer
*buffer
,
55 struct sl_pp_token_info
*out
)
57 /* Pop from stack first if not empty. */
59 *out
= buffer
->tokens
[--buffer
->size
];
63 assert(buffer
->context
);
64 return sl_pp_token_get(buffer
->context
, out
);
68 sl_pp_token_buffer_unget(struct sl_pp_token_buffer
*buffer
,
69 const struct sl_pp_token_info
*in
)
71 /* Resize if needed. */
72 if (buffer
->size
== buffer
->capacity
) {
73 buffer
->capacity
+= 64;
74 buffer
->tokens
= realloc(buffer
->tokens
,
75 buffer
->capacity
* sizeof(struct sl_pp_token_info
));
76 assert(buffer
->tokens
);
79 /* Push token on stack. */
80 buffer
->tokens
[buffer
->size
++] = *in
;
84 sl_pp_token_buffer_skip_white(struct sl_pp_token_buffer
*buffer
,
85 struct sl_pp_token_info
*out
)
87 if (sl_pp_token_buffer_get(buffer
, out
)) {
91 while (out
->token
== SL_PP_WHITESPACE
) {
92 if (sl_pp_token_buffer_get(buffer
, out
)) {
103 sl_pp_token_peek_init(struct sl_pp_token_peek
*peek
,
104 struct sl_pp_token_buffer
*buffer
)
106 peek
->buffer
= buffer
;
109 peek
->tokens
= malloc(peek
->capacity
* sizeof(struct sl_pp_token_info
));
117 sl_pp_token_peek_destroy(struct sl_pp_token_peek
*peek
)
121 sl_pp_token_buffer_unget(peek
->buffer
, &peek
->tokens
[--peek
->size
]);
127 sl_pp_token_peek_get(struct sl_pp_token_peek
*peek
,
128 struct sl_pp_token_info
*out
)
130 /* Get token from buffer. */
131 if (sl_pp_token_buffer_get(peek
->buffer
, out
)) {
136 if (peek
->size
== peek
->capacity
) {
137 peek
->capacity
+= 64;
138 peek
->tokens
= realloc(peek
->tokens
,
139 peek
->capacity
* sizeof(struct sl_pp_token_info
));
140 assert(peek
->tokens
);
142 peek
->tokens
[peek
->size
++] = *out
;
147 sl_pp_token_peek_commit(struct sl_pp_token_peek
*peek
)
153 sl_pp_token_peek_to_buffer(const struct sl_pp_token_peek
*peek
,
154 struct sl_pp_token_buffer
*buffer
)
158 if (sl_pp_token_buffer_init(buffer
, NULL
)) {
161 for (i
= peek
->size
; i
> 0; i
--) {
162 sl_pp_token_buffer_unget(buffer
, &peek
->tokens
[i
- 1]);
168 sl_pp_token_peek_skip_white(struct sl_pp_token_peek
*peek
,
169 struct sl_pp_token_info
*out
)
171 if (sl_pp_token_peek_get(peek
, out
)) {
175 while (out
->token
== SL_PP_WHITESPACE
) {
176 if (sl_pp_token_peek_get(peek
, out
)) {