pSepBy1

fun <I : Any, O, S, U : Any> pSepBy1(item: Parser<I, O, U>, separator: Parser<I, S, U>): Parser<I, List<O>, U>

Returns a Parser that parses one or more occurrences of item separated by separator, collecting the item values (not the separators) into a non-empty List.

The grammar matched is item (separator item)*. The trailing separator, if present, is not consumed.

Behaviour

ConditionResult
item fails on the first attemptFailure from item
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.

  • O — the output type of item; each value is collected.

  • S — the output type of separator; discarded.

  • U — the user context type threaded through unchanged.

Example

val digit = pSatisfy<Char, Unit> { it.isDigit() }
val comma = pSatisfy<Char, Unit> { it == ',' }
val parser = pSepBy1(digit, comma)

val input = ParserInput.of("1,2,3".toList(), Unit)
val result = parser(input) // Success(['1','2','3'], nextIndex=5, ...)

Return

a Parser that succeeds with a non-empty list of item values.

Parameters

item

the parser for each item.

separator

the parser for the delimiter between items; its value is discarded.

See also