datalust / superpower

A C# parser construction toolkit with high-quality error reporting
Apache License 2.0
1.07k stars 98 forks source link

ASTs using shallow type hierarchies #116

Closed chrisspre closed 4 years ago

chrisspre commented 4 years ago

Relatively new to using superpower but my inclination is to model the AST using shallow type hierarchies. For example interface ITypeElement {} class Property : ITypeElement {} class Function : ITypeElement {}

So the parser at some point conceptually looks like

static readonly TokenListParser<Token, ITypeElement > TypeElementParser  =
    PropertyParser.Or(FunctionParser);

In reality that doesn't work since the individual parsers need to be cast to TokenListParser<Token, ITypeElement > first.

Is there an established pattern to write these parser with less noise (i.e. fewer casts )?

nblumhardt commented 4 years ago

Hi Christof! Thanks for the note. Normally, return type covariance would help with this, but it's not possible in Superpower's design. Some earlier discussion in #112 . There could be a better API/novel solution we've overlooked so far, though :-)

chrisspre commented 4 years ago

I will think about it but if you haven't come up with anything it is unlikely. Thanks.