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"
35 sl_pp_version(struct sl_pp_context
*context
,
36 unsigned int *version
)
38 struct sl_pp_token_peek peek
;
39 unsigned int line
= context
->line
;
41 /* Default values if `#version' is not present. */
44 if (sl_pp_token_peek_init(&peek
, &context
->tokens
)) {
48 /* There can be multiple `#version' directives present.
49 * Accept the value of the last one.
52 struct sl_pp_token_info input
;
54 int found_version
= 0;
58 /* Skip whitespace and newlines and seek for hash. */
60 if (sl_pp_token_peek_get(&peek
, &input
)) {
61 sl_pp_token_peek_destroy(&peek
);
65 switch (input
.token
) {
70 case SL_PP_WHITESPACE
:
78 sl_pp_token_peek_destroy(&peek
);
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
);
90 switch (input
.token
) {
91 case SL_PP_WHITESPACE
:
94 case SL_PP_IDENTIFIER
:
95 if (input
.data
.identifier
!= context
->dict
.version
) {
96 sl_pp_token_peek_destroy(&peek
);
103 sl_pp_token_peek_destroy(&peek
);
108 sl_pp_token_peek_commit(&peek
);
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
);
117 switch (input
.token
) {
118 case SL_PP_WHITESPACE
:
122 *version
= atoi(sl_pp_context_cstr(context
, input
.data
._uint
));
127 strcpy(context
->error_msg
, "expected version number after `#version'");
128 sl_pp_token_peek_destroy(&peek
);
133 /* Skip whitespace and seek for either newline or eof. */
135 if (sl_pp_token_buffer_get(&context
->tokens
, &input
)) {
136 sl_pp_token_peek_destroy(&peek
);
140 switch (input
.token
) {
141 case SL_PP_WHITESPACE
:
148 context
->line
= line
;
153 strcpy(context
->error_msg
, "expected end of line after version number");
154 sl_pp_token_peek_destroy(&peek
);
160 /* Should not get here. */