Parser
The sourcecode for the TypeScript parser is located entirely in parser.ts
. Scanner is controlled internally by the Parser
to convert the source code to an AST. Here is a review of what the desired outcome is.
The parser is implemented as a singleton (similar reasons to scanner
, don't want to recreate it if we can reinit it). It is actually implemented as namespace Parser
which contains state variables for the Parser as well as a singleton scanner
. As mentioned before it contains a const scanner
. The parser functions manage this scanner.
Usage by program
Parser is driven indirectly by Program (indirectly as its actually by CompilerHost
which we mentioned previously). Basically this is the simplified call stack:
The parseSourceFile
not only primes the state for the Parser but also primes the state for the scanner
by calling initializeState
. It then goes on to parse the source file using parseSourceFileWorker
.
Sample Usage
Before we dig too deep into the parser internals, here is a sample code that uses the TypeScript's parser to get the AST of a source file (using ts.createSourceFile
), and then print it.
code/compiler/parser/runParser.ts
This will print out the following:
This looks like a (very right sided) tree if you tilt your head to the left.
Last updated