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
|
use proc_macro2::Span;
use syn::{
bracketed,
ext::IdentExt,
parenthesized,
parse::{Parse, ParseStream},
punctuated::Punctuated,
token::{Bracket, Comma, Let, Match, Paren},
LitStr, Token,
};
use crate::chomp::ast;
use super::convert::{Context, Convert};
pub type Epsilon = Token![_];
pub type Ident = syn::Ident;
pub type Literal = LitStr;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ArgList<T> {
paren_token: Paren,
args: Punctuated<T, Comma>,
}
impl<T> ArgList<T> {
pub fn span(&self) -> Span {
self.paren_token.span
}
pub fn len(&self) -> usize {
self.args.len()
}
pub fn is_empty(&self) -> bool {
self.args.is_empty()
}
}
impl<T> IntoIterator for ArgList<T> {
type Item = T;
type IntoIter = <Punctuated<T, Comma> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.args.into_iter()
}
}
impl<T: Parse> Parse for ArgList<T> {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let args;
let paren_token = parenthesized!(args in input);
let args = args.call(Punctuated::parse_terminated)?;
Ok(Self { paren_token, args })
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Call {
pub name: Ident,
pub args: ArgList<Expression>,
}
impl Call {
pub fn span(&self) -> Option<Span> {
self.name.span().join(self.args.span())
}
}
impl Parse for Call {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let name = input.call(Ident::parse_any)?;
let args = input.parse()?;
Ok(Self { name, args })
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Fix {
bracket_token: Bracket,
pub arg: Ident,
paren_token: Paren,
pub expr: Expression,
}
impl Fix {
pub fn span(&self) -> Option<Span> {
self.bracket_token.span.join(self.paren_token.span)
}
}
impl Parse for Fix {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let arg;
let bracket_token = bracketed!(arg in input);
let arg = arg.call(Ident::parse_any)?;
let expr;
let paren_token = parenthesized!(expr in input);
let expr = expr.parse()?;
Ok(Self {
bracket_token,
arg,
paren_token,
expr,
})
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParenExpression {
paren_token: Paren,
pub expr: Expression,
}
impl Parse for ParenExpression {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let expr;
let paren_token = parenthesized!(expr in input);
let expr = expr.parse()?;
Ok(Self { paren_token, expr })
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Term {
Epsilon(Epsilon),
Ident(Ident),
Literal(Literal),
Call(Call),
Fix(Fix),
Parens(ParenExpression),
}
impl Parse for Term {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(Token![_]) {
input.parse().map(Self::Epsilon)
} else if lookahead.peek(LitStr) {
input.parse().map(Self::Literal)
} else if lookahead.peek(Bracket) {
input.parse().map(Self::Fix)
} else if lookahead.peek(Paren) {
input.parse().map(Self::Parens)
} else if lookahead.peek(Ident::peek_any) {
let name = input.call(Ident::parse_any)?;
if input.peek(Paren) {
input.parse().map(|args| Self::Call(Call { name, args }))
} else {
Ok(Self::Ident(name))
}
} else {
Err(lookahead.error())
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Cat(pub Punctuated<Term, Token![.]>);
impl Parse for Cat {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
input.call(Punctuated::parse_separated_nonempty).map(Self)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Alt(pub Punctuated<Cat, Token![|]>);
impl Parse for Alt {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
input.call(Punctuated::parse_separated_nonempty).map(Self)
}
}
pub type Expression = Alt;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LetStatement {
let_token: Token![let],
name: Ident,
args: Option<ArgList<Ident>>,
eq_token: Token![=],
expr: Expression,
semi_token: Token![;],
}
impl LetStatement {
pub fn span(&self) -> Option<Span> {
self.let_token.span.join(self.semi_token.span)
}
}
impl Parse for LetStatement {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let let_token = input.parse()?;
let name = input.call(Ident::parse_any)?;
let args = if input.peek(Paren) {
Some(input.parse()?)
} else {
None
};
let eq_token = input.parse()?;
let expr = input.parse()?;
let semi_token = input.parse()?;
Ok(Self {
let_token,
name,
args,
eq_token,
expr,
semi_token,
})
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GoalStatement {
match_token: Token![match],
expr: Expression,
semi_token: Token![;],
}
impl Parse for GoalStatement {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let match_token = input.parse()?;
let expr = input.parse()?;
let semi_token = input.parse()?;
Ok(Self {
match_token,
expr,
semi_token,
})
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct File {
lets: Vec<LetStatement>,
goal: GoalStatement,
}
impl File {
pub fn convert(self) -> Option<(Vec<ast::Function>, ast::Expression)> {
let mut names = Vec::new();
let mut map = Vec::new();
for stmt in self.lets {
let count = stmt.args.as_ref().map(ArgList::len).unwrap_or_default();
let span = stmt.span();
let mut context = Context::new(
&names,
stmt.args.into_iter().flat_map(|args| args.into_iter()),
);
names.push(stmt.name.clone());
map.push(ast::Function::new(
stmt.name.clone(),
count,
stmt.expr.convert(&mut context)?,
span,
));
}
let mut context = Context::new(&names, Vec::new());
let goal = self.goal.expr.convert(&mut context)?;
Some((map, goal))
}
}
impl Parse for File {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let mut lets = Vec::new();
let mut lookahead = input.lookahead1();
while lookahead.peek(Let) {
lets.push(input.parse()?);
lookahead = input.lookahead1();
}
let goal = if lookahead.peek(Match) {
input.parse()?
} else {
return Err(lookahead.error());
};
Ok(Self { lets, goal })
}
}
|