summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2009-07-02 17:53:28 +0000
committerMartin Odersky <odersky@gmail.com>2009-07-02 17:53:28 +0000
commit3b72f6de8213b573675650d4ca16c0563981f965 (patch)
tree0c1b0432ee4ca068a00db330ccf413b79e2530a1 /src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
parent08a4234ce025896bdcf4910a67d8178cf4336cb2 (diff)
downloadscala-3b72f6de8213b573675650d4ca16c0563981f965.tar.gz
scala-3b72f6de8213b573675650d4ca16c0563981f965.tar.bz2
scala-3b72f6de8213b573675650d4ca16c0563981f965.zip
refined range positions some more; eliminated A...
refined range positions some more; eliminated Array.withDims
Diffstat (limited to 'src/compiler/scala/tools/nsc/ast/parser/Parsers.scala')
-rwxr-xr-xsrc/compiler/scala/tools/nsc/ast/parser/Parsers.scala74
1 files changed, 43 insertions, 31 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
index 5b22914b23..e5eaf8a8e0 100755
--- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
@@ -160,6 +160,7 @@ self =>
object treeBuilder extends TreeBuilder {
val global: self.global.type = self.global
def freshName(prefix: String): Name = Parser.this.freshName(prefix)
+ def o2p(offset: Int) = Parser.this.o2p(offset)
}
import treeBuilder.{global => _, _}
@@ -338,9 +339,9 @@ self =>
ret
}
- def errorTypeTree = TypeTree().setType(ErrorType).setPos(o2p(in.offset))
- def errorTermTree = Literal(Constant(null)).setPos(o2p(in.offset))
- def errorPatternTree = Ident(nme.WILDCARD).setPos(o2p(in.offset))
+ def errorTypeTree = TypeTree().setType(ErrorType).setPos(o2p(in.offset).toSynthetic)
+ def errorTermTree = Literal(Constant(null)).setPos(o2p(in.offset).toSynthetic)
+ def errorPatternTree = Ident(nme.WILDCARD).setPos(o2p(in.offset).toSynthetic)
/** Check that type parameter is not by name T* */
def checkNotByName(t: Tree) = t match {
@@ -453,7 +454,7 @@ self =>
ValDef(Modifiers(Flags.PARAM), name, tpe, EmptyTree)
case _ =>
syntaxError(tree.pos, "not a legal formal parameter", false)
- ValDef(Modifiers(Flags.PARAM), nme.ERROR, errorTypeTree setPos o2p(tree.pos.end), EmptyTree)
+ ValDef(Modifiers(Flags.PARAM), nme.ERROR, errorTypeTree setPos o2p(tree.pos.end).toSynthetic, EmptyTree)
}
}
@@ -472,11 +473,11 @@ self =>
}
/** part {`,' part} */
- def commaSeparated(part: () => Tree): List[Tree] = {
- val ts = new ListBuffer[Tree] += part()
+ def commaSeparated(part: => Tree): List[Tree] = {
+ val ts = new ListBuffer[Tree] += part
while (in.token == COMMA) {
in.nextToken()
- ts += part()
+ ts += part
}
ts.toList
}
@@ -553,6 +554,7 @@ self =>
def selector(t: Tree): Tree = {
val point = in.offset
+ //assert(t.pos.isDefined, t)
Select(t, ident()) setPos r2p(t.pos.start, point, in.lastOffset)
}
@@ -700,7 +702,7 @@ self =>
/** Types ::= Type {`,' Type}
*/
def types(isPattern: Boolean, isTypeApply: Boolean, isFuncArg: Boolean): List[Tree] =
- commaSeparated(() => argType(isPattern, isTypeApply, isFuncArg))
+ commaSeparated(argType(isPattern, isTypeApply, isFuncArg))
/** Type ::= InfixType `=>' Type
* | `(' [`=>' Type] `)' `=>' Type
@@ -1173,7 +1175,7 @@ self =>
id
case LPAREN =>
atPos(in.skipToken()) {
- val ts = if (in.token == RPAREN) List() else commaSeparated(expr _)
+ val ts = if (in.token == RPAREN) List() else commaSeparated(expr)
accept(RPAREN)
Parens(ts)
}
@@ -1182,10 +1184,12 @@ self =>
blockExpr()
case NEW =>
canApply = false
- atPos(in.skipToken()) {
- val (parents, argss, self, stats) = template(false)
- makeNew(parents, self, stats, argss)
- }
+ val nstart = in.skipToken()
+ val npos = r2p(nstart, nstart, in.lastOffset)
+ val tstart = in.offset
+ val (parents, argss, self, stats) = template(false)
+ val cpos = r2p(tstart, tstart, in.lastOffset)
+ makeNew(parents, self, stats, argss, npos, cpos)
case _ =>
syntaxErrorOrIncomplete("illegal start of simple expression", true)
errorTermTree
@@ -1236,14 +1240,14 @@ self =>
* | [nl] BlockExpr
*/
def argumentExprs(): List[Tree] = {
- def args(): List[Tree] = commaSeparated(() => {
+ def args(): List[Tree] = commaSeparated {
val maybeNamed = isIdent
expr() match {
case a @ Assign(id, rhs) if maybeNamed =>
atPos(a.pos) { AssignOrNamedArg(id, rhs) }
case e => e
}
- })
+ }
// if arg has the form "x$1 => a = x$1" it's treated as "a = x$1" with x$1
// in placeholderParams. This allows e.g. "val f: Int => Int = foo(a = 1, b = _)"
@@ -1358,7 +1362,7 @@ self =>
* SeqPatterns ::= SeqPattern { `,' SeqPattern }
*/
def patterns(seqOK: Boolean): List[Tree] =
- commaSeparated(() => pattern(seqOK))
+ commaSeparated(pattern(seqOK))
/** Pattern ::= Pattern1 { `|' Pattern1 }
* SeqPattern ::= SeqPattern1 { `|' SeqPattern1 }
@@ -1827,7 +1831,7 @@ self =>
*/
def importClause(): List[Tree] = {
accept(IMPORT)
- commaSeparated(() => importExpr())
+ commaSeparated(importExpr())
}
/** ImportExpr ::= StableId `.' (Id | `_' | ImportSelectors)
@@ -1961,7 +1965,7 @@ self =>
} while (in.token == COMMA)
val lhs = lhsBuf.toList
val tp = typedOpt()
- var rhs =
+ val rhs =
if (tp.isEmpty || in.token == EQUALS) {
accept(EQUALS)
if (!tp.isEmpty && newmods.hasFlag(Flags.MUTABLE) &&
@@ -1976,12 +1980,13 @@ self =>
newmods = newmods | Flags.DEFERRED
EmptyTree
}
- def mkDefs(p: Tree): List[Tree] = {
+ def mkDefs(p: Tree, tp: Tree, rhs: Tree): List[Tree] = {
//Console.println("DEBUG: p = "+p.toString()); // DEBUG
val trees =
- makePatDef(newmods, if (tp.isEmpty) p else Typed(p, tp), rhs) map
- atPos(p.pos.start, p.pos.point)
- rhs = rhs.syntheticDuplicate
+ makePatDef(newmods,
+ if (tp.isEmpty) p
+ else Typed(p, tp) setPos (p.pos union tp.pos),
+ rhs)
if (newmods hasFlag Flags.DEFERRED) {
trees match {
case List(ValDef(_, _, _, EmptyTree)) =>
@@ -1991,7 +1996,7 @@ self =>
}
trees
}
- for (p <- lhs.toList; d <- mkDefs(p)) yield d
+ (lhs.toList.init flatMap (mkDefs(_, tp.syntheticDuplicate, rhs.syntheticDuplicate))) ::: mkDefs(lhs.last, tp, rhs)
}
/** VarDef ::= PatDef
@@ -2032,8 +2037,12 @@ self =>
atPos(start, in.skipToken()) {
val vparamss = paramClauses(nme.CONSTRUCTOR, implicitClassViews map (_.syntheticDuplicate), false)
newLineOptWhenFollowedBy(LBRACE)
- val rhs = if (in.token == LBRACE) constrBlock(vparamss)
- else { accept(EQUALS); constrExpr(vparamss) }
+ val rhs = if (in.token == LBRACE) {
+ atPos(in.offset) { constrBlock(vparamss) }
+ } else {
+ accept(EQUALS)
+ atPos(in.offset) { constrExpr(vparamss) }
+ }
DefDef(mods, nme.CONSTRUCTOR, List(), vparamss, TypeTree(), rhs)
}
} else {
@@ -2162,6 +2171,7 @@ self =>
val savedViews = implicitClassViews
val implicitViewBuf = new ListBuffer[Tree]
val tparams = typeParamClauseOpt(name, implicitViewBuf)
+ val tstart = in.offset
implicitClassViews = implicitViewBuf.toList
if (!implicitClassViews.isEmpty && mods.hasFlag(Flags.TRAIT)) {
syntaxError("traits cannot have type parameters with <% bounds", false)
@@ -2177,7 +2187,7 @@ self =>
} else if (in.token == SUBTYPE) {
syntaxError("classes are not allowed to be virtual", false)
}
- var template = templateOpt(mods1, name, constrMods withAnnotations constrAnnots, vparamss)
+ val template = templateOpt(mods1, name, constrMods withAnnotations constrAnnots, vparamss, tstart)
if (isInterface(mods1, template.body)) mods1 |= Flags.INTERFACE
val result = ClassDef(mods1, name, tparams, template)
implicitClassViews = savedViews
@@ -2191,9 +2201,10 @@ self =>
val start = in.skipToken()
val nameOffset = in.offset
val name = ident()
+ val tstart = in.offset
atPos(start, if (name == nme.ERROR) start else nameOffset) {
val mods1 = if (in.token == SUBTYPE) mods | Flags.DEFERRED else mods
- val template = templateOpt(mods1, name, NoMods, List())
+ val template = templateOpt(mods1, name, NoMods, List(), tstart)
ModuleDef(mods1, name, template)
}
}
@@ -2255,10 +2266,9 @@ self =>
/** ClassTemplateOpt ::= 'extends' ClassTemplate | [['extends'] TemplateBody]
* TraitTemplateOpt ::= TraitExtends TraitTemplate | [['extends'] TemplateBody] | '<:' TemplateBody
* TraitExtends ::= 'extends' | `<:'
- * @note leaves result unpositioned.
*/
def templateOpt(mods: Modifiers, name: Name, constrMods: Modifiers,
- vparamss: List[List[ValDef]]): Template = {
+ vparamss: List[List[ValDef]], tstart: Int): Template = {
val (parents0, argss, self, body) =
if (in.token == EXTENDS || settings.Xexperimental.value && (mods hasFlag Flags.TRAIT) && in.token == SUBTYPE) {
in.nextToken()
@@ -2277,7 +2287,9 @@ self =>
if (parents.isEmpty)
parents = List(scalaAnyRefConstr)
if (mods.hasFlag(Flags.CASE)) parents = parents ::: List(productConstr)
- Template(parents, self, constrMods, vparamss, argss, body)
+ atPos(tstart) {
+ Template(parents, self, constrMods, vparamss, argss, body, o2p(tstart).toSynthetic)
+ }
}
/* -------- TEMPLATES ------------------------------------------- */
@@ -2493,7 +2505,7 @@ self =>
stats ++= localDef
if (in.token == RBRACE || in.token == CASE) {
syntaxError("block must end in result expression, not in definition", false)
- stats += Literal(()).setPos(o2p(in.offset))
+ stats += Literal(()).setPos(o2p(in.offset).toSynthetic)
} else acceptStatSep()
} else if (isStatSep) {
in.nextToken()