Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(#1898): do not panic on typst parsing error (#1921)
Grant Lemons168bd75a0d9e
diff --git a/harper-typst/src/lib.rs b/harper-typst/src/lib.rsindex 4f02b9311..152ab2e81 100644--- a/harper-typst/src/lib.rs+++ b/harper-typst/src/lib.rs@@ -79,3 +79,14 @@ fn convert_parbreaks<'a>(buf: &'a mut Vec<SyntaxNode>, exprs: &'a [Expr]) -> Vecres}++#[cfg(test)]+mod tests {+ use super::*;+ use harper_core::parsers::StrParser;++ #[test]+ fn issue_1898() {+ Typst.parse_str("#for ");+ }+}diff --git a/harper-typst/src/offset_cursor.rs b/harper-typst/src/offset_cursor.rsindex 4ef8df48f..a81f24f9e 100644--- a/harper-typst/src/offset_cursor.rs+++ b/harper-typst/src/offset_cursor.rs@@ -20,24 +20,24 @@ impl<'a> OffsetCursor<'a> {}/// Returns a new [`OffsetCursor`] at the given byte based on the current cursor.- pub fn push_to(self, new_byte: usize) -> Self {+ pub fn push_to(self, new_byte: usize) -> Option<Self> {assert!(new_byte >= self.byte);if new_byte == self.byte {- return self;+ return Some(self);}- Self {- char: self.char + self.doc.get(self.byte..new_byte).unwrap().chars().count(),+ Some(Self {+ char: self.char + self.doc.get(self.byte..new_byte)?.chars().count(),byte: new_byte,..self- }+ })}/// Returns a new [`OffsetCursor`] at the beginning of the given [`typst_syntax::Span`] based/// on the current cursor.- pub fn push_to_span(self, span: typst_syntax::Span) -> Self {- let new_byte = self.doc.range(span).unwrap().start;+ pub fn push_to_span(self, span: typst_syntax::Span) -> Option<Self> {+ let new_byte = self.doc.range(span)?.start;self.push_to(new_byte)}diff --git a/harper-typst/src/typst_translator.rs b/harper-typst/src/typst_translator.rsindex 2c2db3c84..baddba8d3 100644--- a/harper-typst/src/typst_translator.rs+++ b/harper-typst/src/typst_translator.rs@@ -16,8 +16,8 @@ use typst_syntax::{macro_rules! def_token {($doc:expr, $a:expr, $kind:expr, $offset:ident) => {{let range = $doc.range($a.span()).unwrap();- let start = $offset.push_to(range.start);- let end_char_loc = start.push_to(range.end).char;+ let start = $offset.push_to(range.start)?;+ let end_char_loc = start.push_to(range.end)?.char;Some(vec![Token {span: harper_core::Span::new(start.char, end_char_loc),@@ -122,7 +122,7 @@ impl<'a> TypstTranslator<'a> {pub fn parse_expr(self, expr: Expr, offset: OffsetCursor) -> Option<Vec<Token>> {// Update the offset that will be passed to other functions by moving it to the beginning// of the current expression's span.- let offset = offset.push_to_span(expr.span());+ let offset = offset.push_to_span(expr.span())?;/// Simplification of [`def_token!`] that bakes-in local variablesmacro_rules! token {@@ -241,7 +241,7 @@ impl<'a> TypstTranslator<'a> {//// A full list of variants is available in the [typst_syntax docs](https://docs.rs/typst/latest/typst/syntax/ast/enum.Expr.html)match expr {- Expr::Text(text) => self.parse_english(text.get(), offset.push_to_span(text.span())),+ Expr::Text(text) => self.parse_english(text.get(), offset.push_to_span(text.span())?),Expr::Space(a) => {let mut chars = get_text!(a).chars();let first_char = chars.next().unwrap();@@ -280,7 +280,7 @@ impl<'a> TypstTranslator<'a> {.chain(term_item.description().exprs()),),Expr::Str(text) => {- let offset = offset.push_to_span(text.span()).char + 1;+ let offset = offset.push_to_span(text.span())?.char + 1;let string = text.to_untyped().text();Some(@@ -367,3 +367,21 @@ impl<'a> TypstTranslator<'a> {}}}++#[cfg(test)]+mod tests {+ use super::*;+ use typst_syntax::ast::None;++ #[test]+ fn parse_none_returns_none() {+ let source = Source::detached("");+ let translator = TypstTranslator::new(&source);++ assert!(+ translator+ .parse_expr(Expr::None(None::default()), OffsetCursor::new(&source))+ .is_none()+ )+ }+}