Parser Combinators

(t3.at)

27 points | by schindlabua 2 hours ago

7 comments

  • mrkeen 12 minutes ago
    Not a good starting type:

      type Parser<T> = (input: string) => [T, string]
    
    This implies that it accepts a string and will return a T. Parsers might return a T.

    The article also starts with the concept of a 'partial parser', from which other (non-partial?) parsers can be constructed?

      Or, and this is where the real power of partial parsers comes in: we wrap the chain up as a new parser of its own.
    
    Nope, there's no distinction. If you combine partial parsers, you end up with a partial parser. If you combine full parsers (whatever that means) you end up with full parsers.

    (Ironically there is a fitting definition of 'partial' here: the function isn't well defined on all inputs. For some inputs, T is returned, on other inputs, maybe an error is thrown, who knows..)

    • raddan 8 minutes ago
      Combinators are functional—they always return something. But if you want to return nothing, fine; T is any type. Just return something like unit (ie, the singleton type that has nothing as a value).
    • schindlabua 8 minutes ago
      Good input. Since we're working in typescript a partial parser can always throw an exception (It's what the parsimmon library does aswell I believe). I decided not to get into those technicalities too much!
  • justinnk 9 minutes ago
    Very cool visual guide! I feel it is appropriate here to recite this little poem about parsers by Fritz Ruehr that stuck with me since my first semester of CS. With this, you will never forget the type signature of a parser.

    „A Parser for Things is a function from Strings to Lists of Pairs of Things and Strings!“

    https://people.willamette.edu/~fruehr/haskell/seuss.html

  • stevefan1999 2 minutes ago
    I love parser combinators so much. Those are possible because parsing itself exhibits monadic structure, it forms transition monoid, of its more well known name as the semiautomaton in the world of CS.
  • monster_truck 33 minutes ago
    I would suggest parsing my scroll wheel input and combinating it into page events
  • tikimcfee 48 minutes ago
    Love me some combinators. In my earlier days it helped bridge the academic gap of CS grammars and languages to something I could not readily apply to internship dev work. I seem to recall an attempt to map or generate the from regex and in reverse, but I might be crazy this early in the morning.

    Nice visuals btw. If I may be so bold: slightly smaller font and header would make scrolling through the bottom readable code snippets a little easier!

  • lacoolj 16 minutes ago
    Did you use the new Claude Motion for this?
  • adrianhqs 25 minutes ago
    Nice design