bjpop / language-python

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

Empty class body should pretty print as "pass" #31

Open puffnfresh opened 8 years ago

puffnfresh commented 8 years ago
prettyText $ Py.Class (Py.Ident "Hello" ()) [] [] ()

Should give:

class Hello:
  pass

But currently the "pass" is missing.

bjpop commented 8 years ago

Thanks @puffnfresh.

I'm not sure how I feel about this.

On the one hand I can see the convenience, but on the other hand the pretty printer assumes that it is given a valid AST.

I think the bigger problem is that we don't have a convenient way to construct ASTs programmatically. Therefore we are currently forced to build the AST values directly, but the AST data type has too much slack in it. It is easy to create ASTs which do not correspond to valid Python parses.

We could patch the pretty printer to allow conveniences, like the one described in this issue. However, that is a bit of a slippery slope, given that there are probably lots of places where such conveniences could be introduced in the pretty printer.

I think a better solution is to add functionality for conveniently building valid ASTs programmatically, and leave the pretty printer to assume that it is given a valid AST.

Cheers, Bernie

puffnfresh commented 8 years ago

@bjpop I agree:

data ClassyBody
  = ClassBody (NonEmptyList a)
  | ClassBodyPass
bjpop commented 8 years ago

I'm thinking of a library of "smart" constructor functions. Perhaps also quasi quotation, as mentioned in https://github.com/bjpop/language-python/issues/13.