Merge branch 'mesa_7_7_branch'
[mesa.git] / src / glsl / pp / sl_pp_token_util.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include <assert.h>
29 #include <stdlib.h>
30 #include "sl_pp_token_util.h"
31
32
33 int
34 sl_pp_token_buffer_init(struct sl_pp_token_buffer *buffer,
35 struct sl_pp_context *context)
36 {
37 buffer->context = context;
38 buffer->size = 0;
39 buffer->capacity = 64;
40 buffer->tokens = malloc(buffer->capacity * sizeof(struct sl_pp_token_info));
41 if (!buffer->tokens) {
42 return -1;
43 }
44 return 0;
45 }
46
47 void
48 sl_pp_token_buffer_destroy(struct sl_pp_token_buffer *buffer)
49 {
50 free(buffer->tokens);
51 }
52
53 int
54 sl_pp_token_buffer_get(struct sl_pp_token_buffer *buffer,
55 struct sl_pp_token_info *out)
56 {
57 /* Pop from stack first if not empty. */
58 if (buffer->size) {
59 *out = buffer->tokens[--buffer->size];
60 return 0;
61 }
62
63 assert(buffer->context);
64 return sl_pp_token_get(buffer->context, out);
65 }
66
67 void
68 sl_pp_token_buffer_unget(struct sl_pp_token_buffer *buffer,
69 const struct sl_pp_token_info *in)
70 {
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);
77 }
78
79 /* Push token on stack. */
80 buffer->tokens[buffer->size++] = *in;
81 }
82
83 int
84 sl_pp_token_buffer_skip_white(struct sl_pp_token_buffer *buffer,
85 struct sl_pp_token_info *out)
86 {
87 if (sl_pp_token_buffer_get(buffer, out)) {
88 return -1;
89 }
90
91 while (out->token == SL_PP_WHITESPACE) {
92 if (sl_pp_token_buffer_get(buffer, out)) {
93 return -1;
94 }
95 }
96
97 return 0;
98 }
99
100
101
102 int
103 sl_pp_token_peek_init(struct sl_pp_token_peek *peek,
104 struct sl_pp_token_buffer *buffer)
105 {
106 peek->buffer = buffer;
107 peek->size = 0;
108 peek->capacity = 64;
109 peek->tokens = malloc(peek->capacity * sizeof(struct sl_pp_token_info));
110 if (!peek->tokens) {
111 return -1;
112 }
113 return 0;
114 }
115
116 void
117 sl_pp_token_peek_destroy(struct sl_pp_token_peek *peek)
118 {
119 /* Abort. */
120 while (peek->size) {
121 sl_pp_token_buffer_unget(peek->buffer, &peek->tokens[--peek->size]);
122 }
123 free(peek->tokens);
124 }
125
126 int
127 sl_pp_token_peek_get(struct sl_pp_token_peek *peek,
128 struct sl_pp_token_info *out)
129 {
130 /* Get token from buffer. */
131 if (sl_pp_token_buffer_get(peek->buffer, out)) {
132 return -1;
133 }
134
135 /* Save it. */
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);
141 }
142 peek->tokens[peek->size++] = *out;
143 return 0;
144 }
145
146 void
147 sl_pp_token_peek_commit(struct sl_pp_token_peek *peek)
148 {
149 peek->size = 0;
150 }
151
152 int
153 sl_pp_token_peek_to_buffer(const struct sl_pp_token_peek *peek,
154 struct sl_pp_token_buffer *buffer)
155 {
156 unsigned int i;
157
158 if (sl_pp_token_buffer_init(buffer, NULL)) {
159 return -1;
160 }
161 for (i = peek->size; i > 0; i--) {
162 sl_pp_token_buffer_unget(buffer, &peek->tokens[i - 1]);
163 }
164 return 0;
165 }
166
167 int
168 sl_pp_token_peek_skip_white(struct sl_pp_token_peek *peek,
169 struct sl_pp_token_info *out)
170 {
171 if (sl_pp_token_peek_get(peek, out)) {
172 return -1;
173 }
174
175 while (out->token == SL_PP_WHITESPACE) {
176 if (sl_pp_token_peek_get(peek, out)) {
177 return -1;
178 }
179 }
180
181 return 0;
182 }