Skip to content

takeUntil ​

takeUntil combinator applies source parser, collects its output, and stops after terminator parser succeeds. Returns a tuple of values collected by parser and terminator. Fails if parser fails, or with the terminator's error if parser succeeds without consuming input. A committed failure from either of them propagates, and the cursor rewinds to where the combinator started.

Usage ​

ts
const CommentParser = map(
  sequence(string('/*'), takeUntil(any(), string('*/'))),
  (values) => values.flat()
)

const FailingParser = takeUntil(regexp(/\p{Nd}/gu, 'decimal digit'), string('.'))

Success

ts
run(CommentParser).with('/* Hello */')

{
  isOk: true,
  start: 0,
  end: 11,
  pos: 11,
  value: [
    '/*',
    [ ' ', 'H', 'e', 'l', 'l', 'o', ' ' ],
    '*/'
  ]
}

Failure

ts
run(FailingParser).with('one.')

{
  isOk: false,
  start: 0,
  end: 0,
  pos: 0,
  expected: 'decimal digit'
}