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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#[macro_use]
use corollary_support::*;
use data::position::Position;
use data::position::Position::NoPosition;
use parser::tokens::*;
use data::input_stream::*;
use data::ident::Ident;
use data::name::Name;
use data::error::*;
use std::boxed::FnBox;
use std::rc::Rc;
#[derive(Debug)]
pub struct ParseError(pub (Vec<String>, Position));
#[derive(Clone, Debug)]
pub enum ParseResult<a> {
POk(PState, a),
PFailed(Vec<String>, Position),
}
pub use self::ParseResult::*;
#[derive(Clone, Debug)]
pub struct PState {
curPos: Position,
curInput: InputStream,
prevToken: Option<CToken>,
savedToken: Option<CToken>,
namesupply: Vec<Name>,
tyidents: Set<Ident>,
scopes: Vec<Set<Ident>>,
}
fn curPos(a: PState) -> Position {
a.curPos
}
fn curInput(a: PState) -> InputStream {
a.curInput
}
fn prevToken(a: PState) -> CToken {
a.prevToken.expect("CLexer.execParser: Touched undefined token!")
}
fn savedToken(a: PState) -> CToken {
a.savedToken.expect("CLexer.execParser: Touched undefined token (safed token)!")
}
fn namesupply(a: PState) -> Vec<Name> {
a.namesupply
}
fn tyidents(a: PState) -> Set<Ident> {
a.tyidents
}
fn scopes(a: PState) -> Vec<Set<Ident>> {
a.scopes
}
#[must_use]
pub struct P<a>(pub Rc<Box<Fn(PState) -> ParseResult<a>>>);
pub fn unP<a>(p: P<a>) -> Rc<Box<Fn(PState) -> ParseResult<a>>> {
p.0
}
impl<a> P<a> {
fn with(item: Box<Fn(PState) -> ParseResult<a>>) -> P<a> {
P(Rc::new(item))
}
}
impl<a> Clone for P<a> {
fn clone(&self) -> Self {
P(self.0.clone())
}
}
impl<A: Clone + 'static> From<A> for P<A> {
fn from(item: A) -> P<A> {
P::with(box move |state| POk(state, item.clone()))
}
}
pub fn execParser<a>(P(parser): P<a>,
input: InputStream,
pos: Position,
builtins: Vec<Ident>,
names: Vec<Name>)
-> Either<ParseError, (a, Vec<Name>)> {
let initialState = PState {
curPos: pos,
curInput: input,
prevToken: None,
savedToken: None,
namesupply: names,
tyidents: Set::fromList(builtins),
scopes: vec![],
};
match parser(initialState) {
PFailed(message, errpos) => Left((ParseError((message, errpos)))),
POk(st, result) => Right((result, namesupply(st))),
}
}
pub fn returnP<a: Clone + 'static>(a: a) -> P<a> {
P::with(box move |s| POk(s, a.clone()))
}
pub fn thenP<a: 'static, b: 'static>(P(m): P<a>, k: Box<Fn(a) -> P<b>>) -> P<b> {
P::with(box move |s| match m(s) {
POk(s_q, a) => (unP((k(a))))(s_q),
PFailed(err, pos) => PFailed(err, pos),
})
}
pub fn failP<a>(pos: Position, msg: Vec<String>) -> P<a> {
P::with(box move |_| PFailed(msg.clone(), pos.clone()))
}
pub fn getNewName() -> P<Name> {
P::with(box move |s: PState| {
let mut ns = s.namesupply.clone();
let n = ns.remove(0);
seq(n.clone(), POk(__assign!(s, { namesupply: ns }), n))
})
}
pub fn setPos(pos: Position) -> P<()> {
P::with(box move |s: PState| POk(__assign!(s, { curPos: pos.clone() }), ()))
}
pub fn getPos() -> P<Position> {
P::with(box move |s: PState| POk(s.clone(), s.curPos.clone()))
}
pub fn addTypedef(ident: Ident) -> P<()> {
P::with(box move |s: PState| POk(__assign!(s.clone(), { tyidents: Set::insert(ident.clone(), s.tyidents.clone()) }), ()))
}
pub fn shadowTypedef(ident: Ident) -> P<()> {
P::with(box move |s: PState| {
POk(__assign!(s.clone(), {
tyidents: (if Set::member(ident.clone(), s.tyidents.clone()) {
Set::delete(ident.clone(), s.tyidents.clone())
} else {
s.tyidents.clone()
}),
}),
())
})
}
pub fn isTypeIdent(mut ident: Ident) -> P<bool> {
P::with(box move |s: PState| POk(s.clone(), Set::member(ident.clone(), s.tyidents.clone())))
}
pub fn enterScope() -> P<()> {
P::with(box move |s: PState| POk(__assign!(s.clone(), { scopes: __op_concat(s.tyidents.clone(), s.scopes.clone()) }), ()))
}
pub fn leaveScope() -> P<()> {
P::with(box |s: PState| {
let mut ss = s.scopes.clone();
if ss.is_empty() {
__error!("leaveScope: already in global scope".to_string());
} else {
let tyids = ss.remove(0);
let ss_q = ss;
POk(__assign!(s, {
tyidents: tyids,
scopes: ss_q,
}), ())
}
})
}
pub fn getInput() -> P<InputStream> {
P::with(box |s: PState| POk(s.clone(), s.curInput.clone()))
}
pub fn setInput(i: InputStream) -> P<()> {
P::with(box move |s: PState| POk(__assign!(s, { curInput: i.clone() }), ()))
}
pub fn getLastToken() -> P<CToken> {
P::with(box |s: PState| POk(s.clone(), prevToken(s).clone()))
}
pub fn getSavedToken() -> P<CToken> {
P::with(box |s: PState| POk(s.clone(), savedToken(s).clone()))
}
pub fn setLastToken(_0: CToken) -> P<()> {
match (_0) {
CTokEof => P::with(box |s| POk(__assign!(s.clone(), { savedToken: s.prevToken.clone() }), ())),
tok => {
P::with(box move |s| {
POk(__assign!(s.clone(), {
prevToken: Some(tok.clone()),
savedToken: s.prevToken.clone(),
}),
())
})
}
}
}
pub fn handleEofToken() -> P<()> {
P::with(box |s: PState| POk(__assign!(s.clone(), { savedToken: s.prevToken.clone() }), ()))
}
pub fn getCurrentPosition() -> P<Position> {
P::with(box |s: PState| POk(s.clone(), s.curPos.clone()))
}
pub fn rshift_monad<a: 'static, b: 'static>(a: P<a>, b: P<b>) -> P<b> {
thenP(a, box move |_| b.clone())
}