simplified parser
This commit is contained in:
parent
4034c08cd9
commit
28fb30d493
3 changed files with 19 additions and 16 deletions
|
|
@ -53,7 +53,7 @@ class Parser:
|
|||
worry_inc, test,
|
||||
lambda number, items, worry_inc, test:
|
||||
Monkey(number, items, worry_inc, *test))
|
||||
monkey_list: P[list[Monkey]] = P.first(monkey, P.eol().optional()).many()
|
||||
monkey_list: P[list[Monkey]] = P.second(P.eol().optional(), monkey).many()
|
||||
|
||||
|
||||
WorryIncreaser = Callable[[int], int]
|
||||
|
|
@ -111,7 +111,7 @@ class Troop_While_Worried(Troop):
|
|||
""" The """
|
||||
@classmethod
|
||||
def parse(cls, lines: Iterator[str]) -> Self:
|
||||
monkeys = Parser.monkey_list.parse_iterator(lines).get()
|
||||
monkeys = Parser.monkey_list.parse(lines).get()
|
||||
return Troop_While_Worried(monkeys)
|
||||
|
||||
def single_round(self):
|
||||
|
|
@ -126,7 +126,7 @@ class Troop_While_Kinda_Relieved(Troop):
|
|||
|
||||
@classmethod
|
||||
def parse(cls, lines: Iterator[str]) -> Self:
|
||||
monkeys = Parser.monkey_list.parse_iterator(lines).get()
|
||||
monkeys = Parser.monkey_list.parse(lines).get()
|
||||
return Troop_While_Kinda_Relieved(monkeys, prod(monkey.modulator for monkey in monkeys))
|
||||
|
||||
def single_round(self):
|
||||
|
|
|
|||
|
|
@ -112,24 +112,27 @@ class P(Generic[T]):
|
|||
def __init__(self, func: ParserFunc[T]):
|
||||
self.func = func
|
||||
|
||||
def parse(self, s: str) -> Result[T]:
|
||||
all_results = self.func(SimpleParserInput(s, 0))
|
||||
def parse(self, input: str | Iterator[str]) -> Result[T]:
|
||||
if isinstance(input, str):
|
||||
parser_input = SimpleParserInput(input, 0)
|
||||
else:
|
||||
parser_input = IteratorParserInput(StringDispenser(input), 0)
|
||||
|
||||
all_results = self.func(parser_input)
|
||||
try:
|
||||
_, result = next(all_results)
|
||||
return Result.of(result)
|
||||
except StopIteration:
|
||||
return Result.fail("No result")
|
||||
|
||||
def parse_iterator(self, it: Iterator[str]) -> Result[T]:
|
||||
all_results = self.func(IteratorParserInput(StringDispenser(it), 0))
|
||||
try:
|
||||
_, result = next(all_results)
|
||||
return Result.of(result)
|
||||
except StopIteration:
|
||||
return Result.fail("No result")
|
||||
def parse_multi(self, input: str | Iterator[str]) -> Iterator[T]:
|
||||
if isinstance(input, str):
|
||||
parser_input = SimpleParserInput(input, 0)
|
||||
else:
|
||||
parser_input = IteratorParserInput(StringDispenser(input), 0)
|
||||
|
||||
def parse_multi(self, s: str, i: int = 0) -> Iterator[T]:
|
||||
return (v for _, v in self.func(SimpleParserInput(s, i)))
|
||||
all_results = self.func(parser_input)
|
||||
return (v for _, v in all_results)
|
||||
|
||||
@classmethod
|
||||
def pure(cls, value: T) -> P[T]:
|
||||
|
|
|
|||
|
|
@ -275,7 +275,7 @@ def test_iterator_input():
|
|||
input = iter(['1', '2'])
|
||||
parser = P.unsigned().line().many()
|
||||
expected = [1, 2]
|
||||
result = parser.parse_iterator(input).get()
|
||||
result = parser.parse(input).get()
|
||||
assert result == expected
|
||||
|
||||
|
||||
|
|
@ -283,5 +283,5 @@ def test_iterator_trim_input():
|
|||
input = iter(['1 ', '2 '])
|
||||
parser = P.unsigned().trim().line().many()
|
||||
expected = [1, 2]
|
||||
result = parser.parse_iterator(input).get()
|
||||
result = parser.parse(input).get()
|
||||
assert result == expected
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue