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 publicparse(), public AST types and a cache inevaluate(), 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?)withParseOptions, returning the public, immutable AST:ExpressionNodeand its node types, with spans.analyze()andcompile()accept anExpressionNode. A hand-made tree is checked before analysis: known node kinds, own properties only, and the nesting limit (syntax/nesting-limit, never aRangeError).CompiledExpression.ast, andevaluate()andevaluateAsync()with aCompiledExpressionmade bycompile().- The
knownVariablescompile option of the sketch: the names of the%variablesthe evaluations will supply, so analysis can report an unknown%namewhen the expression is compiled instead ofeval/undefined-variablewhen 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 aminorchangeset.
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 examplecreateCompileCache({ 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()raisessyntax/nesting-limitfor 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 likecompile(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%namethat is neither listed nor an environment variable is reported when the expression is compiled; without it, variables are checked at evaluation as before.