How to use the parsita.state.Output function in parsita

To help you get started, we’ve selected a few parsita examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github drhagen / parsita / examples / positioned.py View on Github external
"""Produce a new value with the position set.

        This abstract method must be implemented by subclasses of
        ``PositionAware``. It receives the position in the input that was
        consumed and returns a new value, typically an object similar to the old
        value, but with the position set. Important: the old value is not
        expected to be mutated.

        Args:
            start: The index of the first character consumed by the parser
            length: The number of characters consumed by the parser
        """
        pass


class PositionedParser(Generic[Input, Output], Parser[Input, Output]):
    def __init__(self, parser: Parser[Input, PositionAware[Output]]):
        super().__init__()
        self.parser = parser

    def consume(self, reader: Reader[Input]) -> Status[Input, Output]:
        start = reader.position
        status = self.parser.consume(reader)

        if isinstance(status, Continue):
            end = status.remainder.position
            return Continue(status.remainder, status.value.set_position(start, end - start)).merge(status)
        else:
            return status

    def __repr__(self):
        return self.name_or_nothing() + 'positioned({})'.format(

parsita

Parser combinator library for Python

MIT
Latest version published 1 year ago

Package Health Score

59 / 100
Full package analysis