typed_eval/
error.rs

1use crate::{BinOp, TypeInfo, UnOp};
2use chumsky::error::{Rich, RichReason};
3use thiserror::Error;
4
5#[derive(Debug, PartialEq, Error)]
6pub enum Error {
7    #[error("Invalid float literal: {0}")]
8    InvalidLiteral(String),
9
10    #[error("Wrong number of arguments: expected {expected}, got {got}")]
11    ArgCountMismatch { expected: usize, got: usize },
12
13    #[error("Cannot cast from type `{from}` to `{to}`")]
14    CantCast { from: TypeInfo, to: TypeInfo },
15
16    #[error("Cannot cast expressions of type `{0}` and `{1}` to same type")]
17    CantCastSameType(TypeInfo, TypeInfo),
18
19    #[error("No unary operator {op:?} for type `{ty}`")]
20    UnknownUnaryOp { op: UnOp, ty: TypeInfo },
21
22    #[error("No binary operator {op:?} for type `{ty}`")]
23    UnknownBinaryOp { op: BinOp, ty: TypeInfo },
24
25    #[error("Field `{field}` not found for type `{ty}`")]
26    FieldNotFound { ty: TypeInfo, field: String },
27
28    #[error("Method `{method}` not found for type `{ty}`")]
29    MethodNotFound { ty: TypeInfo, method: String },
30
31    #[error("Unsupported function call")]
32    UnsupportedFunctionCall,
33
34    #[error("Cast from `{from}` to `{to}` already registered")]
35    DuplicateCast { from: TypeInfo, to: TypeInfo },
36
37    #[error("Unary operation {op:?} on type `{ty}` already registered")]
38    DuplicateUnOp { op: UnOp, ty: TypeInfo },
39
40    #[error("Binary operation {op:?} on type `{ty}` already registered")]
41    DuplicateBinOp { op: BinOp, ty: TypeInfo },
42
43    #[error("Field access `{field}` on type `{ty}` already registered")]
44    DuplicateField { ty: TypeInfo, field: &'static str },
45
46    #[error("Method `{method}` on type `{ty}` already registered")]
47    DuplicateMethod { ty: TypeInfo, method: &'static str },
48
49    #[error("Parse error: expected: {expected:?}, found {found:?}")]
50    ParseError {
51        expected: Vec<String>,
52        found: String,
53    },
54
55    #[error("Parse error: {message}")]
56    CustomParseError { message: String },
57
58    #[error("Unknown error")]
59    UnknownError,
60
61    #[error(
62        "Internal compiler error: Wrong number of arguments, expected {expected}, got {got}"
63    )]
64    InternalArgCountMismatch { expected: usize, got: usize },
65
66    #[error(
67        "Internal compiler error: DynFn downcast failed, expected {expected_arg}->{expected_ret}, got {got_arg}->{got_ret}"
68    )]
69    InternalDynFnDowncastError {
70        expected_arg: TypeInfo,
71        expected_ret: TypeInfo,
72        got_arg: TypeInfo,
73        got_ret: TypeInfo,
74    },
75}
76
77impl<'a> From<Rich<'a, char>> for Error {
78    fn from(value: Rich<'a, char>) -> Self {
79        match value.into_reason() {
80            RichReason::Custom(message) => Error::CustomParseError { message },
81            RichReason::ExpectedFound { expected, found } => {
82                Error::ParseError {
83                    expected: expected
84                        .into_iter()
85                        .map(|c| c.to_string())
86                        .collect::<Vec<_>>(),
87                    found: found
88                        .map(|c| c.to_string())
89                        .unwrap_or_else(|| "End of input".to_string()),
90                }
91            }
92        }
93    }
94}