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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* 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.
*/
using Antlr4.Runtime;
using Antlr4.Runtime.Atn;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
namespace Antlr4.Runtime.Atn
{
/// <summary>
/// This implementation of
/// <see cref="ILexerAction"/>
/// is used for tracking input offsets
/// for position-dependent actions within a
/// <see cref="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
/// <see cref="LexerActionExecutor.Append(LexerActionExecutor, ILexerAction)"/>
/// and
/// <see cref="LexerActionExecutor.FixOffsetBeforeMatch(int)"/>
/// .</p>
/// </summary>
/// <author>Sam Harwell</author>
/// <since>4.2</since>
public sealed class LexerIndexedCustomAction : ILexerAction
{
private readonly int offset;
private readonly ILexerAction action;
/// <summary>
/// Constructs a new indexed custom action by associating a character offset
/// with a
/// <see cref="ILexerAction"/>
/// .
/// <p>Note: This class is only required for lexer actions for which
/// <see cref="ILexerAction.IsPositionDependent()"/>
/// returns
/// <see langword="true"/>
/// .</p>
/// </summary>
/// <param name="offset">
/// The offset into the input
/// <see cref="Antlr4.Runtime.ICharStream"/>
/// , relative to
/// the token start index, at which the specified lexer action should be
/// executed.
/// </param>
/// <param name="action">
/// The lexer action to execute at a particular offset in the
/// input
/// <see cref="Antlr4.Runtime.ICharStream"/>
/// .
/// </param>
public LexerIndexedCustomAction(int offset, ILexerAction action)
{
this.offset = offset;
this.action = action;
}
/// <summary>
/// Gets the location in the input
/// <see cref="Antlr4.Runtime.ICharStream"/>
/// at which the lexer
/// action should be executed. The value is interpreted as an offset relative
/// to the token start index.
/// </summary>
/// <returns>
/// The location in the input
/// <see cref="Antlr4.Runtime.ICharStream"/>
/// at which the lexer
/// action should be executed.
/// </returns>
public int Offset
{
get
{
return offset;
}
}
/// <summary>Gets the lexer action to execute.</summary>
/// <remarks>Gets the lexer action to execute.</remarks>
/// <returns>
/// A
/// <see cref="ILexerAction"/>
/// object which executes the lexer action.
/// </returns>
[NotNull]
public ILexerAction Action
{
get
{
return action;
}
}
/// <summary><inheritDoc/></summary>
/// <returns>
/// This method returns the result of calling
/// <see cref="ActionType()"/>
/// on the
/// <see cref="ILexerAction"/>
/// returned by
/// <see cref="Action()"/>
/// .
/// </returns>
public LexerActionType ActionType
{
get
{
return action.ActionType;
}
}
/// <summary><inheritDoc/></summary>
/// <returns>
/// This method returns
/// <see langword="true"/>
/// .
/// </returns>
public bool IsPositionDependent
{
get
{
return true;
}
}
/// <summary>
/// <inheritDoc/>
/// <p>This method calls
/// <see cref="Execute(Antlr4.Runtime.Lexer)"/>
/// on the result of
/// <see cref="Action()"/>
/// using the provided
/// <paramref name="lexer"/>
/// .</p>
/// </summary>
public void Execute(Lexer lexer)
{
// assume the input stream position was properly set by the calling code
action.Execute(lexer);
}
public override int GetHashCode()
{
int hash = MurmurHash.Initialize();
hash = MurmurHash.Update(hash, offset);
hash = MurmurHash.Update(hash, action);
return MurmurHash.Finish(hash, 2);
}
public override bool Equals(object obj)
{
if (obj == this)
{
return true;
}
else
{
if (!(obj is Antlr4.Runtime.Atn.LexerIndexedCustomAction))
{
return false;
}
}
Antlr4.Runtime.Atn.LexerIndexedCustomAction other = (Antlr4.Runtime.Atn.LexerIndexedCustomAction)obj;
return offset == other.offset && action.Equals(other.action);
}
}
}