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
// Copyright (c) Facebook, Inc. and its affiliates
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::syntax::{Equality, Ident};

/// Raw error cases.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum RawError {
    // Lexer
    InvalidUtf8String(std::string::FromUtf8Error),
    InvalidInteger(std::num::ParseIntError),
    InvalidHexadecimal(String),
    UnexpectedChar(Option<u8>, Vec<u8>),
    UnexpectedWord(String, Vec<&'static str>),
    // Parser
    UndefinedIdent(Ident),
    CannotAttachMeaning(Ident),
    CannotAttachVarNames(Ident),
    UnknownCommand(String),
    InvalidEndOfInstance,
    InvalidInstanceKey,
    InvalidMatchKey,
    MissingBody,
    InvalidEnodeGeneration,
    CannotAttachEnode(usize, usize),
    CannotProcessEquality(Ident, Equality),
    CannotCheckEquality(Ident, Ident),
}

/// Record a position in the input stream.
#[derive(Clone, Eq, PartialEq)]
pub struct Position {
    /// Optional path name for the input stream.
    pub path_name: Option<String>,
    /// Line number in the input stream.
    pub line: usize,
    /// Column number in the line.
    pub column: usize,
}

/// An error together with a position where the error occurred.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Error {
    pub position: Position,
    pub error: RawError,
}

/// Result type based on `RawError`.
pub type RawResult<T> = std::result::Result<T, RawError>;

/// Result type based on `Error`.
pub type Result<T> = std::result::Result<T, Error>;

impl std::fmt::Debug for Position {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let file = match &self.path_name {
            Some(p) => format!("{}:", p),
            None => String::new(),
        };
        write!(f, "{}{}:{}", file, self.line + 1, self.column + 1)
    }
}