semantic_analyzer/types/
error.rs

1//! # Errors types
2//! Errors types for Semantic analyzer result of Error state.
3
4use crate::ast::CodeLocation;
5#[cfg(feature = "codec")]
6use serde::{Deserialize, Serialize};
7
8/// Common errors kind for the State.
9#[derive(Debug, Clone, Eq, PartialEq)]
10#[cfg_attr(
11    feature = "codec",
12    derive(Serialize, Deserialize),
13    serde(tag = "type", content = "content")
14)]
15pub enum StateErrorKind {
16    /// Common error indicate errors in the State
17    Common,
18    ConstantAlreadyExist,
19    ConstantNotFound,
20    WrongLetType,
21    WrongExpressionType,
22    TypeAlreadyExist,
23    FunctionAlreadyExist,
24    ValueNotFound,
25    ValueNotStruct,
26    ValueNotStructField,
27    ValueIsNotMutable,
28    FunctionNotFound,
29    FunctionParameterTypeWrong,
30    ReturnNotFound,
31    ReturnAlreadyCalled,
32    IfElseDuplicated,
33    TypeNotFound,
34    WrongReturnType,
35    ConditionExpressionWrongType,
36    ConditionIsEmpty,
37    ConditionExpressionNotSupported,
38    ForbiddenCodeAfterReturnDeprecated,
39    ForbiddenCodeAfterContinueDeprecated,
40    ForbiddenCodeAfterBreakDeprecated,
41    FunctionArgumentNameDuplicated,
42}
43
44/// State error location. Useful to determine location of error
45#[derive(Debug, Clone, Eq, PartialEq)]
46#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
47pub struct StateErrorLocation(pub CodeLocation);
48
49/// State error result data representation
50#[derive(Debug, Clone, Eq, PartialEq)]
51#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
52pub struct StateErrorResult {
53    /// Kind of error
54    pub kind: StateErrorKind,
55    /// Error value
56    pub value: String,
57    /// Error location
58    pub location: StateErrorLocation,
59}
60
61impl StateErrorResult {
62    #[must_use]
63    pub const fn new(kind: StateErrorKind, value: String, location: CodeLocation) -> Self {
64        Self {
65            kind,
66            value,
67            location: StateErrorLocation(location),
68        }
69    }
70}
71
72impl StateErrorResult {
73    /// Get state trace data from error result as string
74    #[must_use]
75    pub fn trace_state(&self) -> String {
76        format!(
77            "[{:?}] for value {:?} at: {:?}:{:?}",
78            self.kind,
79            self.value,
80            self.location.0.line(),
81            self.location.0.offset()
82        )
83    }
84}