// process scope x. ... end scope blocks static void tok_scopes(L tok) { if (!tok.contains("scope")) ret; int i; while ((i = rjfind(tok, "scope .")) >= 0) { int j = jfind(tok, i+6, "end scope"); if (j < 0) j = l(tok)-1; S scopeName = tok.get(i+2); new HashSet names; new HashSet functions; clearTokens(tok, i, i+5); clearTokens(tok, j, j+3); L subTok = subList(tok, i+5, j); // auto-make #lock variable if (jfind(subTok, "lock #lock") >= 0 && jfind(subTok, "Lock #lock") < 0) tok.set(i, "static Lock " + scopeName + "_lock = lock();\n"); // first pass (find # declarations) for (int k = i+6; k < j-2; k += 2) { S t = get(tok, k+2); if (eqGet(tok, k, "#") && isIdentifier(t)) { names.add(t); if (eqGet(tok, k+4), "(")) functions.add(t); replaceTokens(tok, k, k+3, scopeName + "_" + t); } } // second pass for (int k = i+6; k < j; k += 2) { S t = get(tok, k); if (isIdentifier(t) && names.contains(t)) if (eqGet(tok, k-2, ".")) {} else if (eqGet(tok, k+2, "(") && !functions.contains(t)) {} else if (eq(t, "lock") && isIdentifier(get(tok, k+2))) {} else tok.set(k, scopeName + "_" + t); } reTok(tok, i, j+1); } }