summaryrefslogtreecommitdiffstats
path: root/src/parse.y
blob: e8d26d34b18bc1ba00f7515a2f287841ee5e3f93 (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
%namespace solar

%header {
#include "grammar.hpp"
}

%source {
namespace solar {

typedef std::pair<std::string, bool> var_t;
typedef std::vector<var_t> vars_t;
typedef std::pair<std::vector<solar::symbol_t>, vars_t> rhs_t;

}
}


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


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

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

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

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

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

%type CHAR {unsigned char} c


grammar |=;
grammar |= grammar directive;


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

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

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

directive |= "%namespace" namespace(ns) {
	grammar->ns = *ns;
}

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

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

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

directive |= SYMBOL(lhs) "|=" rhs(rhs) action(action) {
	grammar->add_rule({item_t(*lhs, rhs->first), rhs->second, *action});
}

directive |= SYMBOL(lhs) "|=" '(' CHAR(c1) "..." CHAR(c2) ')' ';' {
	vars_t vars(1);

	for (unsigned int c = c1; c <= c2; c++)
		grammar->add_rule({item_t(*lhs, {symbol_t::make_char(c)}), vars, std::string()});
}


%type rhs {rhs_t *}
%destructor rhs delete

rhs |= [new rhs_t]

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

	return rhs;
}

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

	return rhs;
}


%type variable {var_t *}
%destructor variable delete

variable |=		[new var_t]
variable |= '(' consume(consume) csymbol(var) ')'
			[new var_t(*var, consume)]


%type consume {bool}

consume |=		[false]
consume |= '='		[true]


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

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


%type namespace {std::vector<std::string> *}
%destructor namespace delete

namespace |= csymbol(v) [new std::vector<std::string> {*v}]
namespace |= namespace(=ns) "::" csymbol(v) {
	ns->push_back(*v);
	return ns;
}


%type symbol {symbol_t *}
%destructor symbol delete

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


%type term {symbol_t *}
%destructor term delete

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


%type csymbol {std::string *}
%destructor csymbol delete

csymbol |= SYMBOL_UC(=v) [v]
csymbol |= SYMBOL(=v)	[v]