pSepBy
fun <I : Any, O, S, U : Any> pSepBy(item: Parser<I, O, U>, separator: Parser<I, S, U>): Parser<I, List<O>, U>
Returns a Parser that parses zero or more occurrences of item separated by separator, collecting the item values (not the separators) into a List.
The grammar matched is (item (separator item)*)?. The trailing separator, if present, is not consumed. Always succeeds; returns an empty list when item fails on the first attempt.
Behaviour
| Condition | Result |
|---|---|
| item fails on the first attempt | Success with an empty list; index unchanged |
| item succeeds N times (N ≥ 1) | Success with N values; index advanced past all items and separators |
Type parameters
I — the shared token type consumed by both parsers.
U — the user context type threaded through unchanged.
Example
val digit = pSatisfy<Char, Unit> { it.isDigit() }
val comma = pSatisfy<Char, Unit> { it == ',' }
val parser = pSepBy(digit, comma)
val input = ParserInput.of("1,2,3".toList(), Unit)
val result = parser(input) // Success(['1','2','3'], nextIndex=5, ...)
val empty = ParserInput.of("abc".toList(), Unit)
val result2 = parser(empty) // Success([], nextIndex=0, ...)Content copied to clipboard
Return
a Parser that always succeeds with a (possibly empty) list of item values.
Parameters
item
the parser for each item.
separator
the parser for the delimiter between items; its value is discarded.