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

the parser doesn't understand Haskell characters like 'a' #45

Closed neongreen closed 4 years ago

neongreen commented 5 years ago
Text.Pretty.Simple> pStringNoColor "[']']"
"[ ' ]'"

The correct output should be "[']'".

cdepillabout commented 5 years ago

@neongreen I think the problem with pretty-simple is two-fold:

  1. pretty-simple doesn't handle non-balanced brackets: #27. Here's the expression that "[']']" is parsed into:

    [ Brackets 
        ( CommaSeparated 
            { unCommaSeparated = 
                [ [ Other "'" ] ]
            }
        )
    , Other "'" 
    ]

    Basically this means there are brackets surrounding a ', and then a single '. The final bracket is removed. The reason there are spaces around the first quote is that pretty-simple prints spaces inside of brackets by default.

    This should instead be parsed into something like this:

    [ Brackets 
        ( CommaSeparated 
            { unCommaSeparated = 
                [ [ CharLit ']' ] ]
            }
        )
    ]
  2. pretty-simple doesn't have a special parser for Haskell characters (e.g. things like 'a' or 'X' or ']'). So it doesn't realize that ']' is supposed to be a character (and not a closing bracket).

    It would be nice to add an expression for characters (like the CharLit I've used above).


If you want to send a PR fixing either of these things, it would definitely be accepted!

If you wanted to fix this, you'd mainly have to edit the expression parser, which lives in these two files:

https://github.com/cdepillabout/pretty-simple/blob/787cec7d170077f21407d5b7f65b6694b5b318d3/src/Text/Pretty/Simple/Internal/Expr.hs

https://github.com/cdepillabout/pretty-simple/blob/787cec7d170077f21407d5b7f65b6694b5b318d3/src/Text/Pretty/Simple/Internal/ExprParser.hs

sjakobi commented 4 years ago
Text.Pretty.Simple> pStringNoColor "[']']"
"[ ' ]'"

The correct output should be "[']'".

Should that be "[ ']' ]" in analogy to

> pStringNoColor "[1]"
"[ 1 ]"

?