[Lang]RewriteAn edit list over one unit's source: collect replacements by node span, apply splices them (back to front, byte-exact โ spans are byte offsets) into new source text. Overlapping edits throw. The output is just source: parse it again to verify, eval it to use it.
use std:lang/ast var src = 'total = 2 + 3' var rw = [Lang]Rewrite.over:src ([Lang]Parser.parse:src).walk:{ |n| (n.kind == #integerLiteral).if:{ rw.replace:n with:((n.at:#value) * 10).s } } rw.apply "* -> total = 20 + 30
A rewriter over source (the exact text that was parsed โ spans must refer to it).
The rewritten source. Edits apply back to front so earlier spans stay valid; two edits that overlap throw a ValueError (zero-width inserts at the same position are fine).
Remove node's span entirely. Answers self.
Internal.
Insert text immediately after node. Answers self.
Insert text immediately before node. Answers self.
Replace node's exact span with text. Answers self.