Skip to content

Commit 6ffd5ad

Browse files
committed
fix: incremental parsing
1 parent 7ce8cbc commit 6ffd5ad

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/Std/Internal/Parsec/ByteArray.lean

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,26 @@ def skipByte (b : UInt8) : Parser Unit :=
5656
/--
5757
Skip a sequence of bytes equal to the given `ByteArray`.
5858
-/
59-
def skipBytes (arr : ByteArray) : Parser Unit := do
60-
for b in arr do
61-
skipByte b
59+
def skipBytes (arr : ByteArray) : Parser Unit := fun it =>
60+
if it.remainingBytes < arr.size then
61+
.error it .eof
62+
else
63+
let rec go (idx : Nat) (it : ByteArray.Iterator) : ParseResult Unit ByteArray.Iterator :=
64+
if h : idx < arr.size then
65+
match skipByte arr[idx] it with
66+
| .success it' _ => go (idx + 1) it'
67+
| .error it' err => .error it' err
68+
else
69+
.success it ()
70+
go 0 it
6271

6372
/--
6473
Parse a string by matching its UTF-8 bytes, returns the string on success.
6574
-/
6675
@[inline]
6776
def pstring (s : String) : Parser String := do
68-
skipBytes s.toUTF8
77+
let utf8 := s.toUTF8
78+
skipBytes utf8
6979
return s
7080

7181
/--

0 commit comments

Comments
 (0)