glsl/pp: Do processing inline with tokenisation.
[mesa.git] / src / glsl / pp / sl_pp_version.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 <stdlib.h>
29 #include <string.h>
30 #include "sl_pp_public.h"
31 #include "sl_pp_context.h"
32
33
34 int
35 sl_pp_version(struct sl_pp_context *context,
36 unsigned int *version)
37 {
38 struct sl_pp_token_peek peek;
39 unsigned int line = context->line;
40
41 /* Default values if `#version' is not present. */
42 *version = 110;
43
44 if (sl_pp_token_peek_init(&peek, &context->tokens)) {
45 return -1;
46 }
47
48 /* There can be multiple `#version' directives present.
49 * Accept the value of the last one.
50 */
51 for (;;) {
52 struct sl_pp_token_info input;
53 int found_hash = 0;
54 int found_version = 0;
55 int found_number = 0;
56 int found_end = 0;
57
58 /* Skip whitespace and newlines and seek for hash. */
59 while (!found_hash) {
60 if (sl_pp_token_peek_get(&peek, &input)) {
61 sl_pp_token_peek_destroy(&peek);
62 return -1;
63 }
64
65 switch (input.token) {
66 case SL_PP_NEWLINE:
67 line++;
68 break;
69
70 case SL_PP_WHITESPACE:
71 break;
72
73 case SL_PP_HASH:
74 found_hash = 1;
75 break;
76
77 default:
78 sl_pp_token_peek_destroy(&peek);
79 return 0;
80 }
81 }
82
83 /* Skip whitespace and seek for `version'. */
84 while (!found_version) {
85 if (sl_pp_token_peek_get(&peek, &input)) {
86 sl_pp_token_peek_destroy(&peek);
87 return -1;
88 }
89
90 switch (input.token) {
91 case SL_PP_WHITESPACE:
92 break;
93
94 case SL_PP_IDENTIFIER:
95 if (input.data.identifier != context->dict.version) {
96 sl_pp_token_peek_destroy(&peek);
97 return 0;
98 }
99 found_version = 1;
100 break;
101
102 default:
103 sl_pp_token_peek_destroy(&peek);
104 return 0;
105 }
106 }
107
108 sl_pp_token_peek_commit(&peek);
109
110 /* Skip whitespace and seek for version number. */
111 while (!found_number) {
112 if (sl_pp_token_buffer_get(&context->tokens, &input)) {
113 sl_pp_token_peek_destroy(&peek);
114 return -1;
115 }
116
117 switch (input.token) {
118 case SL_PP_WHITESPACE:
119 break;
120
121 case SL_PP_UINT:
122 *version = atoi(sl_pp_context_cstr(context, input.data._uint));
123 found_number = 1;
124 break;
125
126 default:
127 strcpy(context->error_msg, "expected version number after `#version'");
128 sl_pp_token_peek_destroy(&peek);
129 return -1;
130 }
131 }
132
133 /* Skip whitespace and seek for either newline or eof. */
134 while (!found_end) {
135 if (sl_pp_token_buffer_get(&context->tokens, &input)) {
136 sl_pp_token_peek_destroy(&peek);
137 return -1;
138 }
139
140 switch (input.token) {
141 case SL_PP_WHITESPACE:
142 break;
143
144 case SL_PP_NEWLINE:
145 line++;
146 /* pass thru */
147 case SL_PP_EOF:
148 context->line = line;
149 found_end = 1;
150 break;
151
152 default:
153 strcpy(context->error_msg, "expected end of line after version number");
154 sl_pp_token_peek_destroy(&peek);
155 return -1;
156 }
157 }
158 }
159
160 /* Should not get here. */
161 }