bjpop / language-python

A parser for Python 2.x and 3.x written in Haskell
157 stars 45 forks source link

Some stuff missing in Pretty instances #30

Closed fcostantini closed 8 years ago

fcostantini commented 8 years ago

Hi, I'm using your library to generate (somewhat) random Python code and pretty print it. We found that sometimes the generation fails because the Pretty instances are not exhaustive. The 2 exceptions we got were:

There is no pattern for SliceEllipsis, so if we happen to generate one, we get an exception when trying to print it. If I'm not mistaken, this should fix it:
pretty (SliceEllipsis {}) = text "..."

This happens when you try to pretty a Conditional with empty guards. Pretty instance for Conditional is

pretty stmt@(Conditional { cond_guards = guards, cond_else = optionalElse }) =
    case guards of
        (cond,body):xs -> ...

but since there is no alternative for [], it fails. Since a conditional without guards doesn't make much sense, perhaps it could be prettied as empty :: Doc.

Regards.

bjpop commented 8 years ago

Thanks @fcostantini

I've pushed a commit which addresses these issues: d3da62a9d449d23b8d665c441468b0ea01de2c7e

The first one was a genuine bug.

The second one is less clear. A conditional statement with no guard is syntactically incorrect. I've modified the pretty printer to generate an error in that case. It is unfortunate that the AST allows things which are not syntactically valid. We really need a better interface for constructing Python code.

fcostantini commented 8 years ago

Awesome, glad to be helpful. Thanks for the fix!

Can you upload the new version to hackage?

bjpop commented 8 years ago

Done. Uploaded to Hackage as 0.5.4. Thanks for your input.