glsl/pp: Do processing inline with tokenisation.
[mesa.git] / src / glsl / pp / sl_pp_line.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_process.h"
32
33
34 int
35 sl_pp_process_line(struct sl_pp_context *context,
36 struct sl_pp_token_buffer *buffer,
37 struct sl_pp_process_state *pstate)
38 {
39 struct sl_pp_process_state state;
40 int found_end = 0;
41 int line_number = -1;
42 int file_number = -1;
43 unsigned int line;
44 unsigned int file;
45
46 memset(&state, 0, sizeof(state));
47 while (!found_end) {
48 struct sl_pp_token_info input;
49
50 sl_pp_token_buffer_get(buffer, &input);
51 switch (input.token) {
52 case SL_PP_WHITESPACE:
53 break;
54
55 case SL_PP_IDENTIFIER:
56 sl_pp_token_buffer_unget(buffer, &input);
57 if (sl_pp_macro_expand(context, buffer, NULL, &state, sl_pp_macro_expand_normal)) {
58 free(state.out);
59 return -1;
60 }
61 break;
62
63 case SL_PP_NEWLINE:
64 case SL_PP_EOF:
65 found_end = 1;
66 break;
67
68 default:
69 if (sl_pp_process_out(&state, &input)) {
70 strcpy(context->error_msg, "out of memory");
71 free(state.out);
72 return -1;
73 }
74 }
75 }
76
77 if (state.out_len > 0 && state.out[0].token == SL_PP_UINT) {
78 line_number = state.out[0].data._uint;
79 } else {
80 strcpy(context->error_msg, "expected a number after `#line'");
81 free(state.out);
82 return -1;
83 }
84
85 if (state.out_len > 1) {
86 if (state.out[1].token == SL_PP_UINT) {
87 file_number = state.out[1].data._uint;
88 } else {
89 strcpy(context->error_msg, "expected a number after line number");
90 free(state.out);
91 return -1;
92 }
93
94 if (state.out_len > 2) {
95 strcpy(context->error_msg, "expected an end of line after file number");
96 free(state.out);
97 return -1;
98 }
99 }
100
101 free(state.out);
102
103 line = atoi(sl_pp_context_cstr(context, line_number));
104 if (file_number != -1) {
105 file = atoi(sl_pp_context_cstr(context, file_number));
106 } else {
107 file = context->file;
108 }
109
110 if (context->line != line || context->file != file) {
111 struct sl_pp_token_info ti;
112
113 ti.token = SL_PP_LINE;
114 ti.data.line.lineno = line;
115 ti.data.line.fileno = file;
116 if (sl_pp_process_out(pstate, &ti)) {
117 strcpy(context->error_msg, "out of memory");
118 return -1;
119 }
120
121 context->line = line;
122 context->file = file;
123 }
124
125 return 0;
126 }