// map: index of opening bracket -> index of closing bracket static Map getBracketMap(L tok) { ret getBracketMap(tok, getBracketMap_opening, getBracketMap_closing); } static Map getBracketMap(L tok, Collection opening, Collection closing) { ret getBracketMap(tok, opening, closing, 0, l(tok)); } static Map getBracketMap(L tok, Collection opening, Collection closing, int from, int to) { new TreeMap map; new L stack; for (int i = from|1; i < to; i+= 2) { if (opening.contains(tok.get(i))) stack.add(i); else if (closing.contains(tok.get(i))) { if (!empty(stack)) map.put(liftLast(stack), i); } } ret map; } static Set getBracketMap_opening = lithashset("{", "("); static Set getBracketMap_closing = lithashset("}", ")");