generated from norskeld/serpent
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(combinators/*Until): add
takeUntil
and skipUntil
combinators
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { type Parser } from '../state' | ||
|
||
export function takeUntil<T, S>(parser: Parser<T>, terminator: Parser<S>): Parser<[Array<T>, S]> { | ||
return { | ||
parse(input, pos) { | ||
const values: Array<T> = [] | ||
let nextPos = pos | ||
|
||
while (true) { | ||
const resultT = terminator.parse(input, nextPos) | ||
|
||
switch (resultT.isOk) { | ||
// If ok, then we stumbled upon a terminating parser, so push final matches, and then | ||
// return accumulated values. | ||
case true: { | ||
return { | ||
isOk: true, | ||
pos: resultT.pos, | ||
value: [values, resultT.value] | ||
} | ||
} | ||
|
||
// Otherwise try to run source parser and push results into `values`. | ||
// If it fails, then return early and stop parsing. | ||
case false: { | ||
const resultP = parser.parse(input, nextPos) | ||
|
||
if (resultP.isOk) { | ||
values.push(resultP.value) | ||
nextPos = resultP.pos | ||
continue | ||
} | ||
|
||
return resultP | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function skipUntil<T, S>(parser: Parser<T>, terminator: Parser<S>): Parser<S> { | ||
return { | ||
parse(input, pos) { | ||
let nextPos = pos | ||
|
||
while (true) { | ||
const resultT = terminator.parse(input, nextPos) | ||
|
||
switch (resultT.isOk) { | ||
// If ok, then we stumbled upon a terminating parser, so return its value. | ||
case true: { | ||
return { | ||
isOk: true, | ||
pos: resultT.pos, | ||
value: resultT.value | ||
} | ||
} | ||
|
||
// Otherwise try to run source parser *ignoring* its results. | ||
// If it fails, then return early and stop parsing. | ||
case false: { | ||
const resultP = parser.parse(input, nextPos) | ||
|
||
if (resultP.isOk) { | ||
nextPos = resultP.pos | ||
continue | ||
} | ||
|
||
return resultP | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |