Skip to content
This repository was archived by the owner on Mar 22, 2024. It is now read-only.

Commit 04bb80f

Browse files
committed
impl Match.group
1 parent f728755 commit 04bb80f

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

interp.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub(crate) fn pymatch(
115115
has_matched: None,
116116
};
117117
state.context_stack.push(ctx);
118+
let mut dispatcher = OpcodeDispatcher::new();
118119

119120
let mut has_matched = None;
120121
loop {
@@ -123,7 +124,6 @@ pub(crate) fn pymatch(
123124
}
124125
let ctx_id = state.context_stack.len() - 1;
125126
let mut drive = StackDrive::drive(ctx_id, state);
126-
let mut dispatcher = OpcodeDispatcher::new();
127127

128128
has_matched = dispatcher.pymatch(&mut drive);
129129
state = drive.take();
@@ -132,11 +132,11 @@ pub(crate) fn pymatch(
132132
}
133133
}
134134

135-
if has_matched == None || has_matched == Some(false) {
136-
return None;
135+
if has_matched != Some(true) {
136+
None
137+
} else {
138+
Some(Match::new(&state, pattern.clone(), string.clone()))
137139
}
138-
139-
Some(Match::new(&state, pattern.clone(), string.clone()))
140140
}
141141

142142
#[derive(Debug, Copy, Clone)]
@@ -344,7 +344,9 @@ impl OpcodeDispatcher {
344344
while drive.remaining_codes() > 0 && drive.ctx().has_matched.is_none() {
345345
let code = drive.peek_code(0);
346346
let opcode = SreOpcode::try_from(code).unwrap();
347-
self.dispatch(opcode, drive);
347+
if !self.dispatch(opcode, drive) {
348+
return None;
349+
}
348350
}
349351
match drive.ctx().has_matched {
350352
Some(matched) => Some(matched),
@@ -469,7 +471,7 @@ impl OpcodeDispatcher {
469471
SreOpcode::MAX_UNTIL => Box::new(OpMaxUntil::default()),
470472
SreOpcode::MIN_UNTIL => Box::new(OpMinUntil::default()),
471473
SreOpcode::REPEAT => Box::new(OpRepeat::default()),
472-
SreOpcode::REPEAT_ONE => Box::new(OpMinRepeatOne::default()),
474+
SreOpcode::REPEAT_ONE => Box::new(OpRepeatOne::default()),
473475
SreOpcode::MIN_REPEAT_ONE => Box::new(OpMinRepeatOne::default()),
474476
SreOpcode::GROUPREF => once(|drive| general_op_groupref(drive, |x| x)),
475477
SreOpcode::GROUPREF_IGNORE => once(|drive| general_op_groupref(drive, lower_ascii)),
@@ -1329,7 +1331,7 @@ struct OpRepeatOne {
13291331
child_ctx_id: usize,
13301332
mincount: usize,
13311333
maxcount: usize,
1332-
count: usize,
1334+
count: isize,
13331335
}
13341336
impl Default for OpRepeatOne {
13351337
fn default() -> Self {
@@ -1354,9 +1356,9 @@ impl OpcodeExecutor for OpRepeatOne {
13541356
return None;
13551357
}
13561358
drive.state.string_position = drive.ctx().string_position;
1357-
self.count = count(drive, self.maxcount);
1358-
drive.skip_char(self.count);
1359-
if self.count < self.mincount {
1359+
self.count = count(drive, self.maxcount) as isize;
1360+
drive.skip_char(self.count as usize);
1361+
if self.count < self.mincount as isize {
13601362
drive.ctx_mut().has_matched = Some(false);
13611363
return None;
13621364
}
@@ -1378,7 +1380,7 @@ impl OpcodeExecutor for OpRepeatOne {
13781380
}
13791381
1 => {
13801382
// General case: backtracking
1381-
if self.count >= self.mincount {
1383+
if self.count >= self.mincount as isize {
13821384
drive.state.string_position = drive.ctx().string_position;
13831385
self.child_ctx_id = drive.push_new_context(drive.peek_code(1) as usize + 1);
13841386
self.jump_id = 2;

0 commit comments

Comments
 (0)