summaryrefslogtreecommitdiffstats
path: root/src/parse.y
blob: 46f5ff63fbb0cb36c475b6779673419e98d20ab0 (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
%type SYMBOL {std::string *} str
%type SYMBOL_UC {std::string *} str
%type SYMBOL_LC {std::string *} str
%type BLOCK {std::string *} str
%type CHAR {char} c

%type rule {solar::rule_t *}
%type rhs {std::pair<std::vector<solar::symbol_t>, std::vector<std::string>> *}
%type action {std::string *}
%type symbol {solar::symbol_t *}
%type term {solar::symbol_t *}
%type varname {std::string *}


%header {
#include "grammar.hpp"
}

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


grammar |=;

grammar |= grammar 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), std::vector<std::string>(), std::string()});
	}

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

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

	delete nonterm;
	delete type;
}

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

	delete term;
	delete type;
	delete name;
}

grammar |= grammar SOURCE BLOCK(block) {
	grammar->source_block = *block;
	delete block;
}

grammar |= grammar HEADER BLOCK(block) {
	grammar->header_block = *block;
	delete block;
}

grammar |= grammar EXTRA_ARG BLOCK(type) varname(name) {
	grammar->extra_args.push_back(std::make_pair(*type, *name));

	delete type;
	delete name;
}


rule |= SYMBOL_LC(lhs) '|' '=' rhs(rhs) action(action) {
	auto *ret = new solar::rule_t {solar::item_t(*lhs, rhs->first), rhs->second, *action};

	delete lhs;
	delete rhs;
	delete action;

	return ret;
}


rhs |= {
	return new std::pair<std::vector<solar::symbol_t>, std::vector<std::string>>();
}

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

	return rhs;
}

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

	delete sym;
	delete var;

	return rhs;
}


action |= ';' {
	return new std::string;
}

action |= BLOCK(v) {
	return v;
}


symbol |= term(v) {
	return v;
}

symbol |= SYMBOL_LC(v) {
	solar::symbol_t *ret = new solar::symbol_t(solar::symbol_t::make_nonterm(*v));
	delete v;
	return ret;
}


term |= SYMBOL_UC(v) {
	solar::symbol_t *ret = new solar::symbol_t(solar::symbol_t::make_term(*v));
	delete v;
	return ret;
}

term |= CHAR(v) {
	return new solar::symbol_t(solar::symbol_t::make_char(v));
}


varname |= SYMBOL_LC(v) {
	return v;
}

varname |= SYMBOL_UC(v) {
	return v;
}

varname |= SYMBOL(v) {
	return v;
}