Skip to content

Commit

Permalink
ScalametaParser: quote tokens in syntax error (#3660)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Mar 17, 2024
1 parent d5e812a commit ee84cbe
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 55 deletions.
Expand Up @@ -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]}`"

}

Expand Down
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Expand Up @@ -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)
Expand Down Expand Up @@ -1451,7 +1451,7 @@ class TermSuite extends ParseSuite {
runTestError[Term](
"""|() match
| case _: Unit => ()""".stripMargin,
"error: { expected but case found"
"error: `{` expected but `case` found"
)

}
Expand Down
Expand Up @@ -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"))
}

}
Expand Up @@ -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"
)
}

Expand All @@ -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"
)
}

Expand All @@ -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"
)
}

Expand Down
Expand Up @@ -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
)
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
Expand Down
Expand Up @@ -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](
Expand Down
Expand Up @@ -621,7 +621,7 @@ class FewerBracesSuite extends BaseDottySuite {
| y > 0
| (0)
|""".stripMargin,
"error: ; expected but . found"
"error: `;` expected but `.` found"
)
}

Expand Down
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
)
Expand Down
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
)
Expand Down
Expand Up @@ -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") {
Expand All @@ -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)
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -554,7 +557,7 @@ class MinorDottySuite extends BaseDottySuite {

runTestError[Stat](
"{ inline @foo def foo(): Int }",
"; expected but @ found"
"`;` expected but `@` found"
)
}

Expand Down
Expand Up @@ -618,7 +618,7 @@ class NewFunctionsSuite extends BaseDottySuite {
| (e: Entry)
|=> e.Key
|""".stripMargin,
"""|error: ; expected but => found
"""|error: `;` expected but `=>` found
|=> e.Key
|^""".stripMargin
)
Expand Down
Expand Up @@ -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") {
Expand Down Expand Up @@ -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](
Expand Down
Expand Up @@ -149,7 +149,7 @@ class TypeSuite extends BaseDottySuite {
| with
| type D <: Product
|""".stripMargin,
"error: ; expected but with found"
"error: `;` expected but `with` found"
)
}

Expand Down Expand Up @@ -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
)
Expand Down
Expand Up @@ -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
}
}
Expand All @@ -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") {
Expand Down

0 comments on commit ee84cbe

Please sign in to comment.