aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiron Aseev <aseev.miron@gmail.com>2016-11-11 20:49:54 +0700
committerMiron Aseev <aseev.miron@gmail.com>2016-11-16 12:05:23 +0700
commit6ce79460116ddb2ffa3d6645fe6a73b1080f01c3 (patch)
tree63b7016eb6a344739aa9dd2d3c2310f656341174
parent58a3ed99c98fb7caf0c0f94e6b5f57773a50c6c3 (diff)
downloaddotty-6ce79460116ddb2ffa3d6645fe6a73b1080f01c3.tar.gz
dotty-6ce79460116ddb2ffa3d6645fe6a73b1080f01c3.tar.bz2
dotty-6ce79460116ddb2ffa3d6645fe6a73b1080f01c3.zip
Add an error message for illegal start of simple pattern
-rw-r--r--src/dotty/tools/dotc/parsing/Parsers.scala2
-rw-r--r--src/dotty/tools/dotc/reporting/diagnostic/messages.scala79
2 files changed, 80 insertions, 1 deletions
diff --git a/src/dotty/tools/dotc/parsing/Parsers.scala b/src/dotty/tools/dotc/parsing/Parsers.scala
index 717faa849..aade359f8 100644
--- a/src/dotty/tools/dotc/parsing/Parsers.scala
+++ b/src/dotty/tools/dotc/parsing/Parsers.scala
@@ -1436,7 +1436,7 @@ object Parsers {
case _ =>
if (isLiteral) literal()
else {
- syntaxErrorOrIncomplete("illegal start of simple pattern")
+ syntaxErrorOrIncomplete(IllegalStartOfSimplePattern())
errorTermTree
}
}
diff --git a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
index e8f2ab8b7..5e6a402ef 100644
--- a/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
+++ b/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
@@ -812,4 +812,83 @@ object messages {
|would give 3 as a result"""
}
}
+
+ case class IllegalStartOfSimplePattern()(implicit ctx: Context) extends Message(32) {
+ val kind = "Syntax"
+ val msg = "illegal start of simple pattern"
+ val explanation = {
+ val sipCode =
+ """def f(x: Int, y: Int) = x match {
+ | case `y` => ...
+ |}
+ """
+ val constructorPatternsCode =
+ """case class Person(name: String, age: Int)
+ |
+ |def test(p: Person) = p match {
+ | case Person(name, age) => ...
+ |}
+ """
+ val tupplePatternsCode =
+ """def swap(tuple: (String, Int)): (Int, String) = tuple match {
+ | case (text, number) => (number, text)
+ |}
+ """
+ val patternSequencesCode =
+ """def getSecondValue(list: List[Int]): Int = list match {
+ | case List(_, second, x:_*) => second
+ | case _ => 0
+ |}"""
+ hl"""|Simple patterns can be divided into several groups:
+ |- Variable Patterns: ${"case x => ..."}.
+ | It matches any value, and binds the variable name to that value.
+ | A special case is the wild-card pattern _ which is treated as if it was a fresh
+ | variable on each occurrence.
+ |
+ |- Typed Patterns: ${"case x: Int => ..."} or ${"case _: Int => ..."}.
+ | This pattern matches any value matched by the specified type; it binds the variable
+ | name to that value.
+ |
+ |- Literal Patterns: ${"case 123 => ..."} or ${"case 'A' => ..."}.
+ | This type of pattern matches any value that is equal to the specified literal.
+ |
+ |- Stable Identifier Patterns:
+ |
+ | $sipCode
+ |
+ | the match succeeds only if the x argument and the y argument of f are equal.
+ |
+ |- Constructor Patterns:
+ |
+ | $constructorPatternsCode
+ |
+ | The pattern binds all object's fields to the variable names (name and age, in this
+ | case).
+ |
+ |- Tuple Patterns:
+ |
+ | $tupplePatternsCode
+ |
+ | Calling:
+ |
+ | ${"""swap(("Luftballons", 99)"""}
+ |
+ | would give ${"""(99, "Luftballons")"""} as a result.
+ |
+ |- Pattern Sequences:
+ |
+ | $patternSequencesCode
+ |
+ | Calling:
+ |
+ | ${"getSecondValue(List(1, 10, 2))"}
+ |
+ | would give 10 as a result.
+ | This pattern is possible because a companion object for the List class has a method
+ | with the following signature:
+ |
+ | ${"def unapplySeq[A](x: List[A]): Some[List[A]]"}
+ |"""
+ }
+ }
}