aboutsummaryrefslogtreecommitdiffstats
path: root/src/parse/regex/mod.rs
diff options
context:
space:
mode:
authorJonas Maier <jonas@x77.dev>2026-06-03 20:17:25 +0200
committerJonas Maier <jonas@x77.dev>2026-06-03 20:19:16 +0200
commitb39823a4f9d13a5d313ca665e06884b587bceb9a (patch)
tree387e5883ff4f5bbd72f972fa199cb1d15058b6cb /src/parse/regex/mod.rs
parent38e8e7bf000aaa1f7f6be15d72be35d6dc585271 (diff)
downloadpish-b39823a4f9d13a5d313ca665e06884b587bceb9a.tar.gz
regex: explicitly return an error for unsupported non-greedy repetitions
Diffstat (limited to 'src/parse/regex/mod.rs')
-rw-r--r--src/parse/regex/mod.rs39
1 files changed, 20 insertions, 19 deletions
diff --git a/src/parse/regex/mod.rs b/src/parse/regex/mod.rs
index f35d3f2..72e11da 100644
--- a/src/parse/regex/mod.rs
+++ b/src/parse/regex/mod.rs
@@ -1,4 +1,4 @@
-use crate::parse::OtherHighlights;
+use crate::parse::{NotImplementedKind, OtherHighlights};
use super::{Parse, ParseError, Result};
@@ -98,26 +98,27 @@ fn parse_rep(s: &mut super::Cursor<'_>) -> Result<Pattern> {
let begin = s.loc();
- match s.peek() {
- b'*' => {
- s.adv();
- s.highlight_from(begin, OtherHighlights::RegexSymbol);
- Ok(Pattern::Rep(Box::new(atom), 0, None))
- }
- b'+' => {
- s.adv();
- s.highlight_from(begin, OtherHighlights::RegexSymbol);
- Ok(Pattern::Rep(Box::new(atom), 1, None))
- }
- b'?' => {
- s.adv();
- s.highlight_from(begin, OtherHighlights::RegexSymbol);
- Ok(Pattern::Rep(Box::new(atom), 0, Some(1)))
+ let rep = match s.peek() {
+ b'*' => Some((0, None)),
+ b'+' => Some((1, None)),
+ b'?' => Some((0, Some(1))),
+ _ => None,
+ };
+
+ if let Some((min_rep, max_rep)) = rep {
+ s.adv();
+ s.highlight_from(begin, OtherHighlights::RegexSymbol);
+
+ if s.has() && s.peek() == b'?' {
+ return Err(ParseError::NotYetImplemented(
+ NotImplementedKind::NonGreedyRegexRepetition,
+ ));
}
- _ => Ok(atom),
- }
- // TODO: non-greedy
+ Ok(Pattern::Rep(Box::new(atom), min_rep, max_rep))
+ } else {
+ Ok(atom)
+ }
}
const SYMBOLS: &[u8] = b"{}[]()*+-?|.\\ ";