Documentation
¶
Overview ¶
Package optaccess rewrites the optional-access operators `?.` and `?[` into calls on internal sentinel functions that the evaluator dispatches as special forms.
obj?.field becomes __try_select__(obj, "field") obj?[i] becomes __try_index__(obj, i)
The rewrite is token-based. Strings, runes, and comments are scanner tokens we never look inside, so a `?.` written inside a string literal or comment is preserved verbatim. The transform is also a no-op when src contains no `?` byte: the scanner pass is skipped entirely.
The LHS of `?.` / `?[` is the primary expression that ends just before the `?`. The walker matches balanced parens and brackets so `f(a)?.b`, `a[0]?.b`, and `(a + b)?.c` all rewrite with the expected LHS. Chained optional access (`a?.b?.c`) is processed by repeated single-rewrite passes until the source no longer contains `?.` / `?[`, producing nested calls like `__try_select__(__try_select__(a, "b"), "c")`.
optaccess does not validate. Anything it cannot classify unambiguously is left alone for the parser to reject.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Rewrite ¶
Rewrite returns src with `?.field` and `?[idx]` rewritten to calls on the sentinel functions __try_select__ and __try_index__.
When src contains no `?`, Rewrite returns src unchanged without invoking the scanner. Chained optional access is handled by iteratively rewriting the leftmost remaining `?.` or `?[` until the source contains none, so each rewrite sees the previous one already rewritten as its LHS.
Types ¶
This section is empty.