FS FormScript
Package@formscript/ast
Specification4, 9.9
Depends onnothing — it is a leaf

@formscript/ast

The abstract syntax tree of §4 of the specification: the node types every production in the grammar can produce.

This package depends on nothing. The node definitions are the contract between the lexer, the parser, the scope builder, the analyser and the engine, so they are kept free of even a dependency on the error catalogue.

Nodes

Every node is a discriminated union member with a kind and a span:

ts
import type { Expression, Statement } from '@formscript/ast';

function describe(node: Expression): string {
  switch (node.kind) {
    case 'identifier': return node.name;
    case 'literal': return node.raw;
    default: return node.kind;
  }
}

SourceSpan is a half-open range with 1-based line and column, plus the byte offset, so a diagnostic can point at the exact source text that caused it.

Walking

ts
import { allExpressions, allStatements, childExpressions } from '@formscript/ast';

for (const expression of allExpressions(document)) {
  // every expression in the document, each node once
}

for (const child of childExpressions(expression)) {
  // the direct children of one expression
}

allExpressions returns each node once, in source order, which is what the diagnostic order of §9.9 requires.

Public API

89 exported symbols.

AbsenceTest type
AcceptedConstraint interface
ActionStatement interface
allExpressions re-export
allStatements re-export
AnyTypeRef interface
ArrayLiteralExpression interface
ArrayTypeRef interface
AttributeStatement interface
BetweenExpression interface

`x is between a and b` (§6.13).

BinaryExpression interface
BinaryOperator type
Block interface
BlockKind type
BlockStatement interface
Bound interface
CallExpression interface
childExpressions re-export
CoalesceExpression interface

`left ?? right` — coalescing (§6.11).

ComprehensionExpression interface

`map(i in items, i * 2)` (§6.5).

ComprehensionFunction type
Constraint type
CrossConstraint interface
CustomConstraint interface
DateLikeTypeRef interface
Document interface
EMPTY_SPAN re-export
EnumOption interface
EnumTypeRef interface
EqualityConstraint interface
Expression type
expressionsOf re-export
ExpressionVisitor type
FileTypeRef interface
FormatConstraint interface
FormDefinition interface
FormMeta interface
GeoTypeRef interface
IdentifierExpression interface
ImportStatement interface
IncludeStatement interface
IndexExpression interface

`target[index]`

IsExpression interface

`x is known` and friends (§6.9).

LengthConstraint interface
LiteralExpression interface
LiteralKind type
MatchArm interface
MatchesExpression interface

`x matches "regex"` — the RE2 subset (§6.46).

MatchPattern type
MatchStatement interface
MembershipConstraint interface
MultipleOfConstraint interface
NamespaceDefinition interface
NamespaceExpression interface

`$env.foo.bar` — a runtime-resolved path (§2.4).

Node interface

Every node carries the source range it was parsed from.

NowExpression interface

`$now` — the transaction's fixed evaluation timestamp (§6.4).

NumericConstraint interface
ObjectLiteralEntry interface
ObjectLiteralExpression interface
ObjectTypeMember interface
ObjectTypeRef interface
OptionEntry interface
OptionsStatement interface
OutputEntry interface
OutputStatement interface
PathRef type
PatternConstraint interface
RelationEquation interface
RelationStatement interface
RequiredConstraint interface
ScalarTypeRef interface
SelectorExpression interface

`target.property`

SelfExpression interface

`self` — the field currently being computed (§6.3).

SourcePosition interface
SourceSpan interface

A half-open source range, 1-based line and column.

SourceStatement interface
spanBetween re-export
Statement type
TernaryExpression interface
TextEntry interface
TextStatement interface
TypeRef type
TypeStatement interface
UnaryExpression interface
UnaryOperator type
UniqueConstraint interface
ValidateStatement interface
Validation interface
WildcardExpression interface

`list.*` — the wildcard projection (§6.3).