This GitLab instance reached the end of its service life. It won't be possible to create new users or projects.

Please read the deprecation notice for more information concerning the deprecation timeline

Visit migration.git.tu-berlin.de (internal network only) to import your old projects to the new GitLab platform 📥

PrettyPrinter.scala 1.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
package de.bbisping.coupledsim.ts

import Syntax._

class PrettyPrinter {
  
  def showAttribute(kv: (String, String)): String = kv match {
    case (k, v) => if (v.isEmpty) {
      k
    } else {
      k + "=" + v
    }
  }
  
  def show(e: Expression): String = e match {
    case MetaDeclaration(k, v, p0) =>
      "@" + k + (if (v != "") " \"" + v + "\" " else "")
    case SingleNode(n, p0) =>
      n
    case NodeDeclaration(n, aa, p0) =>
      if (aa.isEmpty) {
        n
      } else {
        n + aa.map(showAttribute).mkString("(", ", ", ")")
      }
    case StepTo(e1, l, e2, p0) if l.name.isEmpty() =>
      show(e1) + " --> " + show(e2)
    case StepTo(e1, l, e2, p0) =>
      show(e1) + " |-" + show(l) + "-> " + show(e2)
    case Label(l, p0) =>
      l
    case d: Definition =>
      showDefinition(d)
  }
  
  def showDefinition(d: Definition) = {
    val lineOffsets = 0 :: d.defs sliding 2 map {
      case List(a: Expression, b: Expression) =>
        (b.position.line - a.position.getEndLine) max 1
      case _ => 0
    } toList
    
    d.defs.map { e =>
      " " * e.position.col + show(e)
    } zip lineOffsets map { case (l, o) =>
      "\n"*o + l
    } mkString
  }
  
  def vsep(strs: Iterable[String]) = 
    strs.mkString("\n")

}