difftreelog
Merge pull request #27 from CertainLach/syntax-error-display
in: master
Syntax error display
8 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7,6 +7,7 @@
checksum = "5c96c3d1062ea7101741480185a6a1275eab01cbe8b20e378d1311bc056d2e08"
dependencies = [
"unicode-width",
+ "yansi-term",
]
[[package]]
@@ -513,3 +514,12 @@
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+
+[[package]]
+name = "yansi-term"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fe5c30ade05e61656247b2e334a031dfd0cc466fadef865bdcdea8d537951bf1"
+dependencies = [
+ "winapi",
+]
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -55,6 +55,7 @@
# Explaining traces
[dependencies.annotate-snippets]
version = "0.9.0"
+features = ["color"]
optional = true
[build-dependencies]
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth1use crate::{2 builtin::{format::FormatError, sort::SortError},3 ValType,4};5use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};6use std::{path::PathBuf, rc::Rc};7use thiserror::Error;89#[derive(Error, Debug, Clone)]10pub enum Error {11 #[error("intrinsic not found: {0}")]12 IntrinsicNotFound(Rc<str>),13 #[error("argument reordering in intrisics not supported yet")]14 IntrinsicArgumentReorderingIsNotSupportedYet,1516 #[error("operator {0} does not operate on type {1}")]17 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),18 #[error("binary operation {1} {0} {2} is not implemented")]19 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),2021 #[error("no top level object in this context")]22 NoTopLevelObjectFound,23 #[error("self is only usable inside objects")]24 CantUseSelfOutsideOfObject,25 #[error("super is only usable inside objects")]26 CantUseSuperOutsideOfObject,2728 #[error("for loop can only iterate over arrays")]29 InComprehensionCanOnlyIterateOverArray,3031 #[error("array out of bounds: {0} is not within [0,{1})")]32 ArrayBoundsError(usize, usize),3334 #[error("assert failed: {0}")]35 AssertionFailed(Rc<str>),3637 #[error("variable is not defined: {0}")]38 VariableIsNotDefined(Rc<str>),39 #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]40 TypeMismatch(&'static str, Vec<ValType>, ValType),41 #[error("no such field: {0}")]42 NoSuchField(Rc<str>),4344 #[error("only functions can be called, got {0}")]45 OnlyFunctionsCanBeCalledGot(ValType),46 #[error("parameter {0} is not defined")]47 UnknownFunctionParameter(String),48 #[error("argument {0} is already bound")]49 BindingParameterASecondTime(Rc<str>),50 #[error("too many args, function has {0}")]51 TooManyArgsFunctionHas(usize),52 #[error("founction argument is not passed: {0}")]53 FunctionParameterNotBoundInCall(Rc<str>),5455 #[error("external variable is not defined: {0}")]56 UndefinedExternalVariable(Rc<str>),57 #[error("native is not defined: {0}")]58 UndefinedExternalFunction(Rc<str>),5960 #[error("field name should be string, got {0}")]61 FieldMustBeStringGot(ValType),6263 #[error("attempted to index array with string {0}")]64 AttemptedIndexAnArrayWithString(Rc<str>),65 #[error("{0} index type should be {1}, got {2}")]66 ValueIndexMustBeTypeGot(ValType, ValType, ValType),67 #[error("cant index into {0}")]68 CantIndexInto(ValType),6970 #[error("super can't be used standalone")]71 StandaloneSuper,7273 #[error("can't resolve {1} from {0}")]74 ImportFileNotFound(PathBuf, PathBuf),75 #[error("resolved file not found: {0}")]76 ResolvedFileNotFound(PathBuf),77 #[error("imported file is not valid utf-8: {0:?}")]78 ImportBadFileUtf8(PathBuf),79 #[error("tried to import {1} from {0}, but imports is not supported")]80 ImportNotSupported(PathBuf, PathBuf),81 #[error("syntax error")]82 ImportSyntaxError {83 path: Rc<PathBuf>,84 source_code: Rc<str>,85 error: Box<jrsonnet_parser::ParseError>,86 },8788 #[error("runtime error: {0}")]89 RuntimeError(Rc<str>),90 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]91 StackOverflow,92 #[error("tried to index by fractional value")]93 FractionalIndex,94 #[error("attempted to divide by zero")]95 DivisionByZero,9697 #[error("string manifest output is not an string")]98 StringManifestOutputIsNotAString,99 #[error("stream manifest output is not an array")]100 StreamManifestOutputIsNotAArray,101 #[error("multi manifest output is not an object")]102 MultiManifestOutputIsNotAObject,103104 #[error("cant recurse stream manifest")]105 StreamManifestOutputCannotBeRecursed,106 #[error("stream manifest output cannot consist of raw strings")]107 StreamManifestCannotNestString,108109 #[error("{0}")]110 ImportCallbackError(String),111 #[error("invalid unicode codepoint: {0}")]112 InvalidUnicodeCodepointGot(u32),113114 #[error("format error: {0}")]115 Format(#[from] FormatError),116 #[error("sort error: {0}")]117 Sort(#[from] SortError),118}119impl From<Error> for LocError {120 fn from(e: Error) -> Self {121 Self::new(e)122 }123}124125#[derive(Clone, Debug)]126pub struct StackTraceElement {127 pub location: ExprLocation,128 pub desc: String,129}130#[derive(Debug, Clone)]131pub struct StackTrace(pub Vec<StackTraceElement>);132133#[derive(Debug, Clone)]134pub struct LocError(Box<(Error, StackTrace)>);135impl LocError {136 pub fn new(e: Error) -> Self {137 Self(Box::new((e, StackTrace(vec![]))))138 }139140 pub const fn error(&self) -> &Error {141 &(self.0).0142 }143 pub const fn trace(&self) -> &StackTrace {144 &(self.0).1145 }146 pub fn trace_mut(&mut self) -> &mut StackTrace {147 &mut (self.0).1148 }149}150151pub type Result<V> = std::result::Result<V, LocError>;152153#[macro_export]154macro_rules! throw {155 ($e: expr) => {156 return Err($e.into());157 };158}crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -419,18 +419,12 @@
use Expr::*;
let LocExpr(expr, loc) = expr;
Ok(match &**expr {
- Literal(LiteralType::This) => Val::Obj(
- context
- .this()
- .clone()
- .ok_or_else(|| CantUseSelfOutsideOfObject)?,
- ),
- Literal(LiteralType::Dollar) => Val::Obj(
- context
- .dollar()
- .clone()
- .ok_or_else(|| NoTopLevelObjectFound)?,
- ),
+ Literal(LiteralType::This) => {
+ Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
+ }
+ Literal(LiteralType::Dollar) => {
+ Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)
+ }
Literal(LiteralType::True) => Val::Bool(true),
Literal(LiteralType::False) => Val::Bool(false),
Literal(LiteralType::Null) => Val::Null,
crates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ b/crates/jrsonnet-evaluator/src/trace/location.rs
@@ -1,5 +1,7 @@
#[derive(Clone, PartialEq, Debug)]
pub struct CodeLocation {
+ pub offset: usize,
+
pub line: usize,
pub column: usize,
@@ -25,6 +27,7 @@
let mut out = vec![
CodeLocation {
+ offset: 0,
column: 0,
line: 0,
line_start_offset: 0,
@@ -40,6 +43,7 @@
Some(x) if x.0 == pos => {
let out_idx = x.1;
with_no_known_line_ending.push(out_idx);
+ out[out_idx].offset = pos;
out[out_idx].line = line;
out[out_idx].column = column;
out[out_idx].line_start_offset = this_line_offset;
@@ -82,12 +86,14 @@
),
vec![
CodeLocation {
+ offset: 0,
line: 1,
column: 2,
line_start_offset: 0,
- line_end_offset: 11
+ line_end_offset: 11,
},
CodeLocation {
+ offset: 14,
line: 2,
column: 4,
line_start_offset: 12,
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -1,6 +1,6 @@
mod location;
-use crate::{EvaluationState, LocError};
+use crate::{error::Error, EvaluationState, LocError};
pub use location::*;
use std::path::PathBuf;
@@ -87,6 +87,33 @@
error: &LocError,
) -> Result<(), std::fmt::Error> {
writeln!(out, "{}", error.error())?;
+ if let Error::ImportSyntaxError {
+ path,
+ source_code,
+ error,
+ } = error.error()
+ {
+ use std::fmt::Write;
+ let mut n = self.resolver.resolve(path);
+ let mut offset = error.location.offset;
+ let is_eof = if offset >= source_code.len() {
+ offset = source_code.len() - 1;
+ true
+ } else {
+ false
+ };
+ let mut location = offset_to_location(source_code, &[offset])
+ .into_iter()
+ .next()
+ .unwrap();
+ if is_eof {
+ location.column += 1;
+ }
+
+ write!(n, ":").unwrap();
+ print_code_location(&mut n, &location, &location).unwrap();
+ write!(out, "{:<p$}{}", "", n, p = self.padding,)?;
+ }
let file_names = error
.trace()
.0
@@ -167,52 +194,101 @@
evaluation_state: &EvaluationState,
error: &LocError,
) -> Result<(), std::fmt::Error> {
- use annotate_snippets::{
- display_list::{DisplayList, FormatOptions},
- snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},
- };
writeln!(out, "{}", error.error())?;
+ if let Error::ImportSyntaxError {
+ path,
+ source_code,
+ error,
+ } = error.error()
+ {
+ let mut offset = error.location.offset;
+ if offset >= source_code.len() {
+ offset = source_code.len() - 1;
+ }
+ let mut location = offset_to_location(source_code, &[offset])
+ .into_iter()
+ .next()
+ .unwrap();
+ if location.column >= 1 {
+ location.column -= 1;
+ }
+
+ self.print_snippet(
+ out,
+ source_code,
+ path,
+ &location,
+ &location,
+ "^ syntax error",
+ )?;
+ }
let trace = &error.trace();
for item in trace.0.iter() {
let desc = &item.desc;
let source = item.location.clone();
let start_end = evaluation_state.map_source_locations(&source.0, &[source.1, source.2]);
- let source_fragment: String = evaluation_state
- .get_source(&source.0)
- .unwrap()
- .chars()
- .skip(start_end[0].line_start_offset)
- .take(start_end[1].line_end_offset - start_end[0].line_start_offset)
- .collect();
+ self.print_snippet(
+ out,
+ &evaluation_state.get_source(&source.0).unwrap(),
+ &source.0,
+ &start_end[0],
+ &start_end[1],
+ desc,
+ )?;
+ }
+ Ok(())
+ }
+}
+
+impl ExplainingFormat {
+ fn print_snippet(
+ &self,
+ out: &mut dyn std::fmt::Write,
+ source: &str,
+ origin: &PathBuf,
+ start: &CodeLocation,
+ end: &CodeLocation,
+ desc: &str,
+ ) -> Result<(), std::fmt::Error> {
+ use annotate_snippets::{
+ display_list::{DisplayList, FormatOptions},
+ snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},
+ };
+
+ let source_fragment: String = source
+ .chars()
+ .skip(start.line_start_offset)
+ .take(end.line_end_offset - end.line_start_offset)
+ .collect();
- let origin = self.resolver.resolve(&source.0);
- let snippet = Snippet {
- opt: FormatOptions {
- color: true,
- ..Default::default()
- },
- title: None,
- footer: vec![],
- slices: vec![Slice {
- source: &source_fragment,
- line_start: start_end[0].line,
- origin: Some(&origin),
- fold: false,
- annotations: vec![SourceAnnotation {
- label: desc,
- annotation_type: AnnotationType::Error,
- range: (
- source.1 - start_end[0].line_start_offset,
- source.2 - start_end[0].line_start_offset,
- ),
- }],
+ let origin = self.resolver.resolve(origin);
+ let snippet = Snippet {
+ opt: FormatOptions {
+ color: true,
+ ..Default::default()
+ },
+ title: None,
+ footer: vec![],
+ slices: vec![Slice {
+ source: &source_fragment,
+ line_start: start.line,
+ origin: Some(&origin),
+ fold: false,
+ annotations: vec![SourceAnnotation {
+ label: desc,
+ annotation_type: AnnotationType::Error,
+ range: (
+ start.offset - start.line_start_offset,
+ end.offset - start.line_start_offset,
+ ),
}],
- };
+ }],
+ };
- let dl = DisplayList::from(snippet);
- writeln!(out, "{}", dl)?;
- }
+ let dl = DisplayList::from(snippet);
+ writeln!(out, "{}", dl)?;
+
Ok(())
}
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -349,7 +349,7 @@
for v in arr.iter() {
out.push_str("---\n");
out.push_str(&v.manifest(format)?);
- out.push_str("\n");
+ out.push('\n');
}
out.push_str("...");
}
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -37,7 +37,7 @@
rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()
rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")
- rule keyword(id: &'static str)
+ rule keyword(id: &'static str) -> ()
= ##parse_string_literal(id) end_of_ident()
// Adds location data information to existing expression
rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr
@@ -85,11 +85,11 @@
[' ' | '\t']*<, {prefix.len() - 1}> "|||"
{let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}
pub rule string() -> String
- = "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}
+ = quiet!{ "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}
/ "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}
/ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}
/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}
- / string_block()
+ / string_block() } / expected!("<string>")
pub rule field_name(s: &ParserSettings) -> expr::FieldName
= name:$(id()) {expr::FieldName::Fixed(name.into())}
@@ -208,54 +208,59 @@
SliceDesc { start, end, step }
}
+ rule binop(x: rule<()>) -> ()
+ = quiet!{ x() } / expected!("<binary op>")
+ rule unaryop(x: rule<()>) -> ()
+ = quiet!{ x() } / expected!("<unary op>")
+
rule expr(s: &ParserSettings) -> LocExpr
= start:position!() a:precedence! {
- a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}
+ a:(@) _ binop(<"||">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}
--
- a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}
+ a:(@) _ binop(<"&&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}
--
- a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}
+ a:(@) _ binop(<"|">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}
--
- a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}
+ a:@ _ binop(<"^">) _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}
--
- a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}
+ a:(@) _ binop(<"&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}
--
- a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::Apply(
+ a:(@) _ binop(<"==">) _ b:@ {loc_expr_todo!(Expr::Apply(
el!(Expr::Intrinsic("equals".into())),
ArgsDesc(vec![Arg(None, a), Arg(None, b)]),
true
))}
- a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(
+ a:(@) _ binop(<"!=">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(
el!(Expr::Intrinsic("equals".into())),
ArgsDesc(vec![Arg(None, a), Arg(None, b)]),
true
))))}
--
- a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}
- a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}
- a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}
- a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}
- a:(@) _ keyword("in") _ b:@ {loc_expr_todo!(Expr::Apply(
+ a:(@) _ binop(<"<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}
+ a:(@) _ binop(<">">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}
+ a:(@) _ binop(<"<=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}
+ a:(@) _ binop(<">=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}
+ a:(@) _ binop(<keyword("in")>) _ b:@ {loc_expr_todo!(Expr::Apply(
el!(Expr::Intrinsic("objectHasEx".into())), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),
true
))}
--
- a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}
- a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}
+ a:(@) _ binop(<"<<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}
+ a:(@) _ binop(<">>">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}
--
- a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}
- a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}
+ a:(@) _ binop(<"+">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}
+ a:(@) _ binop(<"-">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}
--
- a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}
- a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}
- a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::Apply(
+ a:(@) _ binop(<"*">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}
+ a:(@) _ binop(<"/">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}
+ a:(@) _ binop(<"%">) _ b:@ {loc_expr_todo!(Expr::Apply(
el!(Expr::Intrinsic("mod".into())), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),
false
))}
--
- "-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}
- "!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}
- "~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }
+ unaryop(<"-">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}
+ unaryop(<"!">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}
+ unaryop(<"~">) _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }
--
a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(
el!(Expr::Intrinsic("slice".into())),