diff options
author | liu fengyun <liufengyunchina@gmail.com> | 2016-03-15 17:47:39 +0100 |
---|---|---|
committer | liu fengyun <liufengyunchina@gmail.com> | 2016-03-18 15:08:51 +0100 |
commit | 72b44d9b5df579e90b5aba5c55d04f6b34081d02 (patch) | |
tree | f4ae237bfee63c57131bd6a53760c10d6acbe8ea /src | |
parent | f8ebf777b92e858a7ff8cfdf022e151b4f056920 (diff) | |
download | dotty-72b44d9b5df579e90b5aba5c55d04f6b34081d02.tar.gz dotty-72b44d9b5df579e90b5aba5c55d04f6b34081d02.tar.bz2 dotty-72b44d9b5df579e90b5aba5c55d04f6b34081d02.zip |
support `xs @ _*` and `_*` in Scala2 mode
The standard syntax in Dotty now is `xs : _*`. In Scala2 mode,
following code should be valid:
list match {
case List(_, _, _, _ @ _*) => 0
case List(_, _, _*) => 1
case List(_, _: _*) => 2
case Nil => 3
}
Diffstat (limited to 'src')
-rw-r--r-- | src/dotty/tools/dotc/parsing/Parsers.scala | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/src/dotty/tools/dotc/parsing/Parsers.scala b/src/dotty/tools/dotc/parsing/Parsers.scala index 6ec75a8b2..ea9da8db9 100644 --- a/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/src/dotty/tools/dotc/parsing/Parsers.scala @@ -259,11 +259,19 @@ object Parsers { } finally inFunReturnType = saved } + private val isScala2Mode = + ctx.settings.language.value.contains(nme.Scala2.toString) + + def migrationWarningOrError(msg: String, offset: Int = in.offset) = + if (isScala2Mode) + ctx.migrationWarning(msg, source atPos Position(offset)) + else + syntaxError(msg, offset) + /** Cannot use ctx.featureEnabled because accessing the context would force too much */ private def testScala2Mode(msg: String, pos: Position = Position(in.offset)) = { - val s2 = ctx.settings.language.value.contains(nme.Scala2.toString) - if (s2) ctx.migrationWarning(msg, source atPos pos) - s2 + if (isScala2Mode) ctx.migrationWarning(msg, source atPos pos) + isScala2Mode } /* ---------- TREE CONSTRUCTION ------------------------------------------- */ @@ -1309,7 +1317,20 @@ object Parsers { */ val pattern2 = () => infixPattern() match { case p @ Ident(name) if isVarPattern(p) && in.token == AT => - atPos(p.pos.start, in.skipToken()) { Bind(name, infixPattern()) } + val pos = in.skipToken() + + // compatibility for Scala2 `x @ _*` syntax + infixPattern() match { + case pt @ Ident(tpnme.WILDCARD_STAR) => + migrationWarningOrError("The syntax `x @ _*' is no longer supported; use `x : _*' instead", p.pos.start) + atPos(p.pos.start, pos) { Typed(p, pt) } + case p => + atPos(p.pos.start, pos) { Bind(name, p) } + } + case p @ Ident(tpnme.WILDCARD_STAR) => + // compatibility for Scala2 `_*` syntax + migrationWarningOrError("The syntax `_*' is no longer supported; use `x : _*' instead", p.pos.start) + atPos(p.pos.start) { Typed(Ident(nme.WILDCARD), p) } case p => p } @@ -1337,7 +1358,15 @@ object Parsers { case t => simplePatternRest(t) } case USCORE => - wildcardIdent() + val wildIndent = wildcardIdent() + + // compatibility for Scala2 `x @ _*` and `_*` syntax + // `x: _*' is parsed in `ascription' + if (isIdent(nme.raw.STAR)) { + in.nextToken() + if (in.token != RPAREN) syntaxError("`_*' can be used only for last argument", wildIndent.pos) + atPos(wildIndent.pos) { Ident(tpnme.WILDCARD_STAR) } + } else wildIndent case LPAREN => atPos(in.offset) { makeTupleOrParens(inParens(patternsOpt())) } case LBRACE => |