difftreelog
feat show full error range instead of just start
in: master
4 files changed
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -15,14 +15,9 @@
};
#[derive(Debug, Clone)]
-pub struct SyntaxErrorLocation {
- pub offset: usize,
-}
-
-#[derive(Debug, Clone)]
pub struct SyntaxError {
pub message: String,
- pub location: SyntaxErrorLocation,
+ pub location: (u32, u32),
}
impl fmt::Display for SyntaxError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth50#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]50#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]51compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");51compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");525253pub use error::{SyntaxError, SyntaxErrorLocation};53pub use error::SyntaxError;54pub use obj::*;54pub use obj::*;55pub use rustc_hash;55pub use rustc_hash;56use rustc_hash::FxHashMap;56use rustc_hash::FxHashMap;87 jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {87 jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {88 SyntaxError {88 SyntaxError {89 message: e.message,89 message: e.message,90 location: SyntaxErrorLocation {90 location: (e.location.0, e.location.1),91 offset: e.location.offset,92 },93 }91 }94 })92 })95}93}107 "expected {}, got {:?}",105 "expected {}, got {:?}",108 e.expected,106 e.expected,109 code.chars()107 code.chars()110 .nth(e.location.offset)108 .nth(e.location.0)111 .map_or_else(|| "EOF".into(), |c: char| c.to_string())109 .map_or_else(|| "EOF".into(), |c: char| c.to_string())112 )110 )113 },111 },114 |v| v[3..].into(),112 |v| v[3..].into(),115 );113 );116 SyntaxError {114 SyntaxError {117 message,115 message,118 location: SyntaxErrorLocation {116 location: e.location,119 offset: e.location.offset,120 },121 }117 }122 })118 })123}119}crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -122,7 +122,7 @@
|| path.source_path().to_string(),
|r| self.resolver.resolve(r),
);
- let mut offset = error.location.offset;
+ let mut offset = error.location.0 as usize;
let is_eof = if offset >= path.code().len() {
offset = path.code().len().saturating_sub(1);
true
@@ -263,11 +263,15 @@
write!(out, "{}", error.error())?;
if let ErrorKind::ImportSyntaxError { path, error } = error.error() {
writeln!(out)?;
- let offset = error.location.offset;
+ let mut offset = error.location;
+ // To inclusive range
+ if offset.1 > offset.0 {
+ offset.1 -= 1;
+ }
let mut builder = SnippetBuilder::new(path.code());
builder
.error(Text::fragment("syntax error", Formatting::default()))
- .range(offset..=offset)
+ .range(offset.0 as usize..=offset.1 as usize)
.build();
let source = builder.build();
let ansi = source_to_ansi(&source);
crates/jrsonnet-ir-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-ir-parser/src/lib.rs
+++ b/crates/jrsonnet-ir-parser/src/lib.rs
@@ -7,21 +7,16 @@
ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers, Slice, SliceDesc,
Source, Span, Spanned, UnaryOpType, Visibility, unescape,
};
-use jrsonnet_lexer::{Lexeme, Lexer, SyntaxKind, T, collect_lexed_str_block};
+use jrsonnet_lexer::{Lexeme, Lexer, Span as LexSpan, SyntaxKind, T, collect_lexed_str_block};
pub struct ParserSettings {
pub source: Source,
-}
-
-#[derive(Debug, Clone)]
-pub struct ParseErrorLocation {
- pub offset: usize,
}
#[derive(Debug, Clone)]
pub struct ParseError {
pub message: String,
- pub location: ParseErrorLocation,
+ pub location: LexSpan,
}
impl std::fmt::Display for ParseError {
@@ -131,9 +126,7 @@
fn error(&self, message: String) -> ParseError {
ParseError {
- location: ParseErrorLocation {
- offset: self.span_start() as usize,
- },
+ location: self.lexemes[self.offset].range,
message,
}
}
@@ -143,9 +136,6 @@
return Err(self.error(format!("expected identifier, got {}", self.current_desc())));
}
let text = self.text();
- if is_reserved(text) {
- return Err(self.error(format!("expected identifier, got reserved word '{text}'")));
- }
let s: IStr = text.into();
self.eat_any();
Ok(s)
@@ -156,23 +146,6 @@
}
}
-fn is_reserved(s: &str) -> bool {
- matches!(
- s,
- "assert"
- | "else" | "error"
- | "false" | "for"
- | "function"
- | "if" | "import"
- | "importstr"
- | "importbin"
- | "in" | "local"
- | "null" | "tailstrict"
- | "then" | "self"
- | "super" | "true"
- )
-}
-
fn spanned<T: Acyclic>(
p: &mut Parser<'_>,
cb: impl FnOnce(&mut Parser<'_>) -> Result<T>,
@@ -1040,9 +1013,7 @@
if let Some(desc) = lexeme.kind.error_description() {
return Err(ParseError {
message: desc.to_owned(),
- location: ParseErrorLocation {
- offset: lexeme.range.0 as usize,
- },
+ location: lexeme.range,
});
}
}