diff options
author | Martin Odersky <odersky@gmail.com> | 2007-04-04 16:30:11 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2007-04-04 16:30:11 +0000 |
commit | 51d9edec14b5c56f6fc4d283fa27fb7c7d782249 (patch) | |
tree | 2ba1165c504cac44b3b1545b8b1274ccf2b05ff9 /src/compiler/scala/tools/nsc/ast/parser | |
parent | 7f3c7c392467b128af32d416edf521f468caf34c (diff) | |
download | scala-51d9edec14b5c56f6fc4d283fa27fb7c7d782249.tar.gz scala-51d9edec14b5c56f6fc4d283fa27fb7c7d782249.tar.bz2 scala-51d9edec14b5c56f6fc4d283fa27fb7c7d782249.zip |
explicit supercalls + new style for syntax + ba...
explicit supercalls + new style for syntax + backquoted ids in patterns
Diffstat (limited to 'src/compiler/scala/tools/nsc/ast/parser')
-rw-r--r-- | src/compiler/scala/tools/nsc/ast/parser/Parsers.scala | 79 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/ast/parser/Scanners.scala | 56 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/ast/parser/Tokens.scala | 1 |
3 files changed, 98 insertions, 38 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index 0517e50624..863d489667 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -1057,9 +1057,10 @@ trait Parsers requires SyntaxAnalyzer { parents += annotType(false) } newLineOptWhenFollowedBy(LBRACE) - val (self, stats) = if (in.token == LBRACE) templateBody() - else (emptyValDef, List()) - makeNew(parents.toList, self, stats, argss.toList) + val (self, stats, superpos, superargss) = + if (in.token == LBRACE) templateBody() + else (emptyValDef, List(), Position.NOPOS, List(List())) + makeNew(parents.toList, self, stats, superArgs(superpos, argss.toList, superargss)) } canApply = false case _ => @@ -1075,6 +1076,17 @@ trait Parsers requires SyntaxAnalyzer { simpleExprRest(t, canApply) } + private def isEmpty(argss: List[List[Tree]]) = argss.head.isEmpty && argss.tail.isEmpty + + private def superArgs(pos: int, extargss: List[List[Tree]], superargss: List[List[Tree]]) = { + var argss = extargss + if (isEmpty(argss)) + argss = superargss + else if (!isEmpty(superargss)) + syntaxError(pos, "super call arguments are specified twice; once here and once in a subsequent super call", false) + argss + } + def simpleExprRest(t: Tree, canApply: boolean): Tree = { if (canApply) newLineOptWhenFollowedBy(LBRACE) in.token match { @@ -1150,10 +1162,19 @@ trait Parsers requires SyntaxAnalyzer { * | Expr */ def enumerators(): List[Enumerator] = { + val newStyle = in.token != VAL val enums = new ListBuffer[Enumerator] + generator(false) while (isStatSep) { in.nextToken() - enums += (if (in.token == VAL) generator(true) else Filter(expr())) + enums += { + if (newStyle) { + if (in.token == IF) { in.nextToken(); Filter(expr()) } + else generator(true) + } else { + if (in.token == VAL) generator(true) + else Filter(expr()) + } + } } enums.toList } @@ -2040,25 +2061,34 @@ trait Parsers requires SyntaxAnalyzer { } val ps = parents.toList newLineOptWhenFollowedBy(LBRACE) - val (self, body) = + val (self, body, superpos, superargss) = if (in.token == LBRACE) templateBody() - else { acceptEmptyTemplateBody("`{' expected"); (emptyValDef, List()) } - (self, - atPos(pos) { - if (!mods.hasFlag(Flags.TRAIT)) Template(ps, constrMods, vparamss, argss.toList, body) - else Template(ps, body) - }) + else { + acceptEmptyTemplateBody("`{' expected") + (emptyValDef, List(), Position.NOPOS, List(List())) + } + val argumentss = superArgs(pos, argss.toList, superargss) + val impl = atPos(pos) { + if (mods.hasFlag(Flags.TRAIT)) { + if (!isEmpty(argumentss)) + syntaxError(superpos, "traits cannot have supercall arguments", false) + Template(ps, body) + } else { + Template(ps, constrMods, vparamss, argumentss, body) + } + } + (self, impl) } ////////// TEMPLATES //////////////////////////////////////////////////////////// /** TemplateBody ::= [nl] `{' TemplateStatSeq `}' */ - def templateBody(): (ValDef, List[Tree]) = { + def templateBody(): (ValDef, List[Tree], int, List[List[Tree]]) = { accept(LBRACE) - val result @ (self, stats) = templateStatSeq() + val result @ (self, stats, superpos, superargss) = templateStatSeq() accept(RBRACE) - if (stats.isEmpty) (self, List(EmptyTree)) else result + if (stats.isEmpty) (self, List(EmptyTree), Position.NOPOS, superargss) else result } /** Refinement ::= [nl] `{' RefineStat {semi RefineStat} `}' @@ -2124,9 +2154,11 @@ trait Parsers requires SyntaxAnalyzer { * | super ArgumentExprs {ArgumentExprs} * | */ - def templateStatSeq(): (ValDef, List[Tree]) = { + def templateStatSeq(): (ValDef, List[Tree], int, List[List[Tree]]) = { var self: ValDef = emptyValDef - val stats = new ListBuffer[Tree] + var stats = new ListBuffer[Tree] + var superpos = Position.NOPOS + val superargss = new ListBuffer[List[Tree]] if (isExprIntro) { val first = expr(InTemplate) if (in.token == ARROW) { @@ -2146,12 +2178,25 @@ trait Parsers requires SyntaxAnalyzer { } else if (isDefIntro || isModifier || in.token == LBRACKET /*todo: remove */ || in.token == AT) { val annots = annotations() stats ++ joinComment(defOrDcl(modifiers() withAnnotations annots)) + } else if (in.token == SUPERCALL) { + superpos = in.skipToken() + stats = new ListBuffer[Tree] ++ (stats.toList map markPreSuper) + while (in.token == LPAREN) { superargss += argumentExprs() } } else if (!isStatSep) { syntaxErrorOrIncomplete("illegal start of definition", true) } if (in.token != RBRACE && in.token != EOF) acceptStatSep() } - (self, stats.toList) + if (!superargss.hasNext) superargss += List() + (self, stats.toList, superpos, superargss.toList) + } + + private def markPreSuper(stat: Tree): Tree = stat match { + case ValDef(mods, name, tpt, rhs) => + copy.ValDef(stat, mods | Flags.PRESUPER, name, tpt, rhs) + case _ => + syntaxError(stat.pos, "only value definitions may precede super call", false) + stat } /** RefineStatSeq ::= RefineStat {semi RefineStat} diff --git a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala index 080c83f49c..8eb785b38e 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala @@ -37,12 +37,24 @@ trait Scanners requires SyntaxAnalyzer { /** the base of a number */ var base: int = 0 - def copyFrom(td: TokenData) = { + /** further lookahead tokens after this one */ + var following: TokenData = null + + def copyFrom(td: TokenData) { this.token = td.token this.pos = td.pos this.lastPos = td.lastPos this.name = td.name this.base = td.base + td.token = EMPTY + } + + def pushFrom(td: TokenData) { + if (token != EMPTY) { + following = new TokenData + following copyFrom this + } + this copyFrom td } } @@ -145,42 +157,45 @@ trait Scanners requires SyntaxAnalyzer { if (next.token == EMPTY) { fetchToken() } else { - this.copyFrom(next) - next.token = EMPTY + this copyFrom next + if (next.following != null) { + next copyFrom next.following + next.following = null + } } if (token == CASE) { - prev.copyFrom(this) + prev copyFrom this fetchToken() if (token == CLASS) { + this copyFrom prev token = CASECLASS - lastPos = prev.lastPos } else if (token == OBJECT) { + this copyFrom prev token = CASEOBJECT - lastPos = prev.lastPos } else { - next.copyFrom(this) - this.copyFrom(prev) + next pushFrom this + this copyFrom prev } + } else if (token == SUPER && next.token == EMPTY) { + prev copyFrom this + fetchToken() + val isSuperCall = token == LPAREN + next pushFrom this + this copyFrom prev + if (isSuperCall) token = SUPERCALL } else if (token == SEMI) { - prev.copyFrom(this) + prev copyFrom this fetchToken() if (token != ELSE) { - next.copyFrom(this) - this.copyFrom(prev) + next pushFrom this + this copyFrom prev } - } else if (token == IDENTIFIER && name == nme.MIXINkw) { //todo: remove eventually - prev.copyFrom(this) - fetchToken() - if (token == CLASS) - unit.warning(prev.pos, "`mixin' is no longer a reserved word; you should use `trait' instead of `mixin class'"); - next.copyFrom(this) - this.copyFrom(prev) } if (afterLineEnd() && inLastOfStat(lastToken) && inFirstOfStat(token) && (sepRegions.isEmpty || sepRegions.head == RBRACE)) { - next.copyFrom(this) + next pushFrom this pos = in.lineStartPos if (settings.migrate.value) newNewLine = lastToken != RBRACE && token != EOF; token = if (in.lastBlankLinePos > lastPos) NEWLINES else NEWLINE @@ -273,7 +288,7 @@ trait Scanners requires SyntaxAnalyzer { return case '`' => in.next - getStringLit('`', IDENTIFIER) + getStringLit('`', BACKQUOTED_IDENT) return case '\"' => in.next @@ -569,7 +584,6 @@ trait Scanners requires SyntaxAnalyzer { } private def getStringLit(delimiter: char, litType: int): unit = { - assert((litType==STRINGLIT) || (litType==IDENTIFIER)) while (in.ch != delimiter && (in.isUnicode || in.ch != CR && in.ch != LF && in.ch != SU)) { getlitch() } diff --git a/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala b/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala index 0aed4e8eae..8044744e6c 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Tokens.scala @@ -69,6 +69,7 @@ object Tokens { final val RETURN = 57 final val MATCH = 58 final val REQUIRES = 59 + final val SUPERCALL = 60 /** special symbols */ final val COMMA = 61 |