summaryrefslogtreecommitdiff
path: root/src/nibble/mod.rs
blob: dbb05b0db7bbdc9bcf019bee627ce6f4099c99d8 (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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
pub mod convert;

use std::fmt;

use proc_macro2::Span;
use syn::{
    bracketed,
    ext::IdentExt,
    parenthesized,
    parse::{Parse, ParseStream},
    punctuated::{Pair, Punctuated},
    token::{Bracket, Comma, Let, Match, Paren},
    LitStr, Token,
};

use crate::chomp::{Name, ast::{self, TopLevel}};

use convert::{Context, Convert, ConvertError};

pub type Epsilon = Token![_];

pub type Ident = syn::Ident;

pub type Literal = LitStr;

#[derive(Clone)]
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 })
    }
}

impl<T: fmt::Debug> fmt::Debug for ArgList<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "ArgList")?;
        f.debug_list().entries(self.args.iter()).finish()
    }
}

#[derive(Clone)]
pub struct Fix {
    bang_token: Token![!],
    pub expr: Box<Term>,
}

impl Fix {
    pub fn span(&self) -> Option<Span> {
        self.bang_token.span.join(self.expr.span()?)
    }
}

impl Parse for Fix {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let bang_token = input.parse()?;
        let expr = input.parse()?;
        Ok(Self { bang_token, expr })
    }
}

impl fmt::Debug for Fix {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Fix").field("expr", &self.expr).finish()
    }
}

#[derive(Clone)]
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 })
    }
}

impl fmt::Debug for ParenExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ParenExpression")
            .field("expr", &self.expr)
            .finish()
    }
}

#[derive(Clone)]
pub enum Term {
    Epsilon(Epsilon),
    Ident(Ident),
    Literal(Literal),
    Fix(Fix),
    Parens(ParenExpression),
}

impl Term {
    pub fn span(&self) -> Option<Span> {
        match self {
            Self::Epsilon(e) => Some(e.span),
            Self::Ident(i) => Some(i.span()),
            Self::Literal(l) => Some(l.span()),
            Self::Fix(f) => f.span(),
            Self::Parens(p) => Some(p.paren_token.span),
        }
    }
}

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(Token![!]) {
            input.parse().map(Self::Fix)
        } else if lookahead.peek(Paren) {
            input.parse().map(Self::Parens)
        } else if lookahead.peek(Ident::peek_any) {
            input.call(Ident::parse_any).map(Self::Ident)
        } else {
            Err(lookahead.error())
        }
    }
}

impl fmt::Debug for Term {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Term::Epsilon(_) => write!(f, "Term::Epsilon"),
            Term::Ident(i) => write!(f, "Term::Ident({:?})", i),
            Term::Literal(l) => write!(f, "Term::Literal({:?})", l.value()),
            Term::Fix(x) => write!(f, "Term::Fix({:?})", x),
            Term::Parens(p) => write!(f, "Term::Parens({:?})", p),
        }
    }
}

#[derive(Clone, Debug)]
pub struct Call(pub Vec<Term>);

impl Call {
    pub fn span(&self) -> Option<Span> {
        let mut iter = self.0.iter();
        let first = iter.next()?.span()?;
        iter.try_fold(first, |span, t| t.span().and_then(|s| span.join(s)))
    }
}

impl Parse for Call {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let mut out = Vec::new();
        out.push(input.parse()?);
        loop {
            let lookahead = input.lookahead1();
            if lookahead.peek(Token![_])
                || lookahead.peek(LitStr)
                || lookahead.peek(Token![!])
                || lookahead.peek(Paren)
                || lookahead.peek(Ident::peek_any)
            {
                out.push(input.parse()?);
            } else {
                break;
            }
        }

        Ok(Self(out))
    }
}

#[derive(Clone)]
pub struct Cat(pub Punctuated<Call, Token![.]>);

impl Parse for Cat {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        input.call(Punctuated::parse_separated_nonempty).map(Self)
    }
}

impl Cat {
    pub fn span(&self) -> Option<Span> {
        let mut iter = self.0.pairs();
        let span = match iter.next()? {
            Pair::Punctuated(t, p) => t.span().and_then(|s| s.join(p.span)),
            Pair::End(t) => t.span(),
        }?;

        iter.try_fold(span, |span, pair| match pair {
            Pair::Punctuated(t, p) => t
                .span()
                .and_then(|s| span.join(s))
                .and_then(|s| s.join(p.span)),
            Pair::End(t) => t.span().and_then(|s| span.join(s)),
        })
    }
}

impl fmt::Debug for Cat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Cat")?;
        f.debug_list().entries(self.0.iter()).finish()
    }
}

#[derive(Clone)]
pub struct Label {
    colon_tok: Token![:],
    pub label: Ident,
}

impl Label {
    pub fn span(&self) -> Option<Span> {
        self.colon_tok.span.join(self.label.span())
    }
}

impl Parse for Label {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let colon_tok = input.parse()?;
        let label = input.call(Ident::parse_any)?;
        Ok(Self { colon_tok, label })
    }
}

