Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions packages/playground-v2/src/ast-tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
export type AstNode = {
detail: string
hasChildren: boolean
id: string
kind: string
start: number
end: number
}

export type AstSnapshot = {
revision: number
root: AstNode
}

export type AstSyntaxNode = {
kind: number
flags?: number
forEachChild<T>(callback: (child: any) => T | undefined): T | undefined
getEnd(): number
}

// Enum reverse mappings resolve to the marker aliases declared last
// (FirstStatement for VariableStatement), so keep the first declared name.
export function syntaxKindNames(syntaxKind: object) {
const names = new Map<number, string>()
for (const [name, value] of Object.entries(syntaxKind)) {
if (typeof value === "number" && !names.has(value)) names.set(value, name)
}
return (kind: number) => names.get(kind)
}

type CachedAstNode = AstNode & { children: CachedAstNode[] }

// Snapshots the tree so child and path requests keep resolving against the
// source text the root was built from, even after later compiles.
export class AstCache {
private revision = 0
private readonly nodes = new Map<string, CachedAstNode>()

store<T extends AstSyntaxNode>(
root: T | undefined,
kindName: (kind: number) => string | undefined,
start: (node: T) => number
): AstSnapshot | undefined {
this.revision++
this.nodes.clear()
if (!root) return undefined
const cachedRoot = describe(root, "", kindName, start)
this.nodes.set("", cachedRoot)
const pending: Array<[T, CachedAstNode]> = [[root, cachedRoot]]
while (pending.length) {
const [node, cached] = pending.pop()!
node.forEachChild((child: T) => {
const id = cached.id === "" ? String(cached.children.length) : `${cached.id}.${cached.children.length}`
const entry = describe(child, id, kindName, start)
cached.children.push(entry)
this.nodes.set(id, entry)
pending.push([child, entry])
return undefined
})
cached.hasChildren = cached.children.length > 0
}
return { revision: this.revision, root: summarize(cachedRoot) }
}

children(revision: number, id: string): AstNode[] | undefined {
if (revision !== this.revision) return undefined
return this.nodes.get(id)?.children.map(summarize)
}

path(revision: number, position: number): AstNode[] | undefined {
if (revision !== this.revision) return undefined
let node = this.nodes.get("")
const path: AstNode[] = []
while (node) {
path.push(summarize(node))
node =
node.children.find(child => position >= child.start && position < child.end) ??
node.children.find(child => position === child.end && child.end > child.start)
}
return path
}
}

function describe<T extends AstSyntaxNode>(
node: T,
id: string,
kindName: (kind: number) => string | undefined,
start: (node: T) => number
): CachedAstNode {
const properties = node as T & { text?: unknown; escapedText?: unknown }
const text =
typeof properties.text === "string"
? properties.text
: typeof properties.escapedText === "string"
? properties.escapedText
: ""
const preview = text ? JSON.stringify(text.length > 60 ? `${text.slice(0, 57)}…` : text) : ""
const flags = typeof node.flags === "number" && node.flags !== 0 ? `flags=${node.flags}` : ""
return {
children: [],
detail: [preview, flags].filter(Boolean).join(" · "),
end: node.getEnd(),
hasChildren: false,
id,
kind: kindName(node.kind) ?? String(node.kind),
start: start(node),
}
}

function summarize({ children: _children, ...node }: CachedAstNode): AstNode {
return node
}
23 changes: 23 additions & 0 deletions packages/playground-v2/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,29 @@ <h2>Emit</h2>
</div>
</div>
<div id="emit-output" class="emit-output"></div>
<section id="ast-panel" class="ast-panel" aria-label="Abstract syntax tree" data-collapsed="true">
<header class="ast-panel-header">
<div class="output-section-title">
<button
id="toggle-ast-button"
class="action-button output-section-toggle"
type="button"
aria-label="Expand AST"
aria-controls="ast-tree-container"
aria-expanded="false"
title="Expand AST"
>
▸
</button>
<h2>AST</h2>
</div>
<span>Active file</span>
</header>
<div id="ast-tree-container" class="ast-tree-container">
<p id="ast-message" class="empty-message">Waiting for the compiler.</p>
<ul id="ast-tree" class="ast-tree" role="tree" aria-label="Active file syntax tree" hidden></ul>
</div>
</section>
<details id="diagnostics-panel" class="diagnostics-panel">
<summary>
<span>Problems</span>
Expand Down
Loading
Loading