ANTLR4: How to inject tokens

前端 未结 1 384
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 15:39

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(toke

相关标签:
1条回答
  • 2020-12-21 16:32

    You need to override Lexer.nextToken to provide this feature. In your lexer, keep a Deque<Token> of injected tokens that have not yet been returned by nextToken. When the queue is empty, your implementation of nextToken should return the next token according to the superclass implementation.

    Here's some sample code. I have not tried to compile or run it so it might not be perfect.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题