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
//! The parser is a bit more complex than it needs to be as it needs to be fully specialized to
//! avoid a recompilation every time a later part of the compiler is changed. Due to this the
//! string interner and therefore also garbage collector needs to compiled before the parser.
#![doc(html_root_url = "https://docs.rs/gluon_parser/0.6.1")] // # GLUON

#[macro_use]
extern crate collect_mac;
extern crate gluon_base as base;
extern crate itertools;
extern crate lalrpop_util;
#[macro_use]
extern crate log;
extern crate pretty;
#[macro_use]
extern crate quick_error;

use std::cell::RefCell;
use std::fmt;

use base::ast::{Comment, Expr, IdentEnv, SpannedExpr, SpannedPattern, TypedIdent, ValueBinding};
use base::error::Errors;
use base::pos::{self, BytePos, Span, Spanned};
use base::symbol::Symbol;
use base::types::{ArcType, TypeCache};

use infix::{OpTable, Reparser};
use layout::Layout;
use token::{Token, Tokenizer};

pub use infix::Error as InfixError;
pub use layout::Error as LayoutError;
pub use token::Error as TokenizeError;

#[cfg_attr(rustfmt, rustfmt_skip)]
mod grammar;
mod infix;
mod layout;
mod token;

fn new_ident<Id>(type_cache: &TypeCache<Id, ArcType<Id>>, name: Id) -> TypedIdent<Id> {
    TypedIdent {
        name: name,
        typ: type_cache.hole(),
    }
}

type LalrpopError<'input> = lalrpop_util::ParseError<
    BytePos,
    Token<'input>,
    Spanned<Error, BytePos>,
>;

/// Shrink hidden spans to fit the visible expressions and flatten singleton blocks.
fn shrink_hidden_spans<Id>(mut expr: SpannedExpr<Id>) -> SpannedExpr<Id> {
    match expr.value {
        Expr::Infix(_, _, ref last) |
        Expr::IfElse(_, _, ref last) |
        Expr::LetBindings(_, ref last) |
        Expr::TypeBindings(_, ref last) => expr.span.end = last.span.end,
        Expr::Lambda(ref lambda) => expr.span.end = lambda.body.span.end,
        Expr::Block(ref mut exprs) => match exprs.len() {
            0 => (),
            1 => return exprs.pop().unwrap(),
            _ => expr.span.end = exprs.last().unwrap().span.end,
        },
        Expr::Match(_, ref alts) => if let Some(last_alt) = alts.last() {
            let end = last_alt.expr.span.end;
            expr.span.end = end;
        },
        Expr::App(_, _) |
        Expr::Ident(_) |
        Expr::Literal(_) |
        Expr::Projection(_, _, _) |
        Expr::Array(_) |
        Expr::Record { .. } |
        Expr::Tuple { .. } |
        Expr::Error => (),
    }
    expr
}

fn transform_errors<'a, Iter>(errors: Iter) -> Errors<Spanned<Error, BytePos>>
where
    Iter: IntoIterator<Item = LalrpopError<'a>>,
{
    errors.into_iter().map(Error::from_lalrpop).collect()
}

struct Expected<'a>(&'a [String]);

impl<'a> fmt::Display for Expected<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0.len() {
            0 => (),
            1 => write!(f, "\nExpected ")?,
            _ => write!(f, "\nExpected one of ")?,
        }
        for (i, token) in self.0.iter().enumerate() {
            let sep = match i {
                0 => "",
                i if i + 1 < self.0.len() => ",",
                _ => " or",
            };
            write!(f, "{} {}", sep, token)?;
        }
        Ok(())
    }
}

quick_error! {
    #[derive(Debug, PartialEq)]
    pub enum Error {
        Token(err: TokenizeError) {
            description(err.description())
            display("{}", err)
            from()
        }
        Layout(err: LayoutError) {
            description(err.description())
            display("{}", err)
            from()
        }
        InvalidToken {
            description("invalid token")
            display("Invalid token")
        }
        UnexpectedToken(token: String, expected: Vec<String>) {
            description("unexpected token")
            display("Unexpected token: {}{}", token, Expected(&expected))
        }
        UnexpectedEof(expected: Vec<String>) {
            description("unexpected end of file")
            display("Unexpected end of file{}", Expected(&expected))
        }
        ExtraToken(token: String) {
            description("extra token")
            display("Extra token: {}", token)
        }
        Infix(err: InfixError) {
            description(err.description())
            display("{}", err)
            from()
        }
    }
}

