bite.transformers module

class bite.transformers.Group(parser: Parser[T, VIn_co], *, name: str | None = None)[source]

Group the values of a resulting parse tree node into a tuple.

This allows to introduce structure into the otherwise flat ParsedNote.value tuple.

Parameters:
  • parser – Parser of which the resulting parse tree values will be grouped.

  • name – Name to assign to the resulting parse tree node.

Examples

import asyncio
from bite import CharacterSet, Combine, Group, Literal, parse_bytes, Suppress

item = Combine(CharacterSet(b'[],', invert=True))
delimited_list = Group(
    Suppress(Literal(b'['))
    + item
    + (Suppress(Literal(b',')) + item)[0, ...]
    + Suppress(Literal(b']'))
)
print(asyncio.run(parse_bytes(
    delimited_list[0, ...],
    b'[A,B][1,2,3]'
)).values)
((b'A', b'B'), (b'1', b'2', b'3'))
async parse(buf: ParserBuffer, loc: int = 0) ParsedTransform[T, VIn_co, VOut_co][source]

Try to parse the provided input.

Starts parsing from the given location and does not need to consume all provided input.

Parameters:
  • buf – Buffer providing access to the input.

  • loc – Index into the buffer from where to start parsing.

Returns:

If parsing is successful, a parse tree representing the parse result is returned.

Raises:
  • UnmetExpectationError – If parsing was unsuccessful, because the input does not match what is expected from this parser.

  • # noqa – DAR202:

class bite.transformers.ParsedTransform(name: str | None, parse_tree: T, transform: Callable[[bite.parsers.ParsedNode[+T, +VIn_co]], Iterable[+VOut_co]])[source]
property end_loc: int[source]

End index (exclusive) into the input buffer of the segmend parsed by the node.

name: str | None[source]

Name of the node.

parse_tree: T[source]

Children of the node.

property start_loc: int[source]

Start index into the input buffer of the segmend parsed by the node.

transform: Callable[[ParsedNode[T, VIn_co]], Iterable[VOut_co]][source]

Function to transfrom the child nodes.

property values: Iterable[VOut_co][source]

Transformed values of the child nodes.

class bite.transformers.Suppress(parser: Parser[T, VIn_co], *, name: str | None = None)[source]

Suppresses a parse tree from the values.

Parameters:
  • parser – Parser of which the resulting parse tree will be suppressed.

  • name – Name to assign to the resulting parse tree node.

Examples

import asyncio
from bite import CharacterSet, Combine, Literal, parse_bytes, Suppress

integer_token = Combine(CharacterSet(b'0123456789')[1, ...])
print(asyncio.run(parse_bytes(
    Suppress(Literal(b'[')) + integer_token + Suppress(Literal(b']')),
    b'[42]'
)).values)
(b'42',)
async parse(buf: ParserBuffer, loc: int = 0) ParsedTransform[T, VIn_co, VOut_co][source]

Try to parse the provided input.

Starts parsing from the given location and does not need to consume all provided input.

Parameters:
  • buf – Buffer providing access to the input.

  • loc – Index into the buffer from where to start parsing.

Returns:

If parsing is successful, a parse tree representing the parse result is returned.

Raises:
  • UnmetExpectationError – If parsing was unsuccessful, because the input does not match what is expected from this parser.

  • # noqa – DAR202:

class bite.transformers.Transform(parser: Parser[T, VIn_co], transform: Callable[[ParsedNode[T, VIn_co]], Iterable[VOut_co]], *, name: str | None = None)[source]

Transform a resulting parse tree node to produce different values.

Parameters:
  • parser – Parser of which the resulting parse tree will be transformed.

  • transform – Function that takes the parse tree produced by the parser and produces the transformed values.

  • name – Name to assign to the resulting parse tree node.

See also

TransformValues

Passes only the parse tree node values instead of the complete node to the transform.

Examples

import asyncio
from bite import CharacterSet, Combine, parse_bytes, Transform

integer_token = Combine(CharacterSet(b'0123456789')[1, ...])
print(asyncio.run(parse_bytes(integer_token, b'42')).values)
print(asyncio.run(parse_bytes(
    Transform(integer_token, lambda node: (int(node.parse_tree),)),
    b'42'
)).values)
(b'42',)
(42,)
async parse(buf: ParserBuffer, loc: int = 0) ParsedTransform[T, VIn_co, VOut_co][source]

Try to parse the provided input.

Starts parsing from the given location and does not need to consume all provided input.

Parameters:
  • buf – Buffer providing access to the input.

  • loc – Index into the buffer from where to start parsing.

Returns:

If parsing is successful, a parse tree representing the parse result is returned.

Raises:
  • UnmetExpectationError – If parsing was unsuccessful, because the input does not match what is expected from this parser.

  • # noqa – DAR202:

class bite.transformers.TransformValues(parser: Parser[T, VIn_co], transform: Callable[[Iterable[VIn_co]], Iterable[VOut_co]], *, name: str | None = None)[source]

Transform parsed values.

Parameters:
  • parser – Parser of which the resulting parse tree values will be transformed.

  • transform – Function that takes the values produced by the parser and produces the transformed values.

  • name – Name to assign to the resulting parse tree node.

See also

Transform

Passes the complete parse tree node instead of just the values to the transform.

Examples

import asyncio
from bite import CharacterSet, Combine, Literal, parse_bytes, TransformValues

def sum_values(values):
    return (sum(int(v) for v in values if v != b'+'),)

integer_token = Combine(CharacterSet(b'0123456789')[1, ...])
print(asyncio.run(parse_bytes(
    TransformValues(integer_token + Literal(b'+') + integer_token, sum_values),
    b'42+23'
)).values)
(65,)
async parse(buf: ParserBuffer, loc: int = 0) ParsedTransform[T, VIn_co, VOut_co][source]

Try to parse the provided input.

Starts parsing from the given location and does not need to consume all provided input.

Parameters:
  • buf – Buffer providing access to the input.

  • loc – Index into the buffer from where to start parsing.

Returns:

If parsing is successful, a parse tree representing the parse result is returned.

Raises:
  • UnmetExpectationError – If parsing was unsuccessful, because the input does not match what is expected from this parser.

  • # noqa – DAR202: