Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor to traits and apply namer

Parsing and TagGeneration are now traits that are mixed into SCTags so that
they can have direct access to the compiler instead of messing around with the
implicit arguments. This also helps alot with the types that have to be
written down.

The namer stage is also run on the code now so that symbols in the AST are
populated. This will help with adding additional info to the tags file.

darcs-hash:20080120205424-8d81a-0a6519ab892952c89d1d7efc85858e61abc1b245.gz
  • Loading branch information...
commit 61861b1b85c7cf9b32d1a1a1aba861aee449e740 1 parent d2f18f4
@greedy greedy authored
View
28 src/main/scala/net/pmonk/tools/sctags/Parsing.scala
@@ -8,19 +8,25 @@ import scala.tools.nsc.util.BatchSourceFile
import java.io.File;
-object parse {
- def apply(af: AbstractFile)(implicit compiler: Global): Global#Tree =
- apply(new compiler.CompilationUnit(new BatchSourceFile(af)))(compiler)
+trait Parsing { this: SCTags.type =>
+ import compiler.syntaxAnalyzer._
+ import compiler._
+ object parse {
+ def apply(af: AbstractFile): Tree =
+ apply(new CompilationUnit(new BatchSourceFile(af)))
- def apply(f: File)(implicit compiler: Global): Global#Tree =
- apply(AbstractFile.getFile(f))(compiler)
+ def apply(f: File): Tree =
+ apply(AbstractFile.getFile(f))
- def apply(fname: String)(implicit compiler: Global): Global#Tree =
- apply(AbstractFile.getFile(fname))(compiler)
+ def apply(fname: String): Tree =
+ apply(AbstractFile.getFile(fname))
- def apply(cu: Global#CompilationUnit)(implicit compiler: Global): Global#Tree = {
- new compiler.Run
- val parser = new compiler.syntaxAnalyzer.UnitParser(cu.asInstanceOf[compiler.CompilationUnit])
- parser.compilationUnit
+ def apply(cu: CompilationUnit): Tree = {
+ new compiler.Run
+ val parser = new UnitParser(cu)
+ val tree = parser.compilationUnit
+ compiler.analyzer.newNamer(compiler.analyzer.rootContext(cu, tree, false)).enterSym(tree)
+ tree
+ }
}
}
View
6 src/main/scala/net/pmonk/tools/sctags/SCTags.scala
@@ -14,7 +14,9 @@ import java.io.PrintStream
import java.text.Collator
-object SCTags extends ApplicationWithOptions {
+object SCTags extends ApplicationWithOptions
+ with Parsing with TagGeneration
+{
class CaseInsensitiveOrder(val self: String) extends Ordered[String] {
def compare(that: String) = self.compareToIgnoreCase(that)
@@ -46,7 +48,7 @@ object SCTags extends ApplicationWithOptions {
def error(str: String) = System.err.println("Error: " + str);
val settings = new Settings(error);
val reporter = new StoreReporter;
- implicit val compiler = new Global(settings, reporter);
+ val compiler = new Global(settings, reporter);
def run(fnames: Seq[String]) {
val files = new ListBuffer[File]
View
99 src/main/scala/net/pmonk/tools/sctags/TagGeneration.scala
@@ -6,65 +6,68 @@ import scala.tools.nsc.symtab.Names
import scala.collection.mutable.ListBuffer
-object generateTags {
+trait TagGeneration { this: SCTags.type =>
+ import compiler._
+ object generateTags {
- def apply[T <: Trees with Names](tree: T#Tree)(implicit compiler: T): Seq[Tag] = {
+ def apply[T <: Global](tree: Global#Tree): Seq[Tag] = {
- class GenTagTraverser extends compiler.Traverser {
- val _tags = new ListBuffer[Tag]
- def tags: Seq[Tag] = _tags.toList
+ class GenTagTraverser extends compiler.Traverser {
+ val _tags = new ListBuffer[Tag]
+ def tags: Seq[Tag] = _tags.toList
- import compiler._;
+ import compiler._;
- def source(tree: Tree): Option[Tuple2[String,String]] = {
- tree.pos.source match {
- case Some(source) if tree.pos.lineContent != null =>
- Some((source.path, "/^" + tree.pos.lineContent + "$/"));
- case _ => None;
+ def source(tree: Tree): Option[Tuple2[String,String]] = {
+ tree.pos.source match {
+ case Some(source) if (tree.pos.lineContent != null && tree.symbol != NoSymbol) =>
+ Some((source.path, "/^" + tree.pos.lineContent + "$/"));
+ case _ => None;
+ }
}
- }
- def tag_(file: String, address: String)(name: Name, kind: char, fields: Tuple2[String, String]*) = {
- if (!name.decode.contains('$') && !name.decode.equals("this")) {
- val withKind = Seq.singleton("kind"->kind.toString).projection.append(fields)
- _tags += new Tag(name.decode, file, address, withKind: _*)
+ def tag_(file: String, address: String)(name: Name, kind: char, fields: Tuple2[String, String]*) {
+ if (!name.decode.contains('$') && !name.decode.equals("this")) {
+ val withKind = Seq.singleton("kind"->kind.toString).projection.append(fields)
+ _tags += new Tag(name.decode, file, address, withKind: _*)
+ }
}
- }
- override def traverse(t: Tree) {
- source(t) match {
- case Some((file, address)) =>
- def tag = tag_(file, address)_
- t match {
- case ModuleDef(mods, name, _) =>
- tag(name, 'o');
- case ClassDef(mods, name, tparams, _) => {
- var kind = if (mods.isCase) {
- 'C'
- } else if (mods.isTrait) {
- 'T'
- } else {
- 'c'
- }
- tag(name, kind);
- };
- case ValDef(mods, name, _, _) =>
- tag(name, 'v')
- case DefDef(mods, name, _, _, _, _) =>
- tag(name, 'm')
- case TypeDef(mods, name, _, _) =>
- tag(name, 't')
- case _ => ();
- }
- case _ => ();
+ override def traverse(t: Tree) {
+ source(t) match {
+ case Some((file, address)) =>
+ def tag = tag_(file, address)_
+ t match {
+ case ModuleDef(mods, name, _) =>
+ tag(name, 'o');
+ case classDef @ ClassDef(mods, name, tparams, _) => {
+ var kind = if (mods.isCase) {
+ 'C'
+ } else if (mods.isTrait) {
+ 'T'
+ } else {
+ 'c'
+ }
+ tag(name, kind);
+ };
+ case ValDef(mods, name, _, _) =>
+ tag(name, 'v')
+ case DefDef(mods, name, _, _, _, _) =>
+ tag(name, 'm')
+ case TypeDef(mods, name, _, _) =>
+ tag(name, 't')
+ case _ => ();
+ }
+ case _ => ();
+ }
+ super.traverse(t)
}
- super.traverse(t)
}
- }
- val traverser = new GenTagTraverser
- traverser.traverse(tree.asInstanceOf[compiler.Tree])
- traverser.tags
+ val traverser = new GenTagTraverser
+ traverser.traverse(tree.asInstanceOf[compiler.Tree])
+ traverser.tags
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.