jswebtools / language-ecmascript

Haskell library: ECMAScript parser, pretty-printer and additional tools
Other
46 stars 26 forks source link

Multi-mode pretty-printer #13

Open achudnov opened 11 years ago

achudnov commented 11 years ago

Wouldn't it be nice if we could use the same pretty-printer to generate well-formatted, "pretty" source representation -- as well as the minified source representation (for use in minifiers, compilers back-ends etc)? Another use case is pretty-printing annotation fields as comments (useful in debugging, generating type annotations etc.). I'm using a private fork where the pretty-printer does that. I'd like the functionality to be available to everyone, but I want the possibility to disable it. Yet another use-case is generating source-maps which would also require a modified pretty-printer. I'm not sure of the scope of the modifications, but I'm certain it's not limited to merely omitting certain characters depending on flags (which seems the case for the previous two use-cases).

Anyway, I've been tinkering with a multi-mode pretty-printing library that would, hopefully, help in writing a multi-mode printer for language-ecmascript. But I'd like feedback on the idea as a whole and any additional use-cases you can think of.

achudnov commented 11 years ago

It is my understanding that BlazeBuilder (as well as Data.Text.Builder and Data.ByteString.Builder) is the king when it comes to performance and memory usage in generating text programmatically, so, perhaps, we could consider switching to it as a part of the effort to update the pretty-printer for ECMAScript5.

achudnov commented 8 years ago

https://hackage.haskell.org/package/annotated-wl-pprint seems helpful for pretty-printing comments and, potentially, generating source maps. See https://hackage.haskell.org/package/annotated-wl-pprint-0.7.0/docs/Text-PrettyPrint-Annotated-Leijen.html#v:displayDecorated and https://hackage.haskell.org/package/annotated-wl-pprint-0.7.0/docs/Text-PrettyPrint-Annotated-Leijen.html#v:displaySpans.

Note, that pretty implements the same functionality, but in a cleaner way (less code repetition): https://github.com/haskell/pretty/blob/master/src/Text/PrettyPrint/Annotated/HughesPJ.hs#L950 Still, WadlerLeijen tends to produce prettier output, so it might be a good idea to backport the implementation in pretty to annotated-wl-pprint. Another potential improvement would be to parametrize rendering with any instance of Monoid and IsString so we could plug-in Text.Builder and Data.ByteString.Builder with no effort. All in all, these improvements would bring an interface that is generic enough to meet all of our use cases: (1) pretty output with (2) optional annotation printing, (3) SourceMap generation and (4) minified output. Which obviates my old and semi-coherent dreams in achudnov/printers@25b7e08e5cdc53eaa4527eede988fab2b7aa7234