com.glispa.combo.TemplateWalker Maven / Gradle / Ivy
The newest version!
package com.glispa.combo;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/**
* Internal class for walking through the parsed tree
*/
class TemplateWalker extends MacroBaseListener {
private final MacroRegistry macroRegistry;
private List> currentElements;
private Collection> currentChain;
public TemplateWalker(MacroRegistry macroRegistry) {
this.macroRegistry = macroRegistry;
}
public List> visit(MacroParser.TemplateContext ctx) {
currentElements = new ArrayList<>(ctx.getChildCount());
ParseTreeWalker.DEFAULT.walk(this, ctx);
return currentElements;
}
@Override
public void enterMacroChain(MacroParser.MacroChainContext ctx) {
currentChain = new LinkedList<>();
}
@Override
public void enterMacro(MacroParser.MacroContext ctx) {
MacroFactory macroFactory = macroRegistry.get(ctx.macroId().getText());
List argContexts = ctx.arg();
String[] args = new String[argContexts.size()];
for (int i = 0; i < argContexts.size(); ++i) {
args[i] = argContexts.get(i).getText();
}
currentChain.add(macroFactory.build(args));
}
@Override
public void exitMacroChain(MacroParser.MacroChainContext ctx) {
currentElements.add(new MacroChainTemplateElement<>(currentChain));
}
@Override
public void exitText(MacroParser.TextContext ctx) {
currentElements.add(new TextTemplateElement<>(ctx.getText()));
}
}