@@ -10,16 +10,22 @@ import Decorators._
10
10
import ImportInfo .withRootImports
11
11
import parsing .JavaParsers .JavaParser
12
12
import parsing .Parsers .Parser
13
+ import parsing .{Parser => ParserPhase }
13
14
import config .Config
14
15
import config .Printers .{typr , default }
15
16
import util .Stats ._
16
17
import util .{ SourcePosition , NoSourcePosition }
17
18
import scala .util .control .NonFatal
18
19
import ast .Trees ._
19
20
20
- class FrontEnd extends Phase {
21
+ /**
22
+ *
23
+ * @param addRootImports Set to false in the REPL. Calling [[ImportInfo.withRootImports ]] on the [[Context ]]
24
+ * for each [[CompilationUnit ]] causes dotty.tools.repl.ScriptedTests to fail.
25
+ */
26
+ class TyperPhase (addRootImports : Boolean = true ) extends Phase {
21
27
22
- override def phaseName : String = FrontEnd .name
28
+ override def phaseName : String = TyperPhase .name
23
29
override def isTyper : Boolean = true
24
30
import ast .tpd
25
31
@@ -28,43 +34,14 @@ class FrontEnd extends Phase {
28
34
/** The contexts for compilation units that are parsed but not yet entered */
29
35
private var remaining : List [Context ] = Nil
30
36
31
- /** The position of the first XML literal encountered while parsing,
32
- * NoSourcePosition if there were no XML literals.
33
- */
34
- private var firstXmlPos : SourcePosition = NoSourcePosition
35
-
36
37
/** Does a source file ending with `<name>.scala` belong to a compilation unit
37
38
* that is parsed but not yet entered?
38
39
*/
39
40
def stillToBeEntered (name : String ): Boolean =
40
41
remaining.exists(_.compilationUnit.toString.endsWith(name + " .scala" ))
41
42
42
- def monitor (doing : String )(body : => Unit )(using Context ): Unit =
43
- try body
44
- catch
45
- case NonFatal (ex) =>
46
- report.echo(s " exception occurred while $doing ${ctx.compilationUnit}" )
47
- throw ex
48
-
49
- def parse (using Context ): Unit = monitor(" parsing" ) {
50
- val unit = ctx.compilationUnit
51
-
52
- unit.untpdTree =
53
- if (unit.isJava) new JavaParser (unit.source).parse()
54
- else {
55
- val p = new Parser (unit.source)
56
- // p.in.debugTokenStream = true
57
- val tree = p.parse()
58
- if (p.firstXmlPos.exists && ! firstXmlPos.exists)
59
- firstXmlPos = p.firstXmlPos
60
- tree
61
- }
62
-
63
- val printer = if (ctx.settings.Xprint .value.contains(" parser" )) default else typr
64
- printer.println(" parsed:\n " + unit.untpdTree.show)
65
- if (Config .checkPositions)
66
- unit.untpdTree.checkPos(nonOverlapping = ! unit.isJava && ! ctx.reporter.hasErrors)
67
- }
43
+ // Run regardless of parsing errors
44
+ override def isRunnable (implicit ctx : Context ): Boolean = true
68
45
69
46
def enterSyms (using Context ): Unit = monitor(" indexing" ) {
70
47
val unit = ctx.compilationUnit
@@ -103,19 +80,26 @@ class FrontEnd extends Phase {
103
80
override def runOn (units : List [CompilationUnit ])(using Context ): List [CompilationUnit ] =
104
81
val unitContexts =
105
82
for unit <- units yield
106
- report.inform(s " compiling ${unit.source}" )
107
- ctx.fresh.setCompilationUnit(unit).withRootImports
108
- unitContexts.foreach(parse(using _))
109
- record(" parsedTrees" , ast.Trees .ntrees)
83
+ val newCtx = ctx.fresh.setPhase(this .start).setCompilationUnit(unit)
84
+ report.inform(s " typing ${unit.source}" )
85
+ if (addRootImports)
86
+ newCtx.withRootImports
87
+ else
88
+ newCtx
89
+
110
90
remaining = unitContexts
111
91
while remaining.nonEmpty do
112
92
enterSyms(using remaining.head)
113
93
remaining = remaining.tail
114
-
115
- if firstXmlPos.exists && ! defn.ScalaXmlPackageClass .exists then
116
- report.error(""" To support XML literals, your project must depend on scala-xml.
117
- |See https://github.com/scala/scala-xml for more information.""" .stripMargin,
118
- firstXmlPos)
94
+ val firstXmlPos = ctx.base.parserPhase match {
95
+ case p : ParserPhase =>
96
+ if p.firstXmlPos.exists && ! defn.ScalaXmlPackageClass .exists then
97
+ report.error(
98
+ """ To support XML literals, your project must depend on scala-xml.
99
+ |See https://github.com/scala/scala-xml for more information.""" .stripMargin,
100
+ p.firstXmlPos)
101
+ case _ =>
102
+ }
119
103
120
104
unitContexts.foreach(typeCheck(using _))
121
105
record(" total trees after typer" , ast.Trees .ntrees)
@@ -128,6 +112,12 @@ class FrontEnd extends Phase {
128
112
def run (using Context ): Unit = unsupported(" run" )
129
113
}
130
114
131
- object FrontEnd {
115
+ object TyperPhase {
132
116
val name : String = " typer"
133
117
}
118
+
119
+ @ deprecated(message = " FrontEnd has been split into TyperPhase and Parser. Refer to one or the other." )
120
+ object FrontEnd {
121
+ // For backwards compatibility: some plugins refer to FrontEnd so that they can schedule themselves after it.
122
+ val name : String = TyperPhase .name
123
+ }
0 commit comments