diff options
author | Paul Phillips <paulp@improving.org> | 2011-06-01 18:34:20 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-06-01 18:34:20 +0000 |
commit | 92a2fd5397e1c6f67677e96bbf427ab256ec37f9 (patch) | |
tree | 50c03041acfdf6081edb057e5d5aaaab65573e5c /src/compiler | |
parent | d838e10f7bad42762584a1390ef6fd85b4796d46 (diff) | |
download | scala-92a2fd5397e1c6f67677e96bbf427ab256ec37f9.tar.gz scala-92a2fd5397e1c6f67677e96bbf427ab256ec37f9.tar.bz2 scala-92a2fd5397e1c6f67677e96bbf427ab256ec37f9.zip |
More adjustments to repl parsing to accomodate ...
More adjustments to repl parsing to accomodate inaccurately positioned
parse trees. No review.
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/scala/tools/nsc/interpreter/IMain.scala | 58 |
1 files changed, 34 insertions, 24 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/IMain.scala b/src/compiler/scala/tools/nsc/interpreter/IMain.scala index 83a9669427..6a61f107c8 100644 --- a/src/compiler/scala/tools/nsc/interpreter/IMain.scala +++ b/src/compiler/scala/tools/nsc/interpreter/IMain.scala @@ -440,34 +440,44 @@ class IMain(val settings: Settings, protected val out: PrintWriter) extends Impo trees.last match { case _:Assign => // we don't want to include assignments case _:TermTree | _:Ident | _:Select => // ... but do want other unnamed terms. - // The position of the last tree - val lastpos0 = earliestPosition(trees.last) - // Oh boy, the parser throws away parens so "(2+2)" is mispositioned. - // So until we can fix the parser we'll have to go trawling. - val lastpos = { - val adjustment = (content take lastpos0).reverse takeWhile (ch => ch.isWhitespace || ch == '(') length; - lastpos0 - adjustment - } - // the source code split at the laboriously determined position. - val (l1, l2) = content splitAt lastpos - val prefix = if (l1.trim == "") "" else l1 + ";\n" val varName = if (synthetic) freshInternalVarName() else freshUserVarName() - // Note to self: val source needs to have this precise structure so that - // error messages print the user-submitted part without the "val res0 = " part. - val combined = prefix + "val " + varName + " =\n" + l2 - - repldbg(List( - " line" -> line, - " content" -> content, - " was" -> l2, - "combined" -> combined) map { - case (label, s) => label + ": '" + s + "'" - } mkString "\n" + val rewrittenLine = ( + // In theory this would come out the same without the 1-specific test, but + // it's a cushion against any more sneaky parse-tree position vs. code mismatches: + // this way such issues will only arise on multiple-statement repl input lines, + // which most people don't use. + if (trees.size == 1) "val " + varName + " =\n" + content + else { + // The position of the last tree + val lastpos0 = earliestPosition(trees.last) + // Oh boy, the parser throws away parens so "(2+2)" is mispositioned. + // So until we can fix the parser we'll have to go trawling. + val adjustment = ((content take lastpos0).reverse takeWhile { ch => + ch.isWhitespace || ch == '(' || ch == ')' + }).length + val lastpos = lastpos0 - adjustment + + // the source code split at the laboriously determined position. + val (l1, l2) = content splitAt lastpos + val prefix = if (l1.trim == "") "" else l1 + ";\n" + // Note to self: val source needs to have this precise structure so that + // error messages print the user-submitted part without the "val res0 = " part. + val combined = prefix + "val " + varName + " =\n" + l2 + + repldbg(List( + " line" -> line, + " content" -> content, + " was" -> l2, + "combined" -> combined) map { + case (label, s) => label + ": '" + s + "'" + } mkString "\n" + ) + combined + } ) - // Rewriting "foo ; bar ; 123" // to "foo ; bar ; val resXX = 123" - requestFromLine(combined, synthetic) match { + requestFromLine(rewrittenLine, synthetic) match { case Right(req) => return Right(req withOriginalLine line) case x => return x } |