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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use std::{
	cmp::Ordering,
	fmt::{Debug, Display},
	path::PathBuf,
};

use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_parser::{BinaryOpType, ExprLocation, LocExpr, Source, SourcePath, UnaryOpType};
use jrsonnet_types::ValType;
use thiserror::Error;

use crate::{function::CallLocation, stdlib::format::FormatError, typed::TypeLocError, ObjValue};

pub(crate) fn format_found(list: &[IStr], what: &str) -> String {
	if list.is_empty() {
		return String::new();
	}
	let mut out = String::new();
	out.push_str("\nThere is ");
	out.push_str(what);
	if list.len() > 1 {
		out.push('s');
	}
	out.push_str(" with similar name");
	if list.len() > 1 {
		out.push('s');
	}
	out.push_str(" present: ");
	for (i, v) in list.iter().enumerate() {
		if i != 0 {
			out.push_str(", ");
		}
		out.push_str(v as &str);
	}
	out
}

fn format_signature(sig: &FunctionSignature) -> String {
	let mut out = String::new();
	out.push_str("\nFunction has the following signature: ");
	out.push('(');
	if sig.is_empty() {
		out.push_str("/*no arguments*/");
	} else {
		for (i, (name, has_default)) in sig.iter().enumerate() {
			if i != 0 {
				out.push_str(", ");
			}
			if let Some(name) = name {
				out.push_str(name);
			} else {
				out.push_str("<unnamed>");
			}
			if *has_default {
				out.push_str(" = <default>");
			}
		}
	}
	out.push(')');
	out
}

const fn format_empty_str(str: &str) -> &str {
	if str.is_empty() {
		"\"\" (empty string)"
	} else {
		str
	}
}

pub(crate) fn suggest_object_fields(v: &ObjValue, key: IStr) -> Vec<IStr> {
	let mut heap = Vec::new();
	for field in v.fields_ex(
		true,
		#[cfg(feature = "exp-preserve-order")]
		false,
	) {
		let conf = strsim::jaro_winkler(field.as_str(), key.as_str());
		if conf < 0.8 {
			continue;
		}
		assert!(field.as_str() != key.as_str(), "looks like string pooling failure, please write any info regarding this crash to https://github.com/CertainLach/jrsonnet/issues/113, thanks!");

		heap.push((conf, field));
	}
	heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));
	heap.into_iter().map(|v| v.1).collect()
}

type FunctionSignature = Vec<(Option<IStr>, bool)>;

/// Possible errors
#[allow(missing_docs)]
#[derive(Error, Debug, Clone, Trace)]
#[non_exhaustive]
pub enum ErrorKind {
	#[error("intrinsic not found: {0}")]
	IntrinsicNotFound(IStr),

