Repository: scalameta/scalameta
Pull request #3660 (2024-03-17 04:56:05)

ScalametaParser: quote tokens in syntax error


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 6bb165011..cb0597a30 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
@@ -4880,9 +4880,9 @@ object ScalametaParser {
       case other => other.toLowerCase().stripPrefix("kw")
     }
   private def syntaxExpectedMessage[T <: Token: ClassTag](token: Token): String =
-    s"${getTokenName[T]} expected but ${token.name} found"
+    s"`${getTokenName[T]}` expected but `${token.name}` found"
   private def syntaxNotExpectedMessage[T <: Token: ClassTag]: String =
-    s"not expected ${getTokenName[T]}"
+    s"not expected `${getTokenName[T]}`"
 
 }
 


Test patch:
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/ModSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/ModSuite.scala
index 971d8e2df..1f5d2e5a4 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/ModSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/ModSuite.scala
@@ -540,7 +540,7 @@ class ModSuite extends ParseSuite {
 
   test("covariant-like in type") {
     val error =
-      """|<input>:1: error: ] expected but identifier found
+      """|<input>:1: error: `]` expected but `identifier` found
          |type A[`+`T] = B[T]
          |          ^""".stripMargin
     runTestError[Stat]("type A[`+`T] = B[T]", error)
@@ -586,7 +586,7 @@ class ModSuite extends ParseSuite {
 
   test("contravariant-like in class") {
     val error =
-      """|<input>:1: error: ] expected but identifier found
+      """|<input>:1: error: `]` expected but `identifier` found
          |class A[`-`T](t: T)
          |           ^""".stripMargin
     runTestError[Stat]("class A[`-`T](t: T)", error)
@@ -902,7 +902,7 @@ class ModSuite extends ParseSuite {
   test("missing val after parameter modifier") {
     val actual = interceptParseError("class A(implicit b: B, implicit c: C)")
     val expected =
-      s"""|error: val expected but identifier found
+      s"""|error: `val` expected but `identifier` found
           |class A(implicit b: B, implicit c: C)
           |                                ^""".stripMargin
     assert(actual.contains(expected), actual)
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/TermSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/TermSuite.scala
index 501578025..f38e11f7e 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/TermSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/TermSuite.scala
@@ -915,7 +915,7 @@ class TermSuite extends ParseSuite {
 
   test("x = (ys: _`*`)") {
     val error =
-      """|<input>:1: error: identifier expected but ) found
+      """|<input>:1: error: `identifier` expected but `)` found
          |x = (ys: _`*`)
          |             ^""".stripMargin
     runTestError[Stat]("x = (ys: _`*`)", error)
@@ -1451,7 +1451,7 @@ class TermSuite extends ParseSuite {
     runTestError[Term](
       """|() match
          |  case _: Unit => ()""".stripMargin,
-      "error: { expected but case found"
+      "error: `{` expected but `case` found"
     )
 
   }
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/UnclosedTokenSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/UnclosedTokenSuite.scala
index 9972f5ffe..43dc9c50e 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/UnclosedTokenSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/UnclosedTokenSuite.scala
@@ -7,30 +7,43 @@ import scala.meta.parsers.ParseException
 
 class UnclosedTokenSuite extends ParseSuite {
   test("unclosed-string-1") {
-    val e = intercept[TokenizeException] {
+    interceptMessage[TokenizeException](
+      """|<input>:1: error: unclosed string interpolation
+         | s"start   
+         |   ^""".stripMargin.replace("\n", EOL)
+    ) {
       stat(""" s"start   """)
     }
-    assert(e.getMessage.contains("unclosed string interpolation"))
   }
 
   test("unclosed-string-2") {
-    val e = intercept[TokenizeException] {
+    interceptMessage[TokenizeException](
+      """|<input>:1: error: unclosed string literal
+         | x"${1 + " 
+         |         ^""".stripMargin.replace("\n", EOL)
+    ) {
       stat(""" x"${1 + " """)
     }
-    assert(e.getMessage.contains("unclosed string literal"))
   }
 
   test("unclosed-escape") {
-    val e = intercept[TokenizeException] {
+    interceptMessage[TokenizeException](
+      """|<input>:1: error: unclosed string literal
+         | "start \" 
+         | ^""".stripMargin.replace("\n", EOL)
+    ) {
       stat(""" "start \" """)
     }
   }
 
   test("unclosed-interpolation") {
-    val e = intercept[ParseException] {
+    interceptMessage[ParseException](
+      """|<input>:1: error: `}` expected but `end of file` found
+         | s"${1+ 
+         |        ^""".stripMargin.replace("\n", EOL)
+    ) {
       stat(""" s"${1+ """)
     }
-    assert(e.getMessage.contains("expected but end of file found"))
   }
 
 }
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/VarargParameterSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/VarargParameterSuite.scala
index e58ac7b0a..bf17f4f98 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/VarargParameterSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/VarargParameterSuite.scala
@@ -76,7 +76,7 @@ class VarargParameterSuite extends ParseSuite {
   test("error on return type vararg parameters") {
     checkError(
       "def obj(f: Int*): Boolean* = true",
-      "error: = expected but identifier found"
+      "error: `=` expected but `identifier` found"
     )
   }
 
@@ -90,11 +90,11 @@ class VarargParameterSuite extends ParseSuite {
   test("error on repeated byname parameter") {
     checkError(
       "def fx(x: => Int*): Int = 3",
-      ") expected but identifier found"
+      "`)` expected but `identifier` found"
     )
     checkError(
       "class Foo(bars: => Int*)",
-      ") expected but identifier found"
+      "`)` expected but `identifier` found"
     )
   }
 
@@ -108,11 +108,11 @@ class VarargParameterSuite extends ParseSuite {
   test("vararg-like parameters") {
     checkError(
       "def obj(fa: Int, fb: Int`*`) = true",
-      "error: identifier expected but ) found"
+      "error: `identifier` expected but `)` found"
     )
     checkError(
       "def obj(fa: Int`*`, fb: Int) = true",
-      "error: identifier expected but , found"
+      "error: `identifier` expected but `,` found"
     )
   }
 
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/ControlSyntaxSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/ControlSyntaxSuite.scala
index 86ca19874..e8b3b9edb 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/ControlSyntaxSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/ControlSyntaxSuite.scala
@@ -162,7 +162,7 @@ class ControlSyntaxSuite extends BaseDottySuite {
          |""".stripMargin
     runTestError[Stat](
       code,
-      """|error: ; expected but integer constant found
+      """|error: `;` expected but `integer constant` found
          |  if (x > 0) && y > 0
          |                    ^""".stripMargin
     )
@@ -268,7 +268,7 @@ class ControlSyntaxSuite extends BaseDottySuite {
                   |else
                   |  gx
                   |""".stripMargin
-    runTestError[Stat](code, "then expected but identifier found")
+    runTestError[Stat](code, "`then` expected but `identifier` found")
   }
 
   test("if-else-in-parens-3") {
@@ -827,7 +827,7 @@ class ControlSyntaxSuite extends BaseDottySuite {
          |finally arena.close()
          |""".stripMargin
     val error =
-      """|<input>:5: error: ; expected but finally found
+      """|<input>:5: error: `;` expected but `finally` found
          |finally arena.close()
          |^""".stripMargin
     runTestError[Stat](code, error)
@@ -1622,7 +1622,7 @@ class ControlSyntaxSuite extends BaseDottySuite {
          |""".stripMargin
     runTestError[Stat](
       code,
-      """|error: ; expected but integer constant found
+      """|error: `;` expected but `integer constant` found
          |  while (x > 0) && y > 0
          |                       ^""".stripMargin
     )
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/ExtensionMethodsSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/ExtensionMethodsSuite.scala
index 9e59bf304..0bafe5172 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/ExtensionMethodsSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/ExtensionMethodsSuite.scala
@@ -249,7 +249,7 @@ class ExtensionMethodsSuite extends BaseDottySuite {
          |  def extension(a : Int) = a + 2
          |  extension(2)
          |}""".stripMargin,
-      "identifier expected but integer constant found"
+      "`identifier` expected but `integer constant` found"
     )
 
     runTestAssert[Stat](
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/FewerBracesSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/FewerBracesSuite.scala
index 9b9d193aa..b1796685e 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/FewerBracesSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/FewerBracesSuite.scala
@@ -621,7 +621,7 @@ class FewerBracesSuite extends BaseDottySuite {
          |      y > 0
          |    (0)
          |""".stripMargin,
-      "error: ; expected but . found"
+      "error: `;` expected but `.` found"
     )
   }
 
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/InterleavedDeclSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/InterleavedDeclSuite.scala
index 329cb5873..0b1dad7e9 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/InterleavedDeclSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/InterleavedDeclSuite.scala
@@ -91,7 +91,7 @@ class InterleavedDeclSuite extends BaseDottySuite {
   test("def f[A][B]: A") {
     runTestError[Stat](
       "def f[A][B]: A",
-      """|error: = expected but [ found
+      """|error: `=` expected but `[` found
          |def f[A][B]: A
          |        ^""".stripMargin
     )
@@ -127,7 +127,7 @@ class InterleavedDeclSuite extends BaseDottySuite {
   test("def f[A](implicit a: A)[B](implicit b: B): B") {
     runTestError[Stat](
       "def f[A](implicit a: A)[B](implicit b: B): B",
-      """|error: = expected but [ found
+      """|error: `=` expected but `[` found
          |def f[A](implicit a: A)[B](implicit b: B): B
          |                       ^""".stripMargin
     )
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/InterleavedDefnSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/InterleavedDefnSuite.scala
index 448fd5f21..50cc6373d 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/InterleavedDefnSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/InterleavedDefnSuite.scala
@@ -137,7 +137,7 @@ class InterleavedDefnSuite extends BaseDottySuite {
   test("def f[A][B]: A = ???") {
     runTestError[Stat](
       "def f[A][B]: A = ???",
-      """|error: = expected but [ found
+      """|error: `=` expected but `[` found
          |def f[A][B]: A = ???
          |        ^""".stripMargin
     )
@@ -177,7 +177,7 @@ class InterleavedDefnSuite extends BaseDottySuite {
   test("def f[A](implicit a: A)[B](implicit b: B): B = ???") {
     runTestError[Stat](
       "def f[A](implicit a: A)[B](implicit b: B): B = ???",
-      """|error: = expected but [ found
+      """|error: `=` expected but `[` found
          |def f[A](implicit a: A)[B](implicit b: B): B = ???
          |                       ^""".stripMargin
     )
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/MinorDottySuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/MinorDottySuite.scala
index a1fa3e4ea..3b3abd862 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/MinorDottySuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/MinorDottySuite.scala
@@ -386,7 +386,7 @@ class MinorDottySuite extends BaseDottySuite {
 
   // Super traits were removed in Scala 3
   test("super-trait") {
-    runTestError[Stat]("super trait Foo", ". expected but trait found")
+    runTestError[Stat]("super trait Foo", "`.` expected but `trait` found")
   }
 
   test("question-type") {
@@ -407,7 +407,7 @@ class MinorDottySuite extends BaseDottySuite {
       Decl.Val(Nil, List(Pat.Var(tname("stat"))), Type.Apply(pname("Tree"), List(pname("?"))))
     runTestAssert[Stat]("val stat: Tree[`?`]")(treeWithoutBounds)
     val errorWithBounds =
-      """|<input>:1: error: ] expected but >: found
+      """|<input>:1: error: `]` expected but `>:` found
          |val stat: Tree[`?` >: Untyped]
          |                   ^""".stripMargin
     runTestError[Stat]("val stat: Tree[`?` >: Untyped]", errorWithBounds)
@@ -460,7 +460,10 @@ class MinorDottySuite extends BaseDottySuite {
     runTestError[Stat]("class F[_]", "identifier expected")
     runTestError[Stat]("enum X[T]{ case A[_] extends X[Int] }", "identifier expected")
     runTestError[Stat]("extension [_](x: Int) def inc: Int = x + 1", "identifier expected")
-    runTestError[Stat]("given [_](using Ord[T]): Ord[List[T]]{}", "identifier expected")
+    runTestError[Stat](
+      "given [_](using Ord[T]): Ord[List[T]]{}",
+      "`identifier` expected but `[` found"
+    )
   }
 
   test("repeated-byname-class-parameter") {
@@ -488,7 +491,7 @@ class MinorDottySuite extends BaseDottySuite {
 
   test("repeated-like-class-parameter") {
     val error =
-      """|<input>:1: error: identifier expected but ) found
+      """|<input>:1: error: `identifier` expected but `)` found
          |class Foo(bars: Int`*`)
          |                      ^""".stripMargin
     runTestError[Stat]("class Foo(bars: Int`*`)", error)
@@ -554,7 +557,7 @@ class MinorDottySuite extends BaseDottySuite {
 
     runTestError[Stat](
       "{ inline @foo def foo(): Int }",
-      "; expected but @ found"
+      "`;` expected but `@` found"
     )
   }
 
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/NewFunctionsSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/NewFunctionsSuite.scala
index a64ac931b..8bdceace1 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/NewFunctionsSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/NewFunctionsSuite.scala
@@ -618,7 +618,7 @@ class NewFunctionsSuite extends BaseDottySuite {
          |  (e: Entry) 
          |=> e.Key
          |""".stripMargin,
-      """|error: ; expected but => found
+      """|error: `;` expected but `=>` found
          |=> e.Key
          |^""".stripMargin
     )
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/SignificantIndentationSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/SignificantIndentationSuite.scala
index 4dfe20ebb..34ba09d6c 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/SignificantIndentationSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/SignificantIndentationSuite.scala
@@ -231,7 +231,7 @@ class SignificantIndentationSuite extends BaseDottySuite {
                   |    else
                   |  falsep
                   |""".stripMargin
-    runTestError[Stat](code, "expected but else found")
+    runTestError[Stat](code, "`;` expected but `else` found")
   }
 
   test("indent-inside-brace-ok") {
@@ -812,7 +812,7 @@ class SignificantIndentationSuite extends BaseDottySuite {
          |  def fa: Int = 1
          |  def fb: Int = 2
          |""".stripMargin,
-      "; expected but : found"
+      "`;` expected but `:` found"
     )
 
     runTestAssert[Stat](
diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/TypeSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/TypeSuite.scala
index 9796622b3..8f2077ade 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/TypeSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/TypeSuite.scala
@@ -149,7 +149,7 @@ class TypeSuite extends BaseDottySuite {
          | with
          |    type D <: Product
          |""".stripMargin,
-      "error: ; expected but with found"
+      "error: `;` expected but `with` found"
     )
   }
 
@@ -439,13 +439,13 @@ class TypeSuite extends BaseDottySuite {
     }
     runTestError[Stat](
       "F[`+`_]",
-      """|<input>:1: error: ] expected but _ found
+      """|<input>:1: error: `]` expected but `_` found
          |F[`+`_]
          |     ^""".stripMargin
     )
     runTestError[Stat](
       "F[`-`_]",
-      """|<input>:1: error: ] expected but _ found
+      """|<input>:1: error: `]` expected but `_` found
          |F[`-`_]
          |     ^""".stripMargin
     )
diff --git a/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/PublicSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/PublicSuite.scala
index 77a41aa06..23f5635d7 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/PublicSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/PublicSuite.scala
@@ -309,11 +309,12 @@ class PublicSuite extends TreeSuiteBase {
       try "foo + class".parse[Term].get
       catch {
         case ex: ParseException =>
-          assert(ex.toString == """
-            |<input>:1: error: end of file expected but class found
-            |foo + class
-            |      ^
-          """.trim.stripMargin.split('\n').mkString(EOL))
+          assertEquals(
+            ex.toString,
+            """|<input>:1: error: `end of file` expected but `class` found
+               |foo + class
+               |      ^""".stripMargin.replace("\n", EOL)
+          )
           throw ex
       }
     }
@@ -326,11 +327,12 @@ class PublicSuite extends TreeSuiteBase {
   test("scala.meta.parsers.Parsed.Error.toString") {
     val parsed = "foo + class".parse[Term]
     parsed match { case _: Parsed.Error => ; case _ => }
-    assert(parsed.toString == """
-      |<input>:1: error: end of file expected but class found
-      |foo + class
-      |      ^
-    """.trim.stripMargin.split('\n').mkString(EOL))
+    assertEquals(
+      parsed.toString,
+      """|<input>:1: error: `end of file` expected but `class` found
+         |foo + class
+         |      ^""".stripMargin.replace("\n", EOL)
+    )
   }
 
   test("scala.meta.parsers.Parsed.Success.toString") {
diff --git a/tests/shared/src/test/scala/scala/meta/tests/quasiquotes/ErrorSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/quasiquotes/ErrorSuite.scala
index cf318ae06..b2f06ec34 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/quasiquotes/ErrorSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/quasiquotes/ErrorSuite.scala
@@ -45,7 +45,7 @@ class ErrorSuite extends FunSuite {
       q"foo + class"
     """).replace("\r", ""),
       """
-      |<macro>:4: ; expected but class found
+      |<macro>:4: `;` expected but `class` found
       |      q"foo + class"
       |              ^
     """.trim.stripMargin.replace("\r", "")
@@ -351,7 +351,7 @@ class ErrorSuite extends FunSuite {
       q"expr: ..$tpes"
     """).replace("\r", ""),
       """
-      |<macro>:5: identifier expected but ellipsis found
+      |<macro>:5: `identifier` expected but `ellipsis` found
       |      q"expr: ..$tpes"
       |              ^
     """.trim.stripMargin.replace("\r", "")
@@ -403,7 +403,7 @@ class ErrorSuite extends FunSuite {
       q"expr. ..$names"
     """).replace("\r", ""),
       """
-      |<macro>:5: identifier expected but ellipsis found
+      |<macro>:5: `identifier` expected but `ellipsis` found
       |      q"expr. ..$names"
       |              ^
     """.trim.stripMargin.replace("\r", "")
@@ -486,7 +486,7 @@ class ErrorSuite extends FunSuite {
       q"private final def this(x: X, y: Y) = foo"
     """).replace("\r", ""),
       """
-      |<macro>:4: this expected but identifier found
+      |<macro>:4: `this` expected but `identifier` found
       |      q"private final def this(x: X, y: Y) = foo"
       |                                             ^
     """.trim.stripMargin.replace("\r", "")
@@ -753,7 +753,7 @@ class ErrorSuite extends FunSuite {
       q"foo(...$xss, ...$xss)"
     """).replace("\r", ""),
       """
-      |<macro>:5: ) expected but , found
+      |<macro>:5: `)` expected but `,` found
       |      q"foo(...$xss, ...$xss)"
       |                   ^
     """.trim.stripMargin