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
use crate::directive::Directive;
use crate::macros::{MacroCall, MacroDef};
use erl_tokenize::tokens::SymbolToken;
use erl_tokenize::{LexicalToken, Position, PositionRange};
use std::path::{Path, PathBuf};

/// Possible errors.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
#[allow(missing_docs)]
#[allow(clippy::large_enum_variant)]
pub enum Error {
    /// Unexpected token.
    #[error("expected a {expected:?} token, but found {token:?}")]
    UnexpectedToken {
        token: LexicalToken,
        expected: String,
    },

    /// Include file error.
    #[error("cannot include file: path={target_file_path:?}, reason={source}")]
    IncludeFileError {
        source: std::io::Error,
        directive_start: Position,
        directive_end: Position,
        target_file_path: PathBuf,
    },

    /// Missing a macro argument.
    #[error("expected an macro argument before ',' ({position})")]
    MissingMacroArg { position: Position },

    /// Unbalanced parentheses.
    #[error("unbalanced parentheses: open={open:?}, close={close:?}")]
    UnbalancedParen {
        open: Option<SymbolToken>,
        close: SymbolToken,
    },

    /// Unexpected EOF.
    #[error("unexpected EOF")]
    UnexpectedEof,

    /// Cannot expand ?FILE macro.
    #[error("cannot expand ?FILE macro ({macro_call:?})")]
    FileNotSet { macro_call: MacroCall },

    /// Undefined macro.
    #[error("undefined macro: {macro_call:?}")]
    UndefinedMacro { macro_call: MacroCall },

    /// Undefined macro variable.
    #[error("no such macro variable: {varname:?}")]
    UndefinedMacroVar { varname: String },

    /// Macro arguments mismatched.
    #[error("macro arguments mismatched: def={macro_def:?}, call={macro_call:?}")]
    MacroArgsMismatched {
        macro_call: MacroCall,
        macro_def: MacroDef,
    },

    /// Non UTF-8 path.
    #[error("cannot convert a path {path:?} to a UTF-8 string")]
    NonUtf8Path { path: PathBuf },

    /// Unexpected '.' in `-define` directive.
    #[error("found unexpected '.' in `-define` directive ({position})")]
    UnexpectedDotInMacroDef { position: Position },

    /// Missing `-ifdef` or `-ifndef`.
    #[error("missing `-ifdef` or `ifndef` directives")]
    MissingIfDirective { directive: Directive },

    /// Tokenize error.
    #[error(transparent)]
    TokenizeError(#[from] erl_tokenize::Error),

    /// Glob pattern error.
    #[error(transparent)]
    GlobPatternError(#[from] glob::PatternError),

    /// Glob error.
    #[error(transparent)]
    GlobError(#[from] glob::GlobError),
}

impl Error {
    pub(crate) fn unexpected_token(token: LexicalToken, expected: &str) -> Self {
        Self::UnexpectedToken {
            token,
            expected: expected.to_owned(),
        }
    }

    pub(crate) fn include_file_error(
        source: std::io::Error,
        directive: &impl PositionRange,
        target_file_path: PathBuf,
    ) -> Self {
        Self::IncludeFileError {
            source,
            directive_start: directive.start_position(),
            directive_end: directive.end_position(),
            target_file_path,
        }
    }

    pub(crate) fn missing_macro_arg(position: Position) -> Self {
        Self::MissingMacroArg { position }
    }

    pub(crate) fn unbalanced_paren(open: Option<SymbolToken>, close: SymbolToken) -> Self {
        Self::UnbalancedParen { open, close }
    }

    pub(crate) fn file_not_set(macro_call: MacroCall) -> Self {
        Self::FileNotSet { macro_call }
    }

    pub(crate) fn undefined_macro(macro_call: MacroCall) -> Self {
        Self::UndefinedMacro { macro_call }
    }

    pub(crate) fn non_utf8_path(path: impl AsRef<Path>) -> Self {
        Self::NonUtf8Path {
            path: path.as_ref().to_path_buf(),
        }
    }

    pub(crate) fn unexpected_dot_in_macro_def(token: &LexicalToken) -> Self {
        Self::UnexpectedDotInMacroDef {
            position: token.start_position(),
        }
    }

    pub(crate) fn macro_args_mismatched(macro_call: MacroCall, macro_def: MacroDef) -> Self {
        Self::MacroArgsMismatched {
            macro_call,
            macro_def,
        }
    }

    pub(crate) fn undefined_macro_var(varname: String) -> Self {
        Self::UndefinedMacroVar { varname }
    }

    pub(crate) fn missing_if_directive(directive: Directive) -> Self {
        Self::MissingIfDirective { directive }
    }
}