cdepillabout / pretty-simple

pretty-printer for Haskell data types that have a Show instance
https://hackage.haskell.org/package/pretty-simple
BSD 3-Clause "New" or "Revised" License
243 stars 29 forks source link

Improve laziness for strings #42

Open anka-213 opened 5 years ago

anka-213 commented 5 years ago

By parsing one character at a time with Data.Char.readLitChar, we can avoid the need to read the entire string at once.

cdepillabout commented 5 years ago

@andrew-lei Would you be able to review this one as well?

If it looks good, you can merge it and make a new release if you want. Here is a release checklist:

https://functor.tokyo/blog/2018-07-16-release-haskell-packages-to-hackage

andrew-lei commented 5 years ago

Travis failed this for the test that was implemented yesterday. This could be remedied, but it depends on what parseStringLit should give in cases of malformed input.

The proposed code will parse string literals from a shown string to a literal string. I believe I had wanted to do something along those lines, but there is the problem of malformed input (non-existent escaped characters). The case is accounted for, but results in ambiguity. For example,

> parseStringLit "\\c\""
("\\c","")
> parseStringLit "\\\\c\""
("\\c","")

This would only be the case for a very unusual show instance. For instance, consider

data Foo = Foo

instance Show Foo where
  show Foo = "\"\\c\\\\c\""

In which case

> pPrintNoColor Foo
"\c\c"

It's a pretty weird edge case that is probably unlikely to occur, but that's why I wanted to have the parser preserve the strings verbatim.