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
/*
 * 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 crate::parser::lexer::LexerError;
use crate::parser::Span;

use thiserror::Error as ThisError;

#[derive(ThisError, Debug, Clone, PartialEq, Eq)]
pub enum ParserError {
    #[error(transparent)]
    LexerError(#[from] LexerError),

    #[error("lambda can't be applied to streams in this position")]
    LambdaAppliedToStream(Span),

    #[error("variable '{variable_name}' wasn't defined")]
    UndefinedVariable { span: Span, variable_name: String },

    #[error("iterable '{variable_name}' wasn't defined")]
    UndefinedIterable { span: Span, variable_name: String },

    #[error("last error with non-empty path is ambiguous, please use just %last_error%")]
    AmbiguousFailLastError(Span),

    #[error("new can't be applied to a '{iterator_name}' because it's an iterator")]
    IteratorRestrictionNotAllowed { span: Span, iterator_name: String },

    #[error("multiple iterable values found for iterator name '{iterator_name}'")]
    MultipleIterableValuesForOneIterator { span: Span, iterator_name: String },

    #[error(
        "multiple next instructions for iterator '{iterator_name}' found for one fold, that is prohibited"
    )]
    MultipleNextInFold { span: Span, iterator_name: String },

    #[error("unsupported variable key type in (ap {ap_key_type} value {ap_result_name})")]
    UnsupportedMapKeyType {
        span: Span,
        ap_key_type: String,
        ap_result_name: String,
    },

    #[error("error code 0 with fail is unsupported")]
    UnsupportedLiteralErrCodes { span: Span },

    #[error("fold can not have instructions after next")]
    FoldHasInstructionAfterNext(Span),
}

impl ParserError {
    pub fn span(&self) -> Span {
        match self {
            Self::LexerError(lexer_error) => lexer_error.span(),
            Self::LambdaAppliedToStream(span) => *span,
            Self::UndefinedVariable { span, .. } => *span,
            Self::UndefinedIterable { span, .. } => *span,
            Self::AmbiguousFailLastError(span) => *span,
            Self::IteratorRestrictionNotAllowed { span, .. } => *span,
            Self::MultipleIterableValuesForOneIterator { span, .. } => *span,
            Self::MultipleNextInFold { span, .. } => *span,
            Self::UnsupportedMapKeyType { span, .. } => *span,
            Self::UnsupportedLiteralErrCodes { span } => *span,
            Self::FoldHasInstructionAfterNext(span) => *span,
        }
    }

    pub fn undefined_variable(span: Span, variable_name: impl Into<String>) -> Self {
        Self::UndefinedVariable {
            span,
            variable_name: variable_name.into(),
        }
    }

    pub fn undefined_iterable(span: Span, variable_name: impl Into<String>) -> Self {
        Self::UndefinedIterable {
            span,
            variable_name: variable_name.into(),
        }
    }

    pub fn invalid_iterator_restriction(span: Span, iterator_name: impl Into<String>) -> Self {
        Self::IteratorRestrictionNotAllowed {
            span,
            iterator_name: iterator_name.into(),
        }
    }

    pub fn multiple_iterables(span: Span, iterator_name: impl Into<String>) -> Self {
        Self::MultipleIterableValuesForOneIterator {
            span,
            iterator_name: iterator_name.into(),
        }
    }

    pub fn multiple_next_in_fold(span: Span, iterator_name: impl Into<String>) -> Self {
        Self::MultipleNextInFold {
            span,
            iterator_name: iterator_name.into(),
        }
    }

    pub fn unsupported_map_key_type(
        span: Span,
        ap_key_type: impl Into<String>,
        ap_result_name: impl Into<String>,
    ) -> Self {
        Self::UnsupportedMapKeyType {
            span,
            ap_key_type: ap_key_type.into(),
            ap_result_name: ap_result_name.into(),
        }
    }

    pub fn unsupported_literal_errcodes(span: Span) -> Self {
        Self::UnsupportedLiteralErrCodes { span }
    }

    pub fn fold_has_instruction_after_next(span: Span) -> Self {
        Self::FoldHasInstructionAfterNext(span)
    }
}

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