Skip to content

Commit

Permalink
LoggerOps: add methods to log without position
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 13, 2024
1 parent 2b06383 commit 7e46a26
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ object LoggerOps {
def log2(formatToken: FT): String = formatToken.toString
def log2(formatToken: Option[FT]): String = formatToken.fold("")(log2)

def ftokWithoutPos(ft: FT): String = {
val lt = ft.meta.left.text
val rt = ft.meta.right.text
val ls = tokWithoutPos(ft.left)
val rs = tokWithoutPos(ft.right)
val lo = treeName(ft.leftOwner)
val ro = treeName(ft.rightOwner)
s"[${ft.idx}] $lt $rt >>> $ls | $rs >>> $lo | $ro"
}

def escape(raw: String): String = raw

def log(tokens: T*): String = tokens.map(log).mkString("\n")
Expand All @@ -73,21 +83,28 @@ object LoggerOps {
def logTok(token: T): String = f"[${token.structure}%-40s"
def logTok(token: Option[T]): String = token.fold("")(log)

def tokWithoutPos(token: T): String = {
val desc = token.structure
val posidx = desc.lastIndexOf('[')
if (posidx > 0) desc.substring(0, posidx - 1) else desc
}

def log(range: InputRange): String = s"[${range.start}..${range.end})"

def position(t: Tree): String = log(t.pos)

def treeInfo(t: Tree): String = {
def treeName(t: Tree): String = {
val typeName = t.getClass.getName.stripPrefix("scala.meta.")
val parts = typeName.split('$')
val name = parts.length match {
parts.length match {
case 0 => typeName
case 1 => parts(0)
case _ => s"${parts(0)}.${parts(1)}"
}
s"$name ${position(t)}"
}

def treeInfo(t: Tree): String = s"${treeName(t)} ${position(t)}"

def log(t: Tree): String = log(t, false)
def log(t: Tree, tokensOnly: Boolean): String = {
val tokens = s"TOKENS: ${t.tokens.map(x => reveal(x.text)).mkString(",")}"
Expand Down Expand Up @@ -141,7 +158,7 @@ object LoggerOps {
var tokidx = 0
val toks = ftoks.arr
while (tokidx < toks.length) {
logger.debug(s"FT: ${log2(toks(tokidx))}")
logger.debug(s"FT: ${ftokWithoutPos(toks(tokidx))}")
routes(tokidx).foreach(s => logger.debug(s"> S: ${log(s)}"))
tokidx += 1
}
Expand Down

0 comments on commit 7e46a26

Please sign in to comment.