chiark / gitweb /
Expansion syntax rewrite. Not documented yet, but then nor was the
[disorder] / lib / macros.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21/** @file lib/macros.c
22 * @brief Macro expansion
23 */
24
25#include <config.h>
26#include "types.h"
27
28#include <string.h>
29#include <ctype.h>
30#include <assert.h>
31#include <stdio.h>
32#include <sys/stat.h>
33#include <fcntl.h>
34#include <unistd.h>
35#include <errno.h>
36
37#include "hash.h"
38#include "macros.h"
39#include "mem.h"
40#include "vector.h"
41#include "log.h"
42#include "sink.h"
43#include "syscalls.h"
44#include "printf.h"
45
46VECTOR_TYPE(mx_node_vector, const struct mx_node *, xrealloc);
47
48/** @brief Definition of an expansion */
49struct expansion {
50 /** @brief Minimum permitted arguments */
51 int min;
52
53 /** @brief Maximum permitted arguments */
54 int max;
55
56 /** @brief Flags
57 *
58 * See:
59 * - @ref EXP_SIMPLE
60 * - @ref EXP_MAGIC
61 * - @ref EXP_MACRO
62 * - @ref EXP_TYPE_MASK
63 */
64 unsigned flags;
65
66 /** @brief Macro argument names */
67 char **args;
68
69 /** @brief Callback (cast to appropriate type)
70 *
71 * Cast to @ref mx_simple_callback or @ref mx_magic_callback as required. */
72 void (*callback)();
73
74 /** @brief Macro definition
75 *
76 * Only for @ref EXP_MACRO expansions. */
77 const struct mx_node *definition;
78};
79
80/** @brief Expansion takes pre-expanded strings
81 *
82 * @p callback is cast to @ref mx_simple_callback. */
83#define EXP_SIMPLE 0x0000
84
85/** @brief Expansion takes parsed templates, not strings
86 *
87 * @p callback is cast to @ref mx_magic_callback. The callback must do its own
88 * expansion e.g. via mx_expandstr() where necessary. */
89#define EXP_MAGIC 0x0001
90
91/** @brief Expansion is a macro */
92#define EXP_MACRO 0x0002
93
94/** @brief Mask of types */
95#define EXP_TYPE_MASK 0x0003
96
97/** @brief Hash of all expansions
98 *
99 * Created by mx_register(), mx_register_macro() or mx_register_magic().
100 */
101static hash *expansions;
102
103static int mx__expand_macro(const struct expansion *e,
104 const struct mx_node *m,
105 struct sink *output,
106 void *u);
107
108/* Parsing ------------------------------------------------------------------ */
109
110static int next_non_whitespace(const char *input,
111 const char *end) {
112 while(input < end && isspace((unsigned char)*input))
113 ++input;
114 return input < end ? *input : -1;
115}
116
117/** @brief Parse a template
118 * @param filename Input filename (for diagnostics)
119 * @param line Line number (use 1 on initial call)
120 * @param input Start of text to parse
121 * @param end End of text to parse or NULL
122 * @return Pointer to parse tree root node
123 *
124 * Parses the text in [start, end) and returns an (immutable) parse
125 * tree representing it.
126 *
127 * If @p end is NULL then the whole string is parsed.
128 *
129 * Note that the @p filename value stored in the parse tree is @p filename,
130 * i.e. it is not copied.
131 */
132const struct mx_node *mx_parse(const char *filename,
133 int line,
134 const char *input,
135 const char *end) {
136 int braces, argument_start_line, obracket, cbracket;
137 const char *argument_start, *argument_end;
138 struct mx_node_vector v[1];
139 struct dynstr d[1];
140 struct mx_node *head = 0, **tailp = &head, *e;
141
142 if(!end)
143 end = input + strlen(input);
144 while(input < end) {
145 if(*input != '@') {
146 e = xmalloc(sizeof *e);
147 e->next = 0;
148 e->filename = filename;
149 e->line = line;
150 e->type = MX_TEXT;
151 dynstr_init(d);
152 /* Gather up text without any expansions in. */
153 while(input < end && *input != '@') {
154 if(*input == '\n')
155 ++line;
156 dynstr_append(d, *input++);
157 }
158 dynstr_terminate(d);
159 e->text = d->vec;
160 *tailp = e;
161 tailp = &e->next;
162 continue;
163 }
164 if(input + 1 < end)
165 switch(input[1]) {
166 case '@':
167 /* '@@' expands to '@' */
168 e = xmalloc(sizeof *e);
169 e->next = 0;
170 e->filename = filename;
171 e->line = line;
172 e->type = MX_TEXT;
173 e->text = "@";
174 *tailp = e;
175 tailp = &e->next;
176 input += 2;
177 continue;
178 case '#':
179 /* '@#' starts a (newline-eating comment), like dnl */
180 input += 2;
181 while(input < end && *input != '\n')
182 ++input;
183 if(*input == '\n') {
184 ++line;
185 ++input;
186 }
187 continue;
188 case '_':
189 /* '@_' expands to nothing. It's there to allow dump to terminate
190 * expansions without having to know what follows. */
191 input += 2;
192 continue;
193 }
194 /* It's a full expansion */
195 ++input;
196 e = xmalloc(sizeof *e);
197 e->next = 0;
198 e->filename = filename;
199 e->line = line;
200 e->type = MX_EXPANSION;
201 /* Collect the expansion name. Expansion names start with an alnum and
202 * consist of alnums and '-'. We don't permit whitespace between the '@'
203 * and the name. */
204 dynstr_init(d);
205 if(input == end || !isalnum((unsigned char)*input))
206 fatal(0, "%s:%d: invalid expansion", filename, e->line);
207 while(input < end && (isalnum((unsigned char)*input) || *input == '-'))
208 dynstr_append(d, *input++);
209 dynstr_terminate(d);
210 e->name = d->vec;
211 /* See what the bracket character is */
212 obracket = next_non_whitespace(input, end);
213 switch(obracket) {
214 case '(': cbracket = ')'; break;
215 case '[': cbracket = ']'; break;
216 case '{': cbracket = '}'; break;
217 default: obracket = -1; break; /* no arguments */
218 }
219 mx_node_vector_init(v);
220 if(obracket >= 0) {
221 /* Gather up arguments */
222 while(next_non_whitespace(input, end) == obracket) {
223 while(isspace((unsigned char)*input)) {
224 if(*input == '\n')
225 ++line;
226 ++input;
227 }
228 ++input; /* the bracket */
229 braces = 0;
230 /* Find the end of the argument */
231 argument_start = input;
232 argument_start_line = line;
233 while(input < end && (*input != cbracket || braces > 0)) {
234 const int c = *input++;
235
236 if(c == obracket)
237 ++braces;
238 else if(c == cbracket)
239 --braces;
240 else if(c == '\n')
241 ++line;
242 }
243 if(input >= end) {
244 /* We ran out of input without encountering a balanced cbracket */
245 fatal(0, "%s:%d: unterminated expansion argument '%.*s'",
246 filename, argument_start_line,
247 (int)(input - argument_start), argument_start);
248 }
249 /* Consistency check */
250 assert(*input == cbracket);
251 /* Record the end of the argument */
252 argument_end = input;
253 /* Step over the cbracket */
254 ++input;
255 /* Now we have an argument in [argument_start, argument_end), and we
256 * know its filename and initial line number. This is sufficient to
257 * parse it. */
258 mx_node_vector_append(v, mx_parse(filename, argument_start_line,
259 argument_start, argument_end));
260 }
261 }
262 /* Guarantee a NULL terminator (for the case where there's more than one
263 * argument) */
264 mx_node_vector_terminate(v);
265 /* Fill in the remains of the node */
266 e->nargs = v->nvec;
267 e->args = v->vec;
268 *tailp = e;
269 tailp = &e->next;
270 }
271 return head;
272}
273
274static void mx__dump(struct dynstr *d, const struct mx_node *m) {
275 int n;
276 const struct mx_node *mm;
277
278 if(!m)
279 return;
280 switch(m->type) {
281 case MX_TEXT:
282 if(m->text[0] == '@')
283 dynstr_append(d, '@');
284 dynstr_append_string(d, m->text);
285 break;
286 case MX_EXPANSION:
287 dynstr_append(d, '@');
288 dynstr_append_string(d, m->name);
289 for(n = 0; n < m->nargs; ++n) {
290 dynstr_append(d, '{');
291 mx__dump(d, m->args[n]);
292 dynstr_append(d, '}');
293 }
294 /* If the next non-whitespace is '{', add @_ to stop it being
295 * misinterpreted */
296 mm = m->next;
297 while(mm && mm->type == MX_TEXT) {
298 switch(next_non_whitespace(mm->text, mm->text + strlen(mm->text))) {
299 case -1:
300 mm = mm->next;
301 continue;
302 case '{':
303 dynstr_append_string(d, "@_");
304 break;
305 default:
306 break;
307 }
308 break;
309 }
310 break;
311 default:
312 assert(!"invalid m->type");
313 }
314 mx__dump(d, m->next);
315}
316
317/** @brief Dump a parse macro expansion to a string
318 *
319 * Not of production quality! Only intended for testing!
320 */
321char *mx_dump(const struct mx_node *m) {
322 struct dynstr d[1];
323
324 dynstr_init(d);
325 mx__dump(d, m);
326 dynstr_terminate(d);
327 return d->vec;
328}
329
330/* Expansion registration --------------------------------------------------- */
331
332static int mx__register(unsigned flags,
333 const char *name,
334 int min,
335 int max,
336 char **args,
337 void (*callback)(),
338 const struct mx_node *definition) {
339 struct expansion e[1];
340
341 if(!expansions)
342 expansions = hash_new(sizeof(struct expansion));
343 e->min = min;
344 e->max = max;
345 e->flags = flags;
346 e->args = args;
347 e->callback = callback;
348 e->definition = definition;
349 return hash_add(expansions, name, &e,
350 ((flags & EXP_TYPE_MASK) == EXP_MACRO)
351 ? HASH_INSERT : HASH_INSERT_OR_REPLACE);
352}
353
354/** @brief Register a simple expansion rule
355 * @param name Name
356 * @param min Minimum number of arguments
357 * @param max Maximum number of arguments
358 * @param callback Callback to write output
359 */
360void mx_register(const char *name,
361 int min,
362 int max,
363 mx_simple_callback *callback) {
364 mx__register(EXP_SIMPLE, name, min, max, 0, (void (*)())callback, 0);
365}
366
367/** @brief Register a magic expansion rule
368 * @param name Name
369 * @param min Minimum number of arguments
370 * @param max Maximum number of arguments
371 * @param callback Callback to write output
372 */
373void mx_register_magic(const char *name,
374 int min,
375 int max,
376 mx_magic_callback *callback) {
377 mx__register(EXP_MAGIC, name, min, max, 0, (void (*)())callback, 0);
378}
379
380/** @brief Register a macro
381 * @param name Name
382 * @param nargs Number of arguments
383 * @param args Argument names
384 * @param definition Macro definition
385 * @return 0 on success, negative on error
386 */
387int mx_register_macro(const char *name,
388 int nargs,
389 char **args,
390 const struct mx_node *definition) {
391 if(mx__register(EXP_MACRO, name, nargs, nargs, args, 0/*callback*/,
392 definition)) {
393 /* This locates the error to the definition, which may be a line or two
394 * beyond the @define command itself. The backtrace generated by
395 * mx_expand() may help more. */
396 error(0, "%s:%d: duplicate definition of '%s'",
397 definition->filename, definition->line, name);
398 return -2;
399 }
400 return 0;
401}
402
403/* Expansion ---------------------------------------------------------------- */
404
405/** @brief Expand a template
406 * @param m Where to start
407 * @param output Where to send output
408 * @param u User data
409 * @return 0 on success, non-0 on error
410 *
411 * Interpretation of return values:
412 * - 0 means success
413 * - -1 means an error writing to the sink.
414 * - other negative values mean errors generated from with the macro
415 * expansion system
416 * - positive values are reserved for the application
417 *
418 * If any callback returns non-zero then that value is returned, abandoning
419 * further expansion.
420 */
421int mx_expand(const struct mx_node *m,
422 struct sink *output,
423 void *u) {
424 const struct expansion *e;
425 int rc;
426
427 if(!m)
428 return 0;
429 switch(m->type) {
430 case MX_TEXT:
431 if(sink_writes(output, m->text) < 0)
432 return -1;
433 break;
434 case MX_EXPANSION:
435 rc = 0;
436 if(!(e = hash_find(expansions, m->name))) {
437 error(0, "%s:%d: unknown expansion name '%s'",
438 m->filename, m->line, m->name);
439 if(sink_printf(output, "[['%s' unknown]]", m->name) < 0)
440 return -1;
441 } else if(m->nargs < e->min) {
442 error(0, "%s:%d: expansion '%s' requires %d args, only %d given",
443 m->filename, m->line, m->name, e->min, m->nargs);
444 if(sink_printf(output, "[['%s' too few args]]", m->name) < 0)
445 return -1;
446 } else if(m->nargs > e->max) {
447 error(0, "%s:%d: expansion '%s' takes at most %d args, but %d given",
448 m->filename, m->line, m->name, e->max, m->nargs);
449 if(sink_printf(output, "[['%s' too many args]]", m->name) < 0)
450 return -1;
451 } else switch(e->flags & EXP_TYPE_MASK) {
452 case EXP_MAGIC: {
453 /* Magic callbacks we can call directly */
454 rc = ((mx_magic_callback *)e->callback)(m->nargs,
455 m->args,
456 output,
457 u);
458 break;
459 }
460 case EXP_SIMPLE: {
461 /* For simple callbacks we expand their arguments for them. */
462 char **args = xcalloc(1 + m->nargs, sizeof (char *)), *argname;
463 int n;
464
465 for(n = 0; n < m->nargs; ++n) {
466 /* Argument numbers are at least clear from looking at the text;
467 * adding names as well would be nice. TODO */
468 byte_xasprintf(&argname, "argument #%d", n);
469 if((rc = mx_expandstr(m->args[n], &args[n], u, argname)))
470 break;
471 }
472 if(!rc) {
473 args[n] = NULL;
474 rc = ((mx_simple_callback *)e->callback)(m->nargs,
475 args,
476 output,
477 u);
478 }
479 break;
480 }
481 case EXP_MACRO: {
482 /* Macros we expand by rewriting their definition with argument values
483 * substituted and then expanding that. */
484 rc = mx__expand_macro(e, m, output, u);
485 break;
486 }
487 default:
488 assert(!"impossible EXP_TYPE_MASK value");
489 }
490 if(rc) {
491 /* For non-IO errors we generate some backtrace */
492 if(rc != -1)
493 error(0, " ...in @%s at %s:%d",
494 m->name, m->filename, m->line);
495 return rc;
496 }
497 break;
498 default:
499 assert(!"invalid m->type");
500 }
501 return mx_expand(m->next, output, u);
502}
503
504/** @brief Expand a template storing the result in a string
505 * @param m Where to start
506 * @param sp Where to store string
507 * @param u User data
508 * @param what Token for backtrace, or NULL
509 * @return 0 on success, non-0 on error
510 *
511 * Same return conventions as mx_expand(). This wrapper is slightly more
512 * convenient to use from 'magic' expansions.
513 */
514int mx_expandstr(const struct mx_node *m,
515 char **sp,
516 void *u,
517 const char *what) {
518 struct dynstr d[1];
519 int rc;
520
521 dynstr_init(d);
522 if(!(rc = mx_expand(m, sink_dynstr(d), u))) {
523 dynstr_terminate(d);
524 *sp = d->vec;
525 } else
526 *sp = 0;
527 if(rc && rc != -1 && what)
528 error(0, " ...in %s at %s:%d", what, m->filename, m->line);
529 return rc;
530}
531
532/** @brief Expand a template file
533 * @param path Filename
534 * @param output Where to send output
535 * @param u User data
536 * @return 0 on success, non-0 on error
537 *
538 * Same return conventions as mx_expand().
539 */
540int mx_expand_file(const char *path,
541 struct sink *output,
542 void *u) {
543 int fd, n, rc;
544 struct stat sb;
545 char *b;
546 off_t sofar;
547 const struct mx_node *m;
548
549 if((fd = open(path, O_RDONLY)) < 0)
550 fatal(errno, "error opening %s", path);
551 if(fstat(fd, &sb) < 0)
552 fatal(errno, "error statting %s", path);
553 if(!S_ISREG(sb.st_mode))
554 fatal(0, "%s: not a regular file", path);
555 sofar = 0;
556 b = xmalloc_noptr(sb.st_size);
557 while(sofar < sb.st_size) {
558 n = read(fd, b + sofar, sb.st_size - sofar);
559 if(n > 0)
560 sofar += n;
561 else if(n == 0)
562 fatal(0, "unexpected EOF reading %s", path);
563 else if(errno != EINTR)
564 fatal(errno, "error reading %s", path);
565 }
566 xclose(fd);
567 m = mx_parse(path, 1, b, b + sb.st_size);
568 rc = mx_expand(m, output, u);
569 if(rc && rc != -1)
570 /* Mention inclusion in backtrace */
571 error(0, " ...in inclusion of file '%s'", path);
572 return rc;
573}
574
575/* Macros ------------------------------------------------------------------- */
576
577/** @brief Rewrite a parse tree substituting sub-expansions
578 * @param m Parse tree to rewrite (from macro definition)
579 * @param ... Name/value pairs to rewrite
580 * @return Rewritten parse tree
581 *
582 * The name/value pair list consists of pairs of strings and is terminated by
583 * (char *)0. Names and values are both copied so need not survive the call.
584 */
585const struct mx_node *mx_rewritel(const struct mx_node *m,
586 ...) {
587 va_list ap;
588 hash *h = hash_new(sizeof (struct mx_node *));
589 const char *n, *v;
590 struct mx_node *e;
591
592 va_start(ap, m);
593 while((n = va_arg(ap, const char *))) {
594 v = va_arg(ap, const char *);
595 e = xmalloc(sizeof *e);
596 e->next = 0;
597 e->filename = m->filename;
598 e->line = m->line;
599 e->type = MX_TEXT;
600 e->text = xstrdup(v);
601 hash_add(h, n, &e, HASH_INSERT);
602 /* hash_add() copies n */
603 }
604 return mx_rewrite(m, h);
605}
606
607/** @brief Rewrite a parse tree substituting in macro arguments
608 * @param definition Parse tree to rewrite (from macro definition)
609 * @param h Hash mapping argument names to argument values
610 * @return Rewritten parse tree
611 */
612const struct mx_node *mx_rewrite(const struct mx_node *definition,
613 hash *h) {
614 const struct mx_node *head = 0, **tailp = &head, *argvalue, *m, *mm;
615 struct mx_node *nm;
616 int n;
617
618 for(m = definition; m; m = m->next) {
619 switch(m->type) {
620 case MX_TEXT:
621 nm = xmalloc(sizeof *nm);
622 *nm = *m; /* Dumb copy of text node fields */
623 nm->next = 0; /* Maintain list structure */
624 *tailp = nm;
625 tailp = (const struct mx_node **)&nm->next;
626 break;
627 case MX_EXPANSION:
628 if(m->nargs == 0
629 && (argvalue = *(const struct mx_node **)hash_find(h, m->name))) {
630 /* This expansion has no arguments and its name matches one of the
631 * macro arguments. (Even if it's a valid expansion name we override
632 * it.) We insert its value at this point. We do NOT recursively
633 * rewrite the argument's value - it is outside the lexical scope of
634 * the argument name.
635 *
636 * We need to recreate the list structure but a shallow copy will
637 * suffice here.
638 */
639 for(mm = argvalue; mm; mm = mm->next) {
640 nm = xmalloc(sizeof *nm);
641 *nm = *mm;
642 nm->next = 0;
643 *tailp = nm;
644 tailp = (const struct mx_node **)&nm->next;
645 }
646 } else {
647 /* This is some other expansion. We recursively rewrite its argument
648 * values according to h. */
649 nm = xmalloc(sizeof *nm);
650 *nm = *m;
651 nm->args = xcalloc(nm->nargs, sizeof (struct mx_node *));
652 for(n = 0; n < nm->nargs; ++n)
653 nm->args[n] = mx_rewrite(m->args[n], h);
654 nm->next = 0;
655 *tailp = nm;
656 tailp = (const struct mx_node **)&nm->next;
657 }
658 break;
659 default:
660 assert(!"invalid m->type");
661 }
662 }
663 *tailp = 0; /* Mark end of list */
664 return head;
665}
666
667/** @brief Expand a macro
668 * @param e Macro definition
669 * @param m Macro expansion
670 * @param output Where to send output
671 * @param u User data
672 * @return 0 on success, non-0 on error
673 */
674static int mx__expand_macro(const struct expansion *e,
675 const struct mx_node *m,
676 struct sink *output,
677 void *u) {
678 hash *h = hash_new(sizeof (struct mx_node *));
679 int n;
680
681 /* We store the macro arguments in a hash. Currently there is no check for
682 * duplicate argument names (and this would be the wrong place for it
683 * anyway); if you do that you just lose in some undefined way. */
684 for(n = 0; n < m->nargs; ++n)
685 hash_add(h, e->args[n], &m->args[n], HASH_INSERT);
686 /* Generate a rewritten parse tree */
687 m = mx_rewrite(e->definition, h);
688 /* Expand the result */
689 return mx_expand(m, output, u);
690 /* mx_expand() will update the backtrace */
691}
692
693/*
694Local Variables:
695c-basic-offset:2
696comment-column:40
697fill-column:79
698indent-tabs-mode:nil
699End:
700*/