2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /** @file lib/macros.c
20 * @brief Macro expansion
40 VECTOR_TYPE(mx_node_vector
, const struct mx_node
*, xrealloc
);
42 /** @brief Definition of an expansion */
44 /** @brief Minimum permitted arguments */
47 /** @brief Maximum permitted arguments */
56 * - @ref EXP_TYPE_MASK
60 /** @brief Macro argument names */
63 /** @brief Callback (cast to appropriate type)
65 * Cast to @ref mx_simple_callback or @ref mx_magic_callback as required. */
66 void (*callback
)(void);
68 /** @brief Macro definition
70 * Only for @ref EXP_MACRO expansions. */
71 const struct mx_node
*definition
;
74 /** @brief Expansion takes pre-expanded strings
76 * @p callback is cast to @ref mx_simple_callback. */
77 #define EXP_SIMPLE 0x0000
79 /** @brief Expansion takes parsed templates, not strings
81 * @p callback is cast to @ref mx_magic_callback. The callback must do its own
82 * expansion e.g. via mx_expandstr() where necessary. */
83 #define EXP_MAGIC 0x0001
85 /** @brief Expansion is a macro */
86 #define EXP_MACRO 0x0002
88 /** @brief Mask of types */
89 #define EXP_TYPE_MASK 0x0003
91 /** @brief Hash of all expansions
93 * Created by mx_register(), mx_register_macro() or mx_register_magic().
95 static hash
*expansions
;
97 static int mx__expand_macro(const struct expansion
*e
,
98 const struct mx_node
*m
,
102 /* Parsing ------------------------------------------------------------------ */
104 static int next_non_whitespace(const char *input
,
106 while(input
< end
&& isspace((unsigned char)*input
))
108 return input
< end ?
*input
: -1;
111 /** @brief Parse a template
112 * @param filename Input filename (for diagnostics)
113 * @param line Line number (use 1 on initial call)
114 * @param input Start of text to parse
115 * @param end End of text to parse or NULL
116 * @return Pointer to parse tree root node
118 * Parses the text in [start, end) and returns an (immutable) parse
119 * tree representing it.
121 * If @p end is NULL then the whole string is parsed.
123 * Note that the @p filename value stored in the parse tree is @p filename,
124 * i.e. it is not copied.
126 const struct mx_node
*mx_parse(const char *filename
,
130 int braces
, argument_start_line
, obracket
, cbracket
;
131 const char *argument_start
, *argument_end
;
132 struct mx_node_vector v
[1];
134 struct mx_node
*head
= 0, **tailp
= &head
, *e
;
137 end
= input
+ strlen(input
);
140 e
= xmalloc(sizeof *e
);
142 e
->filename
= filename
;
146 /* Gather up text without any expansions in. */
147 while(input
< end
&& *input
!= '@') {
150 dynstr_append(d
, *input
++);
161 /* '@@' expands to '@' */
162 e
= xmalloc(sizeof *e
);
164 e
->filename
= filename
;
173 /* '@#' starts a (newline-eating comment), like dnl */
175 while(input
< end
&& *input
!= '\n')
183 /* '@_' expands to nothing. It's there to allow dump to terminate
184 * expansions without having to know what follows. */
188 /* It's a full expansion */
190 e
= xmalloc(sizeof *e
);
192 e
->filename
= filename
;
194 e
->type
= MX_EXPANSION
;
195 /* Collect the expansion name. Expansion names start with an alnum and
196 * consist of alnums and '-'. We don't permit whitespace between the '@'
200 disorder_fatal(0, "%s:%d: invalid expansion syntax (truncated)",
202 if(!isalnum((unsigned char)*input
))
203 disorder_fatal(0, "%s:%d: invalid expansion syntax (unexpected %#x)",
204 filename
, e
->line
, (unsigned char)*input
);
205 while(input
< end
&& (isalnum((unsigned char)*input
) || *input
== '-'))
206 dynstr_append(d
, *input
++);
209 /* See what the bracket character is */
210 obracket
= next_non_whitespace(input
, end
);
212 case '(': cbracket
= ')'; break;
213 case '[': cbracket
= ']'; break;
214 case '{': cbracket
= '}'; break;
215 default: cbracket
= obracket
= -1; break; /* no arguments */
217 mx_node_vector_init(v
);
219 /* Gather up arguments */
220 while(next_non_whitespace(input
, end
) == obracket
) {
221 while(isspace((unsigned char)*input
)) {
226 ++input
; /* the bracket */
228 /* Find the end of the argument */
229 argument_start
= input
;
230 argument_start_line
= line
;
231 while(input
< end
&& (*input
!= cbracket
|| braces
> 0)) {
232 const int c
= *input
++;
236 else if(c
== cbracket
)
242 /* We ran out of input without encountering a balanced cbracket */
243 disorder_fatal(0, "%s:%d: unterminated expansion argument '%.*s'",
244 filename
, argument_start_line
,
245 (int)(input
- argument_start
), argument_start
);
247 /* Consistency check */
248 assert(*input
== cbracket
);
249 /* Record the end of the argument */
250 argument_end
= input
;
251 /* Step over the cbracket */
253 /* Now we have an argument in [argument_start, argument_end), and we
254 * know its filename and initial line number. This is sufficient to
256 mx_node_vector_append(v
, mx_parse(filename
, argument_start_line
,
257 argument_start
, argument_end
));
260 /* Guarantee a NULL terminator (for the case where there's more than one
262 mx_node_vector_terminate(v
);
263 /* Fill in the remains of the node */
272 static void mx__dump(struct dynstr
*d
, const struct mx_node
*m
) {
274 const struct mx_node
*mm
;
280 if(m
->text
[0] == '@')
281 dynstr_append(d
, '@');
282 dynstr_append_string(d
, m
->text
);
285 dynstr_append(d
, '@');
286 dynstr_append_string(d
, m
->name
);
287 for(n
= 0; n
< m
->nargs
; ++n
) {
288 dynstr_append(d
, '{');
289 mx__dump(d
, m
->args
[n
]);
290 dynstr_append(d
, '}');
292 /* If the next non-whitespace is '{', add @_ to stop it being
295 while(mm
&& mm
->type
== MX_TEXT
) {
296 switch(next_non_whitespace(mm
->text
, mm
->text
+ strlen(mm
->text
))) {
301 dynstr_append_string(d
, "@_");
310 assert(!"invalid m->type");
312 mx__dump(d
, m
->next
);
315 /** @brief Dump a parse macro expansion to a string
317 * Not of production quality! Only intended for testing!
319 char *mx_dump(const struct mx_node
*m
) {
328 /* Expansion registration --------------------------------------------------- */
330 static int mx__register(unsigned flags
,
335 void (*callback
)(void),
336 const struct mx_node
*definition
) {
337 struct expansion e
[1];
340 expansions
= hash_new(sizeof(struct expansion
));
345 e
->callback
= callback
;
346 e
->definition
= definition
;
347 return hash_add(expansions
, name
, &e
, HASH_INSERT_OR_REPLACE
);
350 /** @brief Register a simple expansion rule
352 * @param min Minimum number of arguments
353 * @param max Maximum number of arguments
354 * @param callback Callback to write output
356 void mx_register(const char *name
,
359 mx_simple_callback
*callback
) {
360 mx__register(EXP_SIMPLE
, name
, min
, max
, 0, (void (*)(void))callback
, 0);
363 /** @brief Register a magic expansion rule
365 * @param min Minimum number of arguments
366 * @param max Maximum number of arguments
367 * @param callback Callback to write output
369 void mx_register_magic(const char *name
,
372 mx_magic_callback
*callback
) {
373 mx__register(EXP_MAGIC
, name
, min
, max
, 0, (void (*)(void))callback
, 0);
376 /** @brief Register a macro
378 * @param nargs Number of arguments
379 * @param args Argument names
380 * @param definition Macro definition
381 * @return 0 on success, negative on error
383 int mx_register_macro(const char *name
,
386 const struct mx_node
*definition
) {
387 if(mx__register(EXP_MACRO
, name
, nargs
, nargs
, args
, 0/*callback*/,
390 /* This locates the error to the definition, which may be a line or two
391 * beyond the @define command itself. The backtrace generated by
392 * mx_expand() may help more. */
393 disorder_error(0, "%s:%d: duplicate definition of '%s'",
394 definition
->filename
, definition
->line
, name
);
401 /* Expansion ---------------------------------------------------------------- */
403 /** @brief Expand a template
404 * @param m Where to start
405 * @param output Where to send output
407 * @return 0 on success, non-0 on error
409 * Interpretation of return values:
411 * - -1 means an error writing to the sink.
412 * - other negative values mean errors generated from with the macro
414 * - positive values are reserved for the application
416 * If any callback returns non-zero then that value is returned, abandoning
419 int mx_expand(const struct mx_node
*m
,
422 const struct expansion
*e
;
429 if(sink_writes(output
, m
->text
) < 0)
434 if(!(e
= hash_find(expansions
, m
->name
))) {
435 disorder_error(0, "%s:%d: unknown expansion name '%s'",
436 m
->filename
, m
->line
, m
->name
);
437 if(sink_printf(output
, "[['%s' unknown]]", m
->name
) < 0)
439 } else if(m
->nargs
< e
->min
) {
440 disorder_error(0, "%s:%d: expansion '%s' requires %d args, only %d given",
441 m
->filename
, m
->line
, m
->name
, e
->min
, m
->nargs
);
442 if(sink_printf(output
, "[['%s' too few args]]", m
->name
) < 0)
444 } else if(m
->nargs
> e
->max
) {
445 disorder_error(0, "%s:%d: expansion '%s' takes at most %d args, but %d given",
446 m
->filename
, m
->line
, m
->name
, e
->max
, m
->nargs
);
447 if(sink_printf(output
, "[['%s' too many args]]", m
->name
) < 0)
449 } else switch(e
->flags
& EXP_TYPE_MASK
) {
451 /* Magic callbacks we can call directly */
452 rc
= ((mx_magic_callback
*)e
->callback
)(m
->nargs
,
459 /* For simple callbacks we expand their arguments for them. */
460 char **args
= xcalloc(1 + m
->nargs
, sizeof (char *)), *argname
;
463 for(n
= 0; n
< m
->nargs
; ++n
) {
464 /* Argument numbers are at least clear from looking at the text;
465 * adding names as well would be nice. TODO */
466 byte_xasprintf(&argname
, "argument #%d", n
);
467 if((rc
= mx_expandstr(m
->args
[n
], &args
[n
], u
, argname
)))
472 rc
= ((mx_simple_callback
*)e
->callback
)(m
->nargs
,
480 /* Macros we expand by rewriting their definition with argument values
481 * substituted and then expanding that. */
482 rc
= mx__expand_macro(e
, m
, output
, u
);
486 assert(!"impossible EXP_TYPE_MASK value");
489 /* For non-IO errors we generate some backtrace */
491 disorder_error(0, " ...in @%s at %s:%d",
492 m
->name
, m
->filename
, m
->line
);
497 assert(!"invalid m->type");
499 return mx_expand(m
->next
, output
, u
);
502 /** @brief Expand a template storing the result in a string
503 * @param m Where to start
504 * @param sp Where to store string
506 * @param what Token for backtrace, or NULL
507 * @return 0 on success, non-0 on error
509 * Same return conventions as mx_expand(). This wrapper is slightly more
510 * convenient to use from 'magic' expansions.
512 int mx_expandstr(const struct mx_node
*m
,
520 if(!(rc
= mx_expand(m
, sink_dynstr(d
), u
))) {
525 if(rc
&& rc
!= -1 && what
)
526 disorder_error(0, " ...in %s at %s:%d", what
, m
->filename
, m
->line
);
530 /** @brief Expand a template file
531 * @param path Filename
532 * @param output Where to send output
534 * @return 0 on success, non-0 on error
536 * Same return conventions as mx_expand().
538 int mx_expand_file(const char *path
,
545 const struct mx_node
*m
;
547 if((fd
= open(path
, O_RDONLY
)) < 0)
548 disorder_fatal(errno
, "error opening %s", path
);
549 if(fstat(fd
, &sb
) < 0)
550 disorder_fatal(errno
, "error statting %s", path
);
551 if(!S_ISREG(sb
.st_mode
))
552 disorder_fatal(0, "%s: not a regular file", path
);
554 b
= xmalloc_noptr(sb
.st_size
);
555 while(sofar
< sb
.st_size
) {
556 n
= read(fd
, b
+ sofar
, sb
.st_size
- sofar
);
560 disorder_fatal(0, "unexpected EOF reading %s", path
);
561 else if(errno
!= EINTR
)
562 disorder_fatal(errno
, "error reading %s", path
);
565 m
= mx_parse(path
, 1, b
, b
+ sb
.st_size
);
566 rc
= mx_expand(m
, output
, u
);
568 /* Mention inclusion in backtrace */
569 disorder_error(0, " ...in inclusion of file '%s'", path
);
573 /* Macros ------------------------------------------------------------------- */
575 /** @brief Rewrite a parse tree substituting sub-expansions
576 * @param m Parse tree to rewrite (from macro definition)
577 * @param ... Name/value pairs to rewrite
578 * @return Rewritten parse tree
580 * The name/value pair list consists of pairs of strings and is terminated by
581 * (char *)0. Names and values are both copied so need not survive the call.
583 const struct mx_node
*mx_rewritel(const struct mx_node
*m
,
586 hash
*h
= hash_new(sizeof (struct mx_node
*));
591 while((n
= va_arg(ap
, const char *))) {
592 v
= va_arg(ap
, const char *);
593 e
= xmalloc(sizeof *e
);
595 e
->filename
= m
->filename
;
598 e
->text
= xstrdup(v
);
599 hash_add(h
, n
, &e
, HASH_INSERT
);
600 /* hash_add() copies n */
602 return mx_rewrite(m
, h
);
605 /** @brief Rewrite a parse tree substituting in macro arguments
606 * @param definition Parse tree to rewrite (from macro definition)
607 * @param h Hash mapping argument names to argument values
608 * @return Rewritten parse tree
610 const struct mx_node
*mx_rewrite(const struct mx_node
*definition
,
612 const struct mx_node
*head
= 0, **tailp
= &head
, *argvalue
, *m
, *mm
, **ap
;
616 for(m
= definition
; m
; m
= m
->next
) {
619 nm
= xmalloc(sizeof *nm
);
620 *nm
= *m
; /* Dumb copy of text node fields */
621 nm
->next
= 0; /* Maintain list structure */
623 tailp
= (const struct mx_node
**)&nm
->next
;
627 && (ap
= hash_find(h
, m
->name
))) {
628 /* This expansion has no arguments and its name matches one of the
629 * macro arguments. (Even if it's a valid expansion name we override
630 * it.) We insert its value at this point. We do NOT recursively
631 * rewrite the argument's value - it is outside the lexical scope of
634 * We need to recreate the list structure but a shallow copy will
638 for(mm
= argvalue
; mm
; mm
= mm
->next
) {
639 nm
= xmalloc(sizeof *nm
);
643 tailp
= (const struct mx_node
**)&nm
->next
;
646 /* This is some other expansion. We recursively rewrite its argument
647 * values according to h. */
648 nm
= xmalloc(sizeof *nm
);
650 nm
->args
= xcalloc(nm
->nargs
, sizeof (struct mx_node
*));
651 for(n
= 0; n
< nm
->nargs
; ++n
)
652 nm
->args
[n
] = mx_rewrite(m
->args
[n
], h
);
655 tailp
= (const struct mx_node
**)&nm
->next
;
659 assert(!"invalid m->type");
662 *tailp
= 0; /* Mark end of list */
666 /** @brief Expand a macro
667 * @param e Macro definition
668 * @param m Macro expansion
669 * @param output Where to send output
671 * @return 0 on success, non-0 on error
673 static int mx__expand_macro(const struct expansion
*e
,
674 const struct mx_node
*m
,
677 hash
*h
= hash_new(sizeof (struct mx_node
*));
680 /* We store the macro arguments in a hash. Currently there is no check for
681 * duplicate argument names (and this would be the wrong place for it
682 * anyway); if you do that you just lose in some undefined way. */
683 for(n
= 0; n
< m
->nargs
; ++n
)
684 hash_add(h
, e
->args
[n
], &m
->args
[n
], HASH_INSERT
);
685 /* Generate a rewritten parse tree */
686 m
= mx_rewrite(e
->definition
, h
);
687 /* Expand the result */
688 return mx_expand(m
, output
, u
);
689 /* mx_expand() will update the backtrace */