Repository: scalameta/scalameta
Pull request #3489 (2024-01-13 23:26:46)

ScalametaParser: keep Lit.{Double,Float} syntax


Patch:
diff --git a/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala b/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
index d2fa41db1..dcf6d93ac 100644
--- a/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
+++ b/scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
@@ -1330,16 +1330,20 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>
         if (value > max) syntaxError("integer number too large", at = token)
         else if (value < min) syntaxError("integer number too small", at = token)
         Lit.Long(value.toLong)
-      case Constant.Float(rawValue) =>
-        val value = if (isNegated) -rawValue else rawValue
-        if (value > Float.MaxValue) syntaxError("floating point number too large", at = token)
-        else if (value < Float.MinValue) syntaxError("floating point number too small", at = token)
-        Lit.Float(value.toString)
-      case Constant.Double(rawValue) =>
-        val value = if (isNegated) -rawValue else rawValue
-        if (value > Double.MaxValue) syntaxError("floating point number too large", at = token)
-        else if (value < Double.MinValue) syntaxError("floating point number too small", at = token)
-        Lit.Double(value.toString)
+      case tok @ Constant.Float(rawValue) =>
+        if (rawValue > Float.MaxValue)
+          syntaxError("floating point number too large", at = token)
+        else if (rawValue < Float.MinValue)
+          syntaxError("floating point number too small", at = token)
+        val value = tok.text
+        Lit.Float(if (isNegated) '-' + value else value)
+      case tok @ Constant.Double(rawValue) =>
+        if (rawValue > Double.MaxValue)
+          syntaxError("floating point number too large", at = token)
+        else if (rawValue < Double.MinValue)
+          syntaxError("floating point number too small", at = token)
+        val value = tok.text
+        Lit.Double(if (isNegated) '-' + value else value)
       case Constant.Char(value) =>
         Lit.Char(value)
       case Constant.String(value) =>


Test patch:
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/Scala213Suite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/Scala213Suite.scala
index 47a11fe14..f6c9f07cc 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/Scala213Suite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/Scala213Suite.scala
@@ -31,7 +31,7 @@ class Scala213Suite extends ParseSuite {
         Term.Name("foo"),
         Nil,
         List(List(Term.Param(Nil, Term.Name("a"), Some(Lit.Float("3.14159f")), None))),
-        Lit.Double("0.1d")
+        Lit.Double(".1d")
       )
     )
 
diff --git a/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/SyntacticSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/SyntacticSuite.scala
index 98cb2178d..5823eefcd 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/SyntacticSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/SyntacticSuite.scala
@@ -405,16 +405,16 @@ class SyntacticSuite extends scala.meta.tests.parsers.ParseSuite {
     assertEquals(templStat("0l").syntax, "0L")
     assertEquals(templStat("0L").syntax, "0L")
     assertEquals(templStat("0f").syntax, "0f")
-    assertEquals(templStat("0F").syntax, "0f")
+    assertEquals(templStat("0F").syntax, "0F")
     assertEquals(templStat("0.0f").syntax, "0.0f")
-    assertEquals(templStat("0.0F").syntax, "0.0f")
+    assertEquals(templStat("0.0F").syntax, "0.0F")
     assertEquals(templStat("1.4f").syntax, "1.4f")
     assertEquals(templStat("1.40f").syntax, "1.40f")
     assertEquals(templStat("0.0").syntax, "0.0d")
     assertEquals(templStat("0d").syntax, "0d")
-    assertEquals(templStat("0D").syntax, "0d")
+    assertEquals(templStat("0D").syntax, "0D")
     assertEquals(templStat("0.0d").syntax, "0.0d")
-    assertEquals(templStat("0.0D").syntax, "0.0d")
+    assertEquals(templStat("0.0D").syntax, "0.0D")
     assertEquals(templStat("'0'").syntax, "'0'")
     assertEquals(templStat("\"0\"").syntax, "\"0\"")
     assertEquals(templStat("'zero").syntax, "'zero")