summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLex Spoon <lex@lexspoon.org>2007-10-02 16:57:28 +0000
committerLex Spoon <lex@lexspoon.org>2007-10-02 16:57:28 +0000
commit40884972d9de5933503cd1f75c1a8f42360a8b62 (patch)
tree6f59dcff6d225ec51e6cba2ca3777d0b1a0eca61
parent0b00cb9fc3ca6f186157703073e0432857b0e7cb (diff)
downloadscala-40884972d9de5933503cd1f75c1a8f42360a8b62.tar.gz
scala-40884972d9de5933503cd1f75c1a8f42360a8b62.tar.bz2
scala-40884972d9de5933503cd1f75c1a8f42360a8b62.zip
Minor changes to improve the output when
there are type errors in the user's code
-rw-r--r--src/compiler/scala/tools/nsc/Interpreter.scala49
-rw-r--r--test/files/run/interpreter.check37
2 files changed, 54 insertions, 32 deletions
diff --git a/src/compiler/scala/tools/nsc/Interpreter.scala b/src/compiler/scala/tools/nsc/Interpreter.scala
index 7805fa32da..c4e67b24e6 100644
--- a/src/compiler/scala/tools/nsc/Interpreter.scala
+++ b/src/compiler/scala/tools/nsc/Interpreter.scala
@@ -207,6 +207,19 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
private def clean(str: String) =
truncPrintString(Interpreter.stripWrapperGunk(str))
+ /** Indent some code by the width of the scala> prompt.
+ * This way, compiler error messages read beettr.
+ */
+ def indentCode(code: String) = {
+ val spaces = " "
+
+ stringFrom(str =>
+ for(line <- code.lines) {
+ str.print(spaces)
+ str.println(line)
+ })
+ }
+
/** Compute imports that allow definitions from previous
* requests to be visible in a new request. Returns
@@ -350,7 +363,7 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
var justNeedsMore = false
reporter.withIncompleteHandler((pos,msg) => {justNeedsMore = true}) {
// simple parse: just parse it, nothing else
- def simpleParse(code: String): (List[Tree]) = {
+ def simpleParse(code: String): List[Tree] = {
reporter.reset
val unit =
new CompilationUnit(
@@ -360,12 +373,13 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
(xxx._2)
}
val (trees) = simpleParse(line)
- if (justNeedsMore) {
- None
- } else if (reporter.hasErrors)
+ if (reporter.hasErrors) {
Some(Nil) // the result did not parse, so stop
- else
+ } else if (justNeedsMore) {
+ None
+ } else {
Some(trees)
+ }
}
}
@@ -387,11 +401,6 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
/** Build a request from the user. <code>trees</code> is <code>line</code>
* after being parsed.
- *
- * @param trees ..
- * @param line ..
- * @param lineName ..
- * @return ..
*/
private def buildRequest(trees: List[Tree], line: String, lineName: String): Request =
trees match {
@@ -435,10 +444,9 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
new compiler.Run // initialize the compiler
// parse
- val trees = parse(line) match {
+ val trees = parse(indentCode(line)) match {
case None => return IR.Incomplete
case (Some(Nil)) => return IR.Error // parse error or empty input
- case _ if reporter.hasErrors => return IR.Error
case Some(trees) => trees
}
@@ -475,10 +483,10 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
/** Bind a specified name to a specified value. The name may
* later be used by expressions passed to interpret.
*
- * @param name ...
- * @param boundType ...
- * @param value ...
- * @return ...
+ * @param name the variable name to bind
+ * @param boundType the type of the variable, as a string
+ * @param value the object value to bind to it
+ * @return an indication of whether the binding succeeded
*/
def bind(name: String, boundType: String, value: Any): IR.Result = {
val binderName = "binder" + binderNum
@@ -655,9 +663,9 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
// the variable to compute, if any
if (needsVarName)
- code.print(" val " + varName + " = ")
+ code.println(" val " + varName + " = ")
- code.println(toCompute)
+ code.println(indentCode(toCompute))
code.println(importsTrailer)
@@ -797,12 +805,15 @@ class Interpreter(val settings: Settings, out: PrintWriter) {
override val needsVarName = true
/** Perform the assignment, and then return the new value */
- override def toCompute = "{" + line + ";" + lhs + "}"
+ override def toCompute = "{\n" + line + "\n;\n" + lhs + "\n}"
/** Print out lhs instead of the generated varName */
override def resultExtractionCode(code: PrintWriter) {
code.print(" + \"" + lhs + ": " + typeOf(compiler.encode(varName)) +
" = \" + " + fullPath(varName) + " + \"\\n\"")
+// override def resultExtractionCode(code: PrintWriter) {
+// {wrapperObj; lhs}
+// }
}
}
diff --git a/test/files/run/interpreter.check b/test/files/run/interpreter.check
index 1778b51a87..31dadfd65b 100644
--- a/test/files/run/interpreter.check
+++ b/test/files/run/interpreter.check
@@ -22,8 +22,8 @@ scala> four: anotherint = 4
scala> <console>:5: error: type mismatch;
found : java.lang.String("hello")
required: anotherint
-val bogus: anotherint = "hello"
- ^
+ val bogus: anotherint = "hello"
+ ^
scala> defined trait PointlessTrait
@@ -135,29 +135,40 @@ scala>
scala>
scala>
scala> <console>:1: error: '=' expected but '=>' found.
-def x => y => z
- ^
+ def x => y => z
+ ^
<console>:1: error: illegal start of simple expression
-def x => y => z
- ^
+ def x => y => z
+ ^
scala> <console>:1: error: identifier expected but integer literal found.
-[1,2,3]
- ^
-<console>:1: error: ']' expected but eof found.
-[1,2,3]
- ^
+ [1,2,3]
+ ^
+<console>:1: error: ']' expected but ';' found.
+ [1,2,3]
+ ^
scala>
scala>
scala>
scala> | | | | res2: scala.xml.Elem =
<a>
-<b d="dd" c="c"></b></a>
+ <b d="dd" c="c"></b></a>
scala>
-scala> | | You typed two blank lines. Starting a new command.
+scala>
+scala> | | | |
+scala>
+scala>
+scala>
+scala> | | | res3: java.lang.String =
+
+ hello
+ there
+
scala>
+scala> | | You typed two blank lines. Starting a new command.
+
scala>
scala>