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
use std::fmt;
use std::error;

pub mod ast;

mod sliding_window;
pub use sliding_window::*;

mod yolol_number;
pub use yolol_number::*;

mod token;
pub use token::*;

// mod operators;
// pub use operators::*;

// mod value;
// pub use value::*;

// mod expression;
// pub use expression::*;

// mod statement;
// pub use statement::*;

// mod line;
// pub use line::*;

#[derive(Debug, Clone)]
pub struct EvaluationError
{
    pub kind: EvaluationErrorKind,
    pub error_text: String
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EvaluationErrorKind
{
    OperatorError,
    NonExhaustivePattern,
    HitGoto,
    Misc
}

impl error::Error for EvaluationError
{
    fn source(&self) -> Option<&(dyn error::Error + 'static)>
    {
        None
    }
}

impl fmt::Display for EvaluationError
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
    {
        write!(f, "[Evaluation Error] Kind: {:?} Error: {}", self.kind, self.error_text)
    }
}

impl From<ast::operators::OperatorError> for EvaluationError
{
    fn from(input: ast::operators::OperatorError) -> EvaluationError
    {
        EvaluationError {
            kind: EvaluationErrorKind::OperatorError,
            error_text: format!("op: {:?}, left: {:?}, right: {:?}, message: {}", input.op, input.left, input.right, input.error_text)
        }
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ParseErrorKind
{
    NoParseRuleMatch,

    RepeatedElseTokens,

    UnbalancedParenthesis,
    NoExtensionAvailable
}

#[derive(Debug, Clone)]
pub struct ExprError
{
    pub input_expr: Option<ast::expression::Expression>,
    pub kind: ParseErrorKind,
    pub error_text: String,
}

impl ExprError
{
    pub fn new(expr: Option<ast::expression::Expression>, kind: ParseErrorKind, error_text: &str) -> ExprError
    {
        ExprError {
            input_expr: expr,
            kind,
            error_text: String::from(error_text)
        }
    }
}

impl fmt::Display for ExprError
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
    {
        write!(f, "{}", self.error_text)
    }
}

impl error::Error for ExprError
{
    fn source(&self) -> Option<&(dyn error::Error + 'static)>
    {
        None
    }
}

#[derive(Debug, Clone)]
pub struct StatError
{
    pub input_stat: Option<ast::statement::Statement>,
    pub kind: ParseErrorKind,
    pub error_text: String,
}

impl StatError
{
    pub fn new(stat: Option<ast::statement::Statement>, kind: ParseErrorKind, error_text: &str) -> StatError
    {
        StatError {
            input_stat: stat,
            kind,
            error_text: String::from(error_text)
        }
    }
}

impl fmt::Display for StatError
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
    {
        write!(f, "{}", self.error_text)
    }
}

impl error::Error for StatError
{
    fn source(&self) -> Option<&(dyn error::Error + 'static)>
    {
        None
    }
}

impl std::convert::From<ExprError> for StatError
{
    fn from(error: ExprError) -> Self
    {
        let ExprError {
            input_expr,
            kind,
            error_text } = error;

        let stat = match input_expr
        {
            Some(expr) => Some(ast::statement::Statement::Expression(Box::new(expr))),
            None => None
        };

        StatError {
            input_stat: stat,
            kind,
            error_text
        }
    }
}