kessel.combinators — Parser combinators

Parser combinators.

These functions combine parsers together to create other parsers.

kessel.combinators.between(first, last, parser)

Runs a parser between two other parsers.

Parameters:
  • first – First parser to run.
  • last – Last parser run.
  • parser – The parser to run whose result will be returned.
Returns:

The parsing function.

kessel.combinators.choice(*parsers)

Tries each parser specified in order.

Please note this parser is predictive – it will only try alternatives if the previous parser did not consume any input.

Parameters:parsers – A series of parsers to attempt.
Returns:The parsing function.
kessel.combinators.count(n, parser)

Runs a parser a specified number of times.

Parameters:
  • n – Number of times to run the parser.
  • parser – The parser to run.
Returns:

The parsing function.

kessel.combinators.many(parser)

Runs a parser zero or more times.

Parameters:parser – The parser to run.
Returns:The parsing function.
kessel.combinators.many1(parser)

Runs a parser one or more times.

Parameters:parser – The parser to run.
Returns:The parsing function.
kessel.combinators.mapf(parser)

Creates a decorator for a parser to specify custom actions.

Parameters:parser – Parser to decorate with.
Returns:A decorator for a function that must take the parser result and returns the intended output result.
kessel.combinators.mapf_star(parser)

Creates a decorator for a parser to specify custom actions (unpacked).

Parameters:parser – Parser to decorate with.
Returns:A decorator for a function that must take the unpacked parser result (which must be a list) and returns the intended output result.
kessel.combinators.not_followed_by(parser, expected=None)

Performs a negative lookahead.

Parameters:parser – The parser that should not succeed.
Returns:The parsing function.
kessel.combinators.option(parser, default=<function <lambda>>)

Runs a parser optionally.

If the parser could not be applied, the default value function will be called and its result will be returned. It will only fail if input was consumed.

Parameters:
  • parser – The parser to run.
  • default – Function providing the default value.
Returns:

The parsing function.

kessel.combinators.sep_by(sep, parser)

Runs a parser zero or more times, with items separated by another parser.

Parameters:
  • sep – Separator parser.
  • parser – The parser to run.
Returns:

The parsing function.

kessel.combinators.sep_by1(sep, parser)

Runs a parser one or more times, with items separated by another parser.

Parameters:
  • sep – Separator parser.
  • parser – The parser to run.
Returns:

The parsing function.

kessel.combinators.sequence(*parsers)

Runs parsers in sequence.

Parameters:parsers – A series of parsers to run in sequence.
Returns:The parsing function.
kessel.combinators.sequence_l(l, r)

Runs two parsers in sequence, preserving only the result of the former.

Parameters:
  • l – Parser whose result should be preserved.
  • r – Parser whose result should be discarded.
Returns:

The parsing function.

kessel.combinators.sequence_r(l, r)

Runs two parsers in sequence, preserving only the result of the latter.

Parameters:
  • l – Parser whose result should be discarded.
  • r – Parser whose result should be preserved.
Returns:

The parsing function.

kessel.combinators.suppress_expect(parser)

Suppress any expectations from parsing errors.

Parameters:parser – The parser to run.
Raises ParseError:
 If the parser fails. It will have all expectations stripped.
Returns:The parsing function.
kessel.combinators.try_(parser)

Wraps a parser such that on error, it is reset to the original position in the stream.

Please note that usage of this function will tee the stream, which may require significant auxiliary storage, so it is best avoided if necessary.

Parameters:parser – Parser to wrap.
Returns:The parsing function..
Raises ParseError:
 If the underlying parser encounters an error. However, the error is re-wrapped such that the stream is reset to where the parser was first attempted.