Skip to content

Commit 2aada7e

Browse files
committed
symbol expander util
1 parent da2824f commit 2aada7e

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package expr_parser.utils;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
7+
import expr_parser.visitors.AntlrExpressionParser;
8+
import expr_parser.visitors.ExpandSymbols;
9+
10+
public class SymbolExpander {
11+
public static final LinkedHashMap<String, String> expandSymbols(
12+
LinkedHashMap<String, String> context) {
13+
// we expect a toposorted context, ergo linkedHM
14+
LinkedList<String> queue = new LinkedList<String>();
15+
queue.addAll(context.keySet());
16+
return expandSymbols(context, queue);
17+
}
18+
19+
private static final LinkedHashMap<String, String> expandSymbols(
20+
LinkedHashMap<String, String> context, Queue<String> queue) {
21+
while (!queue.isEmpty()) {
22+
String s = queue.remove();
23+
AntlrExpressionParser p = new AntlrExpressionParser(context.get(s));
24+
ExpandSymbols expander = new ExpandSymbols(context);
25+
context.put(s, p.parseAndVisitWith(expander));
26+
expandSymbols(context, queue);
27+
}
28+
return context;
29+
}
30+
31+
}

0 commit comments

Comments
 (0)