diff options
| author | PgBiel <9021226+PgBiel@users.noreply.github.com> | 2024-07-23 12:05:05 -0300 |
|---|---|---|
| committer | PgBiel <9021226+PgBiel@users.noreply.github.com> | 2024-07-23 13:31:10 -0300 |
| commit | e105cd1956b311bb90045d4e0c339c51a6567cc8 (patch) | |
| tree | 35080bc60de86ce06c92f9d40f7ce9b23ccf3c1f /crates/typst-syntax | |
| parent | bab391a580f1cb3af5d40b905ed252cfbfb4a488 (diff) | |
improve errors in annotations
Diffstat (limited to 'crates/typst-syntax')
| -rw-r--r-- | crates/typst-syntax/src/lexer.rs | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/crates/typst-syntax/src/lexer.rs b/crates/typst-syntax/src/lexer.rs index ecbf0ea6..d13fc527 100644 --- a/crates/typst-syntax/src/lexer.rs +++ b/crates/typst-syntax/src/lexer.rs @@ -283,7 +283,7 @@ impl Lexer<'_> { // After we finished specifying arguments, there must only // be whitespaces until the line ends. self.s.eat_until(char::is_whitespace); - self.error("expected end of annotation") + self.error("unexpected characters after end of annotation") } Some(c) if is_id_start(c) => { self.s.eat_while(is_id_continue); @@ -294,9 +294,32 @@ impl Lexer<'_> { found_closing_paren = true; SyntaxKind::RightParen } - Some(c) => self.error(eco_format!( - "the character '{c}' is not valid in an annotation" - )), + // Explicitly detect comments for more helpful errors + Some('/') if self.s.at(['/', '*']) => { + if self.s.eat() == Some('*') { + // Found a block comment. Advance until the next + // newline or '*/' just for a more accurate error span. + while !self.s.eat_if("*/") && !self.s.at(is_newline) { + self.s.eat(); + } + } else { + self.s.eat_until(is_newline); + } + self.error(eco_format!("unexpected comment inside annotation")) + } + Some(_) => { + self.s.eat_until(|c: char| { + c.is_whitespace() || has_opening_paren && c == ')' + }); + self.error(eco_format!( + "expected identifier{} in annotation", + if has_opening_paren { + ", string or closing paren" + } else { + " or string" + } + )) + } None => break, }; @@ -308,7 +331,12 @@ impl Lexer<'_> { // Right parenthesis (covered above) if has_opening_paren && !found_closing_paren { - subtree.push(self.emit_error("expected closing paren", self.s.cursor())); + subtree.push( + self.emit_error( + "expected closing paren after annotation", + self.s.cursor(), + ), + ); } SyntaxNode::inner(SyntaxKind::Annotation, subtree) |
