avh4 / elm-format

elm-format formats Elm source code according to a standard set of rules based on the official Elm Style Guide
BSD 3-Clause "New" or "Revised" License
1.31k stars 148 forks source link

Misc refactors to the Elm compiler's parser integration code #748

Closed emmabastas closed 3 years ago

emmabastas commented 3 years ago

Apart from small changes and improvements there is one notable refactors in this PR

Refactor the parsec adapter's error type


elm-format doesn't report errors anyways, why care?

Parsec errors are essentially strings with some additional metadata. The Elm compiler on the other hand defines all possible parse errors as custom types inside Reporting.Error.Syntax. With this approach no information is lost when a parse error is propagated from the lowest level parser through the higher level ones. The higher level ones just construct higher level errors with the lower level errors as children.

393c7ee redefines parsecs error type. Instead of a single string with metadata it's now possible to create a tree structure, kind of like a generic/untyped version of the Elm compilers errors, it looks like this: https://github.com/avh4/elm-format/blob/393c7eea66dd438a00bcdc66e2176eb8180f87d8/elm-format-lib/src/Parse/ParsecAdapter.hs#L348-L351 https://github.com/avh4/elm-format/blob/393c7eea66dd438a00bcdc66e2176eb8180f87d8/elm-format-lib/src/Parse/ParsecAdapter/Message.hs#L4-L7

9770e47 Integrates the parsec errors with the Elm compilers errors. Additional leaf variants are added to Message so that the error types from the compiler parsing code can be added as children to a parsec error without first needing to convert to a string.

What the Message type looks like now: (note that String, Char and Number refer to custom error types) https://github.com/avh4/elm-format/blob/9770e4744392fdb5441a1ebc89f654a7d86033b3/elm-format-lib/src/Reporting/Error/Syntax.hs#L105-L111

avh4 commented 3 years ago

This all looks good. And some of these were pretty big cleanups -- thanks!