Scanner and parser
Concept and boundary
The scanner recognizes lexical tokens while the parser builds syntax structure and can request contextual rescanning. Trivia may be skipped by scanner configuration. Compare a token sequence with the structured AST; neither by itself is a semantic check. These public probes illustrate behavior, not a full grammar implementation.
Pinned implementation breadcrumbs
In the installed TypeScript 5.8.3 bundle lib/typescript.js the following function definitions were located: createScanner (line 12093); parseSourceFile (line 33054). These are navigation anchors for source reading, not public API contracts. The probes below observe public behavior; they do not prove every internal step.
Local tooling setup
For each workshop use a fresh folder. Install the pinned compiler with npm install --save-dev typescript@5.8.3, then create the listed files and run node example.cjs with Node.js 22.16.0. Assertions fail if the expected behavior changes. These are local JavaScript tooling scripts, not browser CodeRunner tasks; they do not independently type-check the tooling source.
Workshop 1: scanner-scan
Revisit reference fixture ts-api-scanner-scan (historical V30, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");function scanner(text, skipTrivia = true) { return ts.createScanner(ts.ScriptTarget.ES2022, skipTrivia, ts.LanguageVariant.Standard, text);}const s = scanner("let x = 2;");const kinds = [];for (let k = s.scan(); k !== ts.SyntaxKind.EndOfFileToken; k = s.scan()) kinds.push(k);assert.deepEqual(kinds,[ts.SyntaxKind.LetKeyword,ts.SyntaxKind.Identifier,ts.SyntaxKind.EqualsToken,ts.SyntaxKind.NumericLiteral,ts.SyntaxKind.SemicolonToken]);assert.equal(s.getToken(),ts.SyntaxKind.EndOfFileToken);console.log("five tokens then EOF");Expected stdout (exit 0):
five tokens then EOFWorkshop 2: node-get-children
Revisit reference fixture ts-api-node-get-children (historical V30, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");function parse(text) { return ts.createSourceFile("input.ts", text, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TS);}const f=parse("const x = 1;"); const d=f.statements[0].declarationList.declarations[0];const children=d.getChildren(f); const structural=[]; ts.forEachChild(d,n=>{structural.push(n.kind);});assert(children.some(n=>n.kind===ts.SyntaxKind.EqualsToken)); assert(!structural.includes(ts.SyntaxKind.EqualsToken));assert(children.some(n=>ts.isIdentifier(n))); console.log("getChildren includes equals token; forEachChild omits it");Expected stdout (exit 0):
getChildren includes equals token; forEachChild omits itPractice and recap
Scan an expression with whitespace, then compare getChildren and forEachChild.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Scanner and parser — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Scanner and parser?
What does workshop 1 (scanner-scan) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");function scanner(text, skipTrivia = true) { return ts.createScanner(ts.ScriptTarget.ES2022, skipTrivia, ts.LanguageVariant.Standard, text);}const s = scanner("let x = 2;");const kinds = [];for (let k = s.scan(); k !== ts.SyntaxKind.EndOfFileToken; k = s.scan()) kinds.push(k);assert.deepEqual(kinds,[ts.SyntaxKind.LetKeyword,ts.SyntaxKind.Identifier,ts.SyntaxKind.EqualsToken,ts.SyntaxKind.NumericLiteral,ts.SyntaxKind.SemicolonToken]);assert.equal(s.getToken(),ts.SyntaxKind.EndOfFileToken);console.log("five tokens then EOF");What does workshop 2 (node-get-children) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");function parse(text) { return ts.createSourceFile("input.ts", text, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TS);}const f=parse("const x = 1;"); const d=f.statements[0].declarationList.declarations[0];const children=d.getChildren(f); const structural=[]; ts.forEachChild(d,n=>{structural.push(n.kind);});assert(children.some(n=>n.kind===ts.SyntaxKind.EqualsToken)); assert(!structural.includes(ts.SyntaxKind.EqualsToken));assert(children.some(n=>ts.isIdentifier(n))); console.log("getChildren includes equals token; forEachChild omits it");