some more on parser
This commit is contained in:
parent
b711075a30
commit
82c7745b47
1 changed files with 10 additions and 15 deletions
|
|
@ -63,18 +63,11 @@ class P(Generic[T]):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def pure(value: T) -> P[T]:
|
def pure(value: T) -> P[T]:
|
||||||
def inner(parserPos: ParserInput) -> ParserResult[T]:
|
return P(lambda pp: iter([(pp, value)]))
|
||||||
yield (parserPos, value)
|
|
||||||
return P(inner)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fail() -> P[Any]:
|
def fail() -> P[Any]:
|
||||||
def inner(_: ParserInput) -> ParserResult[Any]:
|
return P(lambda _: iter([]))
|
||||||
if False:
|
|
||||||
yield
|
|
||||||
pass
|
|
||||||
|
|
||||||
return P(inner)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _fix(p1: Callable[[P[Any]], P[T]]) -> P[T]:
|
def _fix(p1: Callable[[P[Any]], P[T]]) -> P[T]:
|
||||||
|
|
@ -97,12 +90,14 @@ class P(Generic[T]):
|
||||||
return P(inner)
|
return P(inner)
|
||||||
|
|
||||||
def safe_fmap(self, map_func: Callable[[T], TR]) -> P[TR]:
|
def safe_fmap(self, map_func: Callable[[T], TR]) -> P[TR]:
|
||||||
def inner(value: T) -> P[TR]:
|
def inner(parserPos: ParserInput) -> ParserResult[TR]:
|
||||||
try:
|
for pp, v in self.func(parserPos):
|
||||||
return P.pure(map_func(value))
|
try:
|
||||||
except Exception:
|
yield pp, map_func(v)
|
||||||
return P.fail()
|
except Exception:
|
||||||
return self.bind(inner)
|
pass
|
||||||
|
|
||||||
|
return P(inner)
|
||||||
|
|
||||||
def replace(self, value: TR) -> P[TR]:
|
def replace(self, value: TR) -> P[TR]:
|
||||||
return self.fmap(lambda _: value)
|
return self.fmap(lambda _: value)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue