|
| 1 | +from typing import Iterator |
| 2 | + |
1 | 3 | from regex_automata.automata.nfa import NFA |
2 | 4 | from regex_automata.regex.flags import PatternFlag |
3 | 5 | from regex_automata.regex.match import Match |
4 | 6 |
|
5 | 7 |
|
6 | 8 | class NFAEvaluator: |
| 9 | + class Head: |
| 10 | + def __init__(self, evaluator: "NFAEvaluator", start: int) -> None: |
| 11 | + self.start = start |
| 12 | + self.evaluator = evaluator |
| 13 | + self.states: set[int] = set(self.evaluator.initial_states) |
| 14 | + self.entered_final = bool(self.states & self.evaluator.final_states) |
| 15 | + self.left_final = False |
| 16 | + |
| 17 | + def step_epsilon(self, c_previous: int, c_next: int) -> None: |
| 18 | + self.states = new_states = self._step_epsilon(c_previous, c_next, self.states) |
| 19 | + new_in_final = bool(new_states & self.evaluator.final_states) |
| 20 | + self.entered_final = self.entered_final or new_in_final |
| 21 | + self.left_final = self.entered_final and not new_in_final |
| 22 | + |
| 23 | + def _step_epsilon(self, c_previous: int, c_next: int, states: set[int]) -> set[int]: |
| 24 | + return self.evaluator.nfa.epsilon_closure(states, c_previous, c_next) |
| 25 | + |
| 26 | + def step_read(self, c_previous: int, c_next: int) -> None: |
| 27 | + self.states = new_states = self._step_read(c_previous, c_next, self.states) |
| 28 | + new_in_final = bool(new_states & self.evaluator.final_states) |
| 29 | + self.entered_final = self.entered_final or new_in_final |
| 30 | + self.left_final = self.entered_final and not new_in_final |
| 31 | + |
| 32 | + def _step_read(self, c_previous: int, c_next: int, states: set[int]) -> set[int]: |
| 33 | + assert c_next != -1 |
| 34 | + new_states = set() |
| 35 | + for u in states: |
| 36 | + u_transitions = self.evaluator.nfa.transitions.get(u, {}) |
| 37 | + for p, vs in u_transitions.items(): |
| 38 | + if p.consume_char and p.matches(c_previous, c_next): |
| 39 | + new_states.update(vs) |
| 40 | + |
| 41 | + return new_states |
| 42 | + |
| 43 | + def __repr__(self) -> str: |
| 44 | + return f"<Head {self.start=} {self.states=} {self.entered_final=} {self.left_final=}>" |
| 45 | + |
7 | 46 | def __init__(self, nfa: NFA, flags: PatternFlag = PatternFlag.NOFLAG) -> None: |
8 | 47 | self.nfa = nfa |
9 | | - self.states: set[int] = self.nfa.trivial_epsilon_closure({nfa.initial_state}) |
10 | | - self.flags = flags |
11 | 48 | self.initial_states = self.nfa.trivial_epsilon_closure({self.nfa.initial_state}) |
| 49 | + self.heads: list["NFAEvaluator.Head"] = [] |
| 50 | + self.flags = flags |
12 | 51 | self.final_states = set(self.nfa.final_states) |
13 | 52 |
|
14 | | - def match(self, text: str, start: int = 0, end: int | None = None) -> Match | None: |
| 53 | + def finditer(self, text: str, start: int = 0, end: int | None = None, search: bool = True) -> Iterator[Match]: |
15 | 54 | if self.flags & PatternFlag.IGNORECASE: |
16 | 55 | text = text.lower() |
17 | 56 |
|
18 | 57 | end_ = end if end is not None else len(text) |
19 | 58 |
|
20 | | - entered_final = bool(self.states & self.final_states) |
21 | | - left_final = False |
| 59 | + self.heads.append(self.Head(self, min(len(text), start))) |
22 | 60 |
|
23 | 61 | c_previous = -1 |
24 | | - for i in range(min(len(text), start), min(len(text), end_)): |
| 62 | + for char_no, i in enumerate(range(min(len(text), start), min(len(text), end_))): |
| 63 | + match_at_position = False |
| 64 | + if search and char_no > 0: |
| 65 | + self.heads.append(self.Head(self, i)) |
| 66 | + |
25 | 67 | c_next = ord(text[i]) |
26 | 68 |
|
27 | | - new_states = self.step_epsilon(c_previous, c_next, self.states) |
28 | | - new_in_final = bool(new_states & self.final_states) |
29 | | - entered_final = entered_final or new_in_final |
30 | | - left_final = entered_final and not new_in_final |
31 | | - if left_final: |
32 | | - return Match.from_span_and_text(start, i, text) |
33 | | - self.states = new_states |
34 | | - |
35 | | - new_states = self.step_read(c_previous, c_next, self.states) |
36 | | - new_in_final = bool(new_states & self.final_states) |
37 | | - entered_final = entered_final or new_in_final |
38 | | - left_final = entered_final and not new_in_final |
39 | | - if left_final: |
40 | | - return Match.from_span_and_text(start, i, text) |
41 | | - self.states = new_states |
| 69 | + for head in self.heads: |
| 70 | + head.step_epsilon(c_previous, c_next) |
| 71 | + if not match_at_position and head.left_final: |
| 72 | + self.purge_heads(i-1) |
| 73 | + yield Match.from_span_and_text(head.start, i-1, text) |
| 74 | + match_at_position = True # avoid returning multiple matches |
| 75 | + |
| 76 | + for head in self.heads: |
| 77 | + head.step_read(c_previous, c_next) |
| 78 | + if not match_at_position and head.left_final: |
| 79 | + self.purge_heads(i) |
| 80 | + yield Match.from_span_and_text(head.start, i, text) |
| 81 | + match_at_position = True |
42 | 82 |
|
43 | 83 | c_previous = c_next |
44 | 84 |
|
45 | 85 | c_next = -1 |
46 | | - new_states = self.step_epsilon(c_previous, c_next, self.states) |
47 | | - new_in_final = bool(new_states & self.final_states) |
48 | | - entered_final = entered_final or new_in_final |
49 | | - left_final = entered_final and not new_in_final |
50 | | - |
51 | | - if entered_final and not left_final: |
52 | | - return Match.from_span_and_text(start, end_, text) |
53 | | - else: |
54 | | - return None |
55 | | - |
56 | | - def step_epsilon(self, c_previous: int, c_next: int, states: set[int]) -> set[int]: |
57 | | - return self.nfa.epsilon_closure(states, c_previous, c_next) |
58 | | - |
59 | | - def step_read(self, c_previous: int, c_next: int, states: set[int]) -> set[int]: |
60 | | - assert c_next != -1 |
61 | | - new_states = set() |
62 | | - for u in states: |
63 | | - u_transitions = self.nfa.transitions.get(u, {}) |
64 | | - for p, vs in u_transitions.items(): |
65 | | - if p.consume_char and p.matches(c_previous, c_next): |
66 | | - new_states.update(vs) |
67 | | - |
68 | | - return new_states |
| 86 | + for head in self.heads: |
| 87 | + head.step_epsilon(c_previous, c_next) |
| 88 | + |
| 89 | + if head.entered_final and not head.left_final: |
| 90 | + yield Match.from_span_and_text(head.start, end_, text) |
| 91 | + return |
| 92 | + |
| 93 | + def purge_heads(self, start_min: int) -> None: |
| 94 | + self.heads = [h for h in self.heads if h.start >= start_min] |
0 commit comments