Skip to content

Commit

Permalink
fix KwTrue and KwFalse
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Sep 28, 2024
1 parent 36e18f3 commit 78df441
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 61 deletions.
8 changes: 8 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ lazy val commonSettings = Def.settings(

commonSettings

lazy val commonLatest = Def.settings(
Compile / sources ++= {
(((Compile / sourceDirectory).value / "scala-latest") ** "*.scala").get
}
)

lazy val `scalameta-ast` = projectMatrix
.in(file("core"))
.settings(
Expand All @@ -39,6 +45,7 @@ lazy val `scalameta-ast` = projectMatrix
settings = Def.settings(
metaVersion := (LocalProject("scalameta-ast-latestJS") / metaVersion).value,
testBuildInfo,
commonLatest,
libraryDependencies += "org.scalameta" %%% "scalameta" % "4.10.1",
libraryDependencies += "org.scalameta" %%% "scalafmt-core" % "3.8.3",
libraryDependencies += "com.google.inject" % "guice" % "7.0.0" % Test,
Expand Down Expand Up @@ -68,6 +75,7 @@ lazy val `scalameta-ast` = projectMatrix
axisValues = Seq(metaLatest),
settings = Def.settings(
jsProjectSettings,
commonLatest,
libraryDependencies += "org.ekrich" %%% "sconfig" % "1.7.0",
libraryDependencies += ("com.github.xuwei-k" %%% "scalafmt-core" % "3.8.3-fork-1").withSources(),
)
Expand Down
70 changes: 70 additions & 0 deletions core/src/main/scala-latest/scalameta_ast/TokensToString.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package scalameta_ast

import scala.meta.Tokens
import scala.meta.tokens.Token

object TokensToString {
def tokensToString(tokens: Tokens): String = {
tokens.tokens.map { x =>
val n = x.getClass.getSimpleName
def q(a: String): String = s"(${scala.meta.Lit.String(a)})"

PartialFunction
.condOpt(x) {
case y: Token.Ident =>
s"Ident${q(y.value)}"
case y: Token.Comment =>
s"Comment${q(y.value)}"
case y: Token.Interpolation.Id =>
s"Interpolation.Id${q(y.value)}"
case y: Token.Interpolation.Part =>
s"Interpolation.Part${q(y.value)}"
case _: Token.Interpolation.Start =>
s"Interpolation.$n"
case _: Token.Interpolation.SpliceStart =>
s"Interpolation.$n"
case _: Token.Interpolation.SpliceEnd =>
s"Interpolation.$n"
case _: Token.Interpolation.End =>
s"Interpolation.$n"
case y: Token.Constant[?] =>
y match {
case z: Token.Constant.Int =>
s"Constant.Int(BigInt${q(z.value.toString)})"
case z: Token.Constant.Long =>
s"Constant.Long(BigInt${q(z.value.toString)})"
case z: Token.Constant.Float =>
s"Constant.Float(BigDecimal${q(z.value.toString)})"
case z: Token.Constant.Double =>
s"Constant.Double(BigDecimal${q(z.value.toString)})"
case z: Token.Constant.Char =>
s"Constant.Char('${z.value}')"
case z: Token.Constant.Symbol =>
s"Constant.Symbol(scala.Symbol${q(z.value.name)})"
case z: Token.Constant.String =>
s"Constant.String${q(z.value)}"
case _: Token.KwTrue =>
"Token.KwTrue"
case _: Token.KwFalse =>
"Token.KwFalse"
}
case _: Token.Xml.Start =>
s"Xml.${n}"
case y: Token.Xml.Part =>
s"Xml.Part${q(y.value)}"
case _: Token.Xml.SpliceStart =>
s"Xml.${n}"
case _: Token.Xml.SpliceEnd =>
s"Xml.${n}"
case _: Token.Xml.End =>
s"Xml.${n}"
case _: Token.Indentation.Indent =>
s"Indentation.${n}"
case _: Token.Indentation.Outdent =>
s"Indentation.${n}"
}
.getOrElse(n)
}.map("Token." + _).mkString("Seq(", ", ", ")")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package scalameta_ast

import scala.meta.Tokens
import scala.meta.tokens.Token

object TokensToString {
def tokensToString(tokens: Tokens): String = {
tokens.tokens.map { x =>
val n = x.getClass.getSimpleName
def q(a: String): String = s"(${scala.meta.Lit.String(a)})"

PartialFunction
.condOpt(x) {
case y: Token.Ident =>
s"Ident${q(y.value)}"
case y: Token.Comment =>
s"Comment${q(y.value)}"
case y: Token.Interpolation.Id =>
s"Interpolation.Id${q(y.value)}"
case y: Token.Interpolation.Part =>
s"Interpolation.Part${q(y.value)}"
case _: Token.Interpolation.Start =>
s"Interpolation.$n"
case _: Token.Interpolation.SpliceStart =>
s"Interpolation.$n"
case _: Token.Interpolation.SpliceEnd =>
s"Interpolation.$n"
case _: Token.Interpolation.End =>
s"Interpolation.$n"
case y: Token.Constant[?] =>
y match {
case z: Token.Constant.Int =>
s"Constant.Int(BigInt${q(z.value.toString)})"
case z: Token.Constant.Long =>
s"Constant.Long(BigInt${q(z.value.toString)})"
case z: Token.Constant.Float =>
s"Constant.Float(BigDecimal${q(z.value.toString)})"
case z: Token.Constant.Double =>
s"Constant.Double(BigDecimal${q(z.value.toString)})"
case z: Token.Constant.Char =>
s"Constant.Char('${z.value}')"
case z: Token.Constant.Symbol =>
s"Constant.Symbol(scala.Symbol${q(z.value.name)})"
case z: Token.Constant.String =>
s"Constant.String${q(z.value)}"
}
case _: Token.Xml.Start =>
s"Xml.${n}"
case y: Token.Xml.Part =>
s"Xml.Part${q(y.value)}"
case _: Token.Xml.SpliceStart =>
s"Xml.${n}"
case _: Token.Xml.SpliceEnd =>
s"Xml.${n}"
case _: Token.Xml.End =>
s"Xml.${n}"
case _: Token.Indentation.Indent =>
s"Indentation.${n}"
case _: Token.Indentation.Outdent =>
s"Indentation.${n}"
}
.getOrElse(n)
}.map("Token." + _).mkString("Seq(", ", ", ")")
}

}
62 changes: 1 addition & 61 deletions core/src/main/scala/scalameta_ast/ScalametaAST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import java.util.Date
import scala.annotation.tailrec
import scala.meta._
import scala.meta.common.Convert
import scala.meta.tokens.Token
import scala.meta.parsers.Parse
import scala.meta.parsers.Parsed
import scala.meta.tokenizers.Tokenize
Expand Down Expand Up @@ -95,65 +94,6 @@ class ScalametaAST {
implicitly[Parse[Term]].apply(Input.String(str), dialects.Scala3).toOption.exists(_.is[Term.Name])
}

private def tokensToString(tokens: Tokens): String = {
tokens.tokens.map { x =>
val n = x.getClass.getSimpleName
def q(a: String): String = s"(${scala.meta.Lit.String(a)})"

PartialFunction
.condOpt(x) {
case y: Token.Ident =>
s"Ident${q(y.value)}"
case y: Token.Comment =>
s"Comment${q(y.value)}"
case y: Token.Interpolation.Id =>
s"Interpolation.Id${q(y.value)}"
case y: Token.Interpolation.Part =>
s"Interpolation.Part${q(y.value)}"
case _: Token.Interpolation.Start =>
s"Interpolation.$n"
case _: Token.Interpolation.SpliceStart =>
s"Interpolation.$n"
case _: Token.Interpolation.SpliceEnd =>
s"Interpolation.$n"
case _: Token.Interpolation.End =>
s"Interpolation.$n"
case y: Token.Constant[?] =>
y match {
case z: Token.Constant.Int =>
s"Constant.Int(BigInt${q(z.value.toString)})"
case z: Token.Constant.Long =>
s"Constant.Long(BigInt${q(z.value.toString)})"
case z: Token.Constant.Float =>
s"Constant.Float(BigDecimal${q(z.value.toString)})"
case z: Token.Constant.Double =>
s"Constant.Double(BigDecimal${q(z.value.toString)})"
case z: Token.Constant.Char =>
s"Constant.Char('${z.value}')"
case z: Token.Constant.Symbol =>
s"Constant.Symbol(scala.Symbol${q(z.value.name)})"
case z: Token.Constant.String =>
s"Constant.String${q(z.value)}"
}
case _: Token.Xml.Start =>
s"Xml.${n}"
case y: Token.Xml.Part =>
s"Xml.Part${q(y.value)}"
case _: Token.Xml.SpliceStart =>
s"Xml.${n}"
case _: Token.Xml.SpliceEnd =>
s"Xml.${n}"
case _: Token.Xml.End =>
s"Xml.${n}"
case _: Token.Indentation.Indent =>
s"Indentation.${n}"
case _: Token.Indentation.Outdent =>
s"Indentation.${n}"
}
.getOrElse(n)
}.map("Token." + _).mkString("Seq(", ", ", ")")
}

private def addExtractor(parsed: Term, str: String, extractor: String => String): String = {
val values = parsed.collect {
case t @ Term.Select(Term.Name(x1), Term.Name(x2)) =>
Expand Down Expand Up @@ -280,7 +220,7 @@ class ScalametaAST {
implicitly[Tokenize].apply(input, d).get
}
}
tokensToString(loop(input, dialects)) -> None
TokensToString.tokensToString(loop(input, dialects)) -> None

case _: Args.Comment =>
scala.meta.contrib.CommentOps
Expand Down

0 comments on commit 78df441

Please sign in to comment.