T0038c — Public API: parser, AST input and compile cache

Part of the roadmap. Phase 1 · wave 7 · size M.

  • Depends on: T0024b, T0025
  • Area: area:core
  • Tier hint: risk:high: a public parse(), public AST types and a cache in evaluate(), so the core tier with a second reviewer from the other vendor.
  • Parallel with: T0038a, T0038b, T0038d
  • Parent: T0038, split on 2026-11-12.

Goal

Expressions as data for tooling, and repeated string evaluation without repeated compilation: parse(), AST input, the compiled ast property, compile-time variable names and the bounded compile cache of the T0038 sketch. The parser counts chains towards its nesting limit since #201, which resolved the #107 prerequisite.

Scope

  • parse(expression, options?) with ParseOptions, returning the public, immutable AST: ExpressionNode and its node types, with spans.
  • analyze() and compile() accept an ExpressionNode. A hand-made tree is checked before analysis: known node kinds, own properties only, and the nesting limit (syntax/nesting-limit, never a RangeError).
  • CompiledExpression.ast, and evaluate() and evaluateAsync() with a CompiledExpression made by compile().
  • The knownVariables compile option of the sketch: the names of the %variables the evaluations will supply, so analysis can report an unknown %name when the expression is compiled instead of eval/undefined-variable when it is evaluated. Decide how it combines with the FHIR environment variables (%resource, %sct, …), which are always known.
  • The compile cache: evaluate() with a string reuses compiled expressions from a bounded LRU cache, keyed by the expression and the identity of its compile options. Its size is configurable, and it can be turned off.
  • Tests, including bounded-tree regressions for parse() and for hand-made trees, type tests, packed consumers, a guide section on the parser and the cache, and a minor changeset.

Design notes

  • The code style forbids mutable module-level state, and a default cache shared by every evaluate() call is exactly that. Choose between an explicit cache object passed with an option (for example createCompileCache({ size })) and a default cache as a documented exception, and ask the owner before the second.
  • Two option objects with the same content are different keys, so a host that builds options per call gets no reuse but never a wrong result. A model, unit system or registry is part of the key by identity.

Acceptance criteria

From the parent:

  • The #107 parser-depth prerequisite is covered: parse() raises syntax/nesting-limit for long chains and long left-associative operator sequences, and a hand-made tree deeper than the limit is rejected before any recursive walk.
  • No future option is accepted as a silent no-op.
  • publint, attw, the zero-dependency check and the size budget all pass.

And for this child:

  • compile(parse(expression)) evaluates like compile(expression), and the AST cannot be changed through the public API.
  • The cache never returns an expression compiled with other options, stays within its size, and can be turned off.
  • With knownVariables, a %name that is neither listed nor an environment variable is reported when the expression is compiled; without it, variables are checked at evaluation as before.