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
use std::{fmt, error::Error};
use crate::{PropSelector, Token};

pub(crate) type ParserError = lalrpop_util::ParseError<usize, String, AscesisError>;
pub(crate) type RawParserError<'input> =
    lalrpop_util::ParseError<usize, Token<'input>, AscesisError>;
pub(crate) type RawParserRecovery<'input> =
    lalrpop_util::ErrorRecovery<usize, Token<'input>, AscesisError>;

impl From<AscesisError> for ParserError {
    fn from(error: AscesisError) -> Self {
        ParserError::User { error }
    }
}

impl<'input> From<AscesisError> for RawParserError<'input> {
    fn from(error: AscesisError) -> Self {
        RawParserError::User { error }
    }
}

fn format_location(mut pos: usize, script: &str) -> String {
    for (num_lines, line) in script.lines().enumerate() {
        match pos.checked_sub(line.len() + 1) {
            Some(p) => pos = p,
            None => return format!("[{}:{}]", num_lines + 1, pos + 1),
        }
    }

    // FIXME
    "<outside>".into()
}

fn format_span(span: &logos::Span, script: &str) -> String {
    let mut start_location = None;
    let mut end_location = None;
    let mut pos = span.start;

    for (num_lines, line) in script.lines().enumerate() {
        match pos.checked_sub(line.len() + 1) {
            Some(p) => pos = p,
            None => {
                if start_location.is_none() {
                    start_location = Some((num_lines + 1, pos + 1));

                    if span.end > span.start {
                        let remaining = line.len() + 1 - pos;
                        let end_pos = span.end - span.start;

                        if end_pos > remaining {
                            pos = end_pos - remaining;
                        } else {
                            end_location = Some((num_lines + 1, pos + end_pos + 1));
                            break
                        }
                    } else {
                        break
                    }
                } else {
                    end_location = Some((num_lines + 1, pos + 1));
                    break
                }
            }
        }
    }

    if let Some((start_line, start_pos)) = start_location {
        if let Some((end_line, end_pos)) = end_location {
            format!("[{}:{}]..[{}:{}]", start_line, start_pos, end_line, end_pos)
        } else {
            // FIXME
            format!("[{}:{}]", start_line, start_pos)
        }
    } else {
        // FIXME
        "<empty-span>".into()
    }
}

fn display_parsing_recovery(
    errors: &[ParserError],
    script: Option<&str>,
    f: &mut fmt::Formatter,
) -> fmt::Result {
    for (num, err) in errors.iter().enumerate() {
        let message = if let Some(script) = script {
            format!("{}", err.clone().map_location(|pos| format_location(pos, script)))
        } else {
            format!("{}", err)
        };
        let mut lines = message.lines();

        if let Some(line) = lines.next() {
            if num > 0 {
                write!(f, "\nerror: {}", line)?;
            } else {
                write!(f, "{}", line)?;
            }

            for line in lines {
                write!(f, "\n\t{}", line)?;
            }
        }
    }

    Ok(())
}

fn display_lexing_failure(
    token: &str,
    span: &logos::Span,
    script: &str,
    f: &mut fmt::Formatter,
) -> fmt::Result {
    write!(f, "Invalid token \"{}\" at {}", token, format_span(span, script))
}

#[derive(Clone, Debug)]
pub enum AscesisErrorKind {
    ParsingRecovery(Vec<ParserError>),
    LexingFailure(String, logos::Span),
    ParsingFailure,
    AxiomUnknown(String),
    RootUnset,
    RootMissing(String),
    RootRedefined(String),
    RootBlockMismatch,
    RootBlockMissing,
    RootUnresolvable,
    ScriptUncompiled,
    UnexpectedDependency(String),
    InvalidAST,
    FatLeak,
    MissingPropSelector,
    InvalidPropSelector(String),
    InvalidPropType(PropSelector, String),
    InvalidPropValue(PropSelector, String, String),
    InvalidPropValueType(String),
    BlockSelectorMismatch(PropSelector, PropSelector),
    SizeLiteralOverflow,
    ExpectedSizeLiteral,
    ExpectedNameLiteral,
}

