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
use crate::lexer::token_stream::TokenStream;
use crate::lexer::{Location, Token};
use std::fmt::{Display, Formatter};
use std::path::PathBuf;
use std::sync::Arc;
use thiserror::Error;

pub type Result<T> = std::result::Result<T, ParolError>;

#[derive(Error, Debug)]
pub enum ParserError {
    #[error(transparent)]
    TreeError { source: syntree::Error },

    #[error("Error in generated source: {0}")]
    DataError(&'static str),

    #[error("Error in input: {cause}")]
    PredictionError { cause: String },

    #[error("Syntax error(s)")]
    SyntaxErrors { entries: Vec<SyntaxError> },

    #[error("Unprocessed input is left after parsing has finished")]
    UnprocessedInput {
        input: Box<FileSource>,
        last_token: Box<Location>,
    },

    #[error("{context}Tried to pop from an empty scanner stack")]
    PopOnEmptyScannerStateStack {
        context: String,
        input: FileSource,
        source: LexerError,
    },

    #[error("{0}")]
    InternalError(String),
}

#[derive(Error, Debug)]
pub enum LexerError {
    #[error("No valid token read")]
    TokenBufferEmptyError,

    #[error("{0}")]
    InternalError(String),

    #[error("Lookahead exceeds its maximum")]
    LookaheadExceedsMaximum,

    #[error("Lookahead exceeds token buffer length")]
    LookaheadExceedsTokenBufferLength,

    #[error("pop_scanner: Tried to pop from an empty scanner stack!")]
    ScannerStackEmptyError,

    #[error("{0}")]
    RecoveryError(String),
}

#[derive(Error, Debug)]
pub enum ParolError {
    #[error(transparent)]
    ParserError(#[from] ParserError),
    #[error(transparent)]
    LexerError(#[from] LexerError),
    #[error(transparent)]
    UserError(#[from] anyhow::Error),
}

#[derive(Error, Debug, Default)]
#[error("{cause}")]
pub struct SyntaxError {
    pub cause: String,
    pub input: Option<Box<FileSource>>,
    pub error_location: Box<Location>,
    pub unexpected_tokens: Vec<UnexpectedToken>,
    pub expected_tokens: TokenVec,
    pub source: Option<Box<ParolError>>,
}

impl SyntaxError {
    pub(crate) fn with_cause(mut self, cause: &str) -> Self {
        self.cause = cause.to_owned();
        self
    }

    pub(crate) fn with_location(mut self, location: Location) -> Self {
        self.error_location = Box::new(location);
        self
    }
}

#[derive(Debug)]
pub struct UnexpectedToken {
    pub name: String,
    pub token_type: String,
    pub token: Location,
}

impl UnexpectedToken {
    pub fn new(name: String, token_type: String, token: &Token<'_>) -> Self {
        let token = token.into();
        Self {
            name,
            token_type,
            token,
        }
    }
}

impl Display for UnexpectedToken {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} {}: {}", self.name, self.token_type, self.token)
    }
}

#[derive(Debug, Default)]
pub struct TokenVec(Vec<String>);

impl TokenVec {
    pub fn push(&mut self, token: String) {
        self.0.push(token);
    }
}

impl Display for TokenVec {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        write!(
            f,
            "{}",
            self.0.iter().fold(String::new(), |mut acc, e| {
                if !acc.is_empty() {
                    acc.push_str(", ");
                }
                acc.push_str(e.to_string().as_str());
                acc
            })
        )
    }
}

#[derive(Debug)]
pub struct FileSource {
    pub file_name: Arc<PathBuf>,
    pub input: String,
}

impl FileSource {
    pub fn try_new(file_name: Arc<PathBuf>) -> std::result::Result<Self, std::io::Error> {
        let file_name = file_name.clone();
        let input = std::fs::read_to_string(&*file_name)?;
        Ok(Self { file_name, input })
    }

    pub fn from_stream(token_stream: &TokenStream<'_>) -> Self {
        let file_name = token_stream.file_name.clone();
        let input = token_stream.input.to_string();
        Self { file_name, input }
    }
}