ParserBean extension;
try (InputStream is = new FileInputStream(extensionGrammar)) {
ArrayList<Pair<String,Reader>> files = new ArrayList<>();
files.add(new Pair<String, Reader>(extensionGrammar.getName(), new InputStreamReader(is)));
XMLSkinParser xmlSkinParser = new XMLSkinParser(files,
new CompilerLogger(new PrintCompilerLogHandler(new PrintStream(new OutputStream() {
public void write(int b) {
//Nothing - suppress all messages
}
}))));
xmlSkinParser.parse();
Field parserField = xmlSkinParser.getClass().getDeclaredField("currentParser");
parserField.setAccessible(true);
extension = (ParserBean)parserField.get(xmlSkinParser);
} catch (Exception e) {
throw new BuildException(e);
}
try {
CopperElementName grammarName = CopperElementName.newName("_" + hostGrammarName);
((ExtendedParserBean)extension).setHostGrammar(host.getGrammar(grammarName));
Field grammarField = extension.getClass().getSuperclass().getDeclaredField("grammars");
grammarField.setAccessible(true);
Hashtable<CopperElementName,Grammar> grammars =
(Hashtable<CopperElementName, Grammar>) grammarField.get(extension);
grammars.put(grammarName,host.getGrammar(grammarName));
} catch (Exception e) {
throw new BuildException(e);
}
extension.setParserClassAuxCode(host.getParserClassAuxCode());
extension.setParserInitCode(host.getParserInitCode());
Function<CopperElementReference, String> nameGetter = cen -> cen.getGrammarName() +"$"+cen.getName();
Stream<Stream<GrammarElement>> elements = host.getGrammars().stream().map(host::getGrammar)
.map(grammar -> grammar.getGrammarElements().stream().map(grammar::getGrammarElement));
List<DisambiguationFunction> dfl = elements
.map(ielements -> ielements.filter(el -> el instanceof DisambiguationFunction))
.flatMap(e->e.map(el->(DisambiguationFunction)el)).collect(Collectors.toList());
dfl.stream().forEach(df ->
df.setCode(df.getMembers().stream()
.map(cen -> "\t\t\tfinal int " + cen.getName() + " = " + nameGetter.apply(cen) + ";")
.reduce((l, r) -> l + "\n" + r).orElseGet(() -> "") + "\n" + df.getCode()));
host.getGrammars().stream().map(host::getGrammar)
.map(grammar -> grammar.getGrammarElements().stream().map(grammar::getGrammarElement))
.flatMap(els -> els.filter(el -> el instanceof Production).map(el -> (Production) el))
.forEach(this::rewriteTerminals);
host.getGrammars().stream().map(host::getGrammar)
.map(grammar -> grammar.getGrammarElements().stream().map(grammar::getGrammarElement))
.flatMap(els -> els.filter(el -> el instanceof Terminal).map(el -> (Terminal) el))
.forEach(this::rewriteTerminals);
try {
CompilerLogger logger = new CompilerLogger(new PrintCompilerLogHandler(System.out));
ParserSpecProcessor.normalizeParser(extension, logger);
} catch (CopperException e) {
throw new BuildException(e);
}
ParserCompilerParameters pcp = new ParserCompilerParameters();