diff options
author | Paul Phillips <paulp@improving.org> | 2011-09-01 02:31:55 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-09-01 02:31:55 +0000 |
commit | 9954de923ef0c87fcffacabe3e18c31bf7f69f5e (patch) | |
tree | 8ab55c2424b823771ee55b43c67581f86310a981 /test/files/pos/CustomGlobal.scala | |
parent | 0377cad8c6a26be5b6dd59323dd171634400005a (diff) | |
download | scala-9954de923ef0c87fcffacabe3e18c31bf7f69f5e.tar.gz scala-9954de923ef0c87fcffacabe3e18c31bf7f69f5e.tar.bz2 scala-9954de923ef0c87fcffacabe3e18c31bf7f69f5e.zip |
Made it possible to supply a custom Global to t...
Made it possible to supply a custom Global to the core scala runners.
The absence of "Global pluggability", combined with the fact that most
of the functionality in Global is unnecessarily rigid due to the phases
being implemented as objects, means that it has been close to impossible
to do interesting compiler development in a way which doesn't require
modifying the scalac source tree. This then leaves you continually
subject to punishment by code drift as the various places you were
forced to modify change out from under you.
This is somewhat less true now, thanks to new option: -Yglobal-class
The primary wielders of Global (fsc/scala/scalac) now instantiate
the compiler via a (Settings, Reporter) => Global factory method in
the Global companion. If -Yglobal-class was given, that class (which
must have a (Settings, Reporter) constructor) will be instantiated if
possible, falling back on the standard one. See
test/files/pos/CustomGlobal.scala
for a working example. (It's not in run because I would have to be able
to give partest a different set of flags for successive compiles in the
same test.) Review by odersky.
Diffstat (limited to 'test/files/pos/CustomGlobal.scala')
-rw-r--r-- | test/files/pos/CustomGlobal.scala | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/test/files/pos/CustomGlobal.scala b/test/files/pos/CustomGlobal.scala new file mode 100644 index 0000000000..30bf227950 --- /dev/null +++ b/test/files/pos/CustomGlobal.scala @@ -0,0 +1,33 @@ +package custom + +import scala.tools.nsc._, reporters._, typechecker._ + +/** Demonstration of a custom Global with a custom Typer, + * decoupled from trunk. Demonstration: + * +{{{ +scalac -d . CustomGlobal.scala && scala -nc -Yglobal-class custom.CustomGlobal \ + -e 'class Bippy(x: Int) ; def f = new Bippy(5)' + +I'm typing a Bippy! It's a ClassDef. +I'm typing a Bippy! It's a Ident. +I'm typing a Bippy! It's a DefDef. +}}} + * + */ +class CustomGlobal(currentSettings: Settings, reporter: Reporter) extends Global(currentSettings, reporter) { + override lazy val analyzer = new { + val global: CustomGlobal.this.type = CustomGlobal.this + } with Analyzer { + override def newTyper(context: Context): Typer = new CustomTyper(context) + + class CustomTyper(context : Context) extends Typer(context) { + override def typed(tree: Tree, mode: Int, pt: Type): Tree = { + if (tree.summaryString contains "Bippy") + println("I'm typing a Bippy! It's a " + tree.shortClass + ".") + + super.typed(tree, mode, pt) + } + } + } +} |