Imported Upstream version 1.12.0
[platform/upstream/augeas.git] / src / syntax.h
1 /*
2  * syntax.h: Data types to represent language syntax
3  *
4  * Copyright (C) 2007-2016 David Lutterkort
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
19  *
20  * Author: David Lutterkort <dlutter@redhat.com>
21  */
22
23 #ifndef SYNTAX_H_
24 #define SYNTAX_H_
25
26 #include <limits.h>
27 #include "internal.h"
28 #include "lens.h"
29 #include "ref.h"
30 #include "fa.h"
31 #include "regexp.h"
32 #include "info.h"
33
34 void syntax_error(struct info *info, const char *format, ...)
35     ATTRIBUTE_FORMAT(printf, 2, 3);
36
37 void fatal_error(struct info *info, const char *format, ...)
38     ATTRIBUTE_FORMAT(printf, 2, 3);
39
40 enum term_tag {
41     A_MODULE,
42     A_BIND,              /* Module scope binding of a name */
43     A_LET,               /* local LET .. IN binding */
44     A_COMPOSE,
45     A_UNION,
46     A_MINUS,
47     A_CONCAT,
48     A_APP,
49     A_VALUE,
50     A_IDENT,
51     A_BRACKET,
52     A_FUNC,
53     A_REP,
54     A_TEST
55 };
56
57 enum test_result_tag {
58     TR_CHECK,
59     TR_PRINT,
60     TR_EXN
61 };
62
63 enum quant_tag {
64     Q_STAR,
65     Q_PLUS,
66     Q_MAYBE
67 };
68
69 struct term {
70     struct term *next;
71     unsigned int ref;
72     struct info *info;
73     struct type *type;                /* Filled in by the typechecker */
74     enum term_tag tag;
75     union {
76         struct {                       /* A_MODULE */
77             char          *mname;
78             char          *autoload;
79             struct term   *decls;
80         };
81         struct {                       /* A_BIND */
82             char          *bname;
83             struct term   *exp;
84         };
85         struct {              /* A_COMPOSE, A_UNION, A_CONCAT, A_APP, A_LET */
86             struct term *left;
87             struct term *right;
88         };
89         struct value    *value;         /* A_VALUE */
90         struct term     *brexp;         /* A_BRACKET */
91         struct string   *ident;         /* A_IDENT */
92         struct {                        /* A_REP */
93             enum quant_tag quant;
94             struct term  *rexp;
95         };
96         struct {                       /* A_FUNC */
97             struct param *param;
98             struct term   *body;
99         };
100         struct {                       /* A_TEST */
101             enum test_result_tag tr_tag;
102             struct term         *test;
103             struct term         *result;
104         };
105     };
106 };
107
108 struct param {
109     struct info   *info;
110     unsigned int   ref;
111     struct string *name;
112     struct type   *type;
113 };
114
115 /* The protoype for the implementation of a native/builtin function in the
116  * interpreter.
117  *
118  * The arguments are passed as a NULL-terminated array of values.
119  */
120 typedef struct value *(*func_impl)(struct info *, struct value *argv[]);
121
122 struct native {
123     unsigned int argc;
124     struct type *type;
125     func_impl    impl;
126 };
127
128 /* An exception in the interpreter. Some exceptions are reported directly
129  * into the central struct error; an exception for those is only generated
130  * to follow the control flow for exceptions. Such exceptions have both
131  * seen and error set to 1. They are the only exceptions with error == 1.
132  * When error == 1, none of the other fields in the exn will be usable.
133  */
134 struct exn {
135     struct info *info;
136     unsigned int seen : 1;      /* Whether the user has seen this EXN */
137     unsigned int error : 1;
138     char        *message;
139     size_t       nlines;
140     char       **lines;
141 };
142
143 /*
144  * Values in the interpreter
145  */
146 enum value_tag {
147     V_STRING,
148     V_REGEXP,
149     V_LENS,
150     V_TREE,
151     V_FILTER,
152     V_TRANSFORM,
153     V_NATIVE,
154     V_EXN,
155     V_CLOS,
156     V_UNIT
157 };
158
159 #define EXN(v) ((v)->tag == V_EXN)
160
161 struct value {
162     unsigned int   ref;
163     struct info   *info;
164     enum value_tag tag;
165     /* Nothing in this union for V_UNIT */
166     union {
167         struct string  *string;  /* V_STRING */
168         struct regexp  *regexp;  /* V_REGEXP */
169         struct lens    *lens;    /* V_LENS */
170         struct native  *native;  /* V_NATIVE */
171         struct tree    *origin;  /* V_TREE */
172         struct filter  *filter;  /* V_FILTER */
173         struct transform *transform; /* V_TRANSFORM */
174         struct exn     *exn;     /* V_EXN */
175         struct {                 /* V_CLOS */
176             struct term     *func;
177             struct binding  *bindings;
178         };
179     };
180 };
181
182 /* All types except for T_ARROW (functions) are simple. Subtype relations
183  * for the simple types:
184  *   T_STRING <: T_REGEXP
185  * and the usual subtype relation for functions.
186  */
187 enum type_tag {
188     T_STRING,
189     T_REGEXP,
190     T_LENS,
191     T_TREE,
192     T_FILTER,
193     T_TRANSFORM,
194     T_ARROW,
195     T_UNIT
196 };
197
198 struct type {
199     unsigned int   ref;
200     enum type_tag  tag;
201     struct type   *dom;    /* T_ARROW */
202     struct type   *img;    /* T_ARROW */
203 };
204
205 struct binding {
206     unsigned int    ref;
207     struct binding *next;
208     struct string  *ident;
209     struct type    *type;
210     struct value   *value;
211 };
212
213 /* A module maps names to TYPE * VALUE. */
214 struct module {
215     unsigned int       ref;
216     struct module     *next;     /* Only used for the global list of modules */
217     struct transform  *autoload;
218     char              *name;
219     struct binding    *bindings;
220 };
221
222 struct type *make_arrow_type(struct type *dom, struct type *img);
223 struct type *make_base_type(enum type_tag tag);
224 /* Do not call this directly. Use unref(t, type) instead */
225 void free_type(struct type *type);
226
227 /* Constructors for some terms in syntax.c Constructor assumes ownership of
228  * arguments without incrementing. Caller owns returned objects.
229  */
230 struct term *make_term(enum term_tag tag, struct info *info);
231 void free_term(struct term *term);
232 struct term *make_param(char *name, struct type *type, struct info *info);
233 struct value *make_value(enum value_tag tag, struct info *info);
234 struct value *make_unit(struct info *info);
235 struct term *make_app_term(struct term *func, struct term *arg,
236                            struct info *info);
237 struct term *make_app_ident(char *id, struct term *func, struct info *info);
238
239 /* Print a tree in the braces style used in modules */
240 void print_tree_braces(FILE *out, int indent, struct tree *tree);
241
242 /* Make an EXN value
243  * Receive ownership of INFO
244  *
245  * FORMAT and following arguments are printed to a new string. Caller must
246  * clean those up.
247  */
248 struct value *make_exn_value(struct info *info, const char *format, ...)
249     ATTRIBUTE_FORMAT(printf, 2, 3);
250
251 /* Add NLINES lines (passed as const char *) to EXN, which must be a
252  * value with tag V_EXN, created by MAKE_EXN_VALUE.
253  *
254  * The strings belong to EXN * after the call.
255  */
256 void exn_add_lines(struct value *exn, int nlines, ...);
257
258 void exn_printf_line(struct value *exn, const char *format, ...)
259     ATTRIBUTE_FORMAT(printf, 2, 3);
260
261 /* Do not call these directly, use UNREF instead */
262 void free_value(struct value *v);
263 void free_module(struct module *module);
264
265 /* Turn a list of PARAMS (represented as terms tagged as A_FUNC with the
266  * param in PARAM) into nested A_FUNC terms
267  */
268 struct term *build_func(struct term *params, struct term *exp);
269
270 struct module *module_create(const char *name);
271
272 #define define_native(error, module, name, argc, impl, types ...)       \
273     define_native_intl(__FILE__, __LINE__, error, module, name,         \
274                        argc, impl, ## types)
275
276 ATTRIBUTE_RETURN_CHECK
277 int define_native_intl(const char *fname, int line,
278                        struct error *error,
279                        struct module *module, const char *name,
280                        int argc, func_impl impl, ...);
281
282 struct module *builtin_init(struct error *);
283
284 int load_module_file(struct augeas *aug, const char *filename, const char *name);
285
286 /* The name of the builtin function that checks recursive lenses */
287 #define LNS_CHECK_REC_NAME "lns_check_rec"
288
289 int interpreter_init(struct augeas *aug);
290
291 struct lens *lens_lookup(struct augeas *aug, const char *qname);
292 #endif
293
294
295 /*
296  * Local variables:
297  *  indent-tabs-mode: nil
298  *  c-indent-level: 4
299  *  c-basic-offset: 4
300  *  tab-width: 4
301  * End:
302  */