def parse_optional(p: Parser[A]) -> Parser[Sum2[None, A]]: return Parser( p.path(), Fn[JsonType, PreParsed[Sum2[None, A]]] (lambda j: fold2[PreParseError, A, PreParsed[Sum2[None, A]]] ((lambda x: fold2[PreParseError, None, PreParsed[Sum2[None, A]]] ((lambda _: F1(x), lambda x: F2(F1(x))))(parse_none(p.path( )).parse_fn()(j)), lambda x: F2(F2(x))))(p.parse_fn()(j))))
def parse_json(j: str) -> Parsed[Exception, JsonType]: try: x = json.loads(j) # type: ignore if isinstance(x, dict): # type: ignore return F2(JsonDict(x)) elif isinstance(x, list): # type: ignore return F2(JsonList(x)) else: return F2(cast(JsonPrimitive, x)) except Exception as e: return F1(e)
def parse_json(x: any) -> PreParsed[JsonType]: # type: ignore try: if isinstance(x, list): # type: ignore return F2(F2(JsonList(x))) elif isinstance(x, dict): # type: ignore return F2(F3(JsonDict(x))) else: if isinstance(x, str): # type: ignore return F2(F1(F1(x))) elif isinstance(x, int): # type: ignore return F2(F1(F2(x))) elif isinstance(x, bool): # type: ignore return F2(F1(F3(x))) elif isinstance(x, type(None)): # type: ignore return F2(F1(F4(x))) else: return F1(F2(ParseException)) except Exception as e: return F1(F2(UnknownParseError(e)))
def x(j: JsonType) -> Parsed[JsonType]: if len(k) == 0: return F2(j) else: d = parse_dict()(j) try: aj = map2(d, lambda y: y.get(k[0])) # type: ignore ntk = k if (len(tk) == 0) else tk pj = fold2[List[ParseError], JsonType, Parsed[JsonType]]( (lambda x: F1( list( map( Fn[ParseError, ParseError] (lambda y: y._replace(error=TraverseError( k[0], y.error))), x))), lambda x: F2(x)))(ToParsed[JsonType](ntk)(bind2( aj, parse_json))) # type: ignore return bind2(pj, traverse(k[1:], ntk)) except Exception as e: return F1([ParseError(tk, UnknownParseError(e))])
def __call__(self, a: PreParsed[A]) -> Parsed[A]: return fold2[PreParseError, A, Parsed[A]]( (fold2[List[ParseError], ParseException, Parsed[A]]((lambda x: F1( list( map( Fn[ParseError, ParseError] (lambda y: y._replace(path=self.sg.append( self.path, y.path))), x))), lambda x: F1([ParseError(self.path, x)]))), lambda x: F2(x)))(a)
def safe_parse(y: JsonDict, k: str, t: Type[A]) -> Sum2[Exception, A]: try: x = y[k] if isinstance(x, t): return F2(x) else: return F1( TypeError('Could not parse value of type ' + t.__name__ + ' from key ' + k)) except Exception as e: return F1(e)
def __call__(self, j: JsonType) -> PreParsed[B]: return bind2( self._p(j), Compose( fold2[CustomParseError, B, PreParsed[B]]( (lambda x: F1(F2(x)), lambda x: F2(x))), self._c))
def x(a: A) -> Parsed[Exception, A]: return F2(a)
def serialize_list(f: Serializer[A]) -> Serializer[List[A]]: return Serializer( f._path[0], f._path[1:], Fn[List[A], JsonType](lambda x: F2(JsonList(list(map(f._fn, x))))))
def serialize_int_fn(i: int) -> JsonType: return F1(F2(i))
def x(j: JsonType) -> PreParsed[int]: (a, _, c, d) = prims(int).fold prim = fold4[str, int, bool, None, PreParsed[int]]((a, lambda x: F2(x), c, d)) return parse_prim(int, prim)(j)
def x(j: JsonType) -> PreParsed[str]: (_, b, c, d) = prims(str).fold prim = fold4[str, int, bool, None, PreParsed[str]]((lambda x: F2(x), b, c, d)) return parse_prim(str, prim)(j)
def parse_list() -> ParseFn[JsonList]: err = error(JsonList) return fold3[JsonPrimitive, 'JsonList', 'JsonDict', PreParsed[JsonList]]( (lambda _: F1(err('JsonPrimitive')), lambda x: F2(x), lambda _: F1(err('Dict'))))
def bind(self, r: ListResult[A]) -> Parsed[ListResult[A]]: return F1(r[0]) if (len(r[1]) == 0 and not (len(r[0]) == 0)) else F2(r)
def __call__(self, a: Parsed[A]) -> PreParsed[A]: return fold2[List[ParseError], A, PreParsed[A]]((lambda x: F1(F1(x)), lambda x: F2(x)))(a)
def lub(self, l: JsonList) -> JsonType: return F2(l)
def load_json(j: str) -> PreParsed[any]: # type: ignore try: return F2(json.loads(j)) # type: ignore except Exception as e: return F1(F2(UnknownParseError(e)))
def x(j: JsonType) -> PreParsed[bool]: (a, b, _, d) = prims(bool).fold prim = fold4[str, int, bool, None, PreParsed[bool]]((a, b, lambda x: F2(x), d)) return parse_prim(bool, prim)(j)
def serialize_json_list(l: JsonList) -> JsonType: return F2(l)
def x(j: JsonType) -> PreParsed[None]: (a, b, c, _) = prims(type(None)).fold prim = fold4[str, int, bool, None, PreParsed[None]]((a, b, c, lambda x: F2(x))) return parse_prim(type(None), prim)(j)
def serialize_list_fn(f: SerializeFn[A]) -> SerializeFn[List[A]]: return Fn[List[A], JsonType](lambda x: F2(JsonList(list(map(f, x)))))
def __init__(self) -> None: self._run = Fn[JsonType, PreParsed[H]](lambda x: F1(F2(ParseException()))) self._path = []
def error(t: Type[A]) -> Callable[[str], PreParseError]: return Fn[str, PreParseError](lambda s: F2(ParseTypeError(t.__name__, s)))
def run(self, _: JsonType) -> Parsed[None]: return F2(None)