romshark / llparser

A universal LL top-down parser written in Go
BSD 3-Clause "New" or "Revised" License
45 stars 1 forks source link

[FTR] Error pattern #8

Closed romshark closed 4 years ago

romshark commented 4 years ago

This is a real-world situation from romshark/gapi:

E = enum { first second }

The above code represents a valid enum type declaration. If we remove the enum values like so:

E = enum {}

Then we'll get an inconvenient error message:

unexpected token 'E' at file.gapi:1:1

which is not very helpful, instead I’d like to get something like:

enum type E is missing values at file.gapi:1:1

Proposal

Instead of directly returning an UnexpectedToken error the parser should try to match the user-defined error-pattern. If the error pattern is matched then its action should be executed and the returned error should then be returned instead.

This feature would require a slight breaking change to the API:

func (pr Parser) Parse(
  lexer Lexer,
  rule *Rule,
  errorRule *Rule,
) (Fragment, error)

Current Workaround

Currently, we need to allow the wrong syntax (with no values) in the grammar and check whether there are any values in the Action which makes the actions unnecessarily complicated.