/*
 * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

package org.antlr.v4.runtime.atn;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.misc.MurmurHash;

This implementation of LexerAction is used for tracking input offsets for position-dependent actions within a LexerActionExecutor.

This action is not serialized as part of the ATN, and is only required for position-dependent lexer actions which appear at a location other than the end of a rule. For more information about DFA optimizations employed for lexer actions, see LexerActionExecutor.append and LexerActionExecutor.fixOffsetBeforeMatch.

Author:Sam Harwell
Since:4.2
/** * This implementation of {@link LexerAction} is used for tracking input offsets * for position-dependent actions within a {@link LexerActionExecutor}. * * <p>This action is not serialized as part of the ATN, and is only required for * position-dependent lexer actions which appear at a location other than the * end of a rule. For more information about DFA optimizations employed for * lexer actions, see {@link LexerActionExecutor#append} and * {@link LexerActionExecutor#fixOffsetBeforeMatch}.</p> * * @author Sam Harwell * @since 4.2 */
public final class LexerIndexedCustomAction implements LexerAction { private final int offset; private final LexerAction action;
Constructs a new indexed custom action by associating a character offset with a LexerAction.

Note: This class is only required for lexer actions for which LexerAction.isPositionDependent returns true.

Params:
  • offset – The offset into the input CharStream, relative to the token start index, at which the specified lexer action should be executed.
  • action – The lexer action to execute at a particular offset in the input CharStream.
/** * Constructs a new indexed custom action by associating a character offset * with a {@link LexerAction}. * * <p>Note: This class is only required for lexer actions for which * {@link LexerAction#isPositionDependent} returns {@code true}.</p> * * @param offset The offset into the input {@link CharStream}, relative to * the token start index, at which the specified lexer action should be * executed. * @param action The lexer action to execute at a particular offset in the * input {@link CharStream}. */
public LexerIndexedCustomAction(int offset, LexerAction action) { this.offset = offset; this.action = action; }
Gets the location in the input CharStream at which the lexer action should be executed. The value is interpreted as an offset relative to the token start index.
Returns:The location in the input CharStream at which the lexer action should be executed.
/** * Gets the location in the input {@link CharStream} at which the lexer * action should be executed. The value is interpreted as an offset relative * to the token start index. * * @return The location in the input {@link CharStream} at which the lexer * action should be executed. */
public int getOffset() { return offset; }
Gets the lexer action to execute.
Returns:A LexerAction object which executes the lexer action.
/** * Gets the lexer action to execute. * * @return A {@link LexerAction} object which executes the lexer action. */
public LexerAction getAction() { return action; }
{@inheritDoc}
Returns:This method returns the result of calling getActionType on the LexerAction returned by getAction.
/** * {@inheritDoc} * * @return This method returns the result of calling {@link #getActionType} * on the {@link LexerAction} returned by {@link #getAction}. */
@Override public LexerActionType getActionType() { return action.getActionType(); }
{@inheritDoc}
Returns:This method returns true.
/** * {@inheritDoc} * @return This method returns {@code true}. */
@Override public boolean isPositionDependent() { return true; }
{@inheritDoc}

This method calls execute on the result of getAction using the provided lexer.

/** * {@inheritDoc} * * <p>This method calls {@link #execute} on the result of {@link #getAction} * using the provided {@code lexer}.</p> */
@Override public void execute(Lexer lexer) { // assume the input stream position was properly set by the calling code action.execute(lexer); } @Override public int hashCode() { int hash = MurmurHash.initialize(); hash = MurmurHash.update(hash, offset); hash = MurmurHash.update(hash, action); return MurmurHash.finish(hash, 2); } @Override public boolean equals(Object obj) { if (obj == this) { return true; } else if (!(obj instanceof LexerIndexedCustomAction)) { return false; } LexerIndexedCustomAction other = (LexerIndexedCustomAction)obj; return offset == other.offset && action.equals(other.action); } }