Scanner
The source code for the TypeScript scanner is located entirely in scanner.ts
. Scanner is controlled internally by the Parser
to convert the source code to an AST. Here is what the desired outcome is.
Usage by Parser
There is a singleton scanner
created in parser.ts
to avoid the cost of creating scanners over and over again. This scanner is then primed by the parser on demand using the initializeState
function.
Here is a simplied version of the actual code in the parser that you can run demonstrating this concept:
code/compiler/scanner/runScanner.ts
This will print out the following :
Scanner State
After you call scan
the scanner updates its local state (position in the scan, current token details etc). The scanner provides a bunch of utility functions to get the current scanner state. In the below sample we create a scanner and then use it to identify the tokens as well as their positions in the code.
code/compiler/scanner/runScannerWithPosition.ts
This will print out the following:
Standalone scanner
Even though the TypeScript parser has a singleton scanner you can create a standalone scanner using createScanner
and use its setText
/setTextPos
to scan at different points in a file for your amusement.
Last updated