Skip to content
Snippets Groups Projects
Commit d7926296 authored by Manos Koukoutos's avatar Manos Koukoutos
Browse files

Improve printer

parent c29501e8
No related branches found
No related tags found
No related merge requests found
...@@ -41,7 +41,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -41,7 +41,7 @@ class PrettyPrinter(opts: PrinterOptions,
} }
} }
protected def printWithPath(df: Definition)(implicit ctx: PrinterContext) { protected def printNameWithPath(df: Definition)(implicit ctx: PrinterContext) {
(opgm, ctx.parents.collectFirst { case (d: Definition) => d }) match { (opgm, ctx.parents.collectFirst { case (d: Definition) => d }) match {
case (Some(pgm), Some(scope)) => case (Some(pgm), Some(scope)) =>
sb.append(fullNameFrom(df, scope, opts.printUniqueIds)(pgm)) sb.append(fullNameFrom(df, scope, opts.printUniqueIds)(pgm))
...@@ -175,12 +175,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -175,12 +175,7 @@ class PrettyPrinter(opts: PrinterOptions,
} }
case CaseClassSelector(_, e, id) => p"$e.$id" case CaseClassSelector(_, e, id) => p"$e.$id"
case MethodInvocation(rec, _, tfd, args) => case MethodInvocation(rec, _, tfd, args) =>
p"$rec.${tfd.id}" p"$rec.${tfd.id}${nary(tfd.tps, ", ", "[", "]")}"
if (tfd.tps.nonEmpty) {
p"[${tfd.tps}]"
}
// No () for fields // No () for fields
if (tfd.fd.isRealFunction) { if (tfd.fd.isRealFunction) {
// The non-present arguments are synthetic function invocations // The non-present arguments are synthetic function invocations
...@@ -197,10 +192,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -197,10 +192,7 @@ class PrettyPrinter(opts: PrinterOptions,
case FcallMethodInvocation(rec, fd, id, tps, args) => case FcallMethodInvocation(rec, fd, id, tps, args) =>
p"$rec.$id" p"$rec.$id${nary(tps, " ,", "[", "]")}"
if (tps.nonEmpty) {
p"[$tps]"
}
if (fd.isRealFunction) { if (fd.isRealFunction) {
// The non-present arguments are synthetic function invocations // The non-present arguments are synthetic function invocations
...@@ -213,11 +205,9 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -213,11 +205,9 @@ class PrettyPrinter(opts: PrinterOptions,
} }
case FunctionInvocation(TypedFunDef(fd, tps), args) => case FunctionInvocation(TypedFunDef(fd, tps), args) =>
printWithPath(fd) printNameWithPath(fd)
if (tps.nonEmpty) { p"${nary(tps, " ,", "[", "]")}"
p"[$tps]"
}
if (fd.isRealFunction) { if (fd.isRealFunction) {
// The non-present arguments are synthetic function invocations // The non-present arguments are synthetic function invocations
...@@ -357,7 +347,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -357,7 +347,7 @@ class PrettyPrinter(opts: PrinterOptions,
case CaseClassPattern(ob, cct, subps) => case CaseClassPattern(ob, cct, subps) =>
ob.foreach { b => p"$b @ " } ob.foreach { b => p"$b @ " }
// Print only the classDef because we don't want type parameters in patterns // Print only the classDef because we don't want type parameters in patterns
printWithPath(cct.classDef) printNameWithPath(cct.classDef)
if (!cct.classDef.isCaseObject) p"($subps)" if (!cct.classDef.isCaseObject) p"($subps)"
case InstanceOfPattern(ob, cct) => case InstanceOfPattern(ob, cct) =>
...@@ -382,7 +372,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -382,7 +372,7 @@ class PrettyPrinter(opts: PrinterOptions,
mod <- p.modules.find( _.definedFunctions contains tfd.fd ) mod <- p.modules.find( _.definedFunctions contains tfd.fd )
} yield mod) match { } yield mod) match {
case Some(obj) => case Some(obj) =>
printWithPath(obj) printNameWithPath(obj)
case None => case None =>
p"<unknown object>" p"<unknown object>"
} }
...@@ -407,10 +397,8 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -407,10 +397,8 @@ class PrettyPrinter(opts: PrinterOptions,
case TupleType(tpes) => p"($tpes)" case TupleType(tpes) => p"($tpes)"
case FunctionType(fts, tt) => p"($fts) => $tt" case FunctionType(fts, tt) => p"($fts) => $tt"
case c: ClassType => case c: ClassType =>
printWithPath(c.classDef) printNameWithPath(c.classDef)
if (c.tps.nonEmpty) { p"${nary(c.tps, " ,", "[", "]")}"
p"[${c.tps}]"
}
// Definitions // Definitions
case Program(units) => case Program(units) =>
...@@ -439,11 +427,7 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -439,11 +427,7 @@ class PrettyPrinter(opts: PrinterOptions,
|}""" |}"""
case acd @ AbstractClassDef(id, tparams, parent) => case acd @ AbstractClassDef(id, tparams, parent) =>
p"abstract class $id" p"abstract class $id${nary(tparams, " ,", "[", "]")}"
if (tparams.nonEmpty) {
p"[$tparams]"
}
parent.foreach{ par => parent.foreach{ par =>
p" extends ${par.id}" p" extends ${par.id}"
...@@ -462,21 +446,15 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -462,21 +446,15 @@ class PrettyPrinter(opts: PrinterOptions,
p"case class $id" p"case class $id"
} }
if (tparams.nonEmpty) { p"${nary(tparams, " ,", "[", "]")}"
p"[$tparams]"
}
if (!isObj) { if (!isObj) {
p"(${ccd.fields})" p"(${ccd.fields})"
} }
parent.foreach{ par => parent.foreach { par =>
if (par.tps.nonEmpty){ // Remember child and parents tparams are simple bijection
// Remember child and parents tparams are simple bijection p" extends ${par.id}${nary(tparams, " ,", "[", "]")}"
p" extends ${par.id}[$tparams]"
} else {
p" extends ${par.id}"
}
} }
if (ccd.methods.nonEmpty) { if (ccd.methods.nonEmpty) {
...@@ -492,10 +470,8 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -492,10 +470,8 @@ class PrettyPrinter(opts: PrinterOptions,
p"val ${fd.id} : " p"val ${fd.id} : "
} else if (fd.canBeLazyField) { } else if (fd.canBeLazyField) {
p"lazy val ${fd.id} : " p"lazy val ${fd.id} : "
} else if (fd.tparams.nonEmpty) {
p"def ${fd.id}[${nary(fd.tparams, ",")}](${fd.params}): "
} else { } else {
p"def ${fd.id}(${fd.params}): " p"def ${fd.id}${nary(fd.tparams, ",", "[", "]")}(${fd.params}): "
} }
p"${fd.returnType} = ${fd.fullBody}" p"${fd.returnType} = ${fd.fullBody}"
...@@ -587,8 +563,6 @@ class PrettyPrinter(opts: PrinterOptions, ...@@ -587,8 +563,6 @@ class PrettyPrinter(opts: PrinterOptions,
protected def requiresBraces(ex: Tree, within: Option[Tree]) = (ex, within) match { protected def requiresBraces(ex: Tree, within: Option[Tree]) = (ex, within) match {
case (e: Expr, _) if isSimpleExpr(e) => case (e: Expr, _) if isSimpleExpr(e) =>
false false
case (_, None) =>
false
case (e: Expr, Some(within: Expr)) if noBracesSub(within) contains e => case (e: Expr, Some(within: Expr)) if noBracesSub(within) contains e =>
false false
case (e: Expr, Some(_)) => case (e: Expr, Some(_)) =>
......
...@@ -74,8 +74,9 @@ object PrinterHelpers { ...@@ -74,8 +74,9 @@ object PrinterHelpers {
} }
} }
def nary(ls: Seq[Any], sep: String = ", "): Printable = { def nary(ls: Seq[Any], sep: String = ", ", init: String = "", closing: String = ""): Printable = {
val strs = List("") ::: List.fill(ls.size-1)(sep) val (i, c) = if(ls.isEmpty) ("", "") else (init, closing)
val strs = i +: List.fill(ls.size-1)(sep) :+ c
implicit pctx: PrinterContext => implicit pctx: PrinterContext =>
new StringContext(strs: _*).p(ls: _*) new StringContext(strs: _*).p(ls: _*)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment