From 47d6dff4ebc6c91fbe4eb375c2edfec7aeea1863 Mon Sep 17 00:00:00 2001 From: Lex Spoon Date: Fri, 19 May 2006 13:13:12 +0000 Subject: print error messages using line numbers in the ... print error messages using line numbers in the script file --- src/compiler/scala/tools/nsc/MainScript.scala | 106 ++++++++++++++------------ 1 file changed, 56 insertions(+), 50 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/MainScript.scala b/src/compiler/scala/tools/nsc/MainScript.scala index cbd7526d1c..f95059c3e5 100644 --- a/src/compiler/scala/tools/nsc/MainScript.scala +++ b/src/compiler/scala/tools/nsc/MainScript.scala @@ -6,7 +6,9 @@ package scala.tools.nsc -import java.io.{BufferedReader,FileReader} +import java.io.{BufferedReader, FileReader, File} +import scala.tools.nsc.util._ +import scala.tools.nsc.io._ /** A main routine to support putting Scala code into scripts. * @@ -32,49 +34,39 @@ import java.io.{BufferedReader,FileReader} * of stdout.... */ object MainScript { - /** Read the contents of the specified file, skipping the header - * part if there is one. The header part starts with "#!" - * and ends with a line that begins with "!#". - */ - val startRegexp = "^(::)?#!.*" - val endRegexp = "^(::)?!#.*" - - def readFileSkippingHeader(filename: String): String = { - val file = - new BufferedReader( - new FileReader(filename)) - val contents = new StringBuffer - - // skip the header, if there is one - val firstLine = file.readLine - if (firstLine == null) - return "" - else if (firstLine.matches(startRegexp)) { - // skip until !# is seen - def lp: Unit = { - val line = file.readLine - if (line == null) - () - else if (!line.matches(endRegexp)) - lp - } - lp - } - else - contents.append(firstLine) - - // now read the rest of the file - def lp: Unit = { - val line = file.readLine - if (line == null) - return () - contents.append(line) - contents.append("\n") - lp + /** Read the entire contents of a file as a String. */ + private def contentsOfFile(filename: String): String = { + val strbuf = new StringBuffer + val reader = new FileReader(filename) + val cbuf = new Array[Char](1024) + while(true) { + val n = reader.read(cbuf) + if(n <= 0) + return strbuf.toString + strbuf.append(cbuf, 0, n) } - lp + throw new Error("impossible") + } + + /** 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 "!#" ar "::!#". + */ + def headerLength(filename: String): Int = { + import java.util.regex._ + + val fileContents = contentsOfFile(filename) + + if(!(fileContents.startsWith("#!") || fileContents.startsWith("::#!"))) + return 0 + + val matcher = + (Pattern.compile("^(::)?!#.*(\\r|\\n|\\r\\n)", Pattern.MULTILINE) + .matcher(fileContents)) + if(! matcher.find) + throw new Error("script file does not close its header with !# or ::!#") - contents.toString + return matcher.end } /** Print a usage message and then exit. */ @@ -121,6 +113,26 @@ object MainScript { } } + + def wrappedScript(filename: String): SourceFile = { + val preamble = + new SourceFile("