annotate lwcc/lex.c @ 560:dba08c7dff96

Fix off by one handling MOD directive optional arguments Fixes an off by one in handling the optional arguments to the MOD directive as of changeset 928c033c0cd0. Thanks to Alex Evans <varmfskii@gmail.com> for reporting the problem and a suggested fix. This fix is different but should be more stable should the rest of the parsing code be refactored in the future.
author William Astle <lost@l-w.ca>
date Fri, 22 Sep 2023 12:15:09 -0600
parents ee3e52ab2288
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
2 lwcc/lex.c
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
3
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2013 William Astle
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
5
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
7
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
12
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
17
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
21
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
22 #include <ctype.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
23 #include <stdio.h>
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
24
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
25 #include <lw_alloc.h>
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
26 #include <lw_strbuf.h>
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
27
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
28 #include "cpp.h"
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
29 #include "token.h"
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
30
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
31 /* fetch a raw input byte from the current file. Will return CPP_EOF if
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
32 EOF is encountered and CPP_EOL if an end of line sequence is encountered.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
33 End of line is defined as either CR, CRLF, LF, or LFCR. CPP_EOL is
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
34 returned on the first CR or LF encountered. The complementary CR or LF
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
35 is munched, if present, when the *next* character is read. This always
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
36 operates on file_stack.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
37
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
38 This function also accounts for line numbers in input files and also
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
39 character columns.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
40 */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
41 static int fetch_byte_ll(struct preproc_info *pp)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
42 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
43 int c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
44
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
45 if (pp -> eolstate != 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
46 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
47 pp -> lineno++;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
48 pp -> column = 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
49 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
50 c = getc(pp -> fp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
51 pp -> column++;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
52 if (pp -> eolstate == 1)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
53 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
54 // just saw CR, munch LF
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
55 if (c == 10)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
56 c = getc(pp -> fp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
57 pp -> eolstate = 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
58 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
59 else if (pp -> eolstate == 2)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
60 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
61 // just saw LF, much CR
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
62 if (c == 13)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
63 c = getc(pp -> fp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
64 pp -> eolstate = 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
65 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
66
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
67 if (c == 10)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
68 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
69 // we have LF - end of line, flag to munch CR
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
70 pp -> eolstate = 2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
71 c = CPP_EOL;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
72 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
73 else if (c == 13)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
74 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
75 // we have CR - end of line, flag to munch LF
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
76 pp -> eolstate = 1;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
77 c = CPP_EOL;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
78 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
79 else if (c == EOF)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
80 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
81 c = CPP_EOF;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
82 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
83 return c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
84 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
85
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
86 /* This function takes a sequence of bytes from the _ll function above
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
87 and does trigraph interpretation on it, but only if the global
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
88 trigraphs is nonzero. */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
89 static int fetch_byte_tg(struct preproc_info *pp)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
90 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
91 int c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
92
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
93 if (!pp -> trigraphs)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
94 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
95 c = fetch_byte_ll(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
96 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
97 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
98 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
99 /* we have to do the trigraph shit here */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
100 if (pp -> ra != CPP_NOUNG)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
101 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
102 if (pp -> qseen > 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
103 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
104 c = '?';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
105 pp -> qseen -= 1;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
106 return c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
107 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
108 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
109 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
110 c = pp -> ra;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
111 pp -> ra = CPP_NOUNG;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
112 return c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
113 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
114 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
115
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
116 c = fetch_byte_ll(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
117 while (c == '?')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
118 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
119 pp -> qseen++;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
120 c = fetch_byte_ll(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
121 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
122
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
123 if (pp -> qseen >= 2)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
124 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
125 // we have a trigraph
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
126 switch (c)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
127 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
128 case '=':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
129 c = '#';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
130 pp -> qseen -= 2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
131 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
132
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
133 case '/':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
134 c = '\\';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
135 pp -> qseen -= 2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
136 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
137
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
138 case '\'':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
139 c = '^';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
140 pp -> qseen -= 2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
141 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
142
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
143 case '(':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
144 c = '[';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
145 pp -> qseen -= 2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
146 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
147
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
148 case ')':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
149 c = ']';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
150 pp -> qseen -= 2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
151 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
152
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
153 case '!':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
154 c = '|';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
155 pp -> qseen -= 2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
156 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
157
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
158 case '<':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
159 c = '{';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
160 pp -> qseen -= 2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
161 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
162
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
163 case '>':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
164 c = '}';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
165 pp -> qseen -= 2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
166 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
167
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
168 case '-':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
169 c = '~';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
170 pp -> qseen -= 2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
171 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
172 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
173 if (pp -> qseen > 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
174 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
175 pp -> ra = c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
176 c = '?';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
177 pp -> qseen--;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
178 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
179 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
180 else if (pp -> qseen > 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
181 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
182 pp -> ra = c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
183 c = '?';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
184 pp -> qseen--;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
185 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
186 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
187 return c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
188 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
189
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
190 /* This function puts a byte back onto the front of the input stream used
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
191 by fetch_byte(). Theoretically, an unlimited number of characters can
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
192 be unfetched. Line and column counting may be incorrect if unfetched
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
193 characters cross a token boundary. */
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
194 void preproc_lex_unfetch_byte(struct preproc_info *pp, int c)
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
195 {
306
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
196 if (pp -> lexstr)
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
197 {
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
198 if (c == CPP_EOL)
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
199 return;
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
200 if (pp -> lexstrloc > 0)
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
201 {
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
202 pp -> lexstrloc--;
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
203 return;
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
204 }
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
205 }
b08787e5b9f3 Add include search paths and command line macro definitions
William Astle <lost@l-w.ca>
parents: 305
diff changeset
206
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
207 if (pp -> ungetbufl >= pp -> ungetbufs)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
208 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
209 pp -> ungetbufs += 100;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
210 pp -> ungetbuf = lw_realloc(pp -> ungetbuf, pp -> ungetbufs);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
211 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
212 pp -> ungetbuf[pp -> ungetbufl++] = c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
213 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
214
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
215 /* This function retrieves a byte from the input stream. It performs
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
216 backslash-newline splicing on the returned bytes. Any character
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
217 retrieved from the unfetch buffer is presumed to have already passed
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
218 the backslash-newline filter. */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
219 static int fetch_byte(struct preproc_info *pp)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
220 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
221 int c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
222
298
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
223 if (pp -> lexstr)
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
224 {
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
225 if (pp -> lexstr[pp -> lexstrloc])
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
226 return pp -> lexstr[pp -> lexstrloc++];
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
227 else
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
228 return CPP_EOL;
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
229 }
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
230
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
231 if (pp -> ungetbufl > 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
232 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
233 pp -> ungetbufl--;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
234 c = pp -> ungetbuf[pp -> ungetbufl];
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
235 if (pp -> ungetbufl == 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
236 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
237 lw_free(pp -> ungetbuf);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
238 pp -> ungetbuf = NULL;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
239 pp -> ungetbufs = 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
240 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
241 return c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
242 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
243
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
244 again:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
245 if (pp -> unget != CPP_NOUNG)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
246 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
247 c = pp -> unget;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
248 pp -> unget = CPP_NOUNG;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
249 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
250 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
251 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
252 c = fetch_byte_tg(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
253 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
254 if (c == '\\')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
255 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
256 int c2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
257 c2 = fetch_byte_tg(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
258 if (c2 == CPP_EOL)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
259 goto again;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
260 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
261 pp -> unget = c2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
262 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
263 return c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
264 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
265
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
266
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
267
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
268 /*
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
269 Lex a token off the current input file.
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
270
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
271 Returned tokens are as follows:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
272
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
273 * all words starting with [a-zA-Z_] are returned as TOK_IDENT
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
274 * numbers are returned as their appropriate type
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
275 * all whitespace in a sequence, including comments, is returned as
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
276 a single instance of TOK_WSPACE
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
277 * TOK_EOL is returned in the case of the end of a line
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
278 * TOK_EOF is returned when the end of the file is reached
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
279 * If no TOK_EOL appears before TOK_EOF, a TOK_EOL will be synthesised
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
280 * Any symbolic operator, etc., recognized by C will be returned as such
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
281 a token
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
282 * TOK_HASH will be returned for a #
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
283 * trigraphs will be interpreted
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
284 * backslash-newline will be interpreted
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
285 * any instance of CR, LF, CRLF, or LFCR will be interpreted as TOK_EOL
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
286 */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
287
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
288
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
289 int preproc_lex_fetch_byte(struct preproc_info *pp)
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
290 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
291 int c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
292 c = fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
293 if (c == CPP_EOF && pp -> eolseen == 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
294 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
295 preproc_throw_warning(pp, "No newline at end of file");
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
296 pp -> eolseen = 1;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
297 return CPP_EOL;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
298 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
299
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
300 if (c == CPP_EOL)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
301 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
302 pp -> eolseen = 1;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
303 return c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
304 }
300
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
305
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
306 pp -> eolseen = 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
307
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
308 /* convert comments to a single space here */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
309 if (c == '/')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
310 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
311 int c2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
312 c2 = fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
313 if (c2 == '/')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
314 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
315 /* single line comment */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
316 c = ' ';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
317 for (;;)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
318 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
319 c2 = fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
320 if (c2 == CPP_EOF || c2 == CPP_EOL)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
321 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
322 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
323 preproc_lex_unfetch_byte(pp, c2);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
324 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
325 else if (c2 == '*')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
326 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
327 /* block comment */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
328 c = ' ';
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
329 for (;;)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
330 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
331 c2 = fetch_byte(pp);
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
332 if (c2 == CPP_EOF)
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
333 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
334 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
335 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
336 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
337 if (c2 == '*')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
338 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
339 /* maybe end of comment */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
340 c2 = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
341 if (c2 == '/')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
342 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
343 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
344 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
345 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
346 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
347 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
348 /* not a comment - restore lookahead character */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
349 preproc_lex_unfetch_byte(pp, c2);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
350 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
351 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
352 return c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
353 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
354
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
355 struct token *preproc_lex_next_token(struct preproc_info *pp)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
356 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
357 int sline = pp -> lineno;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
358 int scol = pp -> column;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
359 char *strval = NULL;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
360 int ttype = TOK_NONE;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
361 int c, c2;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
362 int cl;
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
363 struct lw_strbuf *strbuf;
304
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents: 300
diff changeset
364 struct token *t = NULL;
300
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
365 struct preproc_info *fs;
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
366
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
367 fileagain:
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
368 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
369 if (c == CPP_EOF)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
370 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
371 if (pp -> nlseen == 0)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
372 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
373 c = CPP_EOL;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
374 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
375 }
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
376
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
377 if (pp -> lineno != sline)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
378 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
379 sline = pp -> lineno;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
380 scol = pp -> column;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 304
diff changeset
381 }
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
382
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
383 if (c == CPP_EOF)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
384 {
300
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
385 /* check if we fell off the end of an include file */
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
386 if (pp -> filestack)
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
387 {
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
388 if (pp -> skip_level || pp -> found_level)
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
389 {
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
390 preproc_throw_error(pp, "Unbalanced conditionals in include file");
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
391 }
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
392 fclose(pp -> fp);
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
393 fs = pp -> filestack;
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
394 *pp = *fs;
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
395 pp -> filestack = fs -> n;
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
396 goto fileagain;
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
397 }
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
398 else
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
399 {
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
400 ttype = TOK_EOF;
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
401 goto out;
8d6c47395653 Implemented #include and #line
William Astle <lost@l-w.ca>
parents: 299
diff changeset
402 }
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
403 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
404 if (c == CPP_EOL)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
405 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
406 pp -> nlseen = 1;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
407 ttype = TOK_EOL;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
408 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
409 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
410
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
411 pp -> nlseen = 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
412 if (isspace(c))
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
413 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
414 while (isspace(c))
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
415 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
416 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
417 ttype = TOK_WSPACE;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
418 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
419 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
420
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
421 switch (c)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
422 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
423 case '?':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
424 ttype = TOK_QMARK;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
425 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
426
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
427 case ':':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
428 ttype = TOK_COLON;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
429 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
430
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
431 case ',':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
432 ttype = TOK_COMMA;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
433 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
434
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
435 case '(':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
436 ttype = TOK_OPAREN;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
437 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
438
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
439 case ')':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
440 ttype = TOK_CPAREN;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
441 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
442
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
443 case '{':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
444 ttype = TOK_OBRACE;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
445 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
446
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
447 case '}':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
448 ttype = TOK_CBRACE;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
449 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
450
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
451 case '[':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
452 ttype = TOK_OSQUARE;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
453 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
454
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
455 case ']':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
456 ttype = TOK_CSQUARE;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
457 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
458
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
459 case '~':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
460 ttype = TOK_COM;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
461 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
462
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
463 case ';':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
464 ttype = TOK_EOS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
465 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
466
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
467 /* and now for the possible multi character tokens */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
468 case '#':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
469 ttype = TOK_HASH;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
470 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
471 if (c == '#')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
472 ttype = TOK_DBLHASH;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
473 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
474 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
475 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
476
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
477 case '^':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
478 ttype = TOK_XOR;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
479 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
480 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
481 ttype = TOK_XORASS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
482 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
483 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
484 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
485
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
486 case '!':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
487 ttype = TOK_BNOT;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
488 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
489 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
490 ttype = TOK_NE;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
491 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
492 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
493 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
494
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
495 case '*':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
496 ttype = TOK_STAR;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
497 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
498 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
499 ttype = TOK_MULASS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
500 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
501 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
502 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
503
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
504 case '/':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
505 ttype = TOK_DIV;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
506 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
507 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
508 ttype = TOK_DIVASS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
509 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
510 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
511 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
512
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
513 case '=':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
514 ttype = TOK_ASS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
515 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
516 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
517 ttype = TOK_EQ;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
518 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
519 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
520 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
521
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
522 case '%':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
523 ttype = TOK_MOD;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
524 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
525 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
526 ttype = TOK_MODASS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
527 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
528 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
529 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
530
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
531 case '-':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
532 ttype = TOK_SUB;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
533 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
534 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
535 ttype = TOK_SUBASS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
536 else if (c == '-')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
537 ttype = TOK_DBLSUB;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
538 else if (c == '>')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
539 ttype = TOK_ARROW;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
540 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
541 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
542 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
543
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
544 case '+':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
545 ttype = TOK_ADD;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
546 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
547 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
548 ttype = TOK_ADDASS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
549 else if (c == '+')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
550 ttype = TOK_DBLADD;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
551 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
552 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
553 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
554
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
555
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
556 case '&':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
557 ttype = TOK_BWAND;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
558 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
559 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
560 ttype = TOK_BWANDASS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
561 else if (c == '&')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
562 ttype = TOK_BAND;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
563 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
564 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
565 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
566
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
567 case '|':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
568 ttype = TOK_BWOR;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
569 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
570 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
571 ttype = TOK_BWORASS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
572 else if (c == '|')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
573 ttype = TOK_BOR;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
574 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
575 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
576 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
577
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
578 case '<':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
579 ttype = TOK_LT;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
580 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
581 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
582 ttype = TOK_LE;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
583 else if (c == '<')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
584 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
585 ttype = TOK_LSH;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
586 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
587 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
588 ttype = TOK_LSHASS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
589 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
590 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
591 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
592 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
593 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
594 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
595
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
596
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
597 case '>':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
598 ttype = TOK_GT;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
599 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
600 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
601 ttype = TOK_GE;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
602 else if (c == '>')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
603 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
604 ttype = TOK_RSH;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
605 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
606 if (c == '=')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
607 ttype = TOK_RSHASS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
608 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
609 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
610 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
611 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
612 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
613 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
614
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
615 case '\'':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
616 /* character constant - turns into a uint */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
617 chrlit:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
618 cl = 0;
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
619 strbuf = lw_strbuf_new();
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
620 for (;;)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
621 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
622 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
623 if (c == CPP_EOF || c == CPP_EOL || c == '\'')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
624 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
625 cl++;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
626 if (c == '\\')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
627 {
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
628 lw_strbuf_add(strbuf, '\\');
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
629 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
630 if (c == CPP_EOF || c == CPP_EOL)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
631 {
298
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
632 if (!pp -> lexstr)
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
633 preproc_throw_error(pp, "Invalid character constant");
299
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
634 ttype = TOK_ERROR;
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
635 strval = lw_strbuf_end(strbuf);
299
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
636 goto out;
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
637 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
638 cl++;
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
639 lw_strbuf_add(strbuf, c);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
640 continue;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
641 }
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
642 lw_strbuf_add(strbuf, c);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
643 }
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
644 strval = lw_strbuf_end(strbuf);
299
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
645 if (cl == 0)
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
646 {
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
647 ttype = TOK_ERROR;
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
648 if (!pp -> lexstr)
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
649 preproc_throw_error(pp, "Invalid character constant");
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
650 }
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
651 else
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
652 ttype = TOK_CHR_LIT;
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
653 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
654
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
655 case '"':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
656 strlit:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
657 /* string literal */
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
658 strbuf = lw_strbuf_new();
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
659 lw_strbuf_add(strbuf, '"');
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
660 for (;;)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
661 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
662 c = preproc_lex_fetch_byte(pp);
299
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
663 if (c == CPP_EOF || c == CPP_EOL)
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
664 {
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
665 ttype = TOK_ERROR;
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
666 strval = lw_strbuf_end(strbuf);
299
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
667 if (!pp -> lexstr)
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
668 preproc_throw_error(pp, "Invalid string constant");
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
669 goto out;
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
670 }
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
671 if (c == '"')
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
672 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
673 if (c == '\\')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
674 {
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
675 lw_strbuf_add(strbuf, '\\');
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
676 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
677 if (c == CPP_EOF || c == CPP_EOL)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
678 {
299
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
679 ttype = TOK_ERROR;
298
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
680 if (!pp -> lexstr)
6112c67728ba Add stringification and token concatenation
William Astle <lost@l-w.ca>
parents: 296
diff changeset
681 preproc_throw_error(pp, "Invalid string constant");
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
682 strval = lw_strbuf_end(strbuf);
299
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 298
diff changeset
683 goto out;
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
684 }
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
685 lw_strbuf_add(strbuf, c);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
686 continue;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
687 }
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
688 lw_strbuf_add(strbuf, c);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
689 }
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
690 lw_strbuf_add(strbuf, '"');
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
691 strval = lw_strbuf_end(strbuf);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
692 ttype = TOK_STR_LIT;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
693 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
694
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
695 case 'L':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
696 /* check for wide string or wide char const */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
697 c2 = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
698 if (c2 == '\'')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
699 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
700 goto chrlit;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
701 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
702 else if (c2 == '"')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
703 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
704 goto strlit;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
705 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
706 preproc_lex_unfetch_byte(pp, c2);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
707 /* fall through for identifier */
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
708 case '_':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
709 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
710 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
711 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
712 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
713 case 'y': case 'z':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
714 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
715 case 'G': case 'H': case 'I': case 'J': case 'K':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
716 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
717 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
718 case 'Y': case 'Z':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
719 /* we have an identifier here */
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
720 strbuf = lw_strbuf_new();
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
721 lw_strbuf_add(strbuf, c);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
722 for (;;)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
723 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
724 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
725 if ((c == '_') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
726 {
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
727 lw_strbuf_add(strbuf, c);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
728 continue;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
729 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
730 else
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
731 {
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
732 lw_strbuf_add(strbuf, 0);
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
733 strval = lw_strbuf_end(strbuf);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
734 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
735 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
736 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
737 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
738 ttype = TOK_IDENT;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
739 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
740
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
741 case '.':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
742 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
743 if (c >= '0' && c <= '9')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
744 {
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
745 strbuf = lw_strbuf_new();
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
746 lw_strbuf_add(strbuf, '.');
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
747 goto numlit;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
748 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
749 else if (c == '.')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
750 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
751 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
752 if (c == '.')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
753 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
754 ttype = TOK_ELLIPSIS;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
755 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
756 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
757 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
758 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
759 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
760 ttype = TOK_DOT;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
761 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
762
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
763 case '0': case '1': case '2': case '3': case '4':
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
764 case '5': case '6': case '7': case '8': case '9':
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
765 strbuf = lw_strbuf_new();
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
766 numlit:
296
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents: 295
diff changeset
767 ttype = TOK_NUMBER;
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
768 lw_strbuf_add(strbuf, c);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
769 for (;;)
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
770 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
771 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
772 if (!((c == '_') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')))
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
773 break;
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
774 lw_strbuf_add(strbuf, c);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
775 if (c == 'e' || c == 'E' || c == 'p' || c == 'P')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
776 {
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
777 c = preproc_lex_fetch_byte(pp);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
778 if (c == '+' || c == '-')
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
779 {
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
780 lw_strbuf_add(strbuf, c);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
781 continue;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
782 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
783 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
784 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
785 }
308
670ea8f90212 Converted preproc logic to library and moved some utility stuff to lwlib
William Astle <lost@l-w.ca>
parents: 306
diff changeset
786 strval = lw_strbuf_end(strbuf);
295
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
787 preproc_lex_unfetch_byte(pp, c);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
788 goto out;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
789
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
790 default:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
791 ttype = TOK_CHAR;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
792 strval = lw_alloc(2);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
793 strval[0] = c;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
794 strval[1] = 0;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
795 break;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
796 }
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
797 out:
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
798 t = token_create(ttype, strval, sline, scol, pp -> fn);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
799 lw_free(strval);
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
800 return t;
4b17780f2777 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
801 }