]> git.cameronkatri.com Git - mandoc.git/blob - macro.c
68a8cc920fe9c190eca3529e474d028fcf4de935
[mandoc.git] / macro.c
1 /* $Id: macro.c,v 1.2 2008/12/15 01:54:58 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23
24 #include "private.h"
25
26 #define _C(p) ((const char **)p)
27
28 static int isdelim(const char *);
29 static int args_next(struct mdoc *, int, int *, char *, char **);
30 static int append_text(struct mdoc *, int, int, int, char *[]);
31 static int append_scoped(struct mdoc *, int, int, int, char *[]);
32
33
34 static int
35 isdelim(const char *p)
36 {
37
38 if (0 == *p)
39 return(0);
40 if (0 != *(p + 1))
41 return(0);
42
43 switch (*p) {
44 case('{'):
45 /* FALLTHROUGH */
46 case('.'):
47 /* FALLTHROUGH */
48 case(','):
49 /* FALLTHROUGH */
50 case(';'):
51 /* FALLTHROUGH */
52 case(':'):
53 /* FALLTHROUGH */
54 case('?'):
55 /* FALLTHROUGH */
56 case('!'):
57 /* FALLTHROUGH */
58 case('('):
59 /* FALLTHROUGH */
60 case(')'):
61 /* FALLTHROUGH */
62 case('['):
63 /* FALLTHROUGH */
64 case(']'):
65 /* FALLTHROUGH */
66 case('}'):
67 return(1);
68 default:
69 break;
70 }
71
72 return(0);
73 }
74
75
76 static int
77 args_next(struct mdoc *mdoc, int tok,
78 int *pos, char *buf, char **v)
79 {
80
81 if (0 == buf[*pos])
82 return(0);
83
84 assert( ! isspace(buf[*pos]));
85
86 if ('\"' == buf[*pos]) {
87 (void)mdoc_err(mdoc, tok, *pos, ERR_SYNTAX_QUOTE);
88 return(-1);
89 }
90
91 *v = &buf[*pos];
92
93 /* Scan ahead to end of token. */
94
95 while (buf[*pos] && ! isspace(buf[*pos]))
96 (*pos)++;
97
98 if (buf[*pos] && buf[*pos + 1] && '\\' == buf[*pos]) {
99 (void)mdoc_err(mdoc, tok, *pos, ERR_SYNTAX_WS);
100 return(-1);
101 }
102
103 if (0 == buf[*pos])
104 return(1);
105
106 /* Scan ahead over trailing whitespace. */
107
108 buf[(*pos)++] = 0;
109 while (buf[*pos] && isspace(buf[*pos]))
110 (*pos)++;
111
112 if (0 == buf[*pos])
113 if ( ! mdoc_warn(mdoc, tok, *pos, WARN_SYNTAX_WS_EOLN))
114 return(-1);
115
116 return(1);
117 }
118
119
120 static int
121 append_scoped(struct mdoc *mdoc, int tok, int pos, int sz, char *args[])
122 {
123
124 args[sz] = NULL;
125 mdoc_block_alloc(mdoc, pos, tok, 0, NULL);
126 mdoc_head_alloc(mdoc, pos, tok, sz, _C(args));
127 mdoc_body_alloc(mdoc, pos, tok);
128 return(1);
129 }
130
131
132 static int
133 append_text(struct mdoc *mdoc, int tok, int pos, int sz, char *args[])
134 {
135
136 args[sz] = NULL;
137
138 switch (tok) {
139 /* ======= ADD MORE MACRO ARGUMENT-LIMITS BELOW. ======= */
140
141 case (MDOC_Ft):
142 /* FALLTHROUGH */
143 case (MDOC_Li):
144 /* FALLTHROUGH */
145 case (MDOC_Ms):
146 /* FALLTHROUGH */
147 case (MDOC_Pa):
148 /* FALLTHROUGH */
149 case (MDOC_Tn):
150 if (0 == sz && ! mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1))
151 return(0);
152 mdoc_elem_alloc(mdoc, pos, tok, 0, NULL, sz, _C(args));
153 return(1);
154
155 case (MDOC_Ar):
156 /* FALLTHROUGH */
157 case (MDOC_Cm):
158 /* FALLTHROUGH */
159 case (MDOC_Fl):
160 mdoc_elem_alloc(mdoc, pos, tok, 0, NULL, sz, _C(args));
161 return(1);
162
163 case (MDOC_Ad):
164 /* FALLTHROUGH */
165 case (MDOC_Em):
166 /* FALLTHROUGH */
167 case (MDOC_Er):
168 /* FALLTHROUGH */
169 case (MDOC_Ev):
170 /* FALLTHROUGH */
171 case (MDOC_Fa):
172 /* FALLTHROUGH */
173 case (MDOC_Dv):
174 /* FALLTHROUGH */
175 case (MDOC_Ic):
176 /* FALLTHROUGH */
177 case (MDOC_Va):
178 /* FALLTHROUGH */
179 case (MDOC_Vt):
180 if (0 == sz)
181 return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1));
182 mdoc_elem_alloc(mdoc, pos, tok, 0, NULL, sz, _C(args));
183 return(1);
184
185 /* ======= ADD MORE MACRO ARGUMENT-LIMITS ABOVE. ======= */
186 default:
187 break;
188 }
189
190 abort();
191 /* NOTREACHED */
192 }
193
194
195 int
196 macro_text(struct mdoc *mdoc, int tok, int ppos, int *pos, char *buf)
197 {
198 int lastarg, j, c, lasttok, lastpunct;
199 char *args[MDOC_LINEARG_MAX], *p;
200
201 lasttok = ppos;
202 lastpunct = 0;
203 j = 0;
204
205 again:
206
207 lastarg = *pos;
208 c = args_next(mdoc, tok, pos, buf, &args[j]);
209
210 if (-1 == c)
211 return(0);
212 if (0 == c && ! lastpunct)
213 return(append_text(mdoc, tok, lasttok, j, args));
214 else if (0 == c)
215 return(1);
216
217 /* Command found. */
218
219 if (MDOC_MAX != (c = mdoc_find(mdoc, args[j]))) {
220 if ( ! lastpunct)
221 if ( ! append_text(mdoc, tok, lasttok, j, args))
222 return(0);
223 return(mdoc_macro(mdoc, c, lastarg, pos, buf));
224 }
225
226 /* Word found. */
227
228 if ( ! isdelim(args[j])) {
229 j++;
230 goto again;
231 }
232
233 /* Punctuation found. */
234
235 p = args[j]; /* Save argument (NULL-ified in append). */
236
237 if ( ! lastpunct)
238 if ( ! append_text(mdoc, tok, lasttok, j, args))
239 return(0);
240
241 args[j] = p;
242
243 mdoc_word_alloc(mdoc, lastarg, args[j]);
244 lastpunct = 1;
245 j = 0;
246
247 goto again;
248
249 /* NOTREACHED */
250 }
251
252
253 int
254 macro_scoped_implicit(struct mdoc *mdoc,
255 int tok, int ppos, int *pos, char *buf)
256 {
257 int j, c, lastarg, t;
258 char *args[MDOC_LINEARG_MAX];
259 struct mdoc_node *n;
260
261 /*
262 * Look for an implicit parent.
263 */
264
265 assert( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags));
266
267 for (n = mdoc->last; n; n = n->parent) {
268 if (MDOC_BLOCK != n->type)
269 continue;
270 if (tok == (t = n->data.block.tok))
271 break;
272 if ( ! (MDOC_EXPLICIT & mdoc_macros[t].flags))
273 continue;
274 return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_BREAK));
275 }
276
277 if (n) {
278 mdoc->last = n;
279 mdoc_msg(mdoc, ppos, "scope: rewound `%s'",
280 mdoc_macronames[tok]);
281 } else
282 mdoc_msg(mdoc, ppos, "scope: new `%s'",
283 mdoc_macronames[tok]);
284
285 j = 0;
286
287 again:
288
289 lastarg = *pos;
290 c = args_next(mdoc, tok, pos, buf, &args[j]);
291
292 if (-1 == c)
293 return(0);
294 if (0 == c)
295 return(append_scoped(mdoc, tok, ppos, j, args));
296
297 /* Command found. */
298
299 if (MDOC_MAX != (c = mdoc_find(mdoc, args[j])))
300 if ( ! mdoc_warn(mdoc, tok, *pos, WARN_SYNTAX_MACLIKE))
301 return(0);
302
303 /* Word found. */
304
305 j++;
306 goto again;
307
308 /* NOTREACHED */
309 }