impl fmt::Debug for Label {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Label").field("label", &self.label).finish()
    }
}

#[derive(Clone, Debug)]
pub struct Labelled {
    pub cat: Cat,
    pub label: Option<Label>,
}

impl Labelled {
    pub fn span(&self) -> Option<Span> {
        self.cat.span().and_then(|s| {
            self.label
                .as_ref()
                .and_then(|l| l.span().and_then(|t| s.join(t)))
        })
    }
}

impl Parse for Labelled {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let cat = input.parse()?;
        let label = if input.peek(Token![:]) {
            Some(input.parse()?)
        } else {
            None
        };
        Ok(Self { cat, label })
    }
}

#[derive(Clone)]
pub struct Alt(pub Punctuated<Labelled, Token![|]>);

impl Alt {
    pub fn span(&self) -> Option<Span> {
        let mut iter = self.0.pairs();
        let span = match iter.next()? {
            Pair::Punctuated(t, p) => t.span().and_then(|s| s.join(p.span)),
            Pair::End(t) => t.span(),
        }?;

        iter.try_fold(span, |span, pair| match pair {
            Pair::Punctuated(t, p) => t
                .span()
                .and_then(|s| span.join(s))
                .and_then(|s| s.join(p.span)),
            Pair::End(t) => t.span().and_then(|s| span.join(s)),
        })
    }
}

impl Parse for Alt {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        input.call(Punctuated::parse_separated_nonempty).map(Self)
    }
}

impl fmt::Debug for Alt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Alt")?;
        f.debug_list().entries(self.0.iter()).finish()
    }
}
#[derive(Clone)]
pub struct Lambda {
    slash_tok_left: Token![/],
    pub args: ArgList<Ident>,
    slash_tok_right: Token![/],
    pub expr: Alt,
}

impl Lambda {
    pub fn span(&self) -> Option<Span> {
        self.slash_tok_left.span.join(self.expr.span()?)
    }
}

impl Parse for Lambda {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let slash_tok_left = input.parse()?;
        let args = input.parse()?;
        let slash_tok_right = input.parse()?;
        let expr = input.parse()?;
        Ok(Self {
            slash_tok_left,
            args,
            slash_tok_right,
            expr,
        })
    }
}

impl fmt::Debug for Lambda {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Lambda")
            .field("args", &self.args)
            .field("expr", &self.expr)
            .finish()
    }
}

#[derive(Clone, Debug)]
pub enum Expression {
    Alt(Alt),
    Lambda(Lambda),
}

impl Parse for Expression {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        if input.peek(Token![/]) {
            input.parse().map(Self::Lambda)
        } else {
            input.parse().map(Self::Alt)
        }
    }
}

#[derive(Clone)]
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,
        })
    }
}

impl fmt::Debug for GoalStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("GoalStatement")
            .field("expr", &self.expr)
            .finish()
    }
}

#[derive(Clone)]
pub struct LetStatement {
    let_token: Token![let],
    name: Ident,
    args: Option<ArgList<Ident>>,
    eq_token: Token![=],
    expr: Expression,
    semi_token: Token![;],
    next: Box<Statement>,
}

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()?;
        let next = Box::new(input.parse()?);

        Ok(Self {
            let_token,
            name,
            args,
            eq_token,
            expr,
            semi_token,
            next,
        })
    }
}

impl fmt::Debug for LetStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LetStatement")
            .field("name", &self.name)
            .field("args", &self.args)
            .field("expr", &self.expr)
            .field("next", &self.next)
            .finish()
    }
}

#[derive(Clone, Debug)]
pub enum Statement {
    Goal(GoalStatement),
    Let(LetStatement),
}

impl Statement {
    pub fn convert(self) -> Result<TopLevel, ConvertError> {
        let mut stmt = self;
        let mut context = Context::new();
        let mut name_val = Vec::new();
        while let Self::Let(let_stmt) = stmt {
            let mut val = match let_stmt.args {
                Some(args) => {
                    todo!()
                }
                None => let_stmt.expr.convert(&mut context),
            }?;
            let name: Name = let_stmt.name.into();
            val.name = val.name.or_else(|| Some(name.clone()));
            context.push_variable(name.clone());
            name_val.push((name, val));
            stmt = *let_stmt.next;
        }

        let goal = match stmt {
            Statement::Goal(goal) => TopLevel::Goal(goal.expr.convert(&mut context)?),
            Statement::Let(_) => unreachable!(),
        };

        Ok(name_val.into_iter().rfold(goal, |inner, (name, val)| {
            TopLevel::Let(ast::Let {
                name,
                val,
                inner: Box::new(inner),
            })
        }))
    }
}

impl Parse for Statement {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let mut lookahead = input.lookahead1();

        if lookahead.peek(Let) {
            input.parse().map(Self::Let)
        } else if lookahead.peek(Match) {
            input.parse().map(Self::Goal)
        } else {
            Err(lookahead.error())
        }
    }
}