/// LALRPOP currently has an unnecessary set of `"` around each expected token
fn remove_extra_quotes(tokens: &mut [String]) {
    for token in tokens {
        if token.starts_with('"') && token.ends_with('"') {
            token.remove(0);
            token.pop();
        }
    }
}

impl Error {
    fn from_lalrpop(err: LalrpopError) -> Spanned<Error, BytePos> {
        use lalrpop_util::ParseError::*;

        match err {
            InvalidToken { location } => pos::spanned2(location, location, Error::InvalidToken),
            UnrecognizedToken {
                token: Some((lpos, token, rpos)),
                mut expected,
            } => {
                remove_extra_quotes(&mut expected);
                pos::spanned2(
                    lpos,
                    rpos,
                    Error::UnexpectedToken(token.to_string(), expected),
                )
            }
            UnrecognizedToken {
                token: None,
                mut expected,
            } => {
                remove_extra_quotes(&mut expected);
                pos::spanned2(0.into(), 0.into(), Error::UnexpectedEof(expected))
            }
            ExtraToken {
                token: (lpos, token, rpos),
            } => pos::spanned2(lpos, rpos, Error::ExtraToken(token.to_string())),
            User { error } => error,
        }
    }
}

/// An iterator which forwards only the `Ok` values. If an `Err` is found the iterator returns
/// `None` and the error can be retrieved using the `result` method.
struct ResultOkIter<I, E> {
    iter: I,
    error: Option<E>,
}

impl<I, E> ResultOkIter<I, E> {
    fn new(iter: I) -> ResultOkIter<I, E> {
        ResultOkIter {
            iter: iter,
            error: None,
        }
    }

    fn result<T>(&mut self, value: T) -> Result<T, E> {
        match self.error.take() {
            Some(err) => Err(err),
            None => Ok(value),
        }
    }
}

impl<I, T, E> Iterator for ResultOkIter<I, E>
where
    I: Iterator<Item = Result<T, E>>,
    E: ::std::fmt::Debug,
{
    type Item = T;

    fn next(&mut self) -> Option<T> {
        match self.iter.next() {
            Some(Ok(t)) => Some(t),
            Some(Err(err)) => {
                self.error = Some(err);
                None
            }
            None => None,
        }
    }
}

/// An iterator which can be shared
struct SharedIter<'a, I: 'a> {
    iter: &'a RefCell<I>,
}

impl<'a, I> Clone for SharedIter<'a, I> {
    fn clone(&self) -> SharedIter<'a, I> {
        SharedIter { iter: self.iter }
    }
}

