Imported Upstream version 1.5.0
[platform/upstream/augeas.git] / src / syntax.h
1 /*
2  * syntax.h: Data types to represent language syntax
3  *
4  * Copyright (C) 2007-2015 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 struct native {
116     unsigned int argc;
117     struct type *type;
118     struct value *(*impl)(void);
119 };
120
121 /* An exception in the interpreter. Some exceptions are reported directly
122  * into the central struct error; an exception for those is only generated
123  * to follow the control flow for exceptions. Such exceptions have both
124  * seen and error set to 1. They are the only exceptions with error == 1.
125  * When error == 1, none of the other fields in the exn will be usable.
126  */
127 struct exn {
128     struct info *info;
129     unsigned int seen : 1;      /* Whether the user has seen this EXN */
130     unsigned int error : 1;
131     char        *message;
132     size_t       nlines;
133     char       **lines;
134 };
135
136 /*
137  * Values in the interpreter
138  */
139 enum value_tag {
140     V_STRING,
141     V_REGEXP,
142     V_LENS,
143     V_TREE,
144     V_FILTER,
145     V_TRANSFORM,
146     V_NATIVE,
147     V_EXN,
148     V_CLOS,
149     V_UNIT
150 };
151
152 #define EXN(v) ((v)->tag == V_EXN)
153
154 struct value {
155     unsigned int   ref;
156     struct info   *info;
157     enum value_tag tag;
158     /* Nothing in this union for V_UNIT */
159     union {
160         struct string  *string;  /* V_STRING */
161         struct regexp  *regexp;  /* V_REGEXP */
162         struct lens    *lens;    /* V_LENS */
163         struct native  *native;  /* V_NATIVE */
164         struct tree    *origin;  /* V_TREE */
165         struct filter  *filter;  /* V_FILTER */
166         struct transform *transform; /* V_TRANSFORM */
167         struct exn     *exn;     /* V_EXN */
168         struct {                 /* V_CLOS */
169             struct term     *func;
170             struct binding  *bindings;
171         };
172     };
173 };
174
175 /* All types except for T_ARROW (functions) are simple. Subtype relations
176  * for the simple types:
177  *   T_STRING <: T_REGEXP
178  * and the usual subtype relation for functions.
179  */
180 enum type_tag {
181     T_STRING,
182     T_REGEXP,
183     T_LENS,
184     T_TREE,
185     T_FILTER,
186     T_TRANSFORM,
187     T_ARROW,
188     T_UNIT
189 };
190
191 struct type {
192     unsigned int   ref;
193     enum type_tag  tag;
194     struct type   *dom;    /* T_ARROW */
195     struct type   *img;    /* T_ARROW */
196 };
197
198 struct binding {
199     unsigned int    ref;
200     struct binding *next;
201     struct string  *ident;
202     struct type    *type;
203     struct value   *value;
204 };
205
206 /* A module maps names to TYPE * VALUE. */
207 struct module {
208     unsigned int       ref;
209     struct module     *next;     /* Only used for the global list of modules */
210     struct transform  *autoload;
211     char              *name;
212     struct binding    *bindings;
213 };
214
215 struct type *make_arrow_type(struct type *dom, struct type *img);
216 struct type *make_base_type(enum type_tag tag);
217 /* Do not call this directly. Use unref(t, type) instead */
218 void free_type(struct type *type);
219
220 /* Constructors for some terms in syntax.c Constructor assumes ownership of
221  * arguments without incrementing. Caller owns returned objects.
222  */
223 struct term *make_term(enum term_tag tag, struct info *info);
224 void free_term(struct term *term);
225 struct term *make_param(char *name, struct type *type, struct info *info);
226 struct value *make_value(enum value_tag tag, struct info *info);
227 struct value *make_unit(struct info *info);
228 struct term *make_app_term(struct term *func, struct term *arg,
229                            struct info *info);
230 struct term *make_app_ident(char *id, struct term *func, struct info *info);
231
232 /* Print a tree in the braces style used in modules */
233 void print_tree_braces(FILE *out, int indent, struct tree *tree);
234
235 /* Make an EXN value
236  * Receive ownership of INFO
237  *
238  * FORMAT and following arguments are printed to a new string. Caller must
239  * clean those up.
240  */
241 struct value *make_exn_value(struct info *info, const char *format, ...)
242     ATTRIBUTE_FORMAT(printf, 2, 3);
243
244 /* Add NLINES lines (passed as const char *) to EXN, which must be a
245  * value with tag V_EXN, created by MAKE_EXN_VALUE.
246  *
247  * The strings belong to EXN * after the call.
248  */
249 void exn_add_lines(struct value *exn, int nlines, ...);
250
251 void exn_printf_line(struct value *exn, const char *format, ...)
252     ATTRIBUTE_FORMAT(printf, 2, 3);
253
254 /* Do not call these directly, use UNREF instead */
255 void free_value(struct value *v);
256 void free_module(struct module *module);
257
258 /* Turn a list of PARAMS (represented as terms tagged as A_FUNC with the
259  * param in PARAM) into nested A_FUNC terms
260  */
261 struct term *build_func(struct term *params, struct term *exp);
262
263 struct module *module_create(const char *name);
264
265 #define define_native(error, module, name, argc, impl, types ...)       \
266     define_native_intl(__FILE__, __LINE__, error, module, name,         \
267                        argc, impl, ## types)
268
269 ATTRIBUTE_RETURN_CHECK
270 int define_native_intl(const char *fname, int line,
271                        struct error *error,
272                        struct module *module, const char *name,
273                        int argc, void *impl, ...);
274
275 struct module *builtin_init(struct error *);
276
277 int load_module_file(struct augeas *aug, const char *filename, const char *name);
278
279 /* The name of the builtin function that checks recursive lenses */
280 #define LNS_CHECK_REC_NAME "lns_check_rec"
281
282 int interpreter_init(struct augeas *aug);
283
284 struct lens *lens_lookup(struct augeas *aug, const char *qname);
285 #endif
286
287
288 /*
289  * Local variables:
290  *  indent-tabs-mode: nil
291  *  c-indent-level: 4
292  *  c-basic-offset: 4
293  *  tab-width: 4
294  * End:
295  */