Updated parser to fix ref locations. Added way of computing a source file's signature.

git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@5389 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
mcdirmid 2005-12-19 11:44:04 +00:00
parent ae54237710
commit ee6fdbc1d0
3 changed files with 134 additions and 72 deletions

View File

@ -324,14 +324,14 @@ import Tokens._;
if (in.token == THIS) {
t = atPos(in.skipToken()) { This(nme.EMPTY.toTypeName) }
if (!thisOK || in.token == DOT)
t = atPos(accept(DOT)) { selectors(t, typeOK) }
t = { selectors(t, typeOK, accept(DOT)) }
} else if (in.token == SUPER) {
t = atPos(in.skipToken()) {
Super(nme.EMPTY.toTypeName, mixinQualifierOpt())
}
t = atPos(accept(DOT)) { Select(t, ident()) }
if (in.token == DOT)
t = atPos(in.skipToken()) { selectors(t, typeOK) }
t = { selectors(t, typeOK, in.skipToken()) }
} else {
val i = atPos(in.currentPos) { Ident(ident()) }
t = i;
@ -341,28 +341,28 @@ import Tokens._;
in.nextToken();
t = atPos(i.pos) { This(i.name.toTypeName) }
if (!thisOK || in.token == DOT)
t = atPos(accept(DOT)) { selectors(t, typeOK) }
t = { selectors(t, typeOK, accept(DOT)) }
} else if (in.token == SUPER) {
in.nextToken();
t = atPos(i.pos) { Super(i.name.toTypeName, mixinQualifierOpt()) }
t = atPos(accept(DOT)) { Select(t, ident())}
if (in.token == DOT)
t = atPos(in.skipToken()) { selectors(t, typeOK) }
t = { selectors(t, typeOK, in.skipToken()) }
} else {
t = atPos(pos) { selectors(t, typeOK) }
t = { selectors(t, typeOK, pos) }
}
}
}
t
}
def selectors(t: Tree, typeOK: boolean): Tree =
def selectors(t: Tree, typeOK: boolean, pos : Int): Tree =
if (typeOK && in.token == TYPE) {
in.nextToken();
SingletonTypeTree(t)
atPos(pos) { SingletonTypeTree(t) }
} else {
val t1 = Select(t, ident());
if (in.token == DOT) atPos(in.skipToken()) { selectors(t1, typeOK) }
val t1 = atPos(pos) { Select(t, ident()); }
if (in.token == DOT) { selectors(t1, typeOK, in.skipToken()) }
else t1
}
@ -389,7 +389,7 @@ import Tokens._;
*/
def qualId(): Tree = {
val id = atPos(in.currentPos) { Ident(ident()) }
if (in.token == DOT) atPos(in.skipToken()) { selectors(id, false) }
if (in.token == DOT) { selectors(id, false, in.skipToken()) }
else id
}

View File

