blob: 9fcd797d9f94bf290dc359d9f1d841b44ae728b0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*
* Copyright (c) 2012-2016 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 {@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 {@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 {@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.
*/
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}
*
* <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);
}
}
|