我正在尝试为 DSL 实现一个预处理器,以代码/附加中的 CPP 示例为模型。但是,我没有使用令牌工厂。是必须的吗?调用 emit(token) 不会按预期将令牌注入令牌流。
这是词法分析器:
// 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);
}
}
}
;
这是包含方法:
@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;
}