Skip to content

Weird bind implementation? #33

Description

@deliciouslytyped

Hi, thanks for making parsy, I like it!

I'm currently succeeding at shooting myself in the foot with heterogeneous inputs, because I want to be able to process a list of "symbols" and strings:

#some context for the following snippets
from dataclasses import dataclass

@dataclass
class Indent:
  depth: int

@generate
def indent():
  return (yield test_item(lambda i: isinstance(i, Indent), "not indent"))

sideToken = indent

newline = string("\r\n")
preline_ = (newline.should_fail("not newline") >> any_char).many().concat()
line = preline_ << newline
take = test_item(lambda x: True, "any nested")

I'm probably not understanding something, why do I need to use .bind like this?:

>>> wrap = lambda fn: lambda res: success(fn.parse(res))
>>> ( sideToken >> take.bind(wrap(line)) ).many()
        .parse([Indent(0), "asd\r\n", Indent(1), "asdf\r\n"])
['asd', 'asdf']

instead of: (the following is pseudocode)

>>> ( sideToken >> take.bind(line) ).many() \
        .parse([Indent(0), "asd\r\n", Indent(1), "asdf\r\n"])
['asd', 'asdf']

Which is to say, why do I have to add an extra layer of wrapping and calling .parse?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions