From 6ce79460116ddb2ffa3d6645fe6a73b1080f01c3 Mon Sep 17 00:00:00 2001 From: Miron Aseev Date: Fri, 11 Nov 2016 20:49:54 +0700 Subject: Add an error message for illegal start of simple pattern --- src/dotty/tools/dotc/parsing/Parsers.scala | 2 +- .../tools/dotc/reporting/diagnostic/messages.scala | 79 ++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) 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]]"} + |""" + } + } } -- cgit v1.2.3