impl fmt::Display for AscesisErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use AscesisErrorKind::*;

        match self {
            ParsingRecovery(ref errors) => display_parsing_recovery(errors, None, f),
            LexingFailure(token, span) => write!(f, "Invalid token \"{}\" at {:?}", token, span),
            ParsingFailure => write!(f, "Recovering from ascesis parsing errors"),
            AxiomUnknown(symbol) => write!(f, "Unknown axiom '{}'", symbol),
            RootUnset => write!(f, "Undeclared root structure"),
            RootMissing(name) => write!(f, "Missing root structure '{}'", name),
            RootRedefined(name) => write!(f, "Redefined root structure '{}'", name),
            RootBlockMismatch => write!(f, "Root block mismatch"),
            RootBlockMissing => write!(f, "Root block missing"),
            RootUnresolvable => write!(f, "Root contains instances without known definitions"),
            ScriptUncompiled => write!(f, "Script uncompiled"),
            UnexpectedDependency(name) => write!(f, "Unexpected uncompiled dependency '{}'", name),
            InvalidAST => write!(f, "Invalid AST"),
            FatLeak => write!(f, "Fat arrow rule leaked through FIT transformation"),
            MissingPropSelector => write!(f, "Property block without selector"),
            InvalidPropSelector(name) => write!(f, "Invalid block selector '{}'", name),
            InvalidPropType(selector, prop) => write!(f, "Invalid {} {} type", selector, prop),
            InvalidPropValue(selector, prop, value) => {
                write!(f, "Invalid {} {} '{}'", selector, prop, value)
            }
            InvalidPropValueType(given) => write!(f, "Property value type not a {}", given),
            BlockSelectorMismatch(expected, actual) => {
                write!(f, "Expecting {} selector, got {}", expected, actual)
            }
            SizeLiteralOverflow => write!(f, "Size literal overflow"),
            ExpectedSizeLiteral => write!(f, "Bad literal, not a size"),
            ExpectedNameLiteral => write!(f, "Bad literal, not a name"),
        }
    }
}

impl AscesisErrorKind {
    pub fn with_script<S: AsRef<str>>(self, script: S) -> AscesisError {
        AscesisError { script: Some(script.as_ref().to_owned()), kind: self }
    }
}

impl From<ParserError> for AscesisErrorKind {
    fn from(err: ParserError) -> Self {
        AscesisErrorKind::ParsingRecovery(vec![err])
    }
}

impl<'input> From<RawParserError<'input>> for AscesisErrorKind {
    fn from(err: RawParserError<'input>) -> Self {
        AscesisErrorKind::ParsingRecovery(vec![err.map_token(|t| t.to_string())])
    }
}

impl<'input> From<Vec<RawParserRecovery<'input>>> for AscesisErrorKind {
    fn from(err: Vec<RawParserRecovery<'input>>) -> Self {
        let err = err.into_iter().map(|e| e.error.map_token(|t| t.to_string())).collect();

        AscesisErrorKind::ParsingRecovery(err)
    }
}

#[derive(Clone, Debug)]
pub struct AscesisError {
    script: Option<String>,
    kind:   AscesisErrorKind,
}

impl From<AscesisErrorKind> for AscesisError {
    #[inline]
    fn from(kind: AscesisErrorKind) -> Self {
        AscesisError { script: None, kind }
    }
}

impl fmt::Display for AscesisError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(ref script) = self.script {
            use AscesisErrorKind::*;

            match self.kind {
                ParsingRecovery(ref errors) => display_parsing_recovery(errors, Some(script), f),
                LexingFailure(ref token, ref span) => {
                    display_lexing_failure(token.as_str(), span, script, f)
                }
                ref kind => kind.fmt(f),
            }
        } else {
            self.kind.fmt(f)
        }
    }
}

impl Error for AscesisError {}