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
/*
 * AquaVM Workflow Engine
 *
 * Copyright (C) 2024 Fluence DAO
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use super::AirPos;
use crate::parser::Span;

use thiserror::Error as ThisError;

use std::num::ParseFloatError;
use std::num::ParseIntError;
use std::ops::Range;

#[derive(ThisError, Debug, Clone, PartialEq, Eq)]
pub enum LexerError {
    #[error("this string literal has unclosed quote")]
    UnclosedQuote(Span),

    #[error("empty string aren't allowed in this position")]
    EmptyString(Span),

    #[error("only alphanumeric, '_', and '-' characters are allowed in this position")]
    IsNotAlphanumeric(Span),

    #[error("a tagged name should be non empty")]
    EmptyTaggedName(Span),

    #[error("a canon name should be non empty")]
    EmptyCanonName(Span),

    #[error("this variable or constant shouldn't have empty name")]
    EmptyVariableOrConst(Span),

    #[error("invalid character in lambda")]
    InvalidLambda(Span),

    #[error("a digit could contain only digits or one dot")]
    UnallowedCharInNumber(Span),

    #[error("{1}")]
    ParseIntError(Span, #[source] ParseIntError),

    #[error("{1}")]
    ParseFloatError(Span, #[source] ParseFloatError),

    // TODO: use LambdaParserError directly here (it'll require introducing a lifetime)
    #[error("{se_lambda_parser_error}")]
    LambdaParserError {
        span: Span,
        se_lambda_parser_error: String,
    },

    #[error("{error_path} is an incorrect path for %last_error%, only .$.instruction, .$.msg, and .$.peer_id are allowed")]
    LastErrorPathError { span: Span, error_path: String },

    #[error("this float is too big, a float could contain less than 12 digits")]
    TooBigFloat(Span),

    #[error("leading dot without any symbols before - please write 0 if it's float or variable name if it's a lambda")]
    LeadingDot(Span),
}

impl LexerError {
    pub fn span(&self) -> Span {
        let span = match self {
            Self::UnclosedQuote(span) => span,
            Self::EmptyString(span) => span,
            Self::IsNotAlphanumeric(span) => span,
            Self::EmptyTaggedName(span) => span,
            Self::EmptyCanonName(span) => span,
            Self::EmptyVariableOrConst(span) => span,
            Self::InvalidLambda(span) => span,
            Self::UnallowedCharInNumber(span) => span,
            Self::ParseIntError(span, _) => span,
            Self::ParseFloatError(span, _) => span,
            Self::LambdaParserError { span, .. } => span,
            Self::LastErrorPathError { span, .. } => span,
            Self::TooBigFloat(span) => span,
            Self::LeadingDot(span) => span,
        };

        *span
    }

    pub fn unclosed_quote(range: Range<AirPos>) -> Self {
        Self::UnclosedQuote(range.into())
    }

    pub fn empty_string(range: Range<AirPos>) -> Self {
        Self::EmptyString(range.into())
    }

    pub fn is_not_alphanumeric(range: Range<AirPos>) -> Self {
        Self::IsNotAlphanumeric(range.into())
    }

    pub fn empty_tagged_name(range: Range<AirPos>) -> Self {
        Self::EmptyTaggedName(range.into())
    }

    pub fn empty_canon_name(range: Range<AirPos>) -> Self {
        Self::EmptyCanonName(range.into())
    }

    pub fn empty_variable_or_const(range: Range<AirPos>) -> Self {
        Self::EmptyVariableOrConst(range.into())
    }

    pub fn invalid_lambda(range: Range<AirPos>) -> Self {
        Self::InvalidLambda(range.into())
    }

    pub fn unallowed_char_in_number(range: Range<AirPos>) -> Self {
        Self::UnallowedCharInNumber(range.into())
    }

    pub fn parse_int_error(range: Range<AirPos>, parse_int_error: ParseIntError) -> Self {
        Self::ParseIntError(range.into(), parse_int_error)
    }

    pub fn parse_float_error(range: Range<AirPos>, parse_float_error: ParseFloatError) -> Self {
        Self::ParseFloatError(range.into(), parse_float_error)
    }

    pub fn lambda_parser_error(
        range: Range<AirPos>,
        se_lambda_parser_error: impl Into<String>,
    ) -> Self {
        Self::LambdaParserError {
            span: range.into(),
            se_lambda_parser_error: se_lambda_parser_error.into(),
        }
    }

    pub fn last_error_path_error(range: Range<AirPos>, error_path: String) -> Self {
        Self::LastErrorPathError {
            span: range.into(),
            error_path,
        }
    }

    pub fn too_big_float(range: Range<AirPos>) -> Self {
        Self::TooBigFloat(range.into())
    }

    pub fn leading_dot(range: Range<AirPos>) -> Self {
        Self::LeadingDot(range.into())
    }
}

use super::Token;
use crate::parser::air::__ToTriple;
use crate::parser::ParserError;

impl<'err, 'input, 'i> __ToTriple<'err, 'input, 'i>
    for Result<(AirPos, Token<'input>, AirPos), LexerError>
{
    #[allow(clippy::wrong_self_convention)]
    fn to_triple(
        value: Self,
    ) -> Result<
        (AirPos, Token<'input>, AirPos),
        lalrpop_util::ParseError<AirPos, Token<'input>, ParserError>,
    > {
        match value {
            Ok(v) => Ok(v),
            Err(error) => {
                let error = ParserError::LexerError(error);
                Err(lalrpop_util::ParseError::User { error })
            }
        }
    }
}

impl From<std::convert::Infallible> for LexerError {
    fn from(_: std::convert::Infallible) -> Self {
        unreachable!()
    }
}