Skip to content

Commit 8173fee

Browse files
committed
scanner complete
1 parent ec92ccb commit 8173fee

File tree

6 files changed

+81
-6
lines changed

6 files changed

+81
-6
lines changed

code/compiler/scanner/runScanner.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function initializeState(text) {
88
scanner.setScriptTarget(1);
99
scanner.setLanguageVariant(0);
1010
}
11-
initializeState("\nvar foo = 123;\n");
11+
initializeState("\nvar foo = 123;\n".trim());
1212
var token = scanner.scan();
1313
while (token != 1) {
1414
console.log(ts.syntaxKindToName(token));

code/compiler/scanner/runScanner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function initializeState(text: string) {
1616
// Sample usage
1717
initializeState(`
1818
var foo = 123;
19-
`);
19+
`.trim());
2020

2121
// Start the scanning
2222
var token = scanner.scan();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var ts = require("ntypescript");
2+
var scanner = ts.createScanner(2, true);
3+
function initializeState(text) {
4+
scanner.setText(text);
5+
scanner.setOnError(function (message, length) {
6+
console.error(message);
7+
});
8+
scanner.setScriptTarget(1);
9+
scanner.setLanguageVariant(0);
10+
}
11+
initializeState("\nvar foo = 123;\n".trim());
12+
var token = scanner.scan();
13+
while (token != 1) {
14+
var currentToken = ts.syntaxKindToName(token);
15+
var tokenStart = scanner.getStartPos();
16+
token = scanner.scan();
17+
var tokenEnd = scanner.getStartPos();
18+
console.log(currentToken, tokenStart, tokenEnd);
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as ts from "ntypescript";
2+
3+
// TypeScript has a singelton scanner
4+
const scanner = ts.createScanner(ts.ScriptTarget.Latest, /*skipTrivia*/ true);
5+
6+
// That is initialized using a function `initializeState` similar to
7+
function initializeState(text: string) {
8+
scanner.setText(text);
9+
scanner.setOnError((message: ts.DiagnosticMessage, length: number) => {
10+
console.error(message);
11+
});
12+
scanner.setScriptTarget(ts.ScriptTarget.ES5);
13+
scanner.setLanguageVariant(ts.LanguageVariant.Standard);
14+
}
15+
16+
// Sample usage
17+
initializeState(`
18+
var foo = 123;
19+
`.trim());
20+
21+
// Start the scanning
22+
var token = scanner.scan();
23+
while (token != ts.SyntaxKind.EndOfFileToken) {
24+
let currentToken = ts.syntaxKindToName(token);
25+
let tokenStart = scanner.getStartPos();
26+
token = scanner.scan();
27+
let tokenEnd = scanner.getStartPos();
28+
console.log(currentToken, tokenStart, tokenEnd);
29+
}

code/compiler/scanner/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
],
2222
"files": [
2323
"./runScanner.ts",
24+
"./runScannerWithPositions.ts",
2425
"./typings/node/node.d.ts",
2526
"./typings/tsd.d.ts"
2627
]

docs/compiler/scanner.md

+30-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ There is a *singleton* `scanner` created in `parser.ts` to avoid the cost of cre
1010

1111
Here is a *simplied* version of the actual code in the parser that you can run demonstrating this concept:
1212

13+
`code/compiler/scanner/runScanner.ts`
1314
```ts
1415
import * as ts from "ntypescript";
1516

@@ -29,7 +30,7 @@ function initializeState(text: string) {
2930
// Sample usage
3031
initializeState(`
3132
var foo = 123;
32-
`);
33+
`.trim());
3334

3435
// Start the scanning
3536
var token = scanner.scan();
@@ -50,9 +51,34 @@ SemicolonToken
5051
```
5152

5253
### Scanner State
53-
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.
54+
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.
5455

55-
// TODO: document a few of these and provide a sample.
56+
`code/compiler/scanner/runScannerWithPosition.ts`
57+
```ts
58+
// Sample usage
59+
initializeState(`
60+
var foo = 123;
61+
`.trim());
62+
63+
// Start the scanning
64+
var token = scanner.scan();
65+
while (token != ts.SyntaxKind.EndOfFileToken) {
66+
let currentToken = ts.syntaxKindToName(token);
67+
let tokenStart = scanner.getStartPos();
68+
token = scanner.scan();
69+
let tokenEnd = scanner.getStartPos();
70+
console.log(currentToken, tokenStart, tokenEnd);
71+
}
72+
```
73+
74+
This will print out the following:
75+
```
76+
VarKeyword 0 3
77+
Identifier 3 7
78+
FirstAssignment 7 9
79+
FirstLiteralToken 9 13
80+
SemicolonToken 13 14
81+
```
5682

5783
### Standalone scanner
58-
Even though the 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.
84+
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.

0 commit comments

Comments
 (0)