org.antlr.v4.runtime.atn.LexerIndexedCustomAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of antlr4-runtime Show documentation
Show all versions of antlr4-runtime Show documentation
The ANTLR 4 Runtime (Optimized)
/*
* Copyright (c) 2012 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;
import org.antlr.v4.runtime.misc.NotNull;
/**
* This implementation of {@link LexerAction} is used for tracking input offsets
* for position-dependent actions within a {@link 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 {@link LexerActionExecutor#append} and
* {@link LexerActionExecutor#fixOffsetBeforeMatch}.
*
* @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 {@link LexerAction}.
*
* Note: This class is only required for lexer actions for which
* {@link LexerAction#isPositionDependent} returns {@code true}.
*
* @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, @NotNull LexerAction action) {
this.offset = offset;
this.action = action;
}
/**
* 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.
*
* @return A {@link LexerAction} object which executes the lexer action.
*/
@NotNull
public LexerAction getAction() {
return action;
}
/**
* {@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}
* @return This method returns {@code true}.
*/
@Override
public boolean isPositionDependent() {
return true;
}
/**
* {@inheritDoc}
*
* This method calls {@link #execute} on the result of {@link #getAction}
* using the provided {@code lexer}.
*/
@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);
}
}