ANTLR4: Cómo inyectar fichas
Estoy intentando implementar un preprocesador para un DSL, modelado después del ejemplo de CPP en código / extras. Sin embargo, no estoy usando token factory. ¿Se requiere uno? La llamada a emit (token) no inyecta los tokens en el flujo de tokens como se esperaba.
Aquí está el lexer:
// string-delimited path
SPATH : '"' (~[\n\r])*? '"'
{
emit(); // inject the current token
// launch another lexer on the include file, get tokens,
// emit them all at once here
List<CommonToken> tokens = Preprocessor.include(getText());
if (null != tokens) {
for (CommonToken tok : tokens) {
emit(tok);
}
}
}
;
Aquí está el método de inclusión:
@SuppressWarnings("unchecked")
public static List<CommonToken> include(String filename) {
List<CommonToken> tokens = null;
try (FileReader fr = openFile(filename.substring(1, filename.length() - 1));
BufferedReader br = new BufferedReader(fr)) {
ANTLRInputStream input = new ANTLRInputStream(br);
PreprocessorLexer lexer = new PreprocessorLexer(input);
tokens = (List<CommonToken>) lexer.getAllTokens();
} catch (IOException ioe) {
log.error("Can't load ~{}~", ioe.getLocalizedMessage());
}
return tokens;
}