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