packages feed

rtk-0.12: test/golden/block/BlockPP.hs

-- Generated by RTK from grammar 'Block'. Do not edit by hand.
-- Block-layout pretty-printer (task 9b): indentation and line breaks for
-- bracket-structured languages. Layout comes structurally from block lists
-- (statement/declaration lists), so it adds no parentheses and no charset is
-- baked in. The only guarantee is the semantic round-trip parse (print ast)
-- == ast; comments and the original whitespace are not recovered (lossy).
module BlockPP where
import BlockParser
import Data.List (intercalate)

data PpItem = PpTok String | PpOpen | PpClose | PpBreak
ppRender :: [PpItem] -> String
ppRender = start
  where
    start []            = ""
    start (PpTok s : r) = s ++ inLine 0 r
    start (PpOpen  : r) = fresh 1 r
    start (PpClose : r) = fresh 0 r
    start (PpBreak : r) = fresh 0 r
    inLine _ []            = ""
    inLine n (PpTok s : r) = ' ' : s ++ inLine n r
    inLine n (PpOpen  : r) = fresh (n + 1) r
    inLine n (PpClose : r) = fresh (max 0 (n - 1)) r
    inLine n (PpBreak : r) = fresh n r
    fresh _ []            = ""
    fresh n (PpTok s : r) = '\n' : (replicate (n * 2) ' ' ++ s) ++ inLine n r
    fresh n (PpOpen  : r) = fresh (n + 1) r
    fresh n (PpClose : r) = fresh (max 0 (n - 1)) r
    fresh n (PpBreak : r) = fresh n r

ppProgram :: Program -> String
ppProgram = ppRender . layProgram
layProgram :: Program -> [PpItem]
layProgram (Ctr__Program__0 _ x1) = concat [[PpTok "tok_Program_dummy_9"], layProgram x1, [PpTok "tok_Program_dummy_9"]]
layProgram (Ctr__Program__1 _ x1) = concat [[PpTok "tok_Block_dummy_8"], layBlock x1, [PpTok "tok_Block_dummy_8"]]
layProgram (Ctr__Program__2 _ x1) = concat [[PpTok "tok_Exp_dummy_7"], layExp x1, [PpTok "tok_Exp_dummy_7"]]
layProgram (Ctr__Program__3 _ x1) = concat [[PpTok "tok_Function_dummy_6"], layFunction x1, [PpTok "tok_Function_dummy_6"]]
layProgram (Ctr__Program__4 _ x1) = concat [[PpTok "tok_Ident_dummy_5"], layIdent x1, [PpTok "tok_Ident_dummy_5"]]
layProgram (Ctr__Program__5 _ x1) = concat [[PpTok "tok_ParamList_dummy_4"], layParamList x1, [PpTok "tok_ParamList_dummy_4"]]
layProgram (Ctr__Program__6 _ x1) = concat [[PpTok "tok_Statement_dummy_3"], layStatement x1, [PpTok "tok_Statement_dummy_3"]]
layProgram (Ctr__Program__7 _ x1) = concat [[PpTok "tok_StatementList_dummy_2"], layStatementList x1, [PpTok "tok_StatementList_dummy_2"]]
layProgram (Anti_Program x1) = concat [[PpTok x1]]
layProgram (Prog _ x1) = concat [layFunction x1]

ppBlock :: Block -> String
ppBlock = ppRender . layBlock
layBlock :: Block -> [PpItem]
layBlock (Anti_Block x1) = concat [[PpTok x1]]
layBlock (Blk _ x1) = concat [[PpTok "{"], layStatementList x1, [PpTok "}"]]

ppExp :: Exp -> String
ppExp = ppRender . layExp
layExp :: Exp -> [PpItem]
layExp (Anti_Exp x1) = concat [[PpTok x1]]
layExp (Var _ x1) = concat [layIdent x1]
layExp (Num _ x1) = concat [[PpTok (show x1)]]

ppFunction :: Function -> String
ppFunction = ppRender . layFunction
layFunction :: Function -> [PpItem]
layFunction (Anti_Function x1) = concat [[PpTok x1]]
layFunction (Func _ x1 x2 x3) = concat [[PpTok "fn"], layIdent x1, [PpTok "("], layParamList x2, [PpTok ")"], layBlock x3]

ppIdent :: Ident -> String
ppIdent = ppRender . layIdent
layIdent :: Ident -> [PpItem]
layIdent (Anti_Ident x1) = concat [[PpTok x1]]
layIdent (Name _ x1) = concat [[PpTok x1]]

ppParamList :: ParamList -> String
ppParamList = ppRender . layParamList
layParamList :: ParamList -> [PpItem]
layParamList xs = intercalate [PpTok ","] (map layIdent xs)

ppStatement :: Statement -> String
ppStatement = ppRender . layStatement
layStatement :: Statement -> [PpItem]
layStatement (Anti_Statement x1) = concat [[PpTok x1]]
layStatement (Assign _ x1 x2) = concat [layIdent x1, [PpTok "="], layExp x2, [PpTok ";"]]
layStatement (Return _ x1) = concat [[PpTok "return"], layExp x1, [PpTok ";"]]
layStatement (Nested _ x1) = concat [layBlock x1]

ppStatementList :: StatementList -> String
ppStatementList = ppRender . layStatementList
layStatementList :: StatementList -> [PpItem]
layStatementList xs = if null xs then [] else [PpOpen] ++ intercalate [PpBreak] (map layStatement xs) ++ [PpClose]