aboutsummaryrefslogtreecommitdiffstats
path: root/src/parse/mod.rs
diff options
context:
space:
mode:
authorJonas Maier <jonas@x77.dev>2026-06-01 21:55:53 +0200
committerJonas Maier <jonas@x77.dev>2026-06-01 21:55:53 +0200
commitc73ed9310d8162b71183688de62bf8c1fc8420df (patch)
treec49063954f665a072cb8e63b10c4e95e31769826 /src/parse/mod.rs
parentc52169f535f89c0300ec06e13ddcaa7e0f459a0b (diff)
downloadpish-c73ed9310d8162b71183688de62bf8c1fc8420df.tar.gz
syntax highlighting for regex; syntax highlighting syntax errors
Diffstat (limited to 'src/parse/mod.rs')
-rw-r--r--src/parse/mod.rs35
1 files changed, 33 insertions, 2 deletions
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<Self> {
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<OtherHighlights> 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();