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"
31 #include "sl_pp_token.h"
35 sl_pp_token_buffer_init(struct sl_pp_token_buffer
*buffer
,
36 struct sl_pp_context
*context
)
38 buffer
->context
= context
;
40 buffer
->capacity
= 64;
41 buffer
->tokens
= malloc(buffer
->capacity
* sizeof(struct sl_pp_token_info
));
42 if (!buffer
->tokens
) {
49 sl_pp_token_buffer_destroy(struct sl_pp_token_buffer
*buffer
)
55 sl_pp_token_buffer_get(struct sl_pp_token_buffer
*buffer
,
56 struct sl_pp_token_info
*out
)
58 /* Pop from stack first if not empty. */
60 *out
= buffer
->tokens
[--buffer
->size
];
64 assert(buffer
->context
);
65 return sl_pp_token_get(buffer
->context
, out
);
69 sl_pp_token_buffer_unget(struct sl_pp_token_buffer
*buffer
,
70 const struct sl_pp_token_info
*in
)
72 /* Resize if needed. */
73 if (buffer
->size
== buffer
->capacity
) {
74 buffer
->capacity
+= 64;
75 buffer
->tokens
= realloc(buffer
->tokens
,
76 buffer
->capacity
* sizeof(struct sl_pp_token_info
));
77 assert(buffer
->tokens
);
80 /* Push token on stack. */
81 buffer
->tokens
[buffer
->size
++] = *in
;
85 sl_pp_token_buffer_skip_white(struct sl_pp_token_buffer
*buffer
,
86 struct sl_pp_token_info
*out
)
88 if (sl_pp_token_buffer_get(buffer
, out
)) {
92 while (out
->token
== SL_PP_WHITESPACE
) {
93 if (sl_pp_token_buffer_get(buffer
, out
)) {
104 sl_pp_token_peek_init(struct sl_pp_token_peek
*peek
,
105 struct sl_pp_token_buffer
*buffer
)
107 peek
->buffer
= buffer
;
110 peek
->tokens
= malloc(peek
->capacity
* sizeof(struct sl_pp_token_info
));
118 sl_pp_token_peek_destroy(struct sl_pp_token_peek
*peek
)
122 sl_pp_token_buffer_unget(peek
->buffer
, &peek
->tokens
[--peek
->size
]);
128 sl_pp_token_peek_get(struct sl_pp_token_peek
*peek
,
129 struct sl_pp_token_info
*out
)
131 /* Get token from buffer. */
132 if (sl_pp_token_buffer_get(peek
->buffer
, out
)) {
137 if (peek
->size
== peek
->capacity
) {
138 peek
->capacity
+= 64;
139 peek
->tokens
= realloc(peek
->tokens
,
140 peek
->capacity
* sizeof(struct sl_pp_token_info
));
141 assert(peek
->tokens
);
143 peek
->tokens
[peek
->size
++] = *out
;
148 sl_pp_token_peek_commit(struct sl_pp_token_peek
*peek
)
154 sl_pp_token_peek_to_buffer(const struct sl_pp_token_peek
*peek
,
155 struct sl_pp_token_buffer
*buffer
)
159 if (sl_pp_token_buffer_init(buffer
, NULL
)) {
162 for (i
= peek
->size
; i
> 0; i
--) {
163 sl_pp_token_buffer_unget(buffer
, &peek
->tokens
[i
- 1]);
169 sl_pp_token_peek_skip_white(struct sl_pp_token_peek
*peek
,
170 struct sl_pp_token_info
*out
)
172 if (sl_pp_token_peek_get(peek
, out
)) {
176 while (out
->token
== SL_PP_WHITESPACE
) {
177 if (sl_pp_token_peek_get(peek
, out
)) {