1use crate::{2 builtin::{format::FormatError, sort::SortError},3 typed::TypeLocError,4};5use jrsonnet_interner::IStr;6use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};7use jrsonnet_types::ValType;8use std::{path::PathBuf, rc::Rc};9use thiserror::Error;1011#[derive(Error, Debug, Clone)]12pub enum Error {13 #[error("intrinsic not found: {0}")]14 IntrinsicNotFound(IStr),15 #[error("argument reordering in intrisics not supported yet")]16 IntrinsicArgumentReorderingIsNotSupportedYet,1718 #[error("operator {0} does not operate on type {1}")]19 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),20 #[error("binary operation {1} {0} {2} is not implemented")]21 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),2223 #[error("no top level object in this context")]24 NoTopLevelObjectFound,25 #[error("self is only usable inside objects")]26 CantUseSelfOutsideOfObject,27 #[error("super is only usable inside objects")]28 CantUseSuperOutsideOfObject,2930 #[error("for loop can only iterate over arrays")]31 InComprehensionCanOnlyIterateOverArray,3233 #[error("array out of bounds: {0} is not within [0,{1})")]34 ArrayBoundsError(usize, usize),3536 #[error("assert failed: {0}")]37 AssertionFailed(IStr),3839 #[error("variable is not defined: {0}")]40 VariableIsNotDefined(IStr),41 #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]42 TypeMismatch(&'static str, Vec<ValType>, ValType),43 #[error("no such field: {0}")]44 NoSuchField(IStr),4546 #[error("only functions can be called, got {0}")]47 OnlyFunctionsCanBeCalledGot(ValType),48 #[error("parameter {0} is not defined")]49 UnknownFunctionParameter(String),50 #[error("argument {0} is already bound")]51 BindingParameterASecondTime(IStr),52 #[error("too many args, function has {0}")]53 TooManyArgsFunctionHas(usize),54 #[error("founction argument is not passed: {0}")]55 FunctionParameterNotBoundInCall(IStr),5657 #[error("external variable is not defined: {0}")]58 UndefinedExternalVariable(IStr),59 #[error("native is not defined: {0}")]60 UndefinedExternalFunction(IStr),6162 #[error("field name should be string, got {0}")]63 FieldMustBeStringGot(ValType),6465 #[error("attempted to index array with string {0}")]66 AttemptedIndexAnArrayWithString(IStr),67 #[error("{0} index type should be {1}, got {2}")]68 ValueIndexMustBeTypeGot(ValType, ValType, ValType),69 #[error("cant index into {0}")]70 CantIndexInto(ValType),7172 #[error("super can't be used standalone")]73 StandaloneSuper,7475 #[error("can't resolve {1} from {0}")]76 ImportFileNotFound(PathBuf, PathBuf),77 #[error("resolved file not found: {0}")]78 ResolvedFileNotFound(PathBuf),79 #[error("imported file is not valid utf-8: {0:?}")]80 ImportBadFileUtf8(PathBuf),81 #[error("tried to import {1} from {0}, but imports is not supported")]82 ImportNotSupported(PathBuf, PathBuf),83 #[error(84 "syntax error, expected one of {}, got {:?}",85 .error.expected,86 .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())87 )]88 ImportSyntaxError {89 path: Rc<PathBuf>,90 source_code: IStr,91 error: Box<jrsonnet_parser::ParseError>,92 },9394 #[error("runtime error: {0}")]95 RuntimeError(IStr),96 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]97 StackOverflow,98 #[error("tried to index by fractional value")]99 FractionalIndex,100 #[error("attempted to divide by zero")]101 DivisionByZero,102103 #[error("string manifest output is not an string")]104 StringManifestOutputIsNotAString,105 #[error("stream manifest output is not an array")]106 StreamManifestOutputIsNotAArray,107 #[error("multi manifest output is not an object")]108 MultiManifestOutputIsNotAObject,109110 #[error("cant recurse stream manifest")]111 StreamManifestOutputCannotBeRecursed,112 #[error("stream manifest output cannot consist of raw strings")]113 StreamManifestCannotNestString,114115 #[error("{0}")]116 ImportCallbackError(String),117 #[error("invalid unicode codepoint: {0}")]118 InvalidUnicodeCodepointGot(u32),119120 #[error("format error: {0}")]121 Format(#[from] FormatError),122 #[error("type error: {0}")]123 TypeError(TypeLocError),124 #[error("sort error: {0}")]125 Sort(#[from] SortError),126}127impl From<Error> for LocError {128 fn from(e: Error) -> Self {129 Self::new(e)130 }131}132133#[derive(Clone, Debug)]134pub struct StackTraceElement {135 pub location: Option<ExprLocation>,136 pub desc: String,137}138#[derive(Debug, Clone)]139pub struct StackTrace(pub Vec<StackTraceElement>);140141#[derive(Debug, Clone)]142pub struct LocError(Box<(Error, StackTrace)>);143impl LocError {144 pub fn new(e: Error) -> Self {145 Self(Box::new((e, StackTrace(vec![]))))146 }147148 pub const fn error(&self) -> &Error {149 &(self.0).0150 }151 pub fn error_mut(&mut self) -> &mut Error {152 &mut (self.0).0153 }154 pub const fn trace(&self) -> &StackTrace {155 &(self.0).1156 }157 pub fn trace_mut(&mut self) -> &mut StackTrace {158 &mut (self.0).1159 }160}161162pub type Result<V> = std::result::Result<V, LocError>;163164#[macro_export]165macro_rules! throw {166 ($e: expr) => {167 return Err($e.into());168 };169}