diff options
author | Martin Odersky <odersky@gmail.com> | 2007-06-05 15:57:59 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2007-06-05 15:57:59 +0000 |
commit | e51207992f525ed9e32a7a9a39512b4d7e503c03 (patch) | |
tree | b9440d03aed294fde3f8aaa293f60ed98b5de0c1 /src/compiler/scala/tools/nsc/ast | |
parent | e060c61b6127ceb2de0e4ce5d3f3d685bc542804 (diff) | |
download | scala-e51207992f525ed9e32a7a9a39512b4d7e503c03.tar.gz scala-e51207992f525ed9e32a7a9a39512b4d7e503c03.tar.bz2 scala-e51207992f525ed9e32a7a9a39512b4d7e503c03.zip |
deprecated &f, .f, requires.
Added existential types.
Diffstat (limited to 'src/compiler/scala/tools/nsc/ast')
6 files changed, 81 insertions, 28 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/TreeInfo.scala b/src/compiler/scala/tools/nsc/ast/TreeInfo.scala index 40a7e26229..eef8aa406c 100644 --- a/src/compiler/scala/tools/nsc/ast/TreeInfo.scala +++ b/src/compiler/scala/tools/nsc/ast/TreeInfo.scala @@ -193,7 +193,8 @@ abstract class TreeInfo { def mayBeTypePat(tree: Tree): boolean = tree match { case CompoundTypeTree(Template(tps, _, List())) => tps exists mayBeTypePat case Annotated(_, tp) => mayBeTypePat(tp) - case AppliedTypeTree(constr, args) => mayBeTypePat(constr) || args.exists(.isInstanceOf[Bind]) + case AppliedTypeTree(constr, args) => + mayBeTypePat(constr) || args.exists(_.isInstanceOf[Bind]) case SelectFromTypeTree(tp, _) => mayBeTypePat(tp) case _ => false } diff --git a/src/compiler/scala/tools/nsc/ast/Trees.scala b/src/compiler/scala/tools/nsc/ast/Trees.scala index 90009ac9cd..f48c389646 100644 --- a/src/compiler/scala/tools/nsc/ast/Trees.scala +++ b/src/compiler/scala/tools/nsc/ast/Trees.scala @@ -348,7 +348,7 @@ trait Trees { def DefDef(sym: Symbol, mods: Modifiers, rhs: List[List[Symbol]] => Tree): DefDef = { val vparamss = syntheticParams(sym, sym.tpe) - DefDef(sym, mods, vparamss map (.map(ValDef)), rhs(vparamss)) + DefDef(sym, mods, vparamss map (_.map(ValDef)), rhs(vparamss)) } def DefDef(sym: Symbol, rhs: List[List[Symbol]] => Tree): DefDef = @@ -460,13 +460,13 @@ trait Trees { def Template(parents: List[Tree], self: ValDef, constrMods: Modifiers, vparamss: List[List[ValDef]], argss: List[List[Tree]], body: List[Tree]): Template = { /** Add constructor to template */ var vparamss1 = - vparamss map (.map (vd => { + vparamss map (vps => vps.map { vd => val ret = ValDef(Modifiers(vd.mods.flags & IMPLICIT | PARAM) withAnnotations vd.mods.annotations, vd.name, vd.tpt.duplicate, EmptyTree).setPos(vd.pos) if (inIDE && vd.symbol != NoSymbol) ret.symbol = vd.symbol ret - })) + }) val (vdefs, rest) = body span treeInfo.isPreSuper val (lvdefs, gvdefs) = List.unzip { vdefs map { @@ -728,6 +728,9 @@ trait Trees { case class WildcardTypeTree(lo: Tree, hi: Tree) extends TypTree + case class ExistentialTypeTree(tpt: Tree, whereClauses: List[Tree]) + extends TypTree + /* A standard pattern match case EmptyTree => case PackageDef(name, stats) => @@ -818,6 +821,8 @@ trait Trees { // tpt[args] case WildcardTypeTree(lo, hi) => (eliminated by uncurry) // todo: get rid of that! + case ExistentialTypeTree(tpt, whereClauses) => + */ abstract class TreeCopier { @@ -865,6 +870,7 @@ trait Trees { def CompoundTypeTree(tree: Tree, templ: Template): CompoundTypeTree def AppliedTypeTree(tree: Tree, tpt: Tree, args: List[Tree]): AppliedTypeTree def WildcardTypeTree(tree: Tree, lo: Tree, hi: Tree): WildcardTypeTree + def ExistentialTypeTree(tree: Tree, tpt: Tree, whereClauses: List[Tree]): ExistentialTypeTree } class StrictTreeCopier extends TreeCopier { @@ -956,6 +962,8 @@ trait Trees { new AppliedTypeTree(tpt, args).copyAttrs(tree) def WildcardTypeTree(tree: Tree, lo: Tree, hi: Tree) = new WildcardTypeTree(lo, hi).copyAttrs(tree) + def ExistentialTypeTree(tree: Tree, tpt: Tree, whereClauses: List[Tree]) = + new ExistentialTypeTree(tpt, whereClauses).copyAttrs(tree) } class LazyTreeCopier(copy: TreeCopier) extends TreeCopier { @@ -1180,6 +1188,11 @@ trait Trees { if (lo0 == lo) && (hi0 == hi) => t case _ => copy.WildcardTypeTree(tree, lo, hi) } + def ExistentialTypeTree(tree: Tree, tpt: Tree, whereClauses: List[Tree]) = tree match { + case t @ ExistentialTypeTree(tpt0, whereClauses0) + if (tpt0 == tpt) && (whereClauses0 == whereClauses) => t + case _ => copy.ExistentialTypeTree(tree, tpt, whereClauses) + } } abstract class Transformer { @@ -1296,6 +1309,8 @@ trait Trees { copy.AppliedTypeTree(tree, transform(tpt), transformTrees(args)) case WildcardTypeTree(lo, hi) => copy.WildcardTypeTree(tree, transform(lo), transform(hi)) + case ExistentialTypeTree(tpt, whereClauses) => + copy.ExistentialTypeTree(tree, transform(tpt), transformTrees(whereClauses)) } def transformTrees(trees: List[Tree]): List[Tree] = @@ -1440,6 +1455,8 @@ trait Trees { traverse(tpt); traverseTrees(args) case WildcardTypeTree(lo, hi) => traverse(lo); traverse(hi) + case ExistentialTypeTree(tpt, whereClauses) => + traverse(tpt); traverseTrees(whereClauses) } def traverseTrees(trees: List[Tree]): unit = diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index 3637a8f050..a6fccb50c2 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -127,6 +127,11 @@ trait Parsers { */ var implicitParams: List[ValDef] = Nil + /** The wildcards introduced by `_' in the current type. + * Parameters appear in reverse order + */ + var wildcards: List[AbsTypeDef] = Nil + /** this is the general parse method */ def parse(): Tree = { @@ -618,12 +623,10 @@ trait Parsers { /** RequiresTypedOpt ::= [requires AnnotType] */ def requiresTypeOpt(): Tree = - if (inToken == COLON | inToken == REQUIRES) { - if (inToken == COLON) - warning("`:' has been deprecated; use `requires' instead") + if (inToken == REQUIRES) { + warning("`requires T' has been deprecated; use `{ self: T => ...' instead") inNextToken; annotType(false) - } - else TypeTree() + } else TypeTree() /** Types ::= Type {`,' Type} * ArgTypePats ::= ArgTypePat {`,' ArgTypePat} @@ -645,32 +648,63 @@ trait Parsers { private final val LeftOp = 1 // left associative private final val RightOp = 2 // right associative - /** Type ::= InfixType [`=>' Type] - * | `(' [`=>' Type] `)' `=>' Type - * XXX: Hook for IDE. + /** GlobalType ::= Type + */ + def globalTyp(): Tree = { + val savedWildcards = wildcards + wildcards = List() + var t = typ() + if (!wildcards.isEmpty) t = ExistentialTypeTree(t, wildcards) + wildcards = savedWildcards + t + } + + /** Type ::= Type1 [where `{' {WhereClause} `}'] */ def typ(): Tree = { + val t = typ1() + if (inToken == FORSOME) { + atPos(inSkipToken) { + val whereClauses = refinement() + for (wc <- whereClauses) { + wc match { + case AbsTypeDef(_, _, _, _, _) | ValDef(_, _, _, EmptyTree) => + ; + case _ => + syntaxError(wc.pos, "not a legal where clause", false) + } + } + ExistentialTypeTree(t, whereClauses) + } + } else t + } + + /** Type1 ::= InfixType [`=>' Type1] + * | `(' [`=>' Type] `)' `=>' Type1 + * XXX: Hook for IDE. + */ + def typ1(): Tree = { val t = if (inToken == LPAREN) { val pos = inSkipToken if (inToken == RPAREN) { inNextToken - atPos(accept(ARROW)) { makeFunctionTypeTree(List(), typ()) } + atPos(accept(ARROW)) { makeFunctionTypeTree(List(), typ1()) } } else if (inToken == ARROW) { inNextToken - val t0 = typ() + val t0 = typ1() accept(RPAREN) - atPos(accept(ARROW)) { makeByNameFunctionTypeTree(t0, typ()) } + atPos(accept(ARROW)) { makeByNameFunctionTypeTree(t0, typ1()) } } else { val ts = types(false) accept(RPAREN) - if (inToken == ARROW) atPos(inSkipToken) { makeFunctionTypeTree(ts, typ()) } + if (inToken == ARROW) atPos(inSkipToken) { makeFunctionTypeTree(ts, typ1()) } else infixTypeRest(pos, makeTupleType(ts, true), false, FirstOp) } } else { infixType(false, FirstOp) } - if (inToken == ARROW) atPos(inSkipToken) { makeFunctionTypeTree(List(t), typ()) } + if (inToken == ARROW) atPos(inSkipToken) { makeFunctionTypeTree(List(t), typ1()) } else t } @@ -856,8 +890,8 @@ trait Parsers { * | Expr1 * ResultExpr ::= (Bindings | Id `:' CompoundType) `=>' Block * | Expr1 - * Expr1 ::= if `(' Expr `)' {nl} Expr [semi] else Expr] - * | try `{' block `}' [catch `{' caseClauses `}'] [finally Expr] + * Expr1 ::= if `(' Expr `)' {nl} Expr [[semi] else Expr] + * | try `{' Block `}' [catch `{' CaseClauses `}'] [finally Expr] * | while `(' Expr `)' {nl} Expr * | do Expr [semi] while `(' Expr `)' * | for (`(' Enumerators `)' | '{' Enumerators '}') {nl} [yield] Expr @@ -865,10 +899,8 @@ trait Parsers { * | return [Expr] * | [SimpleExpr `.'] Id `=' Expr * | SimpleExpr1 ArgumentExprs `=' Expr - * | `.' SimpleExpr * | PostfixExpr Ascription * | PostfixExpr match `{' CaseClauses `}' - * | `.' Id {`.' Id | TypeArgs | ArgumentExprs} * Bindings ::= `(' [Binding {`,' Binding}] `)' * Binding ::= Id [`:' Type] * Ascription ::= `:' CompoundType @@ -956,7 +988,7 @@ trait Parsers { Throw(expr()) } case DOT => - //todo: deprecate + warning("`.f' has been deprecated; use `_.f' instead") atPos(inSkipToken) { if (isIdent) { makeDotClosure(stripParens(simpleExpr())) @@ -1066,6 +1098,7 @@ trait Parsers { val name = unaryOp() atPos(pos) { Select(stripParens(simpleExpr()), name) } } else if (isIdent && inName == AMP) { + warning("`&f' has been deprecated; use `f _' instead") val pos = inCurrentPos val name = ident() atPos(pos) { Typed(stripParens(simpleExpr()), Function(List(), EmptyTree)) } @@ -1218,7 +1251,7 @@ trait Parsers { * | val Pattern1 `=' Expr */ def enumerators(): List[Enumerator] = { - val newStyle = inToken != VAL + val newStyle = inToken != VAL // todo: deprecate old style val enums = new ListBuffer[Enumerator] generator(enums, false) while (isStatSep) { @@ -1940,7 +1973,7 @@ trait Parsers { atPos(inSkipToken) { if (inToken == THIS) { inNextToken - val vparamss = paramClauses(nme.CONSTRUCTOR, implicitClassViews map (.duplicate), false) + val vparamss = paramClauses(nme.CONSTRUCTOR, implicitClassViews map (_.duplicate), false) newLineOptWhenFollowedBy(LBRACE) val rhs = if (inToken == LBRACE) constrBlock(vparamss) else { accept(EQUALS); constrExpr(vparamss) } diff --git a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala index 045d60c005..a68188c3d7 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala @@ -120,6 +120,7 @@ trait Scanners { enterKeyword(nme.FINALkw, FINAL) enterKeyword(nme.FINALLYkw, FINALLY) enterKeyword(nme.FORkw, FOR) + enterKeyword(nme.FORSOMEkw, FORSOME) enterKeyword(nme.IFkw, IF) enterKeyword(nme.IMPLICITkw, IMPLICIT) enterKeyword(nme.IMPORTkw, IMPORT) @@ -675,7 +676,7 @@ trait Scanners { } def inFirstOfStat(token: int) = token match { - case EOF | CASE | CATCH | ELSE | EXTENDS | FINALLY | MATCH | REQUIRES | WITH | YIELD | + case EOF | CASE | CATCH | ELSE | EXTENDS | FINALLY | FORSOME | MATCH | REQUIRES | WITH | YIELD | COMMA | SEMI | NEWLINE | NEWLINES | DOT | USCORE | COLON | EQUALS | ARROW | LARROW | SUBTYPE | VIEWBOUND | SUPERTYPE | HASH | // todo: add LBRACKET RPAREN | RBRACKET | RBRACE => diff --git a/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala b/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala index 4803fbfcce..49a497a669 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala @@ -72,7 +72,8 @@ object Tokens { final val WHILE = 56 final val RETURN = 57 final val MATCH = 58 - final val REQUIRES = 59 + final val FORSOME = 59 + final val REQUIRES = 60 def isKeyword(code : Int) = code >= IF && code <= REQUIRES diff --git a/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala b/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala index 333005fc54..b3ad88caef 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala @@ -300,7 +300,7 @@ abstract class TreeBuilder { ValFrom(pos, pat, makeCombination(nme.filter, rhs, pat.duplicate, test)) :: rest, body) case ValFrom(pos, pat, rhs) :: rest => - val valeqs = rest.take(definitions.MaxTupleArity - 1).takeWhile(.isInstanceOf[ValEq]); + val valeqs = rest.take(definitions.MaxTupleArity - 1).takeWhile(_.isInstanceOf[ValEq]); assert(!valeqs.isEmpty) val rest1 = rest.drop(valeqs.length) val pats = valeqs map { case ValEq(_, pat, _) => pat } @@ -408,7 +408,7 @@ abstract class TreeBuilder { val matchExpr = atPos(pat1.pos){ Match( makeUnchecked(rhs), - List(CaseDef(pat1, EmptyTree, makeTupleTerm(vars map (._1) map Ident, true)))) + List(CaseDef(pat1, EmptyTree, makeTupleTerm(vars map (_._1) map Ident, true)))) } vars match { case List() => |