Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""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(