我正在尝试为DSL实现一个预处理器,该预处理器以代码/附加中的CPP示例为模型。然而,我没有使用令牌工厂。是否需要一个?调用emit(token)不会按预期将令牌注入令牌流。
这是lexer:
// string-delimited path
SPATH : '"' (~[nr])*? '"'
{
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;
}
您需要覆盖Lexer.nextToken
才能提供此功能。在lexer中,保留nextToken
尚未返回的注入令牌的Deque<Token>
。当队列为空时,nextToken
的实现应该根据超类实现返回下一个令牌。
下面是一些示例代码。我没有尝试编译或运行它,所以它可能并不完美。
private final Deque<Token> pendingTokens = new ArrayDeque<>();
@Override
public Token nextToken() {
Token pending = pendingTokens.pollFirst();
if (pending != null) {
return pending;
}
Token next = super.nextToken();
pending = pendingTokens.pollFirst();
if (pending != null) {
pendingTokens.addLast(next);
return pending;
}
return next;
}