summaryrefslogtreecommitdiff
path: root/src/syntax/lexer.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-05-24 16:58:51 +0200
committerLaurenz <laurmaedje@gmail.com>2023-05-24 16:58:51 +0200
commit017027bf392f7be2e73428fa34a3d1ef0b8d255e (patch)
tree55d862cc8c987c0b842ad063df653f97606f6d73 /src/syntax/lexer.rs
parentb9d03be9755dae7f439763d181ec0c55a1e4c3d6 (diff)
Rework delimiter errors
Fixes #572
Diffstat (limited to 'src/syntax/lexer.rs')
-rw-r--r--src/syntax/lexer.rs39
1 files changed, 16 insertions, 23 deletions
diff --git a/src/syntax/lexer.rs b/src/syntax/lexer.rs
index ee73a595..d0e5c9bd 100644
--- a/src/syntax/lexer.rs
+++ b/src/syntax/lexer.rs
@@ -80,12 +80,6 @@ impl Lexer<'_> {
self.error = Some((message.into(), ErrorPos::Full));
SyntaxKind::Error
}
-
- /// Construct a positioned syntax error.
- fn error_at_end(&mut self, message: impl Into<EcoString>) -> SyntaxKind {
- self.error = Some((message.into(), ErrorPos::End));
- SyntaxKind::Error
- }
}
/// Shared.
@@ -209,7 +203,7 @@ impl Lexer<'_> {
if self.s.eat_if("u{") {
let hex = self.s.eat_while(char::is_ascii_alphanumeric);
if !self.s.eat_if('}') {
- return self.error_at_end("expected closing brace");
+ return self.error("unclosed unicode escape sequence");
}
if u32::from_str_radix(hex, 16)
@@ -251,20 +245,15 @@ impl Lexer<'_> {
}
if found != backticks {
- let remaining = backticks - found;
- let noun = if remaining == 1 { "backtick" } else { "backticks" };
- return self.error_at_end(if found == 0 {
- eco_format!("expected {} {}", remaining, noun)
- } else {
- eco_format!("expected {} more {}", remaining, noun)
- });
+ return self.error("unclosed raw text");
}
SyntaxKind::Raw
}
fn link(&mut self) -> SyntaxKind {
- let mut bracket_stack = Vec::new();
+ let mut brackets = Vec::new();
+
#[rustfmt::skip]
self.s.eat_while(|c: char| {
match c {
@@ -275,20 +264,24 @@ impl Lexer<'_> {
| ',' | '-' | '.' | '/' | ':' | ';' | '='
| '?' | '@' | '_' | '~' | '\'' => true,
'[' => {
- bracket_stack.push(SyntaxKind::LeftBracket);
+ brackets.push(SyntaxKind::LeftBracket);
true
}
'(' => {
- bracket_stack.push(SyntaxKind::LeftParen);
+ brackets.push(SyntaxKind::LeftParen);
true
}
- ']' => bracket_stack.pop() == Some(SyntaxKind::LeftBracket),
- ')' => bracket_stack.pop() == Some(SyntaxKind::LeftParen),
+ ']' => brackets.pop() == Some(SyntaxKind::LeftBracket),
+ ')' => brackets.pop() == Some(SyntaxKind::LeftParen),
_ => false,
}
});
- if !bracket_stack.is_empty() {
- return self.error_at_end("expected closing bracket in link");
+
+ if !brackets.is_empty() {
+ return self.error(
+ "automatic links cannot contain unbalanced brackets, \
+ use the `link` function instead",
+ );
}
// Don't include the trailing characters likely to be part of text.
@@ -328,7 +321,7 @@ impl Lexer<'_> {
}
if !self.s.eat_if('>') {
- return self.error_at_end("expected closing angle bracket");
+ return self.error("unclosed label");
}
SyntaxKind::Label
@@ -620,7 +613,7 @@ impl Lexer<'_> {
});
if !self.s.eat_if('"') {
- return self.error_at_end("expected quote");
+ return self.error("unclosed string");
}
SyntaxKind::Str