-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ParserPhase.scala
66 lines (53 loc) · 2.04 KB
/
ParserPhase.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package dotty.tools.dotc.parsing
import dotty.tools.dotc.config.Config
import dotty.tools.dotc.core.Contexts.{ Context, ctx }
import dotty.tools.dotc.core.Phases.Phase
import dotty.tools.dotc.typer.ImportInfo.withRootImports
import dotty.tools.dotc.{ CompilationUnit, ast, report }
import dotty.tools.dotc.util.{ NoSourcePosition, SourcePosition }
import dotty.tools.dotc.util.Stats.record
import dotty.tools.unsupported
class Parser extends Phase {
override def phaseName: String = Parser.name
override def description: String = Parser.description
// We run TreeChecker only after type checking
override def isCheckable: Boolean = false
/** The position of the first XML literal encountered while parsing,
* NoSourcePosition if there were no XML literals.
*/
private[dotc] var firstXmlPos: SourcePosition = NoSourcePosition
def parse(using Context): Boolean = monitor("parser") {
val unit = ctx.compilationUnit
unit.untpdTree =
if (unit.isJava) new JavaParsers.JavaParser(unit.source).parse()
else {
val p = new Parsers.Parser(unit.source)
// p.in.debugTokenStream = true
val tree = p.parse()
ctx.compilationUnit.comments = p.in.comments
if (p.firstXmlPos.exists && !firstXmlPos.exists)
firstXmlPos = p.firstXmlPos
tree
}
if (Config.checkPositions)
unit.untpdTree.checkPos(nonOverlapping = !unit.isJava && !ctx.reporter.hasErrors)
}
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] = {
val unitContexts =
for unit <- units yield
report.inform(s"parsing ${unit.source}")
ctx.fresh.setCompilationUnit(unit).withRootImports
val unitContexts0 =
for
unitContext <- unitContexts
if parse(using unitContext)
yield unitContext
record("parsedTrees", ast.Trees.ntrees)
unitContexts0.map(_.compilationUnit)
}
def run(using Context): Unit = unsupported("run")
}
object Parser{
val name: String = "parser"
val description: String = "scan and parse sources"
}