{-# LANGUAGE CPP #-}
module Language.Python.Common.PrettyAST () where
#if __GLASGOW_HASKELL__ >= 803
import Prelude hiding ((<>))
#endif
import Language.Python.Common.Pretty
import Language.Python.Common.AST
import Data.Maybe (fromMaybe)
dot :: Doc
dot :: Doc
dot = Char -> Doc
char Char
'.'
indent :: Doc -> Doc
indent :: Doc -> Doc
indent Doc
doc = Int -> Doc -> Doc
nest Int
4 Doc
doc
blankLine :: Doc
blankLine :: Doc
blankLine = String -> Doc
text []
prettyString :: String -> Doc
prettyString :: String -> Doc
prettyString String
str = String -> Doc
text String
str
instance Pretty (Module a) where
pretty :: Module a -> Doc
pretty (Module [Statement a]
stmts) = [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Statement a -> Doc) -> [Statement a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Statement a -> Doc
forall a. Pretty a => a -> Doc
pretty [Statement a]
stmts
instance Pretty (Ident a) where
pretty :: Ident a -> Doc
pretty name :: Ident a
name@(Ident {}) = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ Ident a -> String
forall annot. Ident annot -> String
ident_string Ident a
name
prettyDottedName :: DottedName a -> Doc
prettyDottedName :: forall a. DottedName a -> Doc
prettyDottedName [] = Doc
empty
prettyDottedName [Ident a
name] = Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
name
prettyDottedName (Ident a
name:rest :: [Ident a]
rest@(Ident a
_:[Ident a]
_))
= Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
name Doc -> Doc -> Doc
<> Doc
dot Doc -> Doc -> Doc
<> [Ident a] -> Doc
forall a. DottedName a -> Doc
prettyDottedName [Ident a]
rest
instance Pretty (ImportItem a) where
pretty :: ImportItem a -> Doc
pretty (ImportItem {import_item_name :: forall annot. ImportItem annot -> DottedName annot
import_item_name = DottedName a
name, import_as_name :: forall annot. ImportItem annot -> Maybe (Ident annot)
import_as_name = Maybe (Ident a)
asName})
= DottedName a -> Doc
forall a. DottedName a -> Doc
prettyDottedName DottedName a
name Doc -> Doc -> Doc
<+> (Doc -> (Ident a -> Doc) -> Maybe (Ident a) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\Ident a
n -> String -> Doc
text String
"as" Doc -> Doc -> Doc
<+> Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
n) Maybe (Ident a)
asName)
instance Pretty (FromItem a) where
pretty :: FromItem a -> Doc
pretty (FromItem { from_item_name :: forall annot. FromItem annot -> Ident annot
from_item_name = Ident a
name, from_as_name :: forall annot. FromItem annot -> Maybe (Ident annot)
from_as_name = Maybe (Ident a)
asName })
= Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
name Doc -> Doc -> Doc
<+> (Doc -> (Ident a -> Doc) -> Maybe (Ident a) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\Ident a
n -> String -> Doc
text String
"as" Doc -> Doc -> Doc
<+> Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
n) Maybe (Ident a)
asName)
instance Pretty (FromItems a) where
pretty :: FromItems a -> Doc
pretty ImportEverything {} = Char -> Doc
char Char
'*'
pretty (FromItems { from_items_items :: forall annot. FromItems annot -> [FromItem annot]
from_items_items = [FromItem a
item] }) = FromItem a -> Doc
forall a. Pretty a => a -> Doc
pretty FromItem a
item
pretty (FromItems { from_items_items :: forall annot. FromItems annot -> [FromItem annot]
from_items_items = [FromItem a]
items }) = Doc -> Doc
parens ([FromItem a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [FromItem a]
items)
instance Pretty (ImportRelative a) where
pretty :: ImportRelative a -> Doc
pretty (ImportRelative { import_relative_dots :: forall annot. ImportRelative annot -> Int
import_relative_dots = Int
dots, import_relative_module :: forall annot. ImportRelative annot -> Maybe (DottedName annot)
import_relative_module = Maybe (DottedName a)
mod })
= case Maybe (DottedName a)
mod of
Maybe (DottedName a)
Nothing -> Doc
dotDoc
Just DottedName a
name -> Doc
dotDoc Doc -> Doc -> Doc
<> DottedName a -> Doc
forall a. DottedName a -> Doc
prettyDottedName DottedName a
name
where
dotDoc :: Doc
dotDoc = String -> Doc
text (Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
dots Char
'.')
prettySuite :: [Statement a] -> Doc
prettySuite :: forall a. [Statement a] -> Doc
prettySuite [Statement a]
stmts = [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Statement a -> Doc) -> [Statement a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Statement a -> Doc
forall a. Pretty a => a -> Doc
pretty [Statement a]
stmts
optionalKeywordSuite :: String -> [Statement a] -> Doc
optionalKeywordSuite :: forall a. String -> [Statement a] -> Doc
optionalKeywordSuite String
_ [] = Doc
empty
optionalKeywordSuite String
keyword [Statement a]
stmts = String -> Doc
text String
keyword Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
$+$ Doc -> Doc
indent ([Statement a] -> Doc
forall a. [Statement a] -> Doc
prettySuite [Statement a]
stmts)
prettyParenList :: Pretty a => [a] -> Doc
prettyParenList :: forall a. Pretty a => [a] -> Doc
prettyParenList = Doc -> Doc
parens (Doc -> Doc) -> ([a] -> Doc) -> [a] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList
prettyOptionalList :: Pretty a => [a] -> Doc
prettyOptionalList :: forall a. Pretty a => [a] -> Doc
prettyOptionalList [] = Doc
empty
prettyOptionalList [a]
list = Doc -> Doc
parens (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [a]
list
prettyGuards :: [(Expr a, Suite a)] -> Doc
prettyGuards :: forall a. [(Expr a, Suite a)] -> Doc
prettyGuards [] = Doc
empty
prettyGuards ((Expr a
cond,Suite a
body):[(Expr a, Suite a)]
guards)
= String -> Doc
text String
"elif" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
cond Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
$+$ Doc -> Doc
indent (Suite a -> Doc
forall a. [Statement a] -> Doc
prettySuite Suite a
body) Doc -> Doc -> Doc
$+$
[(Expr a, Suite a)] -> Doc
forall a. [(Expr a, Suite a)] -> Doc
prettyGuards [(Expr a, Suite a)]
guards
instance Pretty (Statement a) where
pretty :: Statement a -> Doc
pretty (Import { import_items :: forall annot. Statement annot -> [ImportItem annot]
import_items = [ImportItem a]
items}) = String -> Doc
text String
"import" Doc -> Doc -> Doc
<+> [ImportItem a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [ImportItem a]
items
pretty stmt :: Statement a
stmt@(FromImport {})
= String -> Doc
text String
"from" Doc -> Doc -> Doc
<+> ImportRelative a -> Doc
forall a. Pretty a => a -> Doc
pretty (Statement a -> ImportRelative a
forall annot. Statement annot -> ImportRelative annot
from_module Statement a
stmt) Doc -> Doc -> Doc
<+> String -> Doc
text String
"import" Doc -> Doc -> Doc
<+> FromItems a -> Doc
forall a. Pretty a => a -> Doc
pretty (Statement a -> FromItems a
forall annot. Statement annot -> FromItems annot
from_items Statement a
stmt)
pretty stmt :: Statement a
stmt@(While {})
= String -> Doc
text String
"while" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty (Statement a -> Expr a
forall annot. Statement annot -> Expr annot
while_cond Statement a
stmt) Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
$+$
Doc -> Doc
indent ([Statement a] -> Doc
forall a. [Statement a] -> Doc
prettySuite (Statement a -> [Statement a]
forall annot. Statement annot -> Suite annot
while_body Statement a
stmt)) Doc -> Doc -> Doc
$+$ String -> [Statement a] -> Doc
forall a. String -> [Statement a] -> Doc
optionalKeywordSuite String
"else" (Statement a -> [Statement a]
forall annot. Statement annot -> Suite annot
while_else Statement a
stmt)
pretty stmt :: Statement a
stmt@(For {})
= String -> Doc
text String
"for" Doc -> Doc -> Doc
<+> [Expr a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList (Statement a -> [Expr a]
forall annot. Statement annot -> [Expr annot]
for_targets Statement a
stmt) Doc -> Doc -> Doc
<+> String -> Doc
text String
"in" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty (Statement a -> Expr a
forall annot. Statement annot -> Expr annot
for_generator Statement a
stmt) Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
$+$
Doc -> Doc
indent ([Statement a] -> Doc
forall a. [Statement a] -> Doc
prettySuite (Statement a -> [Statement a]
forall annot. Statement annot -> Suite annot
for_body Statement a
stmt)) Doc -> Doc -> Doc
$+$ String -> [Statement a] -> Doc
forall a. String -> [Statement a] -> Doc
optionalKeywordSuite String
"else" (Statement a -> [Statement a]
forall annot. Statement annot -> Suite annot
for_else Statement a
stmt)
pretty (AsyncFor { for_stmt :: forall annot. Statement annot -> Statement annot
for_stmt = Statement a
fs }) = String -> Doc
zeroWidthText String
"async " Doc -> Doc -> Doc
<> Statement a -> Doc
forall a. Pretty a => a -> Doc
pretty Statement a
fs
pretty stmt :: Statement a
stmt@(Fun {})
= String -> Doc
text String
"def" Doc -> Doc -> Doc
<+> Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty (Statement a -> Ident a
forall annot. Statement annot -> Ident annot
fun_name Statement a
stmt) Doc -> Doc -> Doc
<> Doc -> Doc
parens ([Parameter a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList (Statement a -> [Parameter a]
forall annot. Statement annot -> [Parameter annot]
fun_args Statement a
stmt)) Doc -> Doc -> Doc
<+>
Maybe (Expr a) -> Doc -> Doc
forall a. Pretty a => Maybe a -> Doc -> Doc
perhaps (Statement a -> Maybe (Expr a)
forall annot. Statement annot -> Maybe (Expr annot)
fun_result_annotation Statement a
stmt) (String -> Doc
text String
"->") Doc -> Doc -> Doc
<+>
Maybe (Expr a) -> Doc
forall a. Pretty a => a -> Doc
pretty (Statement a -> Maybe (Expr a)
forall annot. Statement annot -> Maybe (Expr annot)
fun_result_annotation Statement a
stmt) Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
$+$ Doc -> Doc
indent ([Statement a] -> Doc
forall a. [Statement a] -> Doc
prettySuite (Statement a -> [Statement a]
forall annot. Statement annot -> Suite annot
fun_body Statement a
stmt))
pretty (AsyncFun { fun_def :: forall annot. Statement annot -> Statement annot
fun_def = Statement a
fd }) = String -> Doc
zeroWidthText String
"async " Doc -> Doc -> Doc
<+> Statement a -> Doc
forall a. Pretty a => a -> Doc
pretty Statement a
fd
pretty stmt :: Statement a
stmt@(Class {})
= String -> Doc
text String
"class" Doc -> Doc -> Doc
<+> Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty (Statement a -> Ident a
forall annot. Statement annot -> Ident annot
class_name Statement a
stmt) Doc -> Doc -> Doc
<> [Argument a] -> Doc
forall a. Pretty a => [a] -> Doc
prettyOptionalList (Statement a -> [Argument a]
forall annot. Statement annot -> [Argument annot]
class_args Statement a
stmt) Doc -> Doc -> Doc
<>
Doc
colon Doc -> Doc -> Doc
$+$ Doc -> Doc
indent ([Statement a] -> Doc
forall a. [Statement a] -> Doc
prettySuite (Statement a -> [Statement a]
forall annot. Statement annot -> Suite annot
class_body Statement a
stmt))
pretty stmt :: Statement a
stmt@(Conditional { cond_guards :: forall annot. Statement annot -> [(Expr annot, Suite annot)]
cond_guards = [(Expr a, [Statement a])]
guards, cond_else :: forall annot. Statement annot -> Suite annot
cond_else = [Statement a]
optionalElse })
= case [(Expr a, [Statement a])]
guards of
(Expr a
cond,[Statement a]
body):[(Expr a, [Statement a])]
xs ->
String -> Doc
text String
"if" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
cond Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
$+$ Doc -> Doc
indent ([Statement a] -> Doc
forall a. [Statement a] -> Doc
prettySuite [Statement a]
body) Doc -> Doc -> Doc
$+$
[(Expr a, [Statement a])] -> Doc
forall a. [(Expr a, Suite a)] -> Doc
prettyGuards [(Expr a, [Statement a])]
xs Doc -> Doc -> Doc
$+$
String -> [Statement a] -> Doc
forall a. String -> [Statement a] -> Doc
optionalKeywordSuite String
"else" [Statement a]
optionalElse
[] -> String -> Doc
forall a. HasCallStack => String -> a
error String
"Attempt to pretty print conditional statement with empty guards"
pretty (Assign { assign_to :: forall annot. Statement annot -> [Expr annot]
assign_to = [Expr a]
pattern, assign_expr :: forall annot. Statement annot -> Expr annot
assign_expr = Expr a
e })
= [Expr a] -> Doc
forall a. Pretty a => [a] -> Doc
equalsList [Expr a]
pattern Doc -> Doc -> Doc
<+> Doc
equals Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
pretty (AugmentedAssign { aug_assign_to :: forall annot. Statement annot -> Expr annot
aug_assign_to = Expr a
to_expr, aug_assign_op :: forall annot. Statement annot -> AssignOp annot
aug_assign_op = AssignOp a
op, aug_assign_expr :: forall annot. Statement annot -> Expr annot
aug_assign_expr = Expr a
e})
= Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
to_expr Doc -> Doc -> Doc
<+> AssignOp a -> Doc
forall a. Pretty a => a -> Doc
pretty AssignOp a
op Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
pretty (AnnotatedAssign { ann_assign_annotation :: forall annot. Statement annot -> Expr annot
ann_assign_annotation = Expr a
ann_annotate, ann_assign_to :: forall annot. Statement annot -> Expr annot
ann_assign_to = Expr a
ann_to, ann_assign_expr :: forall annot. Statement annot -> Maybe (Expr annot)
ann_assign_expr = Maybe (Expr a)
ann_expr})
= Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
ann_to Doc -> Doc -> Doc
<+> String -> Doc
text String
":" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
ann_annotate Doc -> Doc -> Doc
<+> Doc -> Maybe Doc -> Doc
forall a. a -> Maybe a -> a
fromMaybe Doc
empty (((String -> Doc
text String
"=" Doc -> Doc -> Doc
<+>) (Doc -> Doc) -> (Expr a -> Doc) -> Expr a -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty) (Expr a -> Doc) -> Maybe (Expr a) -> Maybe Doc
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Expr a)
ann_expr)
pretty (Decorated { decorated_decorators :: forall annot. Statement annot -> [Decorator annot]
decorated_decorators = [Decorator a]
decs, decorated_def :: forall annot. Statement annot -> Statement annot
decorated_def = Statement a
stmt})
= [Doc] -> Doc
vcat ((Decorator a -> Doc) -> [Decorator a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Decorator a -> Doc
forall a. Pretty a => a -> Doc
pretty [Decorator a]
decs) Doc -> Doc -> Doc
$+$ Statement a -> Doc
forall a. Pretty a => a -> Doc
pretty Statement a
stmt
pretty (Return { return_expr :: forall annot. Statement annot -> Maybe (Expr annot)
return_expr = Maybe (Expr a)
e }) = String -> Doc
text String
"return" Doc -> Doc -> Doc
<+> Maybe (Expr a) -> Doc
forall a. Pretty a => a -> Doc
pretty Maybe (Expr a)
e
pretty (Try { try_body :: forall annot. Statement annot -> Suite annot
try_body = [Statement a]
body, try_excepts :: forall annot. Statement annot -> [Handler annot]
try_excepts = [Handler a]
handlers, try_else :: forall annot. Statement annot -> Suite annot
try_else = [Statement a]
optionalElse, try_finally :: forall annot. Statement annot -> Suite annot
try_finally = [Statement a]
finally})
= String -> Doc
text String
"try" Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
$+$ Doc -> Doc
indent ([Statement a] -> Doc
forall a. [Statement a] -> Doc
prettySuite [Statement a]
body) Doc -> Doc -> Doc
$+$
[Handler a] -> Doc
forall a. [Handler a] -> Doc
prettyHandlers [Handler a]
handlers Doc -> Doc -> Doc
$+$ String -> [Statement a] -> Doc
forall a. String -> [Statement a] -> Doc
optionalKeywordSuite String
"else" [Statement a]
optionalElse Doc -> Doc -> Doc
$+$
String -> [Statement a] -> Doc
forall a. String -> [Statement a] -> Doc
optionalKeywordSuite String
"finally" [Statement a]
finally
pretty (Raise { raise_expr :: forall annot. Statement annot -> RaiseExpr annot
raise_expr = RaiseExpr a
e })
= String -> Doc
text String
"raise" Doc -> Doc -> Doc
<+> RaiseExpr a -> Doc
forall a. Pretty a => a -> Doc
pretty RaiseExpr a
e
pretty (With { with_context :: forall annot. Statement annot -> [(Expr annot, Maybe (Expr annot))]
with_context = [(Expr a, Maybe (Expr a))]
context, with_body :: forall annot. Statement annot -> Suite annot
with_body = [Statement a]
body })
= String -> Doc
text String
"with" Doc -> Doc -> Doc
<+> [Doc] -> Doc
hcat (Doc -> [Doc] -> [Doc]
punctuate Doc
comma (((Expr a, Maybe (Expr a)) -> Doc)
-> [(Expr a, Maybe (Expr a))] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Expr a, Maybe (Expr a)) -> Doc
forall a. (Expr a, Maybe (Expr a)) -> Doc
prettyWithContext [(Expr a, Maybe (Expr a))]
context)) Doc -> Doc -> Doc
<+> Doc
colon Doc -> Doc -> Doc
$+$
Doc -> Doc
indent ([Statement a] -> Doc
forall a. [Statement a] -> Doc
prettySuite [Statement a]
body)
pretty (AsyncWith { with_stmt :: forall annot. Statement annot -> Statement annot
with_stmt = Statement a
ws }) = String -> Doc
zeroWidthText String
"async " Doc -> Doc -> Doc
<+> Statement a -> Doc
forall a. Pretty a => a -> Doc
pretty Statement a
ws
pretty Pass {} = String -> Doc
text String
"pass"
pretty Break {} = String -> Doc
text String
"break"
pretty Continue {} = String -> Doc
text String
"continue"
pretty (Delete { del_exprs :: forall annot. Statement annot -> [Expr annot]
del_exprs = [Expr a]
es }) = String -> Doc
text String
"del" Doc -> Doc -> Doc
<+> [Expr a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [Expr a]
es
pretty (StmtExpr { stmt_expr :: forall annot. Statement annot -> Expr annot
stmt_expr = Expr a
e }) = Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
pretty (Global { global_vars :: forall annot. Statement annot -> [Ident annot]
global_vars = [Ident a]
idents }) = String -> Doc
text String
"global" Doc -> Doc -> Doc
<+> [Ident a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [Ident a]
idents
pretty (NonLocal { nonLocal_vars :: forall annot. Statement annot -> [Ident annot]
nonLocal_vars = [Ident a]
idents }) = String -> Doc
text String
"nonlocal" Doc -> Doc -> Doc
<+> [Ident a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [Ident a]
idents
pretty (Assert { assert_exprs :: forall annot. Statement annot -> [Expr annot]
assert_exprs = [Expr a]
es }) = String -> Doc
text String
"assert" Doc -> Doc -> Doc
<+> [Expr a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [Expr a]
es
pretty (Print { print_chevron :: forall annot. Statement annot -> Bool
print_chevron = Bool
have_chevron, print_exprs :: forall annot. Statement annot -> [Expr annot]
print_exprs = [Expr a]
es, print_trailing_comma :: forall annot. Statement annot -> Bool
print_trailing_comma = Bool
trail_comma }) =
String -> Doc
text String
"print" Doc -> Doc -> Doc
<> (if Bool
have_chevron then String -> Doc
text String
" >>" else Doc
empty) Doc -> Doc -> Doc
<+>
[Doc] -> Doc
hcat (Doc -> [Doc] -> [Doc]
punctuate Doc
comma ((Expr a -> Doc) -> [Expr a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty [Expr a]
es)) Doc -> Doc -> Doc
<>
if Bool
trail_comma then Doc
comma else Doc
empty
pretty (Exec { exec_expr :: forall annot. Statement annot -> Expr annot
exec_expr = Expr a
e, exec_globals_locals :: forall annot.
Statement annot -> Maybe (Expr annot, Maybe (Expr annot))
exec_globals_locals = Maybe (Expr a, Maybe (Expr a))
gls }) =
String -> Doc
text String
"exec" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e Doc -> Doc -> Doc
<+>
Doc
-> ((Expr a, Maybe (Expr a)) -> Doc)
-> Maybe (Expr a, Maybe (Expr a))
-> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\ (Expr a
globals, Maybe (Expr a)
next) -> String -> Doc
text String
"in" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
globals Doc -> Doc -> Doc
<+>
Doc -> (Expr a -> Doc) -> Maybe (Expr a) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\Expr a
locals -> Doc
comma Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
locals) Maybe (Expr a)
next) Maybe (Expr a, Maybe (Expr a))
gls
prettyWithContext :: (Expr a, Maybe (Expr a)) -> Doc
prettyWithContext :: forall a. (Expr a, Maybe (Expr a)) -> Doc
prettyWithContext (Expr a
e, Maybe (Expr a)
Nothing) = Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
prettyWithContext (Expr a
e, Just Expr a
as) = Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e Doc -> Doc -> Doc
<+> String -> Doc
text String
"as" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
as
prettyHandlers :: [Handler a] -> Doc
prettyHandlers :: forall a. [Handler a] -> Doc
prettyHandlers = (Handler a -> Doc -> Doc) -> Doc -> [Handler a] -> Doc
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Handler a
next Doc
rec -> Handler a -> Doc
forall a. Pretty a => a -> Doc
pretty Handler a
next Doc -> Doc -> Doc
$+$ Doc
rec) Doc
empty
instance Pretty (Handler a) where
pretty :: Handler a -> Doc
pretty (Handler { handler_clause :: forall annot. Handler annot -> ExceptClause annot
handler_clause = ExceptClause a
exceptClause, handler_suite :: forall annot. Handler annot -> Suite annot
handler_suite = Suite a
suite })
= ExceptClause a -> Doc
forall a. Pretty a => a -> Doc
pretty ExceptClause a
exceptClause Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
$+$ Doc -> Doc
indent (Suite a -> Doc
forall a. [Statement a] -> Doc
prettySuite Suite a
suite)
instance Pretty (ExceptClause a) where
pretty :: ExceptClause a -> Doc
pretty (ExceptClause { except_clause :: forall annot.
ExceptClause annot -> Maybe (Expr annot, Maybe (Expr annot))
except_clause = Maybe (Expr a, Maybe (Expr a))
Nothing }) = String -> Doc
text String
"except"
pretty (ExceptClause { except_clause :: forall annot.
ExceptClause annot -> Maybe (Expr annot, Maybe (Expr annot))
except_clause = Just (Expr a
e, Maybe (Expr a)
target)})
= String -> Doc
text String
"except" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e Doc -> Doc -> Doc
<+> Doc -> (Expr a -> Doc) -> Maybe (Expr a) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\Expr a
t -> String -> Doc
text String
"as" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
t) Maybe (Expr a)
target
instance Pretty (RaiseExpr a) where
pretty :: RaiseExpr a -> Doc
pretty (RaiseV3 Maybe (Expr a, Maybe (Expr a))
e) =
Doc
-> ((Expr a, Maybe (Expr a)) -> Doc)
-> Maybe (Expr a, Maybe (Expr a))
-> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\ (Expr a
x, Maybe (Expr a)
fromE) -> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
x Doc -> Doc -> Doc
<+> (Doc -> (Expr a -> Doc) -> Maybe (Expr a) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\Expr a
f -> String -> Doc
text String
"from" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
f) Maybe (Expr a)
fromE)) Maybe (Expr a, Maybe (Expr a))
e
pretty (RaiseV2 Maybe (Expr a, Maybe (Expr a, Maybe (Expr a)))
exp) =
Doc
-> ((Expr a, Maybe (Expr a, Maybe (Expr a))) -> Doc)
-> Maybe (Expr a, Maybe (Expr a, Maybe (Expr a)))
-> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\ (Expr a
e1, Maybe (Expr a, Maybe (Expr a))
next1) -> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e1 Doc -> Doc -> Doc
<>
Doc
-> ((Expr a, Maybe (Expr a)) -> Doc)
-> Maybe (Expr a, Maybe (Expr a))
-> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\ (Expr a
e2, Maybe (Expr a)
next2) -> Doc
comma Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e2 Doc -> Doc -> Doc
<>
Doc -> (Expr a -> Doc) -> Maybe (Expr a) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\ Expr a
e3 -> Doc
comma Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e3) Maybe (Expr a)
next2) Maybe (Expr a, Maybe (Expr a))
next1) Maybe (Expr a, Maybe (Expr a, Maybe (Expr a)))
exp
instance Pretty (Decorator a) where
pretty :: Decorator a -> Doc
pretty (Decorator { decorator_name :: forall annot. Decorator annot -> DottedName annot
decorator_name = DottedName a
name, decorator_args :: forall annot. Decorator annot -> [Argument annot]
decorator_args = [Argument a]
args })
= Char -> Doc
char Char
'@' Doc -> Doc -> Doc
<> DottedName a -> Doc
forall a. DottedName a -> Doc
prettyDottedName DottedName a
name Doc -> Doc -> Doc
<+> [Argument a] -> Doc
forall a. Pretty a => [a] -> Doc
prettyOptionalList [Argument a]
args
instance Pretty (Parameter a) where
pretty :: Parameter a -> Doc
pretty (Param { param_name :: forall annot. Parameter annot -> Ident annot
param_name = Ident a
ident, param_py_annotation :: forall annot. Parameter annot -> Maybe (Expr annot)
param_py_annotation = Maybe (Expr a)
annot, param_default :: forall annot. Parameter annot -> Maybe (Expr annot)
param_default = Maybe (Expr a)
def })
= Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
ident Doc -> Doc -> Doc
<> (Doc -> (Expr a -> Doc) -> Maybe (Expr a) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\Expr a
e -> Doc
colon Doc -> Doc -> Doc
<> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e Doc -> Doc -> Doc
<> Doc
space) Maybe (Expr a)
annot) Doc -> Doc -> Doc
<>
Doc -> (Expr a -> Doc) -> Maybe (Expr a) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\Expr a
e -> Doc
equals Doc -> Doc -> Doc
<> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e) Maybe (Expr a)
def
pretty (VarArgsPos { param_name :: forall annot. Parameter annot -> Ident annot
param_name = Ident a
ident, param_py_annotation :: forall annot. Parameter annot -> Maybe (Expr annot)
param_py_annotation = Maybe (Expr a)
annot})
= Char -> Doc
char Char
'*' Doc -> Doc -> Doc
<> Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
ident Doc -> Doc -> Doc
<> (Doc -> (Expr a -> Doc) -> Maybe (Expr a) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\Expr a
e -> Doc
colon Doc -> Doc -> Doc
<> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e) Maybe (Expr a)
annot)
pretty (VarArgsKeyword { param_name :: forall annot. Parameter annot -> Ident annot
param_name = Ident a
ident, param_py_annotation :: forall annot. Parameter annot -> Maybe (Expr annot)
param_py_annotation = Maybe (Expr a)
annot })
= String -> Doc
text String
"**" Doc -> Doc -> Doc
<> Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
ident Doc -> Doc -> Doc
<> (Doc -> (Expr a -> Doc) -> Maybe (Expr a) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\Expr a
e -> Doc
colon Doc -> Doc -> Doc
<> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e) Maybe (Expr a)
annot)
pretty EndPositional {} = Char -> Doc
char Char
'*'
pretty (UnPackTuple { param_unpack_tuple :: forall annot. Parameter annot -> ParamTuple annot
param_unpack_tuple = ParamTuple a
tuple, param_default :: forall annot. Parameter annot -> Maybe (Expr annot)
param_default = Maybe (Expr a)
def })
= ParamTuple a -> Doc
forall a. Pretty a => a -> Doc
pretty ParamTuple a
tuple Doc -> Doc -> Doc
<> Doc -> (Expr a -> Doc) -> Maybe (Expr a) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\Expr a
e -> Doc
equals Doc -> Doc -> Doc
<> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e) Maybe (Expr a)
def
instance Pretty (ParamTuple a) where
pretty :: ParamTuple a -> Doc
pretty (ParamTupleName { param_tuple_name :: forall annot. ParamTuple annot -> Ident annot
param_tuple_name = Ident a
name }) = Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
name
pretty (ParamTuple { param_tuple :: forall annot. ParamTuple annot -> [ParamTuple annot]
param_tuple = [ParamTuple a]
tuple }) = [ParamTuple a] -> Doc
forall a. Pretty a => [a] -> Doc
prettyParenList [ParamTuple a]
tuple
instance Pretty (Argument a) where
pretty :: Argument a -> Doc
pretty (ArgExpr { arg_expr :: forall annot. Argument annot -> Expr annot
arg_expr = Expr a
e }) = Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
pretty (ArgVarArgsPos { arg_expr :: forall annot. Argument annot -> Expr annot
arg_expr = Expr a
e}) = Char -> Doc
char Char
'*' Doc -> Doc -> Doc
<> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
pretty (ArgVarArgsKeyword { arg_expr :: forall annot. Argument annot -> Expr annot
arg_expr = Expr a
e }) = String -> Doc
text String
"**" Doc -> Doc -> Doc
<> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
pretty (ArgKeyword { arg_keyword :: forall annot. Argument annot -> Ident annot
arg_keyword = Ident a
ident, arg_expr :: forall annot. Argument annot -> Expr annot
arg_expr = Expr a
e })
= Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
ident Doc -> Doc -> Doc
<> Doc
equals Doc -> Doc -> Doc
<> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
instance Pretty (Comprehension a) where
pretty :: Comprehension a -> Doc
pretty (Comprehension { comprehension_expr :: forall annot. Comprehension annot -> ComprehensionExpr annot
comprehension_expr = ComprehensionExpr a
e, comprehension_for :: forall annot. Comprehension annot -> CompFor annot
comprehension_for = CompFor a
for })
= ComprehensionExpr a -> Doc
forall a. Pretty a => a -> Doc
pretty ComprehensionExpr a
e Doc -> Doc -> Doc
<+> CompFor a -> Doc
forall a. Pretty a => a -> Doc
pretty CompFor a
for
instance Pretty (ComprehensionExpr a) where
pretty :: ComprehensionExpr a -> Doc
pretty (ComprehensionExpr Expr a
e) = Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
pretty (ComprehensionDict DictKeyDatumList a
d) = DictKeyDatumList a -> Doc
forall a. Pretty a => a -> Doc
pretty DictKeyDatumList a
d
instance Pretty (CompFor a) where
pretty :: CompFor a -> Doc
pretty (CompFor { comp_for_async :: forall annot. CompFor annot -> Bool
comp_for_async = Bool
ca, comp_for_exprs :: forall annot. CompFor annot -> [Expr annot]
comp_for_exprs = [Expr a]
es, comp_in_expr :: forall annot. CompFor annot -> Expr annot
comp_in_expr = Expr a
e, comp_for_iter :: forall annot. CompFor annot -> Maybe (CompIter annot)
comp_for_iter = Maybe (CompIter a)
iter })
= (String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ if Bool
ca then String
"async for" else String
"for") Doc -> Doc -> Doc
<+> [Expr a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [Expr a]
es
Doc -> Doc -> Doc
<+> String -> Doc
text String
"in" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e Doc -> Doc -> Doc
<+> Maybe (CompIter a) -> Doc
forall a. Pretty a => a -> Doc
pretty Maybe (CompIter a)
iter
instance Pretty (CompIf a) where
pretty :: CompIf a -> Doc
pretty (CompIf { comp_if :: forall annot. CompIf annot -> Expr annot
comp_if = Expr a
e, comp_if_iter :: forall annot. CompIf annot -> Maybe (CompIter annot)
comp_if_iter = Maybe (CompIter a)
iter })
= String -> Doc
text String
"if" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e Doc -> Doc -> Doc
<+> Maybe (CompIter a) -> Doc
forall a. Pretty a => a -> Doc
pretty Maybe (CompIter a)
iter
instance Pretty (CompIter a) where
pretty :: CompIter a -> Doc
pretty (IterFor { comp_iter_for :: forall annot. CompIter annot -> CompFor annot
comp_iter_for = CompFor a
compFor }) = CompFor a -> Doc
forall a. Pretty a => a -> Doc
pretty CompFor a
compFor
pretty (IterIf { comp_iter_if :: forall annot. CompIter annot -> CompIf annot
comp_iter_if = CompIf a
compIf }) = CompIf a -> Doc
forall a. Pretty a => a -> Doc
pretty CompIf a
compIf
instance Pretty (Expr a) where
pretty :: Expr a -> Doc
pretty (Var { var_ident :: forall annot. Expr annot -> Ident annot
var_ident = Ident a
i }) = Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
i
pretty (Int { expr_literal :: forall annot. Expr annot -> String
expr_literal = String
str }) = String -> Doc
text String
str
pretty (LongInt { expr_literal :: forall annot. Expr annot -> String
expr_literal = String
str }) = String -> Doc
text String
str
pretty (Float { expr_literal :: forall annot. Expr annot -> String
expr_literal = String
str }) = String -> Doc
text String
str
pretty (Imaginary { expr_literal :: forall annot. Expr annot -> String
expr_literal = String
str }) = String -> Doc
text String
str
pretty (Bool { bool_value :: forall annot. Expr annot -> Bool
bool_value = Bool
b}) = Bool -> Doc
forall a. Pretty a => a -> Doc
pretty Bool
b
pretty None {} = String -> Doc
text String
"None"
pretty Ellipsis {} = String -> Doc
text String
"..."
pretty (ByteStrings { byte_string_strings :: forall annot. Expr annot -> [String]
byte_string_strings = [String]
bs }) = [Doc] -> Doc
hcat ((String -> Doc) -> [String] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map String -> Doc
forall a. Pretty a => a -> Doc
pretty [String]
bs)
pretty (Strings { strings_strings :: forall annot. Expr annot -> [String]
strings_strings = [String]
ss }) = [Doc] -> Doc
hcat ((String -> Doc) -> [String] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map String -> Doc
prettyString [String]
ss)
pretty (UnicodeStrings { unicodestrings_strings :: forall annot. Expr annot -> [String]
unicodestrings_strings = [String]
ss }) = [Doc] -> Doc
hcat ((String -> Doc) -> [String] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map String -> Doc
prettyString [String]
ss)
pretty (Call { call_fun :: forall annot. Expr annot -> Expr annot
call_fun = Expr a
f, call_args :: forall annot. Expr annot -> [Argument annot]
call_args = [Argument a]
args }) = Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
f Doc -> Doc -> Doc
<> [Argument a] -> Doc
forall a. Pretty a => [a] -> Doc
prettyParenList [Argument a]
args
pretty (Subscript { subscriptee :: forall annot. Expr annot -> Expr annot
subscriptee = Expr a
e, subscript_expr :: forall annot. Expr annot -> Expr annot
subscript_expr = Expr a
sub })
= Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e Doc -> Doc -> Doc
<> Doc -> Doc
brackets (Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
sub)
pretty (SlicedExpr { slicee :: forall annot. Expr annot -> Expr annot
slicee = Expr a
e, slices :: forall annot. Expr annot -> [Slice annot]
slices = [Slice a]
ss })
= Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e Doc -> Doc -> Doc
<> Doc -> Doc
brackets ([Slice a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [Slice a]
ss)
pretty (CondExpr { ce_true_branch :: forall annot. Expr annot -> Expr annot
ce_true_branch = Expr a
trueBranch, ce_condition :: forall annot. Expr annot -> Expr annot
ce_condition = Expr a
cond, ce_false_branch :: forall annot. Expr annot -> Expr annot
ce_false_branch = Expr a
falseBranch })
= Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
trueBranch Doc -> Doc -> Doc
<+> String -> Doc
text String
"if" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
cond Doc -> Doc -> Doc
<+> String -> Doc
text String
"else" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
falseBranch
pretty (BinaryOp { operator :: forall annot. Expr annot -> Op annot
operator = Op a
op, left_op_arg :: forall annot. Expr annot -> Expr annot
left_op_arg = Expr a
left, right_op_arg :: forall annot. Expr annot -> Expr annot
right_op_arg = Expr a
right })
= Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
left Doc -> Doc -> Doc
<+> Op a -> Doc
forall a. Pretty a => a -> Doc
pretty Op a
op Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
right
pretty (UnaryOp { operator :: forall annot. Expr annot -> Op annot
operator = Op a
op, op_arg :: forall annot. Expr annot -> Expr annot
op_arg = Expr a
e }) = Op a -> Doc
forall a. Pretty a => a -> Doc
pretty Op a
op Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
pretty (Dot { dot_expr :: forall annot. Expr annot -> Expr annot
dot_expr = Expr a
e, dot_attribute :: forall annot. Expr annot -> Ident annot
dot_attribute = Ident a
a }) =
Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e Doc -> Doc -> Doc
<> Doc
dot Doc -> Doc -> Doc
<> Ident a -> Doc
forall a. Pretty a => a -> Doc
pretty Ident a
a
pretty (Lambda { lambda_args :: forall annot. Expr annot -> [Parameter annot]
lambda_args = [Parameter a]
args, lambda_body :: forall annot. Expr annot -> Expr annot
lambda_body = Expr a
body })
= String -> Doc
text String
"lambda" Doc -> Doc -> Doc
<+> [Parameter a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [Parameter a]
args Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
body
pretty (Tuple { tuple_exprs :: forall annot. Expr annot -> [Expr annot]
tuple_exprs = [Expr a]
es }) =
case [Expr a]
es of
[] -> String -> Doc
text String
"()"
[Expr a
e] -> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e Doc -> Doc -> Doc
<> Doc
comma
[Expr a]
_ -> [Expr a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [Expr a]
es
pretty (Yield { yield_arg :: forall annot. Expr annot -> Maybe (YieldArg annot)
yield_arg = Maybe (YieldArg a)
arg })
= String -> Doc
text String
"yield" Doc -> Doc -> Doc
<+> Maybe (YieldArg a) -> Doc
forall a. Pretty a => a -> Doc
pretty Maybe (YieldArg a)
arg
pretty (Generator { gen_comprehension :: forall annot. Expr annot -> Comprehension annot
gen_comprehension = Comprehension a
gc }) = Doc -> Doc
parens (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Comprehension a -> Doc
forall a. Pretty a => a -> Doc
pretty Comprehension a
gc
pretty (Await { await_expr :: forall annot. Expr annot -> Expr annot
await_expr = Expr a
ae }) = String -> Doc
text String
"await" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
ae
pretty (ListComp { list_comprehension :: forall annot. Expr annot -> Comprehension annot
list_comprehension = Comprehension a
lc }) = Doc -> Doc
brackets (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Comprehension a -> Doc
forall a. Pretty a => a -> Doc
pretty Comprehension a
lc
pretty (List { list_exprs :: forall annot. Expr annot -> [Expr annot]
list_exprs = [Expr a]
es }) = Doc -> Doc
brackets ([Expr a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [Expr a]
es)
pretty (Dictionary { dict_mappings :: forall annot. Expr annot -> [DictKeyDatumList annot]
dict_mappings = [DictKeyDatumList a]
mappings })
= Doc -> Doc
braces ([Doc] -> Doc
hsep (Doc -> [Doc] -> [Doc]
punctuate Doc
comma ([Doc] -> [Doc]) -> [Doc] -> [Doc]
forall a b. (a -> b) -> a -> b
$ (DictKeyDatumList a -> Doc) -> [DictKeyDatumList a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map DictKeyDatumList a -> Doc
forall a. Pretty a => a -> Doc
pretty [DictKeyDatumList a]
mappings))
pretty (DictComp { dict_comprehension :: forall annot. Expr annot -> Comprehension annot
dict_comprehension = Comprehension a
dc }) = Doc -> Doc
braces (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Comprehension a -> Doc
forall a. Pretty a => a -> Doc
pretty Comprehension a
dc
pretty (Set { set_exprs :: forall annot. Expr annot -> [Expr annot]
set_exprs = [Expr a]
es }) = Doc -> Doc
braces (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Expr a] -> Doc
forall a. Pretty a => [a] -> Doc
commaList [Expr a]
es
pretty (SetComp { set_comprehension :: forall annot. Expr annot -> Comprehension annot
set_comprehension = Comprehension a
sc }) = Doc -> Doc
braces (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Comprehension a -> Doc
forall a. Pretty a => a -> Doc
pretty Comprehension a
sc
pretty (Paren { paren_expr :: forall annot. Expr annot -> Expr annot
paren_expr = Expr a
e }) = Doc -> Doc
parens (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
pretty (StringConversion { backquoted_expr :: forall annot. Expr annot -> Expr annot
backquoted_expr = Expr a
e }) = Char -> Doc
char Char
'`' Doc -> Doc -> Doc
<> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e Doc -> Doc -> Doc
<> Char -> Doc
char Char
'`'
pretty (Starred { starred_expr :: forall annot. Expr annot -> Expr annot
starred_expr = Expr a
e }) = Char -> Doc
char Char
'*' Doc -> Doc -> Doc
<> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
instance Pretty (YieldArg a) where
pretty :: YieldArg a -> Doc
pretty (YieldFrom Expr a
e a
_annot) = String -> Doc
text String
"from" Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
pretty (YieldExpr Expr a
e) = Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
instance Pretty (DictKeyDatumList a) where
pretty :: DictKeyDatumList a -> Doc
pretty (DictMappingPair Expr a
key Expr a
val) = Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
key Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
<+> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
val
pretty (DictUnpacking Expr a
expr) = String -> Doc
text String
"**" Doc -> Doc -> Doc
<> Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
expr
instance Pretty (Slice a) where
pretty :: Slice a -> Doc
pretty (SliceProper { slice_lower :: forall annot. Slice annot -> Maybe (Expr annot)
slice_lower = Maybe (Expr a)
lower, slice_upper :: forall annot. Slice annot -> Maybe (Expr annot)
slice_upper = Maybe (Expr a)
upper, slice_stride :: forall annot. Slice annot -> Maybe (Maybe (Expr annot))
slice_stride = Maybe (Maybe (Expr a))
stride })
= Maybe (Expr a) -> Doc
forall a. Pretty a => a -> Doc
pretty Maybe (Expr a)
lower Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
<> Maybe (Expr a) -> Doc
forall a. Pretty a => a -> Doc
pretty Maybe (Expr a)
upper Doc -> Doc -> Doc
<> (Doc -> (Maybe (Expr a) -> Doc) -> Maybe (Maybe (Expr a)) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\Maybe (Expr a)
s -> Doc
colon Doc -> Doc -> Doc
<> Maybe (Expr a) -> Doc
forall a. Pretty a => a -> Doc
pretty Maybe (Expr a)
s) Maybe (Maybe (Expr a))
stride)
pretty (SliceExpr { slice_expr :: forall annot. Slice annot -> Expr annot
slice_expr = Expr a
e }) = Expr a -> Doc
forall a. Pretty a => a -> Doc
pretty Expr a
e
pretty (SliceEllipsis {}) = String -> Doc
text String
"..."
instance Pretty (Op a) where
pretty :: Op a -> Doc
pretty (And {}) = String -> Doc
text String
"and"
pretty (Or {}) = String -> Doc
text String
"or"
pretty (Not {}) = String -> Doc
text String
"not"
pretty (Exponent {}) = String -> Doc
text String
"**"
pretty (LessThan {}) = String -> Doc
text String
"<"
pretty (GreaterThan {}) = String -> Doc
text String
">"
pretty (Equality {}) = String -> Doc
text String
"=="
pretty (GreaterThanEquals {}) = String -> Doc
text String
">="
pretty (LessThanEquals {}) = String -> Doc
text String
"<="
pretty (NotEquals {}) = String -> Doc
text String
"!="
pretty (NotEqualsV2 {}) = String -> Doc
text String
"<>"
pretty (In {}) = String -> Doc
text String
"in"
pretty (Is {}) = String -> Doc
text String
"is"
pretty (IsNot {}) = String -> Doc
text String
"is not"
pretty (NotIn {}) = String -> Doc
text String
"not in"
pretty (BinaryOr {}) = String -> Doc
text String
"|"
pretty (Xor {}) = String -> Doc
text String
"^"
pretty (BinaryAnd {}) = String -> Doc
text String
"&"
pretty (ShiftLeft {}) = String -> Doc
text String
"<<"
pretty (ShiftRight {}) = String -> Doc
text String
">>"
pretty (Multiply {}) = String -> Doc
text String
"*"
pretty (Plus {}) = String -> Doc
text String
"+"
pretty (Minus {}) = String -> Doc
text String
"-"
pretty (Divide {}) = String -> Doc
text String
"/"
pretty (FloorDivide {}) = String -> Doc
text String
"//"
pretty (MatrixMult {}) = String -> Doc
text String
"@"
pretty (Invert {}) = String -> Doc
text String
"~"
pretty (Modulo {}) = String -> Doc
text String
"%"
instance Pretty (AssignOp a) where
pretty :: AssignOp a -> Doc
pretty (PlusAssign {}) = String -> Doc
text String
"+="
pretty (MinusAssign {}) = String -> Doc
text String
"-="
pretty (MultAssign {}) = String -> Doc
text String
"*="
pretty (DivAssign {}) = String -> Doc
text String
"/="
pretty (ModAssign {}) = String -> Doc
text String
"%="
pretty (PowAssign {}) = String -> Doc
text String
"**="
pretty (BinAndAssign {}) = String -> Doc
text String
"&="
pretty (BinOrAssign {}) = String -> Doc
text String
"|="
pretty (BinXorAssign {}) = String -> Doc
text String
"^="
pretty (LeftShiftAssign {}) = String -> Doc
text String
"<<="
pretty (RightShiftAssign {}) = String -> Doc
text String
">>="
pretty (FloorDivAssign {}) = String -> Doc
text String
"//="
pretty (MatrixMultAssign {}) = String -> Doc
text String
"@="