Skip to main content

Module refs

Module refs 

Source
Expand description

Grammar-based free-name extraction for Polydat expression text.

Consumers that need to know “which wire / param / coordinate names does this expression reference?” — workload validators, comprehension-source analysis, the YAML→Polydat fusion layer — MUST go through this module rather than byte-scanning the source text for {...} or identifier-shaped runs. Byte scanning misclassifies bare wire references (concat(foo)), function names, and string-literal contents; the grammar resolves all three correctly because it parses the same tokens the kernel compiler does.

The extractor is built on the canonical lexer + expression parser (crate::lexer::lex + crate::parser::parse_expression) so a reference is recognised exactly when the compiler would treat it as one. FieldAccess (base.vector) contributes its source name; StringLit contributes the {name} interpolation references it carries; function callee names are NOT references (they resolve in the function registry, not the wire scope).

Functions§

collect_expr_refs
Walk a parsed Expr, inserting every free name reference into out. The traversal mirrors crate::validate::validate_expr’s reference-collection arm minus the diagnostics, so the two stay in lockstep about what counts as a reference.
collect_string_interpolation_refs
Extract {name} interpolation references from a string literal’s contents. Skips format specifiers ({:05}, {:.2}) and non-identifier bodies. This is the string-interpolation grammar — the only place {name} is a reference inside a parsed Polydat expression. (Raw YAML template fields like an op’s prepared: use the same interpolation grammar but never reach the Polydat parser; the YAML-fusion layer applies collect_string_interpolation_refs to those directly.)
referenced_names
Parse text as a single Polydat expression and return the set of free names it references — wire/param/coordinate identifiers, FieldAccess source names, and {name} interpolation references inside string literals. Function callee names are excluded (they resolve in the function registry).
try_referenced_names
Like referenced_names but returns Err with the lexer/parser diagnostic when text is not a single valid Polydat expression. Use when a parse failure should surface to the operator rather than silently yield no references.