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
use std::ops::Range;

use crate::error::Error;
use ariadne::{ColorGenerator, Label, Report, ReportKind, Source};
use itertools::Itertools;
use lalrpop_util::ParseError;
use lexer::{Lexer, LexicalError};
use tokens::Token;

pub mod error;
pub mod lexer;
pub mod tokens;

pub mod grammar {
    #![allow(dead_code, unused_imports, unused_variables)]

    pub use self::grammar::*;
    use lalrpop_util::lalrpop_mod;

    lalrpop_mod!(pub grammar);
}

pub fn parse_ast(
    source: &str,
    module_name: &str,
) -> Result<edlang_ast::Module, ParseError<usize, Token, LexicalError>> {
    let lexer = Lexer::new(source);
    let parser = grammar::TopLevelModuleParser::new();
    parser.parse(module_name, lexer)
}

pub fn print_report<'a>(
    path: &'a str,
    source: &'a str,
    report: Report<'static, (&'a str, Range<usize>)>,
) -> Result<(), std::io::Error> {
    let source = Source::from(source);
    report.eprint((path, source))
}

pub fn error_to_report<'a>(
    path: &'a str,
    error: &Error,
) -> Result<Report<'static, (&'a str, Range<usize>)>, std::io::Error> {
    let mut colors = ColorGenerator::new();
    let report = match error {
        ParseError::InvalidToken { location } => {
            let loc = *location;
            Report::build(ReportKind::Error, path, loc)
                .with_code("InvalidToken")
                .with_label(
                    Label::new((path, loc..(loc + 1)))
                        .with_color(colors.next())
                        .with_message("invalid token"),
                )
                .finish()
        }
        ParseError::UnrecognizedEof { location, expected } => {
            let loc = *location;
            Report::build(ReportKind::Error, path, loc)
                .with_code("UnrecognizedEof")
                .with_label(
                    Label::new((path, loc..(loc + 1)))
                        .with_message(format!(
                            "unrecognized eof, expected one of the following: {}",
                            expected.iter().join(", ")
                        ))
                        .with_color(colors.next()),
                )
                .finish()
        }
        ParseError::UnrecognizedToken { token, expected } => {
            Report::build(ReportKind::Error, path, token.0)
                .with_code("UnrecognizedToken")
                .with_label(
                    Label::new((path, token.0..token.2))
                        .with_message(format!(
                            "unrecognized token {:?}, expected one of the following: {}",
                            token.1,
                            expected.iter().join(", ")
                        ))
                        .with_color(colors.next()),
                )
                .finish()
        }
        ParseError::ExtraToken { token } => Report::build(ReportKind::Error, path, token.0)
            .with_code("ExtraToken")
            .with_message("Extra token")
            .with_label(
                Label::new((path, token.0..token.2))
                    .with_message(format!("unexpected extra token {:?}", token.1)),
            )
            .finish(),
        ParseError::User { error } => match error {
            LexicalError::InvalidToken(err, range) => match err {
                tokens::LexingError::NumberParseError => {
                    Report::build(ReportKind::Error, path, range.start)
                        .with_code("InvalidToken")
                        .with_message("Error parsing literal number")
                        .with_label(
                            Label::new((path, range.start..range.end))
                                .with_message("error parsing literal number")
                                .with_color(colors.next()),
                        )
                        .finish()
                }
                tokens::LexingError::Other => Report::build(ReportKind::Error, path, range.start)
                    .with_code("Other")
                    .with_message("Other error")
                    .with_label(
                        Label::new((path, range.start..range.end))
                            .with_message("other error")
                            .with_color(colors.next()),
                    )
                    .finish(),
            },
        },
    };

    Ok(report)
}

#[cfg(test)]
mod test {}