From c73ed9310d8162b71183688de62bf8c1fc8420df Mon Sep 17 00:00:00 2001 From: Jonas Maier Date: Mon, 1 Jun 2026 21:55:53 +0200 Subject: syntax highlighting for regex; syntax highlighting syntax errors --- src/parse/mod.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'src/parse/mod.rs') diff --git a/src/parse/mod.rs b/src/parse/mod.rs index b3855a8..b07d5f9 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -426,7 +426,7 @@ impl Parse for Block { fn parse(b: &mut Cursor<'_>) -> Result { let mut commands = Vec::new(); - b.consume_keyword(Keyword::OpenBrace)?; + b.expect_keyword(Keyword::OpenBrace)?; loop { while { @@ -444,7 +444,7 @@ impl Parse for Block { commands.push(cmd); } - let finished_parsing = match b.consume_keyword(Keyword::CloseBrace) { + let finished_parsing = match b.expect_keyword(Keyword::CloseBrace) { Ok(_) => true, Err(_) if b.is_completion() => false, Err(e) => Err(e)?, @@ -1365,7 +1365,10 @@ impl From for HighlightKind { pub enum OtherHighlights { String, Variable, + Regex, + RegexSymbol, Escapes, + SyntaxError, } impl OtherHighlights { @@ -1374,6 +1377,9 @@ impl OtherHighlights { OtherHighlights::String => b"string", OtherHighlights::Variable => b"var", OtherHighlights::Escapes => b"escape", + OtherHighlights::Regex => b"regex", + OtherHighlights::RegexSymbol => b"regexsym", + OtherHighlights::SyntaxError => b"syntax_error", } } } @@ -1634,6 +1640,31 @@ impl<'a> Cursor<'a> { T::parse(self) } + fn expect_keyword(&mut self, kw: Keyword) -> Result<()> { + if !self.is_completion() { + return self.consume_keyword(kw) + } + + // very lax parsing that consumes everything in its way until the keyword arrives + self.spaces(); + let begin = self.loc(); + loop { + let end = self.loc_u32(); + if self.consume_keyword(kw).is_ok() { + if end > begin.start { + self.highlight(begin.to(end), OtherHighlights::SyntaxError); + } + return Ok(()); + } else if self.has() { + self.adv(); + } else { + break; + } + } + self.highlight_from(begin, OtherHighlights::SyntaxError); + Err(ParseError::ExpectedKeyword(kw)) + } + fn consume_keyword(&mut self, kw: Keyword) -> Result<()> { let bytes = kw.as_bytes(); -- cgit v1.2.3