From 78df441e70ea13b44c8782792f9f1c8cc29ff007 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Sat, 28 Sep 2024 16:44:50 +0900 Subject: [PATCH] fix KwTrue and KwFalse --- build.sbt | 8 +++ .../scalameta_ast/TokensToString.scala | 70 +++++++++++++++++++ .../scalameta_ast/TokensToString.scala | 66 +++++++++++++++++ .../scala/scalameta_ast/ScalametaAST.scala | 62 +--------------- 4 files changed, 145 insertions(+), 61 deletions(-) create mode 100644 core/src/main/scala-latest/scalameta_ast/TokensToString.scala create mode 100644 core/src/main/scala-scalafix_compat-js/scalameta_ast/TokensToString.scala diff --git a/build.sbt b/build.sbt index 6110cb8..b2f2e46 100644 --- a/build.sbt +++ b/build.sbt @@ -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( @@ -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, @@ -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(), ) diff --git a/core/src/main/scala-latest/scalameta_ast/TokensToString.scala b/core/src/main/scala-latest/scalameta_ast/TokensToString.scala new file mode 100644 index 0000000..a917ba5 --- /dev/null +++ b/core/src/main/scala-latest/scalameta_ast/TokensToString.scala @@ -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(", ", ", ")") + } + +} diff --git a/core/src/main/scala-scalafix_compat-js/scalameta_ast/TokensToString.scala b/core/src/main/scala-scalafix_compat-js/scalameta_ast/TokensToString.scala new file mode 100644 index 0000000..d500dba --- /dev/null +++ b/core/src/main/scala-scalafix_compat-js/scalameta_ast/TokensToString.scala @@ -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(", ", ", ")") + } + +} diff --git a/core/src/main/scala/scalameta_ast/ScalametaAST.scala b/core/src/main/scala/scalameta_ast/ScalametaAST.scala index b4390e8..87ef6e3 100644 --- a/core/src/main/scala/scalameta_ast/ScalametaAST.scala +++ b/core/src/main/scala/scalameta_ast/ScalametaAST.scala @@ -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 @@ -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)) => @@ -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