Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed

* Properly bound repetitions for machine parsers ([#53])
* Group quantized 2+ char literals in regex optimization ([#54])


Expand Down Expand Up @@ -219,4 +220,5 @@ descent parser and a work-in-progress state-machine parser.
[#38]: https://github.com/goodmami/pe/issues/38
[#44]: https://github.com/goodmami/pe/issues/44
[#46]: https://github.com/goodmami/pe/issues/46
[#53]: https://github.com/goodmami/pe/issues/53
[#54]: https://github.com/goodmami/pe/issues/54
5 changes: 3 additions & 2 deletions pe/_cy_machine.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ cdef class _Parser:
continue

elif instr.opcode == UPDATE:
if instr.maxcount == -1 or state.count < instr.maxcount:
state.count += 1
if instr.maxcount < 0 or state.count < instr.maxcount:
state.pos = pos
state.argidx = len(args)
state.kwidx = len(kwargs)
Expand Down Expand Up @@ -433,7 +434,7 @@ def _loop(defn, mincount, maxcount):
*(pi.copy() for _ in range(mincount) for pi in pis),
Instruction(BRANCH, len(pis) + 2),
*pis,
Instruction(UPDATE, -len(pis), maxcount=maxcount)
Instruction(UPDATE, -len(pis), maxcount=(maxcount - mincount))
]


Expand Down
7 changes: 4 additions & 3 deletions pe/_py_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ def _match( # noqa: C901

elif opcode == UPDATE:
next_idx, _, count, prev_mark, _, _ = pop()
if maxcount == -1 or count < maxcount:
push((next_idx, pos, count + 1, prev_mark, len(args), len(kwargs)))
count += 1
if maxcount < 0 or count < maxcount:
push((next_idx, pos, count, prev_mark, len(args), len(kwargs)))
idx += oploc
else:
idx += 1
Expand Down Expand Up @@ -355,7 +356,7 @@ def _loop(defn, mincount: int, maxcount: int):
return [*(pis * mincount), # risk of billion laughs attack
Instruction(BRANCH, len(pis) + 2),
*pis,
Instruction(UPDATE, -len(pis), maxcount=maxcount)]
Instruction(UPDATE, -len(pis), maxcount=(maxcount - mincount))]


def _sym(defn):
Expand Down
1 change: 1 addition & 0 deletions test/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
'aabbcc', 0, 3, _blank),
('Rpt3', Rpt(abc, min=3), 'aaxx', 0, FAIL, None),
('Rpt4', Rpt(abc, max=1), 'aabbcc', 0, 1, _blank),
('Rpt5', Rpt('a', max=2), 'aaaaaa', 0, 2, _blank),

('And0', And(abc), 'a', 0, 0, _blank),
('And1', And(abc), 'd', 0, FAIL, None),
Expand Down