@ -7,7 +7,6 @@ import scala.tools.nsc.util.{NameTransformer,Position,SourceFile};
import scala.collection.mutable.{HashMap,HashSet};
class SemanticTokens(val compiler: Compiler) {
import compiler._;
@ -223,73 +222,71 @@ class SemanticTokens(val compiler: Compiler) {
val tree1 = if (tree.original != null) tree.original; else tree;
buildT(tree1, tree.tpe);
def buildT( tree : Tree, tpe : Type) : Unit = if (tree.pos != Position.NOPOS) tpe match {
case tpe0 : TypeRef => tree match {
case apt : AppliedTypeTree =>
buildUse(tpe.symbol, apt.tpt.pos);
//System.err.println("APT: " + apt.tpt + " sym0=" + apt.tpt.symbol + " sym1=" + tpe0.sym + " " + " " + apt.args + " " + tpe0.args);
buildTs (apt.args, tpe0.args);
case ident : Ident => buildUse(tpe0.sym, ident.pos);
case select : Select =>
// System.err.println("BUILD_SELECT: " + select + " @ " + tpe0);
try {
build(select);
} catch {
case e : Error =>
System.err.println("BUILD_SELECT: " + select + " @ " + tpe0 + " " + unit.source.dbg(select.pos));
throw e;
}
case tpt : TypeTree =>
//System.err.println("UNKNOWN TPT0: " + tpe0 + " " + tpe0.args + " " + tpt);
case sft : SelectFromTypeTree =>
build(sft.qualifier); // XXX: broken
if (false) System.err.println("SFTT: " + sft + " sym=" + sft.symbol + " selector=" + sft.selector + " qual=" + sft.qualifier + " qual.sym=" +
sft.qualifier.symbol +
" qual.pos=" + unit.source.dbg(sft.qualifier.pos) + " symbol=" + sft.symbol + " type=" + tpe0 +
" type.sym=" + tpe0.symbol);
case _ => System.err.println("UNKNOWN TPT2: " + tree + " vs. " + tpe0 + " " + tree.getClass() + " " + unit.source.content(tree.pos));
}
case tpe0 : MethodType => tree match {
case tpt: TypeTree =>
if (tpt.original != null) buildT(tpt.original, tpe);
else {
System.err.println("UNKNOWN TPT3: " + tree + " vs. " + tpe0 + " " + unit.source.content(tree.pos));
}
case ident : Ident => buildT(ident, tpe0.resultType);
case select : Select => buildT(select, tpe0.resultType);
case _ => System.err.println("UNKNOWN TPE: " + tree + " vs. " + tpe0 + " " + tree.getClass());
}
case _ => // System.err.println("UNKNOWN: " + tree + " " + (if (tree != null) tree.getClass() else null) + " vs. " + tpe + " " + (if (tpe != null) tpe.getClass() else null) + " " + (if (tree != null) tree.pos else null));
};
case tpe0 : TypeRef => tree match {
case apt : AppliedTypeTree =>
buildUse(tpe.symbol, apt.tpt.pos);
//System.err.println("APT: " + apt.tpt + " sym0=" + apt.tpt.symbol + " sym1=" + tpe0.sym + " " + " " + apt.args + " " + tpe0.args);
buildTs (apt.args, tpe0.args);
case ident : Ident => buildUse(tpe0.sym, ident.pos);
case select : Select =>
// System.err.println("BUILD_SELECT: " + select + " @ " + tpe0);
try {
build(select);
} catch {
case e : Error =>
System.err.println("BUILD_SELECT: " + select + " @ " + tpe0 + " " + unit.source.dbg(select.pos));
throw e;
}
case tpt : TypeTree =>
//System.err.println("UNKNOWN TPT0: " + tpe0 + " " + tpe0.args + " " + tpt);
case sft : SelectFromTypeTree =>
build(sft.qualifier); // XXX: broken
if (false) System.err.println("SFTT: " + sft + " sym=" + sft.symbol + " selector=" + sft.selector + " qual=" + sft.qualifier + " qual.sym=" +
sft.qualifier.symbol +
" qual.pos=" + unit.source.dbg(sft.qualifier.pos) + " symbol=" + sft.symbol + " type=" + tpe0 +
" type.sym=" + tpe0.symbol);
case _ => System.err.println("UNKNOWN TPT2: " + tree + " vs. " + tpe0 + " " + tree.getClass() + " " + unit.source.content(tree.pos));
}
case tpe0 : MethodType => tree match {
case tpt: TypeTree =>
if (tpt.original != null) buildT(tpt.original, tpe);
else {
System.err.println("UNKNOWN TPT3: " + tree + " vs. " + tpe0 + " " + unit.source.content(tree.pos));
}
case ident : Ident => buildT(ident, tpe0.resultType);
case select : Select => buildT(select, tpe0.resultType);
case _ => System.err.println("UNKNOWN TPE: " + tree + " vs. " + tpe0 + " " + tree.getClass());
}
case _ => // System.err.println("UNKNOWN: " + tree + " " + (if (tree != null) tree.getClass() else null) + " vs. " + tpe + " " + (if (tpe != null) tpe.getClass() else null) + " " + (if (tree != null) tree.pos else null));
};
def buildTs(trees : List[Tree], types : List[Type]): Unit = if (!trees.isEmpty || !types.isEmpty) {
buildT (trees.head, types.head);
buildTs(trees.tail, types.tail);
};
case tree : AbsTypeDef =>
buildDef(tree.symbol, tree.namePos);
case tree : Bind =>
buildDef(tree.symbol, tree.pos);
build(tree.body);
buildT (trees.head, types.head);
buildTs(trees.tail, types.tail);
};
case tree : AbsTypeDef => buildDef(tree.symbol, tree.namePos);
case tree : Bind => buildDef(tree.symbol, tree.pos);
build(tree.body);
case tree : Ident => buildUse(tree.symbol, tree.pos);
case tree : Select =>
// System.err.println("SELECT: " + tree.qualifier + " ** " + tree.symbol + " " + selectPos(tree));
try {
build(tree.qualifier);
} catch {
case e : Error => System.err.println("SELECTQ: " + tree + " " + tree.qualifier + " " + unit.source.dbg(tree.qualifier.pos)); throw e;
}
try {
if (tree.pos >= unit.source.content.length)
System.err.println("BAD_SELECT_QUALIFIER " + tree + " @ " + tree.pos);
else buildUse(tree.symbol, selectPos(tree));
} catch {
case e : Error => System.err.println("SELECTU: " + tree + " " + tree.symbol + " " + unit.source.dbg(tree.pos)); throw e;
}
// System.err.println("SELECT: " + tree.qualifier + " ** " + tree.symbol + " " + selectPos(tree));
try {
build(tree.qualifier);
} catch {
case e : Error => System.err.println("SELECTQ: " + tree + " " + tree.qualifier + " " + unit.source.dbg(tree.qualifier.pos)); throw e;
}
try {
if (tree.pos >= unit.source.content.length)
System.err.println("BAD_SELECT_QUALIFIER " + tree + " @ " + tree.pos);
else buildUse(tree.symbol, selectPos(tree));
} catch {
case e : Error => System.err.println("SELECTU: " + tree + " " + tree.symbol + " " + unit.source.dbg(tree.pos)); throw e;
}
case tree : GenericApply =>
//System.err.println("APPLY-FUN: " + tree.fun0 + " " + tree.args0);
build(tree.fun0);
//System.err.println("APPLY-ARG: " + tree.fun0 + " " + tree.args0);
build(tree.args0);
build(tree.args0);
case tree : Typed => build(tree.expr); build(tree.tpt);
case tree : Import =>
//System.err.println("IMPORT: " + tree.expr + " " + tree.selectors);
@ -461,7 +458,7 @@ class SemanticTokens(val compiler: Compiler) {
}
str;
};
private object cursor {
object cursor {
var token : Token = end;
var offset : Int = 0;

View File

@ -0,0 +1,65 @@
package scala.tools.nsc.models;
import scala.tools.nsc.{Global => Compiler};
import scala.tools.nsc.symtab.{Flags,Names};
import scala.tools.nsc.util.{NameTransformer,Position,SourceFile};
import scala.collection.mutable.{HashMap,HashSet};
class Signatures(val compiler: Compiler) {
import compiler._;
class Signature(val name : String, val children : List[Signature]) {
def asString : String = name + "[" + asString0(children) + "]";
}
def sort(sigs : List[Signature]) =
sigs.sort((l0,l1) => l0.name.compareTo(l1.name) > 0);
def asString0(sigs : List[Signature]) : String = {
var ret = "";
for (val sig <- sort(sigs))
ret = ret + sig.asString;
ret;
}
def signature(unit : CompilationUnit): String = asString0(signature(unit.body, Nil));
def signature(trees : List[Tree]) : List[Signature] = {
var ret : List[Signature] = Nil;
for (val tree <- trees) ret = signature(tree, ret);
ret;
}
def signature(tree0 : Tree, rest : List[Signature]) : List[Signature] = tree0 match {
case tree : MemberDef => if (!tree.mods.isPrivate) {
val name = "" + tree.name + "::" + tree.mods;
val children : List[Signature] = (tree match {
case impl : ImplDef =>
val supers = new Signature("$$supers", signature(impl.impl.parents));
val body = new Signature("$$body", signature(impl.impl.body));
val ret = supers :: body :: Nil;
(impl match {
case cdef : ClassDef => new Signature("$$tparams", signature(cdef.tparams)) :: ret;
case _ => ret;
});
case vdef : ValOrDefDef =>
val ret = signature(vdef.tpt, Nil);
(vdef match {
case ddef : DefDef =>
val tparams = new Signature("$$tparams", signature(ddef.tparams));
var vparamss : List[Signature] = Nil;
for (val list <- ddef.vparamss) vparamss = signature(list) ::: vparamss;
tparams :: vparamss;
case _ => ret;
});
case pdef : PackageDef => signature(pdef.stats);
case _ => Nil;
});
new Signature(name, children) :: rest;
} else rest;
case tree : TypeTree => new Signature("" + tree.tpe, Nil) :: rest;
case _ => rest;
}
}