LexerActionExecutor.java 文件源码

java
阅读 25 收藏 0 点赞 0 评论 0

项目:Scratch-ApuC 作者:
/**
 * Execute the actions encapsulated by this executor within the context of a
 * particular {@link Lexer}.
 *
 * <p>This method calls {@link IntStream#seek} to set the position of the
 * {@code input} {@link CharStream} prior to calling
 * {@link LexerAction#execute} on a position-dependent action. Before the
 * method returns, the input position will be restored to the same position
 * it was in when the method was invoked.</p>
 *
 * @param lexer The lexer instance.
 * @param input The input stream which is the source for the current token.
 * When this method is called, the current {@link IntStream#index} for
 * {@code input} should be the start of the following token, i.e. 1
 * character past the end of the current token.
 * @param startIndex The token start index. This value may be passed to
 * {@link IntStream#seek} to set the {@code input} position to the beginning
 * of the token.
 */
public void execute(@NotNull Lexer lexer, CharStream input, int startIndex) {
    boolean requiresSeek = false;
    int stopIndex = input.index();
    try {
        for (LexerAction lexerAction : lexerActions) {
            if (lexerAction instanceof LexerIndexedCustomAction) {
                int offset = ((LexerIndexedCustomAction)lexerAction).getOffset();
                input.seek(startIndex + offset);
                lexerAction = ((LexerIndexedCustomAction)lexerAction).getAction();
                requiresSeek = (startIndex + offset) != stopIndex;
            }
            else if (lexerAction.isPositionDependent()) {
                input.seek(stopIndex);
                requiresSeek = false;
            }

            lexerAction.execute(lexer);
        }
    }
    finally {
        if (requiresSeek) {
            input.seek(stopIndex);
        }
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号