Skip to content

Commit dc1cd96

Browse files
committed
Disentangled RangePositions from interactive.
This is a stepping stone to having range positions all the time, as well as to modularizing the presentation compiler. It does not enable range positions by default, only places them smoewhere where they can be.
1 parent 8dc1bc7 commit dc1cd96

File tree

20 files changed

+401
-409
lines changed

20 files changed

+401
-409
lines changed

src/compiler/scala/tools/ant/Scalac.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import org.apache.tools.ant.util.facade.{FacadeTaskHelper,
1919
ImplementationSpecificArgument}
2020

2121
import scala.tools.nsc.{Global, Settings, CompilerCommand}
22-
import scala.tools.nsc.interactive.RangePositions
2322
import scala.tools.nsc.io.{Path => SPath}
2423
import scala.tools.nsc.reporters.{Reporter, ConsoleReporter}
2524

@@ -509,10 +508,7 @@ class Scalac extends ScalaMatchingTask with ScalacShared {
509508
new Settings(error)
510509

511510
protected def newGlobal(settings: Settings, reporter: Reporter) =
512-
if (settings.Yrangepos.value)
513-
new Global(settings, reporter) with RangePositions
514-
else
515-
new Global(settings, reporter)
511+
Global(settings, reporter)
516512

517513
/*============================================================================*\
518514
** The big execute method **

src/compiler/scala/tools/nsc/Global.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import scala.collection.{ mutable, immutable }
1212
import io.{ SourceReader, AbstractFile, Path }
1313
import reporters.{ Reporter, ConsoleReporter }
1414
import util.{ ClassPath, MergedClassPath, StatisticsInfo, returning, stackTraceString, stackTraceHeadString }
15+
import scala.reflect.internal.RangePositions
1516
import scala.reflect.internal.util.{ OffsetPosition, SourceFile, NoSourceFile, BatchSourceFile, ScriptSourceFile }
1617
import scala.reflect.internal.pickling.{ PickleBuffer, PickleFormat }
1718
import symtab.{ Flags, SymbolTable, SymbolLoaders, SymbolTrackers }
@@ -1694,6 +1695,10 @@ class Global(var currentSettings: Settings, var reporter: Reporter)
16941695
def createJavadoc = false
16951696
}
16961697

1698+
class RangePositionGlobal(settings0: Settings, reporter0: Reporter) extends Global(settings0, reporter0) with RangePositions
1699+
16971700
object Global {
1698-
def apply(settings: Settings, reporter: Reporter): Global = new Global(settings, reporter)
1701+
def apply(settings: Settings, reporter: Reporter): Global =
1702+
if (settings.Yrangepos.value) new RangePositionGlobal(settings, reporter)
1703+
else new Global(settings, reporter)
16991704
}

src/compiler/scala/tools/nsc/Main.scala

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,24 @@
22
* Copyright 2005-2013 LAMP/EPFL
33
* @author Martin Odersky
44
*/
5-
6-
package scala.tools.nsc
7-
8-
import java.io.File
9-
import File.pathSeparator
10-
import scala.tools.nsc.io.AbstractFile
5+
package scala.tools
6+
package nsc
117

128
/** The main class for NSC, a compiler for the programming
139
* language Scala.
1410
*/
15-
object Main extends Driver with EvalLoop {
16-
17-
def resident(compiler: Global) {
18-
loop { line =>
19-
val args = line.split(' ').toList
20-
val command = new CompilerCommand(args, new Settings(scalacError))
21-
compiler.reporter.reset()
22-
new compiler.Run() compile command.files
23-
}
11+
class MainClass extends Driver with EvalLoop {
12+
def resident(compiler: Global): Unit = loop { line =>
13+
val command = new CompilerCommand(line split "\\s+" toList, new Settings(scalacError))
14+
compiler.reporter.reset()
15+
new compiler.Run() compile command.files
2416
}
2517

26-
override def processSettingsHook(): Boolean =
27-
if (settings.Yidedebug.value) {
28-
settings.Xprintpos.value = true
29-
settings.Yrangepos.value = true
30-
val compiler = new interactive.Global(settings, reporter)
31-
import compiler.{ reporter => _, _ }
32-
33-
val sfs = command.files map getSourceFile
34-
val reloaded = new interactive.Response[Unit]
35-
askReload(sfs, reloaded)
36-
37-
reloaded.get.right.toOption match {
38-
case Some(ex) => reporter.cancelled = true // Causes exit code to be non-0
39-
case None => reporter.reset() // Causes other compiler errors to be ignored
40-
}
41-
askShutdown()
42-
false
43-
}
44-
else true
45-
46-
override def newCompiler(): Global =
47-
if (settings.Yrangepos.value) new Global(settings, reporter) with interactive.RangePositions
48-
else Global(settings, reporter)
49-
18+
override def newCompiler(): Global = Global(settings, reporter)
5019
override def doCompile(compiler: Global) {
51-
if (settings.resident.value)
52-
resident(compiler)
20+
if (settings.resident.value) resident(compiler)
5321
else super.doCompile(compiler)
5422
}
5523
}
24+
25+
object Main extends MainClass { }

src/compiler/scala/tools/nsc/ast/Positions.scala

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import scala.reflect.internal.util.{ SourceFile, OffsetPosition }
66
trait Positions extends scala.reflect.internal.Positions {
77
self: Global =>
88

9-
def rangePos(source: SourceFile, start: Int, point: Int, end: Int) =
10-
new OffsetPosition(source, point)
11-
12-
def validatePositions(tree: Tree) {}
13-
149
class ValidatingPosAssigner extends PosAssigner {
1510
var pos: Position = _
1611
override def traverse(t: Tree) {

src/compiler/scala/tools/nsc/ast/Trees.scala

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,6 @@ import scala.reflect.internal.Flags.TRAIT
1616
import scala.compat.Platform.EOL
1717

1818
trait Trees extends scala.reflect.internal.Trees { self: Global =>
19-
20-
def treeLine(t: Tree): String =
21-
if (t.pos.isDefined && t.pos.isRange) t.pos.lineContent.drop(t.pos.column - 1).take(t.pos.end - t.pos.start + 1)
22-
else t.summaryString
23-
24-
def treeStatus(t: Tree, enclosingTree: Tree = null) = {
25-
val parent = if (enclosingTree eq null) " " else " P#%5s".format(enclosingTree.id)
26-
27-
"[L%4s%8s] #%-6s %-15s %-10s // %s".format(t.pos.safeLine, parent, t.id, t.pos.show, t.shortClass, treeLine(t))
28-
}
29-
def treeSymStatus(t: Tree) = {
30-
val line = if (t.pos.isDefined) "line %-4s".format(t.pos.safeLine) else " "
31-
"#%-5s %s %-10s // %s".format(t.id, line, t.shortClass,
32-
if (t.symbol ne NoSymbol) "(" + t.symbol.fullLocationString + ")"
33-
else treeLine(t)
34-
)
35-
}
36-
3719
// --- additional cases --------------------------------------------------------
3820
/** Only used during parsing */
3921
case class Parens(args: List[Tree]) extends Tree

src/compiler/scala/tools/nsc/ast/parser/Scanners.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,16 @@ trait Scanners extends ScannersCommon {
12971297
class ParensAnalyzer(unit: CompilationUnit, patches: List[BracePatch]) extends UnitScanner(unit, patches) {
12981298
val balance = mutable.Map(RPAREN -> 0, RBRACKET -> 0, RBRACE -> 0)
12991299

1300+
/** The source code with braces and line starts annotated with [NN] showing the index */
1301+
private def markedSource = {
1302+
val code = unit.source.content
1303+
val braces = code.indices filter (idx => "{}\n" contains code(idx)) toSet;
1304+
val mapped = code.indices map (idx => if (braces(idx)) s"${code(idx)}[$idx]" else "" + code(idx))
1305+
mapped.mkString("")
1306+
}
1307+
13001308
init()
1309+
log(s"ParensAnalyzer for ${unit.source} of length ${unit.source.content.length}\n```\n$markedSource\n```")
13011310

13021311
/** The offset of the first token on this line, or next following line if blank
13031312
*/

src/compiler/scala/tools/nsc/doc/DocParser.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,19 @@ package doc
99

1010
import reporters._
1111
import scala.reflect.internal.util._
12-
import interactive.RangePositions
1312
import DocParser.Parsed
1413

1514
/** A very minimal global customized for extracting `DocDefs`. It stops
1615
* right after parsing so it can read `DocDefs` from source code which would
1716
* otherwise cause the compiler to go haywire.
1817
*/
19-
class DocParser(settings: nsc.Settings, reporter: Reporter)
20-
extends Global(settings, reporter)
21-
with RangePositions {
22-
18+
class DocParser(settings: nsc.Settings, reporter: Reporter) extends RangePositionGlobal(settings, reporter) {
2319
def this(settings: Settings) = this(settings, new ConsoleReporter(settings))
2420
def this() = this(new Settings(Console println _))
2521

2622
// the usual global initialization
2723
locally { new Run() }
2824

29-
override def forScaladoc = true
3025
override protected def computeInternalPhases() {
3126
phasesSet += syntaxAnalyzer
3227
}

src/compiler/scala/tools/nsc/interactive/Global.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import scala.language.implicitConversions
2121

2222
/** The main class of the presentation compiler in an interactive environment such as an IDE
2323
*/
24-
class Global(settings: Settings, _reporter: Reporter, projectName: String = "") extends {
24+
class Global(settings: Settings, _reporter: Reporter, projectName: String = "") extends {
2525
/* Is the compiler initializing? Early def, so that the field is true during the
2626
* execution of the super constructor.
2727
*/
2828
private var initializing = true
29-
} with scala.tools.nsc.Global(settings, _reporter)
29+
} with RangePositionGlobal(settings, _reporter)
3030
with CompilerControl
3131
with RangePositions
3232
with ContextTrees

0 commit comments

Comments
 (0)