dtpowl ยท GitHub

For #1599.

The old version of the parser allowed entity field attributes to be wrapped in quotation marks; the enclosing "s would get parsed away and the enclosed text passed through in the attribute text on the ParsedEntityDefinition.

I discussed this with @parsonsmatt , and we'd like to deprecate this behavior. In #1599 it seems to have been in use as a workaround for a parser bug that no longer exists. (If it's necessary to escape whitespace in a field attribute, this can still be done by wrapping the attribute in ( ) instead of quotes.)

This PR restores the old behavior and adds a configurable deprecation message as a warning. It's not easy to do this without some further refactoring of the parser. Banning quotes in entity field arguments will also ban them in entity field types, which would be incorrect โ€” field types can include typelevel string literals.

This is a good opportunity to make the parser smarter. It's currently pretty naive โ€” essentially, it breaks each line of an entity definition block into tokens and then stops. In doing this, it ignores a lot of genuine syntactic data. It doesn't know that the line starts with the field name, is followed by a type, and is then followed by a series of attributes. It's very easy to write a field definition that parses successfully but fails semantically.

Improving this situation is an incremental directional step towards a formal specification for the language.

This PR:

  • Implements a more exact parser for entity field definitions, including structural representation of the field name, type, strictness, and attributes.
  • Separates parsing of entity fields from non-field things like deriving statements. I'm calling these "directives" here. In the interest of keeping the PR from growing even more huge, directives are parsed naively for now.
    • I'd like to add support for Haskell-style deriving syntax in the future; this PR will make it easier to do that.
  • Temporarily restores the old parser's behavior for entity field definitions and directive arguments that are wrapped in quotes.
  • Adds a configurable warning message when entity field definitions and directive arguments are wrapped in quotes.

Currently, all of this new structure is thrown away in mkUnboundEntityDef. There's something of an impedance mismatch:

  • mkUnboundEntityDef doesn't care about the difference between field definitions and directives; it wants the parsed structure to be reduced back to a string of tokens.
  • mkUnboundEntityDef wants to re-parse types itself.
  • mkUnboundEntityDef wants to re-parse key/value attributes itself.
  • etc, etc, etc

I think this situation could be improved with some effort, but that's outside the scope of this PR.

Read the original on github.com โ†—