	#[error("operator {0} does not operate on type {1}")]
	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),
	#[error("binary operation {1} {0} {2} is not implemented")]
	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),

	#[error("no top level object in this context")]
	NoTopLevelObjectFound,
	#[error("self is only usable inside objects")]
	CantUseSelfOutsideOfObject,
	#[error("no super found")]
	NoSuperFound,

	#[error("for loop can only iterate over arrays")]
	InComprehensionCanOnlyIterateOverArray,

	#[error("array out of bounds: {0} is not within [0,{1})")]
	ArrayBoundsError(usize, usize),
	#[error("string out of bounds: {0} is not within [0,{1})")]
	StringBoundsError(usize, usize),

	#[error("assert failed: {}", format_empty_str(.0))]
	AssertionFailed(IStr),

	#[error("variable is not defined: {0}{}", format_found(.1, "variable"))]
	VariableIsNotDefined(IStr, Vec<IStr>),
	#[error("duplicate local var: {0}")]
	DuplicateLocalVar(IStr),

	#[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{e}")).collect::<Vec<_>>().join(", "))]
	TypeMismatch(&'static str, Vec<ValType>, ValType),
	#[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))]
	NoSuchField(IStr, Vec<IStr>),

	#[error("only functions can be called, got {0}")]
	OnlyFunctionsCanBeCalledGot(ValType),
	#[error("parameter {0} is not defined")]
	UnknownFunctionParameter(String),
	#[error("argument {0} is already bound")]
	BindingParameterASecondTime(IStr),
	#[error("too many args, function has {0}{}", format_signature(.1))]
	TooManyArgsFunctionHas(usize, FunctionSignature),
	#[error("function argument is not passed: {}{}", .0.as_ref().map_or("<unnamed>", IStr::as_str), format_signature(.1))]
	FunctionParameterNotBoundInCall(Option<IStr>, FunctionSignature),

	#[error("external variable is not defined: {0}")]
	UndefinedExternalVariable(IStr),

	#[error("field name should be string, got {0}")]
	FieldMustBeStringGot(ValType),
	#[error("duplicate field name: {}", format_empty_str(.0))]
	DuplicateFieldName(IStr),

	#[error("attempted to index array with string {}", format_empty_str(.0))]
	AttemptedIndexAnArrayWithString(IStr),
	#[error("{0} index type should be {1}, got {2}")]
	ValueIndexMustBeTypeGot(ValType, ValType, ValType),
	#[error("cant index into {0}")]
	CantIndexInto(ValType),
	#[error("{0} is not indexable")]
	ValueIsNotIndexable(ValType),

	#[error("super can't be used standalone")]
	StandaloneSuper,

	#[error("can't resolve {1} from {0}")]
	ImportFileNotFound(SourcePath, String),
	#[error("can't resolve absolute {0}")]
	AbsoluteImportFileNotFound(PathBuf),
	#[error("resolved file not found: {:?}", .0)]
	ResolvedFileNotFound(SourcePath),
	#[error("can't import {0}: is a directory")]
	ImportIsADirectory(SourcePath),
	#[error("imported file is not valid utf-8: {0:?}")]
	ImportBadFileUtf8(SourcePath),
	#[error("import io error: {0}")]
	ImportIo(String),
	#[error("tried to import {1} from {0}, but imports are not supported")]
	ImportNotSupported(SourcePath, String),
	#[error("tried to import {0}, but absolute imports are not supported")]
	AbsoluteImportNotSupported(PathBuf),
	#[error("can't import from virtual file")]
	CantImportFromVirtualFile,
	#[error(
		"syntax error: expected {}, got {:?}",
		.error.expected,
		.path.code().chars().nth(error.location.offset)
		.map_or_else(|| "EOF".into(), |c| c.to_string())
	)]
	ImportSyntaxError {
		path: Source,
		#[trace(skip)]
		error: Box<jrsonnet_parser::ParseError>,
	},

	#[error("runtime error: {}", format_empty_str(.0))]
	RuntimeError(IStr),
	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]
	StackOverflow,
	#[error("infinite recursion detected")]
	InfiniteRecursionDetected,
	#[error("tried to index by fractional value")]
	FractionalIndex,
	#[error("attempted to divide by zero")]
	DivisionByZero,

	#[error("string manifest output is not an string")]
	StringManifestOutputIsNotAString,
	#[error("stream manifest output is not an array")]
	StreamManifestOutputIsNotAArray,
	#[error("multi manifest output is not an object")]
	MultiManifestOutputIsNotAObject,

	#[error("cant recurse stream manifest")]
	StreamManifestOutputCannotBeRecursed,
	#[error("stream manifest output cannot consist of raw strings")]
	StreamManifestCannotNestString,

	#[error("{}", format_empty_str(.0))]
	ImportCallbackError(String),
	#[error("invalid unicode codepoint: {0}")]
	InvalidUnicodeCodepointGot(u32),

	#[error("format error: {0}")]
	Format(#[from] FormatError),
	#[error("type error: {0}")]
	TypeError(TypeLocError),

	#[cfg(feature = "anyhow-error")]
	#[error(transparent)]
	Other(#[trace(skip)] std::rc::Rc<anyhow::Error>),
}

#[cfg(feature = "anyhow-error")]
impl From<anyhow::Error> for Error {
	fn from(e: anyhow::Error) -> Self {
		Self::new(ErrorKind::Other(std::rc::Rc::new(e)))
	}
}

impl From<ErrorKind> for Error {
	fn from(e: ErrorKind) -> Self {
		Self::new(e)
	}
}

/// Single stack trace frame
#[derive(Clone, Debug, Trace)]
pub struct StackTraceElement {
	/// Source of this frame
	/// Some frames only act as description, without attached source
	pub location: Option<ExprLocation>,
	/// Frame description
	pub desc: String,
}
#[derive(Debug, Clone, Trace)]
pub struct StackTrace(pub Vec<StackTraceElement>);

#[derive(Clone, Trace)]
pub struct Error(Box<(ErrorKind, StackTrace)>);
impl Error {
	pub fn new(e: ErrorKind) -> Self {
		Self(Box::new((e, StackTrace(vec![]))))
	}

	pub const fn error(&self) -> &ErrorKind {
		&(self.0).0
	}
	pub fn error_mut(&mut self) -> &mut ErrorKind {
		&mut (self.0).0
	}
	pub const fn trace(&self) -> &StackTrace {
		&(self.0).1
	}
	pub fn trace_mut(&mut self) -> &mut StackTrace {
		&mut (self.0).1
	}
}
impl Display for Error {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		writeln!(f, "{}", self.0 .0)?;
		for el in &self.0 .1 .0 {
			write!(f, "\t{}", el.desc)?;
			if let Some(loc) = &el.location {
				write!(f, "at {}", loc.0 .0 .0)?;
				loc.0.map_source_locations(&[loc.1, loc.2]);
			}
			writeln!(f)?;
		}
		Ok(())
	}
}
impl Debug for Error {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_tuple("LocError").field(&self.0).finish()
	}
}
impl std::error::Error for Error {}

