scope tok_findRewrites // finds: rewrite [=|with|to] // (a global version of "replace with") sclass #Found { LS tok; S token; int cIdx, repStart, repEnd; S replacement() { ret joinSubList(tok, repStart, repEnd-1); } int startCIdx() { ret cIdx; } int endNIdx() { ret repEnd+1; } } static SS tok_findRewrites(LS tok) { SS rewrites = orderedMap(); tok_findRewrites(tok, rewrites, null); ret rewrites; } svoid tok_findRewrites(LS tok, SS rewrites default null, IVF1 callback) { for (int i : jfindAll(tok, "rewrite ", (_tok, nIdx) -> eqGetOneOf(_tok, nIdx+5, "with", "=", "to"))) { new Found f; f.tok = tok; f.cIdx = i; f.token = tok.get(i+2); f.repStart = i+6; f.repEnd = smartIndexOf(tok, f.repStart, "."); // TODO: allow expressions with brackets callF(callback, f); rewrites?.put(f.token, f.replacement()); } }