Skip to content

Commit eddb3ab

Browse files
committed
more ast docs
1 parent 3310969 commit eddb3ab

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
* [TypeScript Compiler](docs/compiler/overview.md)
3434
* [Program](docs/compiler/program.md)
3535
* [AST](docs/compiler/ast.md)
36+
* [TIP: Visit Children](docs/compiler/ast-tip-children.md)

docs/compiler/ast-tip-children.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
### AST Tip: Visit Children
2+
3+
There is a utility function `ts.forEachChild` that allows you to visit all the child nodes of any Node in the AST.
4+
5+
Here is simplified snippet of the source code to demonstrate how it functions:
6+
7+
```ts
8+
9+
export function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T {
10+
if (!node) {
11+
return;
12+
}
13+
switch (node.kind) {
14+
case SyntaxKind.BinaryExpression:
15+
return visitNode(cbNode, (<BinaryExpression>node).left) ||
16+
visitNode(cbNode, (<BinaryExpression>node).operatorToken) ||
17+
visitNode(cbNode, (<BinaryExpression>node).right);
18+
case SyntaxKind.IfStatement:
19+
return visitNode(cbNode, (<IfStatement>node).expression) ||
20+
visitNode(cbNode, (<IfStatement>node).thenStatement) ||
21+
visitNode(cbNode, (<IfStatement>node).elseStatement);
22+
```
23+
24+
Basically it checks `node.kind` and based on that assumes an interface offered by the `node` and calls the `cbNode` on the children.

docs/compiler/ast.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
## Node
22
The basic building block of the Abstract Syntax Tree (AST). In general node represent non-terminals in the language grammar; some terminals are kept in the tree such as identifiers and literals.
33

4-
## Note on AST description
5-
Two key things make up an AST node definition. Its `SyntaxKind` which identifies it within the AST and its `interface`, the API the node provides when instantiated for the AST.
4+
Two key things make up an AST node documentation. Its `SyntaxKind` which identifies it within the AST and its `interface`, the API the node provides when instantiated for the AST.
5+
6+
Here are a few key `interface Node` members:
7+
* `TextRange` members that identify the node's `start` and `end` in the source file.
8+
* `parent?: Node` the parent of the node in the AST.
9+
10+
There are other additional members for node flags and modifiers etc. that you can lookup by searching `interface Node` in the source code but the ones we mentioned are vital for node traversal.
611

712
## SourceFile
813

docs/compiler/overview.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ It is split into the follow key parts:
77
* Binder (`bindder.ts`)
88
* Checker (`checker.ts`)
99
* Emitter (`emitter.ts`)
10+
* Program (`program.ts`)
1011

1112
Each of these get their own unique files in the source. These parts will be explained later on in this chapter.
1213

1314
There are a few additional files in the TypeScript compiler that provide utilities to many of these key portions:
1415

1516
## File: Utilities
16-
core.ts : core utilities used by the TypeScript compiler
17+
`core.ts` : core utilities used by the TypeScript compiler
1718

1819
## File: Key Data Structures
1920
`types.ts` contains key data structures and interfaces uses throughout the compiler. Here is a sampling of a few key ones:
@@ -22,10 +23,9 @@ The AST node type is identified by the `SyntaxKind` enum.
2223
* `TypeChecker`
2324
This is the interface provided by the TypeChecker.
2425
* `CompilerHost`
25-
This is used by the `Program` to interact with the `System`
26+
This is used by the `Program` to interact with the `System`.
27+
* `Node`
28+
A AST node.
2629

2730
## File: System
2831
All interaction of the TypeScript compiler with the operating system goes through a `System` interface. Both the interface and its implementations (`WScript` and `Node`) are defined in `system.ts`. You can think of it as the *Operating Environment* (OE).
29-
30-
## File: Program
31-
The compilation context ([a concept we covered previously](docs/project/compilation-context.md)) is represented within the TypeScript compiler as a `Program`.

docs/compiler/program.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Program
22

3+
The compilation context ([a concept we covered previously](docs/project/compilation-context.md)) is represented within the TypeScript compiler as a `Program`. It consists of `SourceFile`s and compiler options.
4+
5+
36
### Usage of `CompilerHost`
47
Its interaction mechanism with the OE:
58

@@ -11,7 +14,4 @@ There are other users of `System` as well (e.g. tests).
1114

1215
### SourceFile
1316

14-
* `SyntaxKind.SourceFile`
15-
* `interface SourceFile`.
16-
17-
The `program` consists of a bunch of source files. A `sourceFile` is actually a top-level AST node as well (`SyntaxKind.SourceFile`). Instances implement `interface SourceFile`.
17+
The program provides an API to get the Source Files `getSourceFiles(): SourceFile[];`. Each is represented as a root-level node for an AST (called `SourceFile`).

0 commit comments

Comments
 (0)