Skip to content

Commit

Permalink
add optimization to nonpostional
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanabrooks committed Apr 9, 2022
1 parent 12003bc commit 4b50e6d
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions dollar_lambda/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class that they instantiate.
import sys
from dataclasses import astuple, dataclass, replace
from functools import partial, reduce
from typing import Any, Callable, Dict, Generic, Optional, Type, TypeVar
from typing import Any, Callable, Dict, Generic, List, Optional, Type, TypeVar

from pytypeclass import Monad, MonadPlus, Monoid
from pytypeclass.nonempty_list import NonemptyList
Expand Down Expand Up @@ -1201,9 +1201,24 @@ def get_alternatives():
tail = [p for j, p in enumerate(parsers) if j != i]
if repeated is not None:
head = head >> repeated.many()
yield head >> _nonpositional(*tail, max=max)

return reduce(operator.or_, get_alternatives())
def f(
p1: Output[A_monoid], tail: List[Parser[Output[A_monoid]]]
) -> Parser[Output[A_monoid]]:
p = _nonpositional(*tail, max=max)

def g(p2: Output[A_monoid]) -> Parser[Output[A_monoid]]:
return Parser.return_(p1 + p2)

return p >= g

yield head >= partial(f, tail=tail)

return replace(
reduce(operator.or_, get_alternatives()),
usage=" ".join([p.usage or "" for p in parsers]),
helps={k: v for p in parsers for k, v in p.helps.items()},
)

parser = _nonpositional(*parsers, max=max)
if repeated is not None:
Expand Down

0 comments on commit 4b50e6d

Please sign in to comment.