annotate directive.c @ 17:76da41da923f

added macro table
author David A. Holland
date Mon, 20 Dec 2010 01:15:43 -0500
parents 9dda765ee85c
children c08a947d8f30
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
1 #include <assert.h>
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
2 #include <stdbool.h>
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
3 #include <stdlib.h>
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
4 #include <string.h>
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
5
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
6 #include "utils.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
7 #include "mode.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
8 #include "place.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
9 #include "files.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
10 #include "directive.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
11 #include "macro.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
12 #include "eval.h"
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
13
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
14 struct ifstate {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
15 struct ifstate *prev;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
16 struct place startplace;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
17 bool curtrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
18 bool evertrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
19 bool seenelse;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
20 };
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
21
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
22 static struct ifstate *ifstate;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
23
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
24 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
25 // common parsing bits
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
26
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
27 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
28 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
29 oneword(const char *what, struct place *p2, char *line)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
30 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
31 size_t pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
32
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
33 pos = strcspn(line, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
34 if (line[pos] != '\0') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
35 p2->column += pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
36 complain(p2, "Garbage after %s argument", what);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
37 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
38 line[pos] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
39 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
40 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
41
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
42 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
43 // if handling
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
44
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
45 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
46 struct ifstate *
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
47 ifstate_create(struct ifstate *prev, struct place *p, bool startstate)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
48 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
49 struct ifstate *is;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
50
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
51 is = domalloc(sizeof(*is));
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
52 is->prev = prev;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
53 if (p != NULL) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
54 is->startplace = *p;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
55 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
56 place_setbuiltin(&is->startplace, 1);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
57 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
58 is->curtrue = startstate;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
59 is->evertrue = is->curtrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
60 is->seenelse = false;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
61 return is;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
62 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
63
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
64 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
65 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
66 ifstate_destroy(struct ifstate *is)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
67 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
68 free(is);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
69 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
70
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
71 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
72 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
73 ifstate_push(struct place *p, bool startstate)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
74 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
75 ifstate = ifstate_create(ifstate, p, startstate);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
76 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
77
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
78 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
79 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
80 ifstate_pop(void)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
81 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
82 struct ifstate *is;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
83
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
84 is = ifstate;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
85 ifstate = ifstate->prev;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
86 ifstate_destroy(is);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
87 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
88
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
89 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
90 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
91 d_if(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
92 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
93 char *expr;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
94 bool val;
16
9dda765ee85c expression evaluator
David A. Holland
parents: 15
diff changeset
95 struct place p3 = *p2;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
96
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
97 expr = macroexpand(p2, line, len, true);
16
9dda765ee85c expression evaluator
David A. Holland
parents: 15
diff changeset
98 val = eval(&p3, expr);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
99 ifstate_push(p, val);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
100 free(expr);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
101 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
102
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
103 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
104 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
105 d_ifdef(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
106 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
107 oneword("#ifdef", p2, line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
108 ifstate_push(p, macro_isdefined(line));
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
109 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
110
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
111 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
112 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
113 d_ifndef(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
114 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
115 oneword("#ifndef", p2, line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
116 ifstate_push(p, !macro_isdefined(line));
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
117 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
118
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
119 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
120 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
121 d_elif(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
122 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
123 char *expr;
16
9dda765ee85c expression evaluator
David A. Holland
parents: 15
diff changeset
124 struct place p3 = *p2;
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
125
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
126 if (ifstate->seenelse) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
127 complain(p, "#elif after #else");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
128 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
129 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
130
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
131 if (ifstate->evertrue) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
132 ifstate->curtrue = false;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
133 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
134 expr = macroexpand(p2, line, len, true);
16
9dda765ee85c expression evaluator
David A. Holland
parents: 15
diff changeset
135 ifstate->curtrue = eval(&p3, expr);
15
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
136 ifstate->evertrue = ifstate->curtrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
137 free(expr);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
138 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
139 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
140
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
141 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
142 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
143 d_else(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
144 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
145 if (ifstate->seenelse) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
146 complain(p, "Multiple #else directives in one conditional");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
147 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
148 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
149
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
150 ifstate->curtrue = !ifstate->evertrue;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
151 ifstate->evertrue = true;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
152 ifstate->seenelse = true;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
153 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
154
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
155 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
156 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
157 d_endif(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
158 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
159 if (ifstate->prev == NULL) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
160 complain(p, "Unmatched #endif");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
161 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
162 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
163 ifstate_pop();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
164 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
165 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
166
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
167 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
168 // macros
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
169
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
170 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
171 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
172 d_define(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
173 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
174 size_t pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
175 struct place p3;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
176
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
177 /*
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
178 * line may be:
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
179 * macro expansion
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
180 * macro(arg, arg, ...) expansion
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
181 */
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
182
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
183 pos = strcspn(line, " \t\f\v(");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
184 if (line[pos] == '(') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
185 pos++;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
186 pos = pos + strcspn(line+pos, "()");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
187 if (line[pos] == '(') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
188 p2->column += pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
189 complain(p2, "Left parenthesis in macro parameters");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
190 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
191 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
192 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
193 if (line[pos] != ')') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
194 p2->column += pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
195 complain(p2, "Unclosed macro parameter list");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
196 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
197 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
198 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
199 pos++;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
200 if (!strchr(ws, line[pos])) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
201 p2->column += pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
202 complain(p2, "Trash after macro parameter list");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
203 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
204 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
205 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
206 line[pos++] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
207 } else if (line[pos] == '\0') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
208 /* nothing */
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
209 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
210 line[pos++] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
211 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
212
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
213 pos += strspn(line+pos, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
214
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
215 p3 = *p2;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
216 p3.column += pos;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
217 macro_define(p2, line, &p3, line + pos);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
218 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
219
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
220 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
221 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
222 d_undef(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
223 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
224 oneword("#undef", p2, line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
225 macro_undef(line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
226 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
227
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
228 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
229 // includes
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
230
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
231 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
232 bool
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
233 tryinclude(struct place *p, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
234 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
235 if (len > 2 && line[0] == '"' && line[len-1] == '"') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
236 line[len-1] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
237 file_readquote(p, line+1);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
238 return true;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
239 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
240 if (len > 2 && line[0] == '<' && line[len-1] == '>') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
241 line[len-1] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
242 file_readbracket(p, line+1);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
243 return true;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
244 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
245 return false;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
246 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
247
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
248 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
249 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
250 d_include(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
251 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
252 char *text;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
253
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
254 if (tryinclude(p, line, len)) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
255 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
256 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
257 text = macroexpand(p2, line, len, false);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
258 if (tryinclude(p, text, strlen(text))) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
259 free(text);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
260 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
261 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
262 free(text);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
263 complain(p, "Illegal #include directive");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
264 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
265 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
266
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
267 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
268 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
269 d_line(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
270 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
271 /* XXX */
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
272 complain(p, "Sorry, no #line yet");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
273 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
274
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
275 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
276 // messages
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
277
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
278 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
279 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
280 d_warning(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
281 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
282 char *msg;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
283
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
284 msg = macroexpand(p2, line, len, false);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
285 complain(p, "#warning: %s", msg);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
286 if (mode.werror) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
287 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
288 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
289 free(msg);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
290 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
291
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
292 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
293 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
294 d_error(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
295 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
296 char *msg;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
297
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
298 msg = macroexpand(p2, line, len, false);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
299 complain(p, "#error: %s", msg);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
300 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
301 free(msg);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
302 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
303
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
304 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
305 // other
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
306
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
307 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
308 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
309 d_pragma(struct place *p, struct place *p2, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
310 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
311 complain(p, "#pragma %s", line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
312 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
313 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
314
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
315 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
316 // directive table
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
317
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
318 static const struct {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
319 const char *name;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
320 bool ifskip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
321 void (*func)(struct place *, struct place *, char *line, size_t len);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
322 } directives[] = {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
323 { "define", true, d_define },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
324 { "elif", false, d_elif },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
325 { "else", false, d_else },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
326 { "endif", false, d_endif },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
327 { "error", true, d_error },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
328 { "if", false, d_if },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
329 { "ifdef", false, d_ifdef },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
330 { "ifndef", false, d_ifndef },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
331 { "include", true, d_include },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
332 { "line", true, d_line },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
333 { "pragma", true, d_pragma },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
334 { "undef", true, d_undef },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
335 { "warning", true, d_warning },
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
336 };
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
337 static const unsigned numdirectives = HOWMANY(directives);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
338
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
339 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
340 size_t
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
341 notrailingws(char *buf, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
342 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
343 while (len > 0 && strchr(ws, buf[len-1])) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
344 buf[--len] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
345 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
346 return len;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
347 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
348
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
349 static
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
350 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
351 directive_gotdirective(struct place *p, char *line, size_t linelen)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
352 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
353 struct place p2;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
354 size_t len, skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
355 unsigned i;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
356
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
357 p2 = *p;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
358 for (i=0; i<numdirectives; i++) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
359 len = strlen(directives[i].name);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
360 if (!strncmp(line, directives[i].name, len) &&
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
361 strchr(ws, line[len])) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
362 if (directives[i].ifskip && !ifstate->curtrue) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
363 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
364 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
365 skip = len + strspn(line+len, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
366 p2.column += skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
367 line += skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
368 linelen -= skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
369 linelen = notrailingws(line, linelen);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
370 directives[i].func(p, &p2, line, linelen);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
371 return;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
372 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
373 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
374 skip = strcspn(line, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
375 complain(p, "Unknown directive #%.*s", (int)skip, line);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
376 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
377 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
378
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
379 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
380 directive_gotline(struct place *p, char *line, size_t len)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
381 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
382 size_t skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
383
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
384 /* check if we have a directive line */
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
385 skip = strspn(line, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
386 if (line[skip] == '#') {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
387 skip = skip + 1 + strspn(line + skip + 1, ws);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
388 p->column += skip;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
389 directive_gotdirective(p, line+skip, len-skip);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
390 } else if (ifstate->curtrue) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
391 macro_sendline(p, line, len);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
392 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
393 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
394
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
395
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
396 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
397 directive_goteof(struct place *p)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
398 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
399 while (ifstate->prev != NULL) {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
400 complain(p, "Missing #endif");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
401 complain(&ifstate->startplace, "...opened at this point");
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
402 complain_failed();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
403 ifstate_pop();
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
404 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
405 macro_sendeof(p);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
406 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
407
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
408 ////////////////////////////////////////////////////////////
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
409 // module initialization
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
410
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
411 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
412 directive_init(void)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
413 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
414 ifstate = ifstate_create(NULL, NULL, true);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
415 }
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
416
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
417 void
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
418 directive_cleanup(void)
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
419 {
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
420 assert(ifstate->prev == NULL);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
421 ifstate_destroy(ifstate);
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
422 ifstate = NULL;
f6177d3ed5c2 handle directives
David A. Holland
parents:
diff changeset
423 }