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_public.h"
31 #include "sl_pp_context.h"
32 #include "sl_pp_token.h"
36 sl_pp_version(struct sl_pp_context
*context
,
37 unsigned int *version
)
39 struct sl_pp_token_peek peek
;
40 unsigned int line
= context
->line
;
42 /* Default values if `#version' is not present. */
45 if (sl_pp_token_peek_init(&peek
, &context
->tokens
)) {
49 /* There can be multiple `#version' directives present.
50 * Accept the value of the last one.
53 struct sl_pp_token_info input
;
55 int found_version
= 0;
59 /* Skip whitespace and newlines and seek for hash. */
61 if (sl_pp_token_peek_get(&peek
, &input
)) {
62 sl_pp_token_peek_destroy(&peek
);
66 switch (input
.token
) {
71 case SL_PP_WHITESPACE
:
79 sl_pp_token_peek_destroy(&peek
);
84 /* Skip whitespace and seek for `version'. */
85 while (!found_version
) {
86 if (sl_pp_token_peek_get(&peek
, &input
)) {
87 sl_pp_token_peek_destroy(&peek
);
91 switch (input
.token
) {
92 case SL_PP_WHITESPACE
:
95 case SL_PP_IDENTIFIER
:
96 if (input
.data
.identifier
!= context
->dict
.version
) {
97 sl_pp_token_peek_destroy(&peek
);
104 sl_pp_token_peek_destroy(&peek
);
109 sl_pp_token_peek_commit(&peek
);
111 /* Skip whitespace and seek for version number. */
112 while (!found_number
) {
113 if (sl_pp_token_buffer_get(&context
->tokens
, &input
)) {
114 sl_pp_token_peek_destroy(&peek
);
118 switch (input
.token
) {
119 case SL_PP_WHITESPACE
:
123 *version
= atoi(sl_pp_context_cstr(context
, input
.data
._uint
));
128 strcpy(context
->error_msg
, "expected version number after `#version'");
129 sl_pp_token_peek_destroy(&peek
);
134 /* Skip whitespace and seek for either newline or eof. */
136 if (sl_pp_token_buffer_get(&context
->tokens
, &input
)) {
137 sl_pp_token_peek_destroy(&peek
);
141 switch (input
.token
) {
142 case SL_PP_WHITESPACE
:
149 context
->line
= line
;
154 strcpy(context
->error_msg
, "expected end of line after version number");
155 sl_pp_token_peek_destroy(&peek
);
161 /* Should not get here. */