liveBook · Manning

Encoded abstract syntax tree

As explained in Building User-Friendly DSLs

An in-memory object encoding an abstract syntax tree so that software can inspect, alter, and traverse its concepts and settings.

An encoded abstract syntax tree is an in-memory JavaScript object that represents abstract syntax tree concepts and settings. Software can inspect, alter, and traverse this representation, making it suitable for the Domain IDE codebase. section 3.0

An AST object contains a concept and settings. Settings may hold strings, arrays, or reference objects whose ref property points to another encoded AST object. section 3.2.2

Recognize encoded AST values by testing their JavaScript shape:

  • Use typeof together with a truthiness check and !Array.isArray(value) to identify ordinary objects. section 3.2.2
  • Identify AST objects by requiring concept and settings properties. section 3.2.2
  • Identify reference objects through ref, then confirm that ref targets an AST object. section 3.2.2

Recognizing a rental AST

For a rental object, typeof returns object; its attributes setting is an array; and its individual attribute entries can satisfy isAstObject. An attribute reference satisfies isAstReference when its ref target is an AST object. section 3.2.2

Listing 3.8 Initial contents of the src/common/ast.js file
const isObject = (value) => (!!value) && (typeof value === "object")
     && !Array.isArray(value)
const isAstObject = (value) => isObject(value) && ("concept" in value)
     && ("settings" in value)
module.exports.isAstObject = isAstObject                                   #1

const isAstReferenceObject = (value) => isObject(value) && ("ref" in value)
const isAstReference = (value) => isAstReferenceObject(value)
     && isAstObject(value.ref)
module.exports.isAstReference = isAstReference

The implementation listing consolidates the object, AST-object, and AST-reference predicates into the initial ast.js module. section 3.2.2

How Building User-Friendly DSLs covers it

How deep it goes
Defines object shapes and implements recognition helpers, stopping short of detailed traversal algorithms.
Examples
Demonstrates rental, Data Attribute, Record Type, and Attribute Reference objects with JavaScript console checks.
What it assumes
Assumes familiarity with AST representation through object-relation diagrams and teaches JavaScript shape checks explicitly.
What you can do after
Build it from scratch by implementing JavaScript predicates that distinguish AST objects, arrays, and AST references.
How it connects
Connects abstract syntax trees, object-relation diagrams, JavaScript objects, arrays, and reference objects.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.