git.delta.rocks / jrsonnet / refs/commits / 96d9345e6c0d

difftreelog

feat fancy syntax errors

Лач2020-06-06parent: #d4fc7a8.patch.diff
in: master

2 files changed

modifiedcmds/jsonnet/Cargo.tomldiffbeforeafterboth
88
9[dependencies]9[dependencies]
10jsonnet-evaluator = { path = "../../crates/jsonnet-evaluator" }10jsonnet-evaluator = { path = "../../crates/jsonnet-evaluator" }
11jsonnet-parser = { path = "../../crates/jsonnet-parser" }
11annotate-snippets = "0.8.0"12annotate-snippets = "0.8.0"
1213
13[dependencies.clap]14[dependencies.clap]
modifiedcmds/jsonnet/src/main.rsdiffbeforeafterboth
4use jsonnet_evaluator::{EvaluationState, LocError, StackTrace, Val};4use jsonnet_evaluator::{EvaluationState, LocError, StackTrace, Val};
5use location::{offset_to_location, CodeLocation};5use location::{offset_to_location, CodeLocation};
6use std::env::current_dir;6use std::env::current_dir;
7use std::str::FromStr;7use std::{path::PathBuf, str::FromStr};
88
9enum Format {9enum Format {
10 None,10 None,
87 }87 }
88 let mut input = current_dir().unwrap();88 let mut input = current_dir().unwrap();
89 input.push(opts.input.clone());89 input.push(opts.input.clone());
90 evaluator
91 .add_file(
92 input.clone(),
93 String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap(),90 let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();
94 )91 if let Err(e) = evaluator.add_file(input.clone(), code_string.clone()) {
95 .unwrap();92 print_syntax_error(e, &input, &code_string);
93 std::process::exit(2);
94 }
96 let result = evaluator.evaluate_file(&input);95 let result = evaluator.evaluate_file(&input);
97 match result {96 match result {
98 Ok(v) => {97 Ok(v) => {
148 print_trace(&(err.1), evaluator, &opts);147 print_trace(&(err.1), evaluator, &opts);
149}148}
149
150fn print_syntax_error(error: jsonnet_parser::ParseError, file: &PathBuf, code: &str) {
151 use annotate_snippets::{
152 display_list::{DisplayList, FormatOptions},
153 snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
154 };
155 //&("Expected: ".to_owned() + error.expected)
156 let origin = file.to_str().unwrap();
157 let error_message = format!("Expected: {}", error.expected);
158 let snippet = Snippet {
159 opt: FormatOptions {
160 color: true,
161 ..Default::default()
162 },
163 title: Some(Annotation {
164 label: Some(&error_message),
165 id: None,
166 annotation_type: AnnotationType::Error,
167 }),
168 footer: vec![],
169 slices: vec![Slice {
170 source: &code,
171 line_start: 1,
172 origin: Some(origin),
173 fold: false,
174 annotations: vec![SourceAnnotation {
175 label: "At this position",
176 annotation_type: AnnotationType::Error,
177 range: (error.location.offset, error.location.offset + 1),
178 }],
179 }],
180 };
181
182 let dl = DisplayList::from(snippet);
183 println!("{}", dl);
184}
150185
151fn print_trace(trace: &StackTrace, evaluator: EvaluationState, opts: &Opts) {186fn print_trace(trace: &StackTrace, evaluator: EvaluationState, opts: &Opts) {
152 use annotate_snippets::{187 use annotate_snippets::{