Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 219 lines (198 sloc) 7.888 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/* NSC -- new Scala compiler
* Copyright 2009-2013 Typesafe/Scala Solutions and LAMP/EPFL
* @author Martin Odersky
*/
package scala.tools.nsc
package interactive

import scala.reflect.internal.util._
import scala.tools.nsc.reporters._
import scala.tools.nsc.io._
import scala.tools.nsc.scratchpad.SourceInserter
import java.io.FileWriter

/** Interface of interactive compiler to a client such as an IDE
*/
object REPL {

  val versionMsg = "Scala compiler " +
    Properties.versionString + " -- " +
    Properties.copyrightString

  val prompt = "> "

  var reporter: ConsoleReporter = _

  private def replError(msg: String) {
    reporter.error(/*new Position */FakePos("scalac"),
                   msg + "\n scalac -help gives more information")
  }

  def process(args: Array[String]) {
    val settings = new Settings(replError)
    reporter = new ConsoleReporter(settings)
    val command = new CompilerCommand(args.toList, settings)
    if (command.settings.version.value)
      reporter.echo(versionMsg)
    else {
      try {
        object compiler extends Global(command.settings, reporter) {
// printTypings = true
        }
        if (reporter.hasErrors) {
          reporter.flush()
          return
        }
        if (command.shouldStopWithInfo) {
          reporter.echo(command.getInfoMessage(compiler))
        } else {
          run(compiler)
        }
      } catch {
        case ex @ FatalError(msg) =>
          if (true || command.settings.debug.value) // !!!
            ex.printStackTrace()
          reporter.error(null, "fatal error: " + msg)
      }
    }
  }

  def main(args: Array[String]) {
    process(args)
    sys.exit(if (reporter.hasErrors) 1 else 0)
  }

  def loop(action: (String) => Unit) {
    Console.print(prompt)
    try {
      val line = Console.readLine()
      if (line.length() > 0) {
        action(line)
      }
      loop(action)
    }
    catch {
      case _: java.io.EOFException => //nop
    }
  }

  /** Commands:
*
* reload file1 ... fileN
* typeat file off1 off2?
* complete file off1 off2?
*/
  def run(comp: Global) {
    val reloadResult = new Response[Unit]
    val typeatResult = new Response[comp.Tree]
    val completeResult = new Response[List[comp.Member]]
    val typedResult = new Response[comp.Tree]
    val structureResult = new Response[comp.Tree]
    @deprecated("SI-6458: Instrumentation logic will be moved out of the compiler.","2.10.0")
    val instrumentedResult = new Response[(String, Array[Char])]

    def makePos(file: String, off1: String, off2: String) = {
      val source = toSourceFile(file)
      comp.rangePos(source, off1.toInt, off1.toInt, off2.toInt)
    }

    def doTypeAt(pos: Position) {
      comp.askTypeAt(pos, typeatResult)
      show(typeatResult)
    }

    def doComplete(pos: Position) {
      comp.askTypeCompletion(pos, completeResult)
      show(completeResult)
    }

    def doStructure(file: String) {
      comp.askParsedEntered(toSourceFile(file), false, structureResult)
      show(structureResult)
    }

    /** Write instrumented source file to disk.
* @param iFullName The full name of the first top-level object in source
* @param iContents An Array[Char] containing the instrumented source
* @return The name of the instrumented source file
*/
    @deprecated("SI-6458: Instrumentation logic will be moved out of the compiler.","2.10.0")
    def writeInstrumented(iFullName: String, suffix: String, iContents: Array[Char]): String = {
      val iSimpleName = iFullName drop ((iFullName lastIndexOf '.') + 1)
      val iSourceName = iSimpleName + suffix
      val ifile = new FileWriter(iSourceName)
      ifile.write(iContents)
      ifile.close()
      iSourceName
    }

    /** The method for implementing worksheet functionality.
* @param arguments a file name, followed by optional command line arguments that are passed
* to the compiler that processes the instrumented source.
* @param line A line number that controls uop to which line results should be produced
* If line = -1, results are produced for all expressions in the worksheet.
* @return The generated file content containing original source in the left column
* and outputs in the right column, or None if the presentation compiler
* does not respond to askInstrumented.
*/
    @deprecated("SI-6458: Instrumentation logic will be moved out of the compiler.","2.10.0")
    def instrument(arguments: List[String], line: Int): Option[(String, String)] = {
      val source = toSourceFile(arguments.head)
      // strip right hand side comment column and any trailing spaces from all lines
      val strippedContents = SourceInserter.stripRight(source.content)
      val strippedSource = new BatchSourceFile(source.file, strippedContents)
      println("stripped source = "+strippedSource+":"+strippedContents.mkString)
      comp.askReload(List(strippedSource), reloadResult)
      comp.askInstrumented(strippedSource, line, instrumentedResult)
      using(instrumentedResult) {
        case (iFullName, iContents) =>
          println(s"instrumented source $iFullName = ${iContents.mkString}")
          val iSourceName = writeInstrumented(iFullName, "$instrumented.scala", iContents)
          val sSourceName = writeInstrumented(iFullName, "$stripped.scala", strippedContents)
          (iSourceName, sSourceName)
/*
* val vdirOpt = compileInstrumented(iSourceName, arguments.tail)
runInstrumented(vdirOpt, iFullName, strippedSource.content)
*/
      }
    }

    loop { line =>
      (line split " ").toList match {
        case "reload" :: args =>
          comp.askReload(args map toSourceFile, reloadResult)
          show(reloadResult)
        case "reloadAndAskType" :: file :: millis :: Nil =>
          comp.askReload(List(toSourceFile(file)), reloadResult)
          Thread.sleep(millis.toInt)
          println("ask type now")
          comp.askLoadedTyped(toSourceFile(file), typedResult)
          typedResult.get
        case List("typeat", file, off1, off2) =>
          doTypeAt(makePos(file, off1, off2))
        case List("typeat", file, off1) =>
          doTypeAt(makePos(file, off1, off1))
        case List("complete", file, off1, off2) =>
          doComplete(makePos(file, off1, off2))
        case List("complete", file, off1) =>
          doComplete(makePos(file, off1, off1))
        case "instrument" :: arguments =>
          println(instrument(arguments, -1))
        case "instrumentTo" :: line :: arguments =>
          println(instrument(arguments, line.toInt))
        case List("quit") =>
          comp.askShutdown()
          sys.exit(1)
        case List("structure", file) =>
          doStructure(file)
        case _ =>
          print("""Available commands:
| reload <file_1> ... <file_n>
| reloadAndAskType <file> <sleep-ms>
| typed <file>
| typeat <file> <start-pos> <end-pos>
| typeat <file> <pos>
| complete <file> <start-pos> <end-pos>
| compile <file> <pos>
| instrument <file> <arg>*
| instrumentTo <line-num> <file> <arg>*
| structure <file>
| quit
|""".stripMargin)
      }
    }
  }

  def toSourceFile(name: String) = new BatchSourceFile(new PlainFile(new java.io.File(name)))

  def using[T, U](svar: Response[T])(op: T => U): Option[U] = {
    val res = svar.get match {
      case Left(result) => Some(op(result))
      case Right(exc) => exc.printStackTrace; println("ERROR: "+exc); None
    }
    svar.clear()
    res
  }

  def show[T](svar: Response[T]) = using(svar)(res => println("==> "+res))
}
Something went wrong with that request. Please try again.