46 lines
1.7 KiB
Java
46 lines
1.7 KiB
Java
package ctbrec.variableexpansion;
|
|
|
|
import ctbrec.variableexpansion.antlr.PostProcessingLexer;
|
|
import ctbrec.variableexpansion.antlr.PostProcessingParser;
|
|
import ctbrec.variableexpansion.functions.AntlrSyntacErrorAdapter;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.antlr.v4.runtime.*;
|
|
|
|
import java.io.IOException;
|
|
import java.io.StringReader;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
|
|
@Slf4j
|
|
abstract class AbstractVariableExpander {
|
|
|
|
protected Map<String, Optional<Object>> placeholderValueSuppliers = new HashMap<>();
|
|
|
|
protected String fillInPlaceHolders(String input, Map<String, Optional<Object>> variables) {
|
|
try (StringReader reader = new StringReader(input)) {
|
|
CharStream s = CharStreams.fromReader(reader);
|
|
PostProcessingLexer lexer = new PostProcessingLexer(s);
|
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
|
PostProcessingParser parser = new PostProcessingParser(tokens);
|
|
parser.addErrorListener(new AntlrSyntacErrorAdapter() {
|
|
@Override
|
|
public void syntaxError(Recognizer<?, ?> recognizer, Object o, int line, int pos, String s, RecognitionException e) {
|
|
log.warn("Syntax error at {}:{} {}", line, pos, s);
|
|
}
|
|
});
|
|
PostProcessingParser.LineContext ctx = parser.line();
|
|
ParserVisitor visitor = new ParserVisitor(variables);
|
|
return visitor.visit(ctx);
|
|
} catch (IOException e) {
|
|
log.error("Couldn't fill in placeholders", e);
|
|
}
|
|
|
|
return input;
|
|
}
|
|
|
|
public Map<String, Optional<Object>> getPlaceholderValueSuppliers() {
|
|
return placeholderValueSuppliers;
|
|
}
|
|
}
|