@@ -4,21 +4,14 @@ package core
44import effekt .core .Type .{PromptSymbol , ResumeSymbol }
55import effekt .source .FeatureFlag
66import kiama .output .ParenPrettyPrinter
7+ import kiama .output .PrettyPrinterTypes .Document
78
89import scala .language .implicitConversions
910import effekt .symbols .{Name , Wildcard , builtins }
1011
11- object PrettyPrinter extends ParenPrettyPrinter {
12-
13- import kiama .output .PrettyPrinterTypes .Document
14-
12+ class PrettyPrinter (humanReadable : Boolean ) extends ParenPrettyPrinter {
1513 override val defaultIndent = 2
1614
17- def formatHumanReadable (t : ModuleDecl ): Document = {
18- val renamer = effekt.core.TestRenamer (Names (builtins.coreBuiltins))
19- format(renamer(t))
20- }
21-
2215 def format (t : ModuleDecl ): Document =
2316 pretty(toDoc(t), 4 )
2417
@@ -57,9 +50,9 @@ object PrettyPrinter extends ParenPrettyPrinter {
5750 // The order of toplevel items must match the parser (where the order is currently fixed).
5851 val includes = vsep(m.includes.map { im => " import" <+> im })
5952 val decls = vsep(m.declarations.map(toDoc))
60- val externs = vsep(m.externs.map(toDoc))
53+ val externs = if humanReadable then emptyDoc else vsep(m.externs.map(toDoc))
6154 val defs = toDoc(m.definitions)
62- val exports = vsep(m.exports.map { id => " export" <+> toDoc(id) })
55+ val exports = if humanReadable then emptyDoc else vsep(m.exports.map { id => " export" <+> toDoc(id) })
6356
6457 " module" <+> m.path <>
6558 emptyline <>
@@ -102,7 +95,7 @@ object PrettyPrinter extends ParenPrettyPrinter {
10295
10396 def toDoc (b : Block , preventBraces : Boolean = false ): Doc = b match {
10497 case BlockVar (id, tpe, capt) =>
105- toDoc(id) <> " :" <+> toDoc(tpe) <+> " @" <+> toDoc(capt)
98+ toDoc(id) <> ( if humanReadable then emptyDoc else " :" <+> toDoc(tpe) <+> " @" <+> toDoc(capt) )
10699 case BlockLit (tps, cps, vps, bps, body) =>
107100 val doc = space <> paramsToDoc(tps, cps, vps, bps) <+> " =>" <+> nest(line <> toDocStmts(body)) <> line
108101 if preventBraces then doc else braces { doc }
@@ -124,7 +117,7 @@ object PrettyPrinter extends ParenPrettyPrinter {
124117 case Literal ((), _) => " ()"
125118 case Literal (s : String , _) => stringLiteral(s)
126119 case Literal (value, _) => value.toString
127- case ValueVar (id, tpe) => toDoc(id) <> " :" <+> toDoc(tpe)
120+ case ValueVar (id, tpe) => toDoc(id) <> ( if humanReadable then emptyDoc else " :" <+> toDoc(tpe) )
128121
129122 case PureApp (b, targs, vargs) => parens(toDoc(b)) <> argsToDoc(targs, vargs, Nil )
130123 case Make (data, tag, targs, vargs) => " make" <+> toDoc(data) <+> toDoc(tag) <> argsToDoc(targs, vargs, Nil )
@@ -346,3 +339,18 @@ object PrettyPrinter extends ParenPrettyPrinter {
346339 multi <> s <> multi
347340 }
348341}
342+
343+ /**
344+ * Instance of PrettyPrinter that produces output that can be parsed back by the core parser.
345+ */
346+ object ReparsablePrettyPrinter extends PrettyPrinter (false ) {}
347+
348+ /**
349+ * Instance of PrettyPrinter that produces less verbose, more human-readable output.
350+ */
351+ object HumanReadablePrettyPrinter extends PrettyPrinter (true ) {
352+ def renameAndFormat (t : ModuleDecl ): Document = {
353+ val renamer = effekt.core.TestRenamer (Names (builtins.coreBuiltins))
354+ format(renamer(t))
355+ }
356+ }
0 commit comments