impl<'a, I> SharedIter<'a, I> {
    fn new(iter: &'a RefCell<I>) -> SharedIter<'a, I> {
        SharedIter { iter: iter }
    }
}

impl<'a, I> Iterator for SharedIter<'a, I>
where
    I: Iterator,
{
    type Item = I::Item;

    fn next(&mut self) -> Option<I::Item> {
        self.iter.borrow_mut().next()
    }
}

pub enum FieldPattern<Id> {
    Type(Spanned<Id, BytePos>, Option<Id>),
    Value(Spanned<Id, BytePos>, Option<SpannedPattern<Id>>),
}

pub enum FieldExpr<Id> {
    Type(Option<Comment>, Spanned<Id, BytePos>, Option<ArcType<Id>>),
    Value(
        Option<Comment>,
        Spanned<Id, BytePos>,
        Option<SpannedExpr<Id>>,
    ),
}

// Hack around LALRPOP's limited type syntax
type MutIdentEnv<'env, Id> = &'env mut IdentEnv<Ident = Id>;
type ErrorEnv<'err, 'input> = &'err mut Errors<LalrpopError<'input>>;

pub type ParseErrors = Errors<Spanned<Error, BytePos>>;

macro_rules! layout {
    ($result_ok_iter: ident, $input: expr) => { {
        let tokenizer = Tokenizer::new($input);
        $result_ok_iter = RefCell::new(ResultOkIter::new(tokenizer));

        Layout::new(SharedIter::new(&$result_ok_iter)).map(|token| {
            // Return the tokenizer error if one exists
            $result_ok_iter.borrow_mut()
                .result(())
                .map_err(|err| {
                    pos::spanned2(err.span.start.absolute,
                                err.span.end.absolute,
                                err.value.into())
                })?;
            let token = token.map_err(|err| pos::spanned(err.span, err.value.into()))?;
            debug!("Lex {:?}", token.value);
            let Span { start, end, .. } = token.span;
            Ok((start.absolute, token.value, end.absolute))
        })
    } }
}

pub fn parse_partial_expr<Id>(
    symbols: &mut IdentEnv<Ident = Id>,
    type_cache: &TypeCache<Id, ArcType<Id>>,
    input: &str,
) -> Result<SpannedExpr<Id>, (Option<SpannedExpr<Id>>, ParseErrors)>
where
    Id: Clone,
{
    let result_ok_iter;
    let layout = layout!(result_ok_iter, input);

    let mut parse_errors = Errors::new();

    let result = grammar::parse_TopExpr(input, type_cache, symbols, &mut parse_errors, layout);

    // If there is a tokenizer error it may still exist in the result iterator wrapper.
    // If that is the case we return that error instead of the unexpected EOF error that lalrpop
    // emitted
    if let Err(err) = result_ok_iter.borrow_mut().result(()) {
        parse_errors.pop(); // Remove the EOF error
        parse_errors.push(lalrpop_util::ParseError::User {
            error: pos::spanned2(
                err.span.start.absolute,
                err.span.end.absolute,
                err.value.into(),
            ),
        });
    }

    match result {
        Ok(mut expr) => {
            let mut errors = transform_errors(parse_errors);
            let mut reparser = Reparser::new(OpTable::default(), symbols);
            if let Err(reparse_errors) = reparser.reparse(&mut expr) {
                errors.extend(reparse_errors.into_iter().map(|err| err.map(Error::Infix)));
            }

            if errors.has_errors() {
                Err((Some(expr), errors))
            } else {
                Ok(expr)
            }
        }
        Err(err) => {
            parse_errors.push(err);
            Err((None, transform_errors(parse_errors)))
        }
    }
}

pub fn parse_expr(
    symbols: &mut IdentEnv<Ident = Symbol>,
    type_cache: &TypeCache<Symbol, ArcType>,
    input: &str,
) -> Result<SpannedExpr<Symbol>, ParseErrors> {
    parse_partial_expr(symbols, type_cache, input).map_err(|t| t.1)
}

pub type LetOrExpr<Id> = Result<SpannedExpr<Id>, ValueBinding<Id>>;

pub fn parse_partial_let_or_expr<Id>(
    symbols: &mut IdentEnv<Ident = Id>,
    input: &str,
) -> Result<LetOrExpr<Id>, (Option<LetOrExpr<Id>>, ParseErrors)>
where
    Id: Clone,
{
    let result_ok_iter;
    let layout = layout!(result_ok_iter, input);

    let mut parse_errors = Errors::new();

    let type_cache = TypeCache::new();

    let result = grammar::parse_LetOrExpr(input, &type_cache, symbols, &mut parse_errors, layout);

    // If there is a tokenizer error it may still exist in the result iterator wrapper.
    // If that is the case we return that error instead of the unexpected EOF error that lalrpop
    // emitted
    if let Err(err) = result_ok_iter.borrow_mut().result(()) {
        parse_errors.pop(); // Remove the EOF error
        parse_errors.push(lalrpop_util::ParseError::User {
            error: pos::spanned2(
                err.span.start.absolute,
                err.span.end.absolute,
                err.value.into(),
            ),
        });
    }

    match result {
        Ok(mut let_or_expr) => {
            let mut errors = transform_errors(parse_errors);
            let mut reparser = Reparser::new(OpTable::default(), symbols);
            let result = match let_or_expr {
                Ok(ref mut expr) => reparser.reparse(expr),
                Err(ref mut let_binding) => reparser.reparse(&mut let_binding.expr),
            };
            if let Err(reparse_errors) = result {
                errors.extend(reparse_errors.into_iter().map(|err| err.map(Error::Infix)));
            }

            if errors.has_errors() {
                Err((Some(let_or_expr), errors))
            } else {
                Ok(let_or_expr)
            }
        }
        Err(err) => {
            parse_errors.push(err);
            Err((None, transform_errors(parse_errors)))
        }
    }
}

#[cfg(feature = "test")]
pub fn parse_string<'env, 'input>(
    symbols: &'env mut IdentEnv<Ident = String>,
    input: &'input str,
) -> Result<SpannedExpr<String>, (Option<SpannedExpr<String>>, ParseErrors)> {
    parse_partial_expr(symbols, &TypeCache::new(), input)
}