summaryrefslogtreecommitdiffstats
path: root/src/parse.y
blob: 3d6f438e1cbb4f2215f7a978a34b84003aa58eae (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
%header {
#include "grammar.hpp"
}

%source {
typedef std::vector<std::pair<std::string, bool>> vars_t;
typedef std::pair<std::vector<solar::symbol_t>, vars_t> rhs_t;


static inline void free_string(std::string *v) {
	delete v;
}

static inline void free_symbol(solar::symbol_t *v) {
	delete v;
}

static inline void free_rule(solar::rule_t *v) {
	delete v;
}

static inline void free_rhs(rhs_t *v) {
	delete v;
}
}


%type SYMBOL {std::string *} str
%destructor SYMBOL free_string

%type SYMBOL_UC {std::string *} str
%destructor SYMBOL_UC free_string

%type SYMBOL_LC {std::string *} str
%destructor SYMBOL_LC free_string

%type BLOCK {std::string *} str
%destructor BLOCK free_string

%type SQBLOCK {std::string *} str
%destructor SQBLOCK free_string

%type STRING {std::string *} str
%destructor STRING free_string

%type CHAR {char} c


%type rule {solar::rule_t *}
%destructor rule free_rule

%type rhs {rhs_t *}
%destructor rhs free_rhs

%type action {std::string *}
%destructor action free_string

%type symbol {solar::symbol_t *}
%destructor symbol free_symbol

%type term {solar::symbol_t *}
%destructor term free_symbol

%type varname {std::string *}
%destructor varname free_string


%extra_arg {__attribute__((unused)) solar::grammar_t *} grammar


grammar |=;
grammar |= grammar directive;


directive |= "%type" SYMBOL_LC(nonterm) BLOCK(type) {
	grammar->nonterm_types.insert(std::make_pair(*nonterm, *type));
}

directive |= "%type" term(term) BLOCK(type) varname(name) {
	grammar->term_types.insert(std::make_pair(*term, std::make_pair(*type, *name)));
}

directive |= "%destructor" symbol(sym) varname(name) {
	grammar->destructors.insert(std::make_pair(*sym, *name));
}

directive |= "%source" BLOCK(block) {
	grammar->source_block = *block;
}

directive |= "%header" BLOCK(block) {
	grammar->header_block = *block;
}

directive |= "%extra_arg" BLOCK(type) varname(name) {
	grammar->extra_args.push_back(std::make_pair(*type, *name));
}

directive |= rule(rule) {
	if (grammar->rules.empty()) {
		solar::item_t init("");
		init.get_rhs().push_back(solar::symbol_t::make_nonterm(rule->item.get_lhs().c_str()));
		grammar->rules.emplace_back(solar::rule_t {std::move(init), vars_t(), std::string()});
	}

	grammar->rules.push_back(*rule);
}


rule |= SYMBOL_LC(lhs) "|=" rhs(rhs) action(action) [
	new solar::rule_t {solar::item_t(*lhs, rhs->first), rhs->second, *action}
]


rhs |= [new rhs_t()]

rhs |= rhs(=rhs) symbol(sym) {
	rhs->first.push_back(*sym);
	rhs->second.emplace_back();

	return rhs;
}

rhs |= rhs(=rhs) symbol(sym) '(' varname(var) ')' {
	rhs->first.push_back(*sym);
	rhs->second.emplace_back(*var, true);

	return rhs;
}

rhs |= rhs(=rhs) symbol(sym) '(' '=' varname(var) ')' {
	rhs->first.push_back(*sym);
	rhs->second.emplace_back(*var, false);

	return rhs;
}

rhs |= rhs(=rhs) STRING(str) {
	for (char c : *str) {
		rhs->first.push_back(solar::symbol_t::make_char(c));
		rhs->second.emplace_back();
	}

	return rhs;
}


action |= ';' [new std::string]
action |= BLOCK(=v) [v]
action |= SQBLOCK(v) [new std::string("return " + *v + ";")]


symbol |= SYMBOL_LC(v) [new solar::symbol_t(solar::symbol_t::make_nonterm(*v))]
symbol |= term(=v) [v]

term |= SYMBOL_UC(v) [new solar::symbol_t(solar::symbol_t::make_term(*v))]
term |= CHAR(v) [new solar::symbol_t(solar::symbol_t::make_char(v))]

varname |= SYMBOL_LC(=v) [v]
varname |= SYMBOL_UC(=v) [v]
varname |= SYMBOL(=v) [v]