diff options
author | Paul Phillips <paulp@improving.org> | 2010-05-23 18:21:07 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-05-23 18:21:07 +0000 |
commit | 41d361a9d26cb550a11691a5b93fb7f3ab5223d3 (patch) | |
tree | 040c9276da673832295571af5585b60a846bf7c0 /src/compiler/scala/tools/nsc/ScriptRunner.scala | |
parent | 3bad6d54b19c9ccc01a491b6483bb205d855381a (diff) | |
download | scala-41d361a9d26cb550a11691a5b93fb7f3ab5223d3.tar.gz scala-41d361a9d26cb550a11691a5b93fb7f3ab5223d3.tar.bz2 scala-41d361a9d26cb550a11691a5b93fb7f3ab5223d3.zip |
Changed the script runner mechanism to alchemiz...
Changed the script runner mechanism to alchemize from AST atoms rather
than generating wrapper source, and fixed script position reporting.
This patch does not include a discussed change to mark some positions as
synthetic. Closes #3119, #3121. Review by milessabin.
Diffstat (limited to 'src/compiler/scala/tools/nsc/ScriptRunner.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/ScriptRunner.scala | 77 |
1 files changed, 9 insertions, 68 deletions
diff --git a/src/compiler/scala/tools/nsc/ScriptRunner.scala b/src/compiler/scala/tools/nsc/ScriptRunner.scala index da2738500b..a7d67a3af9 100644 --- a/src/compiler/scala/tools/nsc/ScriptRunner.scala +++ b/src/compiler/scala/tools/nsc/ScriptRunner.scala @@ -16,11 +16,9 @@ import io.{ Directory, File, Path, PlainFile } import java.lang.reflect.InvocationTargetException import java.net.URL import java.util.jar.{ JarEntry, JarOutputStream } -import java.util.regex.Pattern import scala.tools.util.PathResolver import scala.tools.nsc.reporters.{Reporter,ConsoleReporter} -import scala.tools.nsc.util.{ClassPath, CompoundSourceFile, BatchSourceFile, SourceFile, SourceFileFragment} /** An object that runs Scala code in script files. * @@ -48,8 +46,7 @@ import scala.tools.nsc.util.{ClassPath, CompoundSourceFile, BatchSourceFile, Sou * @todo It would be better if error output went to stderr instead * of stdout... */ -object ScriptRunner -{ +object ScriptRunner { /* While I'm chasing down the fsc and script bugs. */ def DBG(msg: Any) { System.err.println(msg.toString) @@ -68,6 +65,8 @@ object ScriptRunner case x => x } + def isScript(settings: Settings) = settings.script.value != "" + /** Choose a jar filename to hold the compiled version of a script. */ private def jarFileFor(scriptFile: String): File = { val name = @@ -118,22 +117,6 @@ object ScriptRunner /** Read the entire contents of a file as a String. */ private def contentsOfFile(filename: String) = File(filename).slurp() - /** Find the length of the header in the specified file, if - * there is one. The header part starts with "#!" or "::#!" - * and ends with a line that begins with "!#" or "::!#". - */ - private def headerLength(filename: String): Int = { - val headerPattern = Pattern.compile("""^(::)?!#.*(\r|\n|\r\n)""", Pattern.MULTILINE) - val fileContents = contentsOfFile(filename) - def isValid = List("#!", "::#!") exists (fileContents startsWith _) - - if (!isValid) 0 else { - val matcher = headerPattern matcher fileContents - if (matcher.find) matcher.end - else throw new IOException("script file does not close its header with !# or ::!#") - } - } - /** Split a fully qualified object name into a * package and an unqualified object name */ private def splitObjectName(fullname: String): (Option[String], String) = @@ -142,48 +125,6 @@ object ScriptRunner case idx => (Some(fullname take idx), fullname drop (idx + 1)) } - /** Code that is added to the beginning of a script file to make - * it a complete Scala compilation unit. - */ - protected def preambleCode(objectName: String): String = { - val (maybePack, objName) = splitObjectName(objectName) - val packageDecl = maybePack map ("package %s\n" format _) getOrElse ("") - - return """| - | object %s { - | def main(argv: Array[String]): Unit = { - | val args = argv - | new AnyRef { - |""".stripMargin.format(objName) - } - - /** Code that is added to the end of a script file to make - * it a complete Scala compilation unit. - */ - val endCode = """ - | } - | } - | } - |""".stripMargin - - /** Wrap a script file into a runnable object named - * <code>scala.scripting.Main</code>. - */ - def wrappedScript( - objectName: String, - filename: String, - getSourceFile: PlainFile => BatchSourceFile): SourceFile = - { - val preamble = new BatchSourceFile("<script preamble>", preambleCode(objectName).toCharArray) - val middle = { - val bsf = getSourceFile(PlainFile fromPath filename) - new SourceFileFragment(bsf, headerLength(filename), bsf.length) - } - val end = new BatchSourceFile("<script trailer>", endCode.toCharArray) - - new CompoundSourceFile(preamble, middle, end) - } - /** Compile a script using the fsc compilation daemon. * * @param settings ... @@ -242,12 +183,15 @@ object ScriptRunner settings.outdir.value = compiledPath.path if (settings.nocompdaemon.value) { + /** Setting settings.script.value informs the compiler this is not a + * self contained compilation unit. + */ + settings.script.value = scriptMain(settings) val reporter = new ConsoleReporter(settings) val compiler = newGlobal(settings, reporter) val cr = new compiler.Run - val wrapped = wrappedScript(scriptMain(settings), scriptFile, compiler getSourceFile _) - cr compileSources List(wrapped) + cr compile List(scriptFile) if (reporter.hasErrors) None else Some(compiledPath) } else if (compileWithDaemon(settings, scriptFile)) Some(compiledPath) @@ -294,10 +238,7 @@ object ScriptRunner val classpath = pr.asURLs :+ File(compiledLocation).toURL try { - ObjectRunner.run( - classpath, - scriptMain(settings), - scriptArgs) + ObjectRunner.run(classpath, scriptMain(settings), scriptArgs) true } catch { |