difftreelog
Merge pull request #27 from CertainLach/syntax-error-display
in: master
Syntax error display
8 files changed
Cargo.lockdiffbeforeafterboth7checksum = "5c96c3d1062ea7101741480185a6a1275eab01cbe8b20e378d1311bc056d2e08"7checksum = "5c96c3d1062ea7101741480185a6a1275eab01cbe8b20e378d1311bc056d2e08"8dependencies = [8dependencies = [9 "unicode-width",9 "unicode-width",10 "yansi-term",10]11]111212[[package]]13[[package]]514source = "registry+https://github.com/rust-lang/crates.io-index"515source = "registry+https://github.com/rust-lang/crates.io-index"515checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"516checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"517518[[package]]519name = "yansi-term"520version = "0.1.2"521source = "registry+https://github.com/rust-lang/crates.io-index"522checksum = "fe5c30ade05e61656247b2e334a031dfd0cc466fadef865bdcdea8d537951bf1"523dependencies = [524 "winapi",525]516526crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth55# Explaining traces55# Explaining traces56[dependencies.annotate-snippets]56[dependencies.annotate-snippets]57version = "0.9.0"57version = "0.9.0"58features = ["color"]58optional = true59optional = true596060[build-dependencies]61[build-dependencies]crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth78 ImportBadFileUtf8(PathBuf),78 ImportBadFileUtf8(PathBuf),79 #[error("tried to import {1} from {0}, but imports is not supported")]79 #[error("tried to import {1} from {0}, but imports is not supported")]80 ImportNotSupported(PathBuf, PathBuf),80 ImportNotSupported(PathBuf, PathBuf),81 #[error("syntax error")]81 #[error(82 "syntax error, expected one of {}, got {:?}",83 .error.expected,84 .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())85 )]82 ImportSyntaxError {86 ImportSyntaxError {83 path: Rc<PathBuf>,87 path: Rc<PathBuf>,crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth419 use Expr::*;419 use Expr::*;420 let LocExpr(expr, loc) = expr;420 let LocExpr(expr, loc) = expr;421 Ok(match &**expr {421 Ok(match &**expr {422 Literal(LiteralType::This) => Val::Obj(422 Literal(LiteralType::This) => {423 context423 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)424 .this()424 }425 .clone()426 .ok_or_else(|| CantUseSelfOutsideOfObject)?,427 ),428 Literal(LiteralType::Dollar) => Val::Obj(425 Literal(LiteralType::Dollar) => {429 context426 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)430 .dollar()427 }431 .clone()432 .ok_or_else(|| NoTopLevelObjectFound)?,433 ),434 Literal(LiteralType::True) => Val::Bool(true),428 Literal(LiteralType::True) => Val::Bool(true),435 Literal(LiteralType::False) => Val::Bool(false),429 Literal(LiteralType::False) => Val::Bool(false),436 Literal(LiteralType::Null) => Val::Null,430 Literal(LiteralType::Null) => Val::Null,crates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth1#[derive(Clone, PartialEq, Debug)]1#[derive(Clone, PartialEq, Debug)]2pub struct CodeLocation {2pub struct CodeLocation {3 pub offset: usize,43 pub line: usize,5 pub line: usize,4 pub column: usize,6 pub column: usize,252726 let mut out = vec![28 let mut out = vec![27 CodeLocation {29 CodeLocation {30 offset: 0,28 column: 0,31 column: 0,29 line: 0,32 line: 0,30 line_start_offset: 0,33 line_start_offset: 0,40 Some(x) if x.0 == pos => {43 Some(x) if x.0 == pos => {41 let out_idx = x.1;44 let out_idx = x.1;42 with_no_known_line_ending.push(out_idx);45 with_no_known_line_ending.push(out_idx);46 out[out_idx].offset = pos;43 out[out_idx].line = line;47 out[out_idx].line = line;44 out[out_idx].column = column;48 out[out_idx].column = column;45 out[out_idx].line_start_offset = this_line_offset;49 out[out_idx].line_start_offset = this_line_offset;82 ),86 ),83 vec![87 vec![84 CodeLocation {88 CodeLocation {89 offset: 0,85 line: 1,90 line: 1,86 column: 2,91 column: 2,87 line_start_offset: 0,92 line_start_offset: 0,88 line_end_offset: 1193 line_end_offset: 11,89 },94 },90 CodeLocation {95 CodeLocation {96 offset: 14,91 line: 2,97 line: 2,92 column: 4,98 column: 4,93 line_start_offset: 12,99 line_start_offset: 12,crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth1mod location;1mod location;223use crate::{EvaluationState, LocError};3use crate::{error::Error, EvaluationState, LocError};4pub use location::*;4pub use location::*;5use std::path::PathBuf;5use std::path::PathBuf;6687 error: &LocError,87 error: &LocError,88 ) -> Result<(), std::fmt::Error> {88 ) -> Result<(), std::fmt::Error> {89 writeln!(out, "{}", error.error())?;89 writeln!(out, "{}", error.error())?;90 if let Error::ImportSyntaxError {91 path,92 source_code,93 error,94 } = error.error()95 {96 use std::fmt::Write;97 let mut n = self.resolver.resolve(path);98 let mut offset = error.location.offset;99 let is_eof = if offset >= source_code.len() {100 offset = source_code.len() - 1;101 true102 } else {103 false104 };105 let mut location = offset_to_location(source_code, &[offset])106 .into_iter()107 .next()108 .unwrap();109 if is_eof {110 location.column += 1;111 }112113 write!(n, ":").unwrap();114 print_code_location(&mut n, &location, &location).unwrap();115 write!(out, "{:<p$}{}", "", n, p = self.padding,)?;116 }90 let file_names = error117 let file_names = error91 .trace()118 .trace()92 .0119 .0159pub struct ExplainingFormat {186pub struct ExplainingFormat {160 pub resolver: PathResolver,187 pub resolver: PathResolver,161}188}162#[cfg(feature = "explaining-traces")]189#[cfg(feature = "explaining-traces")]190impl TraceFormat for ExplainingFormat {191 fn write_trace(192 &self,193 out: &mut dyn std::fmt::Write,194 evaluation_state: &EvaluationState,195 error: &LocError,196 ) -> Result<(), std::fmt::Error> {197 writeln!(out, "{}", error.error())?;198 if let Error::ImportSyntaxError {199 path,200 source_code,201 error,202 } = error.error()203 {204 let mut offset = error.location.offset;205 if offset >= source_code.len() {206 offset = source_code.len() - 1;207 }208 let mut location = offset_to_location(source_code, &[offset])209 .into_iter()210 .next()211 .unwrap();212 if location.column >= 1 {213 location.column -= 1;214 }215216 self.print_snippet(217 out,218 source_code,219 path,220 &location,221 &location,222 "^ syntax error",223 )?;224 }225 let trace = &error.trace();226 for item in trace.0.iter() {227 let desc = &item.desc;228 let source = item.location.clone();229 let start_end = evaluation_state.map_source_locations(&source.0, &[source.1, source.2]);230231 self.print_snippet(232 out,233 &evaluation_state.get_source(&source.0).unwrap(),234 &source.0,235 &start_end[0],236 &start_end[1],237 desc,238 )?;239 }240 Ok(())241 }242}243163impl TraceFormat for ExplainingFormat {244impl ExplainingFormat {164 fn write_trace(245 fn print_snippet(165 &self,246 &self,166 out: &mut dyn std::fmt::Write,247 out: &mut dyn std::fmt::Write,167 evaluation_state: &EvaluationState,248 source: &str,168 error: &LocError,249 origin: &PathBuf,250 start: &CodeLocation,251 end: &CodeLocation,252 desc: &str,169 ) -> Result<(), std::fmt::Error> {253 ) -> Result<(), std::fmt::Error> {170 use annotate_snippets::{254 use annotate_snippets::{171 display_list::{DisplayList, FormatOptions},255 display_list::{DisplayList, FormatOptions},172 snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},256 snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},173 };257 };174 writeln!(out, "{}", error.error())?;258175 let trace = &error.trace();259 let source_fragment: String = source176 for item in trace.0.iter() {260 .chars()177 let desc = &item.desc;261 .skip(start.line_start_offset)178 let source = item.location.clone();262 .take(end.line_end_offset - end.line_start_offset)179 let start_end = evaluation_state.map_source_locations(&source.0, &[source.1, source.2]);263 .collect();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();188264189 let origin = self.resolver.resolve(&source.0);265 let origin = self.resolver.resolve(origin);190 let snippet = Snippet {266 let snippet = Snippet {191 opt: FormatOptions {267 opt: FormatOptions {192 color: true,268 color: true,196 footer: vec![],272 footer: vec![],197 slices: vec![Slice {273 slices: vec![Slice {198 source: &source_fragment,274 source: &source_fragment,199 line_start: start_end[0].line,275 line_start: start.line,200 origin: Some(&origin),276 origin: Some(&origin),201 fold: false,277 fold: false,202 annotations: vec![SourceAnnotation {278 annotations: vec![SourceAnnotation {203 label: desc,279 label: desc,204 annotation_type: AnnotationType::Error,280 annotation_type: AnnotationType::Error,205 range: (281 range: (206 source.1 - start_end[0].line_start_offset,282 start.offset - start.line_start_offset,207 source.2 - start_end[0].line_start_offset,283 end.offset - start.line_start_offset,208 ),284 ),209 }],285 }],210 }],286 }],211 };287 };212288213 let dl = DisplayList::from(snippet);289 let dl = DisplayList::from(snippet);214 writeln!(out, "{}", dl)?;290 writeln!(out, "{}", dl)?;215 }291216 Ok(())292 Ok(())217 }293 }218}294}crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth349 for v in arr.iter() {349 for v in arr.iter() {350 out.push_str("---\n");350 out.push_str("---\n");351 out.push_str(&v.manifest(format)?);351 out.push_str(&v.manifest(format)?);352 out.push_str("\n");352 out.push('\n');353 }353 }354 out.push_str("...");354 out.push_str("...");355 }355 }crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth37 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()37 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()38 rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")38 rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")393940 rule keyword(id: &'static str)40 rule keyword(id: &'static str) -> ()41 = ##parse_string_literal(id) end_of_ident()41 = ##parse_string_literal(id) end_of_ident()42 // Adds location data information to existing expression42 // Adds location data information to existing expression43 rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr43 rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr85 [' ' | '\t']*<, {prefix.len() - 1}> "|||"85 [' ' | '\t']*<, {prefix.len() - 1}> "|||"86 {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}86 {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}87 pub rule string() -> String87 pub rule string() -> String88 = "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}88 = quiet!{ "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}89 / "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}89 / "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}90 / "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}90 / "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}91 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}91 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}92 / string_block()92 / string_block() } / expected!("<string>")939394 pub rule field_name(s: &ParserSettings) -> expr::FieldName94 pub rule field_name(s: &ParserSettings) -> expr::FieldName95 = name:$(id()) {expr::FieldName::Fixed(name.into())}95 = name:$(id()) {expr::FieldName::Fixed(name.into())}208 SliceDesc { start, end, step }208 SliceDesc { start, end, step }209 }209 }210211 rule binop(x: rule<()>) -> ()212 = quiet!{ x() } / expected!("<binary op>")213 rule unaryop(x: rule<()>) -> ()214 = quiet!{ x() } / expected!("<unary op>")210215211 rule expr(s: &ParserSettings) -> LocExpr216 rule expr(s: &ParserSettings) -> LocExpr212 = start:position!() a:precedence! {217 = start:position!() a:precedence! {213 a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}218 a:(@) _ binop(<"||">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}214 --219 --215 a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}220 a:(@) _ binop(<"&&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}216 --221 --217 a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}222 a:(@) _ binop(<"|">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}218 --223 --219 a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}224 a:@ _ binop(<"^">) _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}220 --225 --221 a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}226 a:(@) _ binop(<"&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}222 --227 --223 a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::Apply(228 a:(@) _ binop(<"==">) _ b:@ {loc_expr_todo!(Expr::Apply(224 el!(Expr::Intrinsic("equals".into())),229 el!(Expr::Intrinsic("equals".into())),225 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),230 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),226 true231 true227 ))}232 ))}228 a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(233 a:(@) _ binop(<"!=">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(229 el!(Expr::Intrinsic("equals".into())),234 el!(Expr::Intrinsic("equals".into())),230 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),235 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),231 true236 true232 ))))}237 ))))}233 --238 --234 a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}239 a:(@) _ binop(<"<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}235 a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}240 a:(@) _ binop(<">">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}236 a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}241 a:(@) _ binop(<"<=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}237 a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}242 a:(@) _ binop(<">=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}238 a:(@) _ keyword("in") _ b:@ {loc_expr_todo!(Expr::Apply(243 a:(@) _ binop(<keyword("in")>) _ b:@ {loc_expr_todo!(Expr::Apply(239 el!(Expr::Intrinsic("objectHasEx".into())), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),244 el!(Expr::Intrinsic("objectHasEx".into())), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),240 true245 true241 ))}246 ))}242 --247 --243 a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}248 a:(@) _ binop(<"<<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}244 a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}249 a:(@) _ binop(<">>">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}245 --250 --246 a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}251 a:(@) _ binop(<"+">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}247 a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}252 a:(@) _ binop(<"-">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}248 --253 --249 a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}254 a:(@) _ binop(<"*">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}250 a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}255 a:(@) _ binop(<"/">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}251 a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::Apply(256 a:(@) _ binop(<"%">) _ b:@ {loc_expr_todo!(Expr::Apply(252 el!(Expr::Intrinsic("mod".into())), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),257 el!(Expr::Intrinsic("mod".into())), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),253 false258 false254 ))}259 ))}255 --260 --256 "-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}261 unaryop(<"-">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}257 "!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}262 unaryop(<"!">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}258 "~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }263 unaryop(<"~">) _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }259 --264 --260 a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(265 a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(261 el!(Expr::Intrinsic("slice".into())),266 el!(Expr::Intrinsic("slice".into())),