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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
use std::{fmt, mem};
use proc_macro2::Span;
use syn::{punctuated::Pair, Token};
use crate::chomp::{
ast::{self, NamedExpression},
Name,
};
use super::cst::{Alt, Call, Cat, Expression, Fix, Ident, Labelled, Lambda, ParenExpression, Term};
#[derive(Debug, Default)]
pub struct Context {
bindings: Vec<Name>,
}
impl Context {
pub fn new() -> Self {
Self::default()
}
pub fn lookup(&self, name: &Name) -> Option<usize> {
self.bindings
.iter()
.enumerate()
.rfind(|(_, n)| *n == name)
.map(|(idx, _)| idx)
}
pub fn push_variable(&mut self, name: Name) {
self.bindings.push(name);
}
pub fn with_variable<F: FnOnce(&mut Self) -> R, R>(&mut self, name: Name, f: F) -> R {
self.bindings.push(name);
let res = f(self);
self.bindings.pop();
res
}
pub fn with_variables<I: IntoIterator<Item = Name>, F: FnOnce(&mut Self) -> R, R>(
&mut self,
names: I,
f: F,
) -> R {
let len = self.bindings.len();
self.bindings.extend(names);
let res = f(self);
self.bindings.resize_with(len, || unreachable!());
res
}
}
#[derive(Clone, Debug)]
pub enum ConvertError {
UndeclaredName(Box<Name>),
EmptyCat(Option<Span>),
EmptyAlt(Option<Span>),
EmptyCall(Option<Span>),
MissingArgs(Option<Span>),
}
impl From<ConvertError> for syn::Error {
fn from(e: ConvertError) -> Self {
let msg = e.to_string();
let span = match e {
ConvertError::UndeclaredName(name) => name.span(),
ConvertError::EmptyCat(span)
| ConvertError::EmptyAlt(span)
| ConvertError::EmptyCall(span)
| ConvertError::MissingArgs(span) => span,
};
Self::new(span.unwrap_or_else(Span::call_site), msg)
}
}
impl fmt::Display for ConvertError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UndeclaredName(name) => {
write!(f, "undeclared name: `{}`", name)
}
Self::EmptyCat(_) => {
write!(f, "concatenation has no elements")
}
Self::EmptyAlt(_) => {
write!(f, "alternation has no elements")
}
Self::EmptyCall(_) => {
write!(f, "call has no function")
}
Self::MissingArgs(_) => {
write!(f, "call has no arguments")
}
}
}
}
impl std::error::Error for ConvertError {}
pub trait Convert {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError>;
}
impl Convert for Ident {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
let span = Some(self.span());
let name = self.into();
let index = context
.lookup(&name)
.ok_or_else(|| ConvertError::UndeclaredName(Box::new(name.clone())))?;
Ok(NamedExpression {
name: Some(name),
expr: ast::Variable { index }.into(),
span,
})
}
}
impl Convert for Fix {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
let span = self.span();
let inner = self.expr.convert(context)?;
Ok(NamedExpression {
name: None,
expr: ast::Fix {
inner: Box::new(inner),
}
.into(),
span,
})
}
}
impl Convert for ParenExpression {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
self.expr.convert(context)
}
}
impl Convert for Term {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
match self {
Self::Epsilon(e) => Ok(NamedExpression {
name: None,
expr: ast::Epsilon.into(),
span: Some(e.span),
}),
Self::Ident(i) => i.convert(context),
Self::Literal(l) => Ok(NamedExpression {
name: None,
expr: l.value().into(),
span: Some(l.span()),
}),
Self::Fix(f) => f.convert(context),
Self::Parens(p) => p.convert(context),
}
}
}
impl Convert for Call {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
let span = self.span();
let mut iter = self.0.into_iter();
let on = iter
.next()
.ok_or_else(|| ConvertError::EmptyCall(span))?
.convert(context)?;
let args = iter
.map(|arg| arg.convert(context))
.collect::<Result<Vec<_>, _>>()?;
if args.is_empty() {
Err(ConvertError::MissingArgs(span))
} else {
Ok(NamedExpression {
name: None,
expr: ast::Call {
on: Box::new(on),
args,
}
.into(),
span,
})
}
}
}
impl Convert for Cat {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
fn convert_pair(
pair: Pair<Call, Token![.]>,
context: &mut Context,
) -> Result<(NamedExpression, Option<Span>), ConvertError> {
match pair {
Pair::Punctuated(t, p) => t.convert(context).map(|expr| (expr, Some(p.span))),
Pair::End(t) => t.convert(context).map(|expr| (expr, None)),
}
}
let span = self.span();
let mut iter = self.0.into_pairs();
let (first, mut punct) = iter
.next()
.ok_or(ConvertError::EmptyCat(span))
.and_then(|pair| convert_pair(pair, context))?;
let mut rest = iter
.map(|pair| {
convert_pair(pair, context).map(|(snd, p)| (mem::replace(&mut punct, p), snd))
})
.peekable();
if let Some(_) = rest.peek() {
Ok(NamedExpression {
name: None,
expr: ast::Cat {
first: Box::new(first),
rest: rest.collect::<Result<_, _>>()?,
}
.into(),
span,
})
} else {
Ok(first)
}
}
}
impl Convert for Labelled {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
let span = self.span();
let named = self.cat.convert(context)?;
let name = self.label.map(|l| l.label.into()).or(named.name);
Ok(NamedExpression {
name,
expr: named.expr,
span,
})
}
}
impl Convert for Alt {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
fn convert_pair(
pair: Pair<Labelled, Token![|]>,
context: &mut Context,
) -> Result<(NamedExpression, Option<Span>), ConvertError> {
match pair {
Pair::Punctuated(t, p) => t.convert(context).map(|expr| (expr, Some(p.span))),
Pair::End(t) => t.convert(context).map(|expr| (expr, None)),
}
}
let span = self.span();
let mut iter = self.0.into_pairs();
let (first, mut punct) = iter
.next()
.ok_or(ConvertError::EmptyAlt(span))
.and_then(|pair| convert_pair(pair, context))?;
let mut rest = iter
.map(|pair| {
convert_pair(pair, context).map(|(snd, p)| (mem::replace(&mut punct, p), snd))
})
.peekable();
if let Some(_) = rest.peek() {
Ok(NamedExpression {
name: None,
expr: ast::Alt {
first: Box::new(first),
rest: rest.collect::<Result<_, _>>()?,
}
.into(),
span,
})
} else {
Ok(first)
}
}
}
impl Convert for Lambda {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
let span = self.span();
let mut args: Vec<_> = self.args.into_iter().map(Name::from).collect();
let expr = self.expr;
let inner = context.with_variables(args.clone(), |ctx| expr.convert(ctx))?;
Ok(NamedExpression {
name: None,
expr: ast::Lambda {
args,
inner: Box::new(inner),
}
.into(),
span,
})
}
}
impl Convert for Expression {
fn convert(self, context: &mut Context) -> Result<NamedExpression, ConvertError> {
match self {
Expression::Alt(a) => a.convert(context),
Expression::Lambda(l) => l.convert(context),
}
}
}
|