djspiewak / parseback

A Scala implementation of parsing with derivatives
http://parseback.io
Apache License 2.0
197 stars 22 forks source link

Add support for arity-based overloads on ^^ #8

Closed djspiewak closed 7 years ago

djspiewak commented 7 years ago

gll-combinators has a very nice syntactic feature which makes it possible to use arity-n functions directly with the ^^ operator (reduction), rather than depending on an arity-1 function that uses deconstruction. For example:

// gll-combinators
a ~ b ~ c ^^ { (a, b, c) => ??? }

// parseback and Scala's parser combinators
a ~ b ~ c ^^ { case a ~ b ~ c => ??? }

Obviously, the gll-combinators version is a lot nicer, especially once you expand this out to line tracking and other complications. Unfortunately, it requires some extremely annoying type-level machinery to make it work. Here is the gll-combinators implementation. The reason this is so annoying is the need to deal with every associative permutation of the ~ operator. In theory, it should be possible to derive this at the type level in Scala, resulting in a more linear growth of implicit cases (as opposed to the current quadratic), but I haven't found a way yet. (puts the bat signal in the sky for @milessabin)