There is a singleton scanner
created in 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:
This will print out the following :
After you call 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.
This will print out the following:
Even though the typescript parser has a singleton scanner you can create a standalone scanner using createScanner
and use its /setTextPos
to scan at different points in a file for your amusement.