Root/b2/subst.h

1/*
2 * subst.h - Substitution rules
3 *
4 * Copyright 2012 by Werner Almesberger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12
13#ifndef SUBST_H
14#define SUBST_H
15
16#include <stdio.h>
17#include <sys/types.h>
18#include <regex.h>
19
20#include "relop.h"
21
22
23#define NMATCH 10 /* $0 (not used), $1...$9 */
24
25
26enum chunk_type {
27    ct_string,
28    ct_var,
29    ct_sub,
30};
31
32struct chunk {
33    enum chunk_type type;
34    union {
35        const char *s;
36        const char *var;
37        int sub; /* 0 if $$ */
38    } u;
39    struct chunk *next;
40};
41
42enum subst_type {
43    st_match, /* try to match a variable */
44    st_assign, /* assign to a variable */
45    st_end, /* end the substitutions, accepting the part */
46    st_ignore, /* ignore the part */
47    st_break, /* jump to an outer block */
48    st_continue, /* repeat an outer block */
49    st_print, /* print a variable, for debugging */
50};
51
52struct subst {
53    enum subst_type type;
54    union {
55        struct {
56            const char *src;
57            regex_t re;
58            struct subst *block;
59            char units[NMATCH-1];
60        } match;
61        struct {
62            const char *dst;
63            enum relop op;
64            struct chunk *pat;
65        } assign;
66        const char *print;
67        const struct subst *jump;
68        const char *tmp; /* jump target name; for subst_finalize */
69    } u;
70    const struct subst *prev; /* for tracking availability of variables */
71    struct subst *next;
72};
73
74
75#define MULT_CHARS "GMkmunpf"
76
77
78extern const char *fn;
79
80
81struct subst *subst_match(const char *src, const char *re, char **res);
82struct subst *subst_assign(const char *dst, enum relop op, const char *pat);
83struct subst *subst_print(const char *var);
84struct subst *subst_end(void);
85struct subst *subst_ignore(void);
86struct subst *subst_break(const char *block);
87struct subst *subst_continue(const char *block);
88
89void subst_finalize(struct subst *sub);
90
91void subst_dump(FILE *file, const struct subst *sub);
92
93void subst_init(void);
94
95#endif /* !SUBST_H */
96

Archive Download this file

Branches:
master



interactive