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.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -78,7 +78,11 @@
ImportBadFileUtf8(PathBuf),
#[error("tried to import {1} from {0}, but imports is not supported")]
ImportNotSupported(PathBuf, PathBuf),
- #[error("syntax error")]
+ #[error(
+ "syntax error, expected one of {}, got {:?}",
+ .error.expected,
+ .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())
+ )]
ImportSyntaxError {
path: Rc<PathBuf>,
source_code: Rc<str>,
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.rsdiffbeforeafterboth1mod location;23use crate::{EvaluationState, LocError};4pub use location::*;5use std::path::PathBuf;67/// The way paths should be displayed8pub enum PathResolver {9 /// Only filename10 FileName,11 /// Absolute path12 Absolute,13 /// Path relative to base directory14 Relative(PathBuf),15}1617impl PathResolver {18 pub fn resolve(&self, from: &PathBuf) -> String {19 match self {20 Self::FileName => from.file_name().unwrap().to_string_lossy().into_owned(),21 Self::Absolute => from.to_string_lossy().into_owned(),22 Self::Relative(base) => {23 if from.is_relative() {24 return from.to_string_lossy().into_owned();25 }26 pathdiff::diff_paths(from, base)27 .unwrap()28 .to_string_lossy()29 .into_owned()30 }31 }32 }33}3435/// Implements pretty-printing of traces36pub trait TraceFormat {37 fn write_trace(38 &self,39 out: &mut dyn std::fmt::Write,40 evaluation_state: &EvaluationState,41 error: &LocError,42 ) -> Result<(), std::fmt::Error>;43 // fn print_trace(44 // &self,45 // evaluation_state: &EvaluationState,46 // error: &LocError,47 // ) -> Result<(), std::fmt::Error> {48 // self.write_trace(&mut std::fmt::stdout(), evaluation_state, error)49 // }50}5152fn print_code_location(53 out: &mut impl std::fmt::Write,54 start: &CodeLocation,55 end: &CodeLocation,56) -> Result<(), std::fmt::Error> {57 if start.line == end.line {58 if start.column == end.column {59 write!(out, "{}:{}", start.line, end.column - 1)?;60 } else {61 write!(out, "{}:{}-{}", start.line, start.column - 1, end.column)?;62 }63 } else {64 write!(65 out,66 "{}:{}-{}:{}",67 start.line,68 end.column - 1,69 start.line,70 end.column71 )?;72 }73 Ok(())74}7576/// vanilla-like jsonnet formatting77pub struct CompactFormat {78 pub resolver: PathResolver,79 pub padding: usize,80}8182impl TraceFormat for CompactFormat {83 fn write_trace(84 &self,85 out: &mut dyn std::fmt::Write,86 evaluation_state: &EvaluationState,87 error: &LocError,88 ) -> Result<(), std::fmt::Error> {89 writeln!(out, "{}", error.error())?;90 let file_names = error91 .trace()92 .093 .iter()94 .map(|el| {95 let resolved_path = self.resolver.resolve(&el.location.0);96 // TODO: Process all trace elements first97 let location = evaluation_state98 .map_source_locations(&el.location.0, &[el.location.1, el.location.2]);99 (resolved_path, location)100 })101 .map(|(mut n, location)| {102 use std::fmt::Write;103 write!(n, ":").unwrap();104 print_code_location(&mut n, &location[0], &location[1]).unwrap();105 n106 })107 .collect::<Vec<_>>();108 let align = file_names.iter().map(|e| e.len()).max().unwrap_or(0);109 for (i, (el, file)) in error.trace().0.iter().zip(file_names).enumerate() {110 if i != 0 {111 writeln!(out)?;112 }113 write!(114 out,115 "{:<p$}{:<w$}: {}",116 "",117 file,118 el.desc,119 p = self.padding,120 w = align121 )?;122 }123 Ok(())124 }125}126127pub struct JSFormat;128impl TraceFormat for JSFormat {129 fn write_trace(130 &self,131 out: &mut dyn std::fmt::Write,132 evaluation_state: &EvaluationState,133 error: &LocError,134 ) -> Result<(), std::fmt::Error> {135 writeln!(out, "{}", error.error())?;136 for (i, item) in error.trace().0.iter().enumerate() {137 if i != 0 {138 writeln!(out)?;139 }140 let desc = &item.desc;141 let source = item.location.clone();142 let start_end = evaluation_state.map_source_locations(&source.0, &[source.1, source.2]);143144 write!(145 out,146 " at {} ({}:{}:{})",147 desc,148 source.0.to_str().unwrap(),149 start_end[0].line,150 start_end[0].column,151 )?;152 }153 Ok(())154 }155}156157/// rustc-like trace displaying158#[cfg(feature = "explaining-traces")]159pub struct ExplainingFormat {160 pub resolver: PathResolver,161}162#[cfg(feature = "explaining-traces")]163impl TraceFormat for ExplainingFormat {164 fn write_trace(165 &self,166 out: &mut dyn std::fmt::Write,167 evaluation_state: &EvaluationState,168 error: &LocError,169 ) -> Result<(), std::fmt::Error> {170 use annotate_snippets::{171 display_list::{DisplayList, FormatOptions},172 snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},173 };174 writeln!(out, "{}", error.error())?;175 let trace = &error.trace();176 for item in trace.0.iter() {177 let desc = &item.desc;178 let source = item.location.clone();179 let start_end = evaluation_state.map_source_locations(&source.0, &[source.1, source.2]);180181 let source_fragment: String = evaluation_state182 .get_source(&source.0)183 .unwrap()184 .chars()185 .skip(start_end[0].line_start_offset)186 .take(start_end[1].line_end_offset - start_end[0].line_start_offset)187 .collect();188189 let origin = self.resolver.resolve(&source.0);190 let snippet = Snippet {191 opt: FormatOptions {192 color: true,193 ..Default::default()194 },195 title: None,196 footer: vec![],197 slices: vec![Slice {198 source: &source_fragment,199 line_start: start_end[0].line,200 origin: Some(&origin),201 fold: false,202 annotations: vec![SourceAnnotation {203 label: desc,204 annotation_type: AnnotationType::Error,205 range: (206 source.1 - start_end[0].line_start_offset,207 source.2 - start_end[0].line_start_offset,208 ),209 }],210 }],211 };212213 let dl = DisplayList::from(snippet);214 writeln!(out, "{}", dl)?;215 }216 Ok(())217 }218}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())),