Context Trees
Traditional compilers parse source into an AST, then traverse the AST. But when your source is already structured data—JSON or YAML—building a separate AST just amplifies memory. We developed context trees: AST-level functionality without building an AST.
JSON as Intermediate Representation
Metadata attaches to JSON values via WeakMaps rather than wrapper objects. Traversal, transformation, and tree-shaking work on the original JSON directly. Shadow context provides parent tracking, reference resolution, and semantic analysis without wrapper objects.
This is what makes toolcog-schema and toolcog-openapi compilers in the true sense—semantic analysis, optimization passes, tree-shaking—but operating directly on the source format rather than a separate IR.
Edge-Efficient Processing
Traditional AST approaches amplify memory several fold. Edge runtimes with constrained memory can’t process large specs this way. Context trees keep overhead proportional to traversal depth, not document size—enabling multi-megabyte API specifications to be processed where they couldn’t be before.
Parent and Ancestry Tracking
Parent-child relationships tracked through WeakMap associations rather than adding properties to JSON nodes. Preserves the original document structure while enabling upward traversal—essential for resolving JSON References where you need to know where you came from.
Reference Resolution
Multiple resolution strategies: JSON Pointer fragments for local navigation, named anchors for direct access, URI resolution for cross-document references. Async support for external resources. Batched resolution aggregates errors instead of failing on first.
Tree-Shaking
Extract minimal, self-contained subgraphs from larger documents. Reference traversal discovers all reachable definitions, relocates them to configurable output structure, rewrites all references to point to new locations. Shared targets appear once with all references pointing to the same object. This enables extracting a single API operation with only its schema dependencies from a massive OpenAPI spec.
Format Boundaries
Resources mark where one format embeds another—JSON Schemas within OpenAPI specs. The traverser switches semantic rules at these boundaries, processing the same document with different interpretation rules in different subtrees.
Cycle Detection
Automatic detection of circular references in the traversal frame stack. Prevents infinite recursion when following references while preserving cycle structure in output when needed.