pub trait ErrorSource {
	fn to_location(self) -> Option<ExprLocation>;
}
impl ErrorSource for &LocExpr {
	fn to_location(self) -> Option<ExprLocation> {
		Some(self.1.clone())
	}
}
impl ErrorSource for &ExprLocation {
	fn to_location(self) -> Option<ExprLocation> {
		Some(self.clone())
	}
}
impl ErrorSource for CallLocation<'_> {
	fn to_location(self) -> Option<ExprLocation> {
		self.0.cloned()
	}
}

pub type Result<V, E = Error> = std::result::Result<V, E>;
pub trait ResultExt: Sized {
	#[must_use]
	fn with_description<O: Into<String>>(self, msg: impl FnOnce() -> O) -> Self;
	#[must_use]
	fn description(self, msg: &str) -> Self {
		self.with_description(|| msg)
	}

	#[must_use]
	fn with_description_src<O: Into<String>>(
		self,
		src: impl ErrorSource,
		msg: impl FnOnce() -> O,
	) -> Self;
	#[must_use]
	fn description_src(self, src: impl ErrorSource, msg: &str) -> Self {
		self.with_description_src(src, || msg)
	}
}
impl<T> ResultExt for Result<T, Error> {
	fn with_description<O: Into<String>>(mut self, msg: impl FnOnce() -> O) -> Self {
		if let Err(e) = &mut self {
			let trace = e.trace_mut();
			trace.0.push(StackTraceElement {
				location: None,
				desc: msg().into(),
			});
		}
		self
	}

	fn with_description_src<O: Into<String>>(
		mut self,
		src: impl ErrorSource,
		msg: impl FnOnce() -> O,
	) -> Self {
		if let Err(e) = &mut self {
			let trace = e.trace_mut();
			trace.0.push(StackTraceElement {
				location: src.to_location(),
				desc: msg().into(),
			});
		}
		self
	}
}

#[macro_export]
macro_rules! throw {
	($w:ident$(::$i:ident)*$(($($tt:tt)*))?) => {
		return Err($w$(::$i)*$(($($tt)*))?.into())
	};
	($w:ident$(::$i:ident)*$({$($tt:tt)*})?) => {
		return Err($w$(::$i)*$({$($tt)*})?.into())
	};
	($l:literal) => {
		return Err($crate::error::ErrorKind::RuntimeError($l.into()).into())
	};
	($l:literal, $($tt:tt)*) => {
		return Err($crate::error::ErrorKind::RuntimeError(format!($l, $($tt)*).into()).into())
	};
}