-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ast.h
124 lines (96 loc) · 2.32 KB
/
ast.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright 2014-2017 Katherine Flavel
*
* See LICENCE for the full copyright terms.
*/
#ifndef KGT_AST_H
#define KGT_AST_H
struct ast_alt;
enum ast_features {
FEATURE_AST_CI_LITERAL = 1 << 0,
FEATURE_AST_PROSE = 1 << 1,
FEATURE_AST_BINARY = 1 << 2,
FEATURE_AST_INVISIBLE = 1 << 3
};
/*
* A term is a sequential list of items within an alt. Each item may be
* a terminal literal, a production rule name, or a group of terms.
*
* A term may be repeated a number of times, as in the EBNF x * 3 construct.
*
* a + b + c ;
*/
struct ast_term {
enum {
TYPE_EMPTY,
TYPE_RULE,
TYPE_CS_LITERAL,
TYPE_CI_LITERAL,
TYPE_TOKEN,
TYPE_PROSE,
TYPE_GROUP
} type;
union {
const struct ast_rule *rule; /* just for sake of the name */
struct txt literal;
const char *token;
const char *prose;
struct ast_alt *group;
} u;
unsigned int min;
unsigned int max; /* false (0) for unlimited */
int invisible;
struct ast_term *next;
};
/*
* An alternative is one of several choices:
*
* a | b | c
*/
struct ast_alt {
struct ast_term *terms;
/* TODO: struct ast_term *negs; - negative terms here */
int invisible;
struct ast_alt *next;
};
/*
* A grammar is a list of production rules. Each rule maps a name onto a list
* of alternatives:
*
* name1 := alt1 | alt2 | alt3 ;
* name2 := alt1 ;
*/
struct ast_rule {
const char *name;
struct ast_alt *alts;
struct ast_rule *next;
};
struct ast_term *
ast_make_empty_term(int invisible);
struct ast_term *
ast_make_rule_term(int invisible, struct ast_rule *rule);
struct ast_term *
ast_make_char_term(int invisible, char c);
struct ast_term *
ast_make_literal_term(int invisible, const struct txt *literal, int ci);
struct ast_term *
ast_make_token_term(int invisible, const char *token);
struct ast_term *
ast_make_prose_term(int invisible, const char *prose);
struct ast_term *
ast_make_group_term(int invisible, struct ast_alt *group);
struct ast_alt *
ast_make_alt(int invisible, struct ast_term *terms);
struct ast_rule *
ast_make_rule(const char *name, struct ast_alt *alts);
struct ast_rule *
ast_find_rule(const struct ast_rule *grammar, const char *name);
void
ast_free_rule(struct ast_rule *rule);
void
ast_free_alt(struct ast_alt *alt);
void
ast_free_term(struct ast_term *term);
int
ast_binary(const struct ast_rule *ast);
#endif