I'm trying to implement a preprocessor for a DSL, modeled after the CPP example in code/extras. However, I'm not using token factory. Is one required? Calling emit(token) does not inject the tokens into the tokens stream as expected.
Here's the lexer:
// string-delimited path
SPATH : '"' (~[
])*? '"'
{
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);
}
}
}
;
Here's the include method:
@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;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…