antlr4 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +27 -0
- data/README.md +46 -0
- data/lib/antlr4.rb +262 -0
- data/lib/antlr4/BufferedTokenStream.rb +306 -0
- data/lib/antlr4/CommonTokenFactory.rb +53 -0
- data/lib/antlr4/CommonTokenStream.rb +56 -0
- data/lib/antlr4/FileStream.rb +14 -0
- data/lib/antlr4/InputStream.rb +82 -0
- data/lib/antlr4/IntervalSet.rb +341 -0
- data/lib/antlr4/LL1Analyzer.rb +177 -0
- data/lib/antlr4/Lexer.rb +335 -0
- data/lib/antlr4/ListTokenSource.rb +140 -0
- data/lib/antlr4/Parser.rb +562 -0
- data/lib/antlr4/ParserInterpreter.rb +149 -0
- data/lib/antlr4/ParserRuleContext.rb +162 -0
- data/lib/antlr4/PredictionContext.rb +690 -0
- data/lib/antlr4/Recognizer.rb +162 -0
- data/lib/antlr4/RuleContext.rb +226 -0
- data/lib/antlr4/Token.rb +124 -0
- data/lib/antlr4/TokenFactory.rb +3 -0
- data/lib/antlr4/TokenSource.rb +4 -0
- data/lib/antlr4/TokenStream.rb +3 -0
- data/lib/antlr4/TraceListener.rb +23 -0
- data/lib/antlr4/atn/ATN.rb +133 -0
- data/lib/antlr4/atn/ATNConfig.rb +146 -0
- data/lib/antlr4/atn/ATNConfigSet.rb +215 -0
- data/lib/antlr4/atn/ATNDeserializationOptions.rb +62 -0
- data/lib/antlr4/atn/ATNDeserializer.rb +604 -0
- data/lib/antlr4/atn/ATNSimulator.rb +43 -0
- data/lib/antlr4/atn/ATNState.rb +253 -0
- data/lib/antlr4/atn/ATNType.rb +22 -0
- data/lib/antlr4/atn/LexerATNSimulator.rb +612 -0
- data/lib/antlr4/atn/LexerAction.rb +311 -0
- data/lib/antlr4/atn/LexerActionExecutor.rb +134 -0
- data/lib/antlr4/atn/ParserATNSimulator.rb +1622 -0
- data/lib/antlr4/atn/PredictionMode.rb +525 -0
- data/lib/antlr4/atn/SemanticContext.rb +355 -0
- data/lib/antlr4/atn/Transition.rb +297 -0
- data/lib/antlr4/base.rb +60 -0
- data/lib/antlr4/dfa/DFA.rb +128 -0
- data/lib/antlr4/dfa/DFASerializer.rb +77 -0
- data/lib/antlr4/dfa/DFAState.rb +133 -0
- data/lib/antlr4/error.rb +151 -0
- data/lib/antlr4/error/DiagnosticErrorListener.rb +136 -0
- data/lib/antlr4/error/ErrorListener.rb +109 -0
- data/lib/antlr4/error/ErrorStrategy.rb +742 -0
- data/lib/antlr4/tree/Chunk.rb +31 -0
- data/lib/antlr4/tree/ParseTreeMatch.rb +105 -0
- data/lib/antlr4/tree/ParseTreePattern.rb +70 -0
- data/lib/antlr4/tree/ParseTreePatternMatcher.rb +334 -0
- data/lib/antlr4/tree/RuleTagToken.rb +39 -0
- data/lib/antlr4/tree/TokenTagToken.rb +38 -0
- data/lib/antlr4/tree/Tree.rb +204 -0
- data/lib/antlr4/tree/Trees.rb +111 -0
- data/lib/antlr4/version.rb +5 -0
- data/lib/antlr4/xpath/XPath.rb +354 -0
- data/lib/double_key_map.rb +78 -0
- data/lib/java_symbols.rb +24 -0
- data/lib/uuid.rb +87 -0
- data/test/test_intervalset.rb +664 -0
- data/test/test_tree.rb +140 -0
- data/test/test_uuid.rb +122 -0
- metadata +109 -0
@@ -0,0 +1,62 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
class ATNDeserializationOptions
|
4
|
+
|
5
|
+
@@defaultOptions = nil
|
6
|
+
def self.defaultOptions
|
7
|
+
if @@defaultOptions.nil?
|
8
|
+
@@defaultOptions = new
|
9
|
+
@@defaultOptions.makeReadOnly()
|
10
|
+
end
|
11
|
+
@@defaultOptions
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_accessor :readonly, :verifyATN ,:generateRuleBypassTransitions
|
15
|
+
def initialize(options=nil )
|
16
|
+
if options.nil?
|
17
|
+
@verifyATN = true
|
18
|
+
@generateRuleBypassTransitions = false
|
19
|
+
else
|
20
|
+
@verifyATN = options.verifyATN
|
21
|
+
@generateRuleBypassTransitions = options.generateRuleBypassTransitions
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
def self.getDefaultOptions()
|
26
|
+
self.defaultOptions
|
27
|
+
end
|
28
|
+
def isReadOnly
|
29
|
+
self.readonly
|
30
|
+
end
|
31
|
+
def makeReadOnly()
|
32
|
+
self.readonly = true
|
33
|
+
end
|
34
|
+
|
35
|
+
def verifyATN=(verifyATN)
|
36
|
+
throwIfReadOnly()
|
37
|
+
@verifyATN = verifyATN
|
38
|
+
end
|
39
|
+
def isVerifyATN()
|
40
|
+
self.verifyATN
|
41
|
+
end
|
42
|
+
def setVerifyATN(verifyATN)
|
43
|
+
self.verifyATN = verifyATN
|
44
|
+
end
|
45
|
+
|
46
|
+
def isGenerateRuleBypassTransitions()
|
47
|
+
self.generateRuleBypassTransitions
|
48
|
+
end
|
49
|
+
def setGenerateRuleBypassTransitions(generateRuleBypassTransitions)
|
50
|
+
self.generateRuleBypassTransitions = generateRuleBypassTransitions
|
51
|
+
end
|
52
|
+
def generateRuleBypassTransitions=(generateRuleBypassTransitions)
|
53
|
+
throwIfReadOnly()
|
54
|
+
@generateRuleBypassTransitions = generateRuleBypassTransitions
|
55
|
+
end
|
56
|
+
protected
|
57
|
+
def throwIfReadOnly()
|
58
|
+
if isReadOnly() then
|
59
|
+
raise IllegalStateException.new("The object is read only.")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,604 @@
|
|
1
|
+
# This is the earliest supported serialized UUID.
|
2
|
+
BASE_SERIALIZED_UUID = UUID.new("AADB8D7E-AEEF-4415-AD2B-8204D6CF042E")
|
3
|
+
|
4
|
+
# This list contains all of the currently supported UUIDs, ordered by when
|
5
|
+
# the feature first appeared in this branch.
|
6
|
+
SUPPORTED_UUIDS = [ BASE_SERIALIZED_UUID ]
|
7
|
+
|
8
|
+
SERIALIZED_VERSION = 3
|
9
|
+
|
10
|
+
# This is the current serialized UUID.
|
11
|
+
SERIALIZED_UUID = BASE_SERIALIZED_UUID
|
12
|
+
|
13
|
+
class ATNDeserializer
|
14
|
+
|
15
|
+
attr_accessor :deserializationOptions
|
16
|
+
attr_accessor :edgeFactories,:stateFactories,:actionFactories
|
17
|
+
attr_accessor :data, :uuid
|
18
|
+
attr :pos
|
19
|
+
def initialize(options=nil)
|
20
|
+
if options.nil?
|
21
|
+
options = ATNDeserializationOptions.defaultOptions
|
22
|
+
end
|
23
|
+
self.deserializationOptions = options
|
24
|
+
self.edgeFactories = nil
|
25
|
+
self.stateFactories = nil
|
26
|
+
self.actionFactories = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
# Determines if a particular serialized representation of an ATN supports
|
30
|
+
# a particular feature, identified by the {@link UUID} used for serializing
|
31
|
+
# the ATN at the time the feature was first introduced.
|
32
|
+
#
|
33
|
+
# @param feature The {@link UUID} marking the first time the feature was
|
34
|
+
# supported in the serialized ATN.
|
35
|
+
# @param actualUuid The {@link UUID} of the actual serialized ATN which is
|
36
|
+
# currently being deserialized.
|
37
|
+
# @return {@code true} if the {@code actualUuid} value represents a
|
38
|
+
# serialized ATN at or after the feature identified by {@code feature} was
|
39
|
+
# introduced; otherwise, {@code false}.
|
40
|
+
|
41
|
+
def isFeatureSupported(feature, actualUuid)
|
42
|
+
idx1 = SUPPORTED_UUIDS.index(feature)
|
43
|
+
if idx1<0
|
44
|
+
return false
|
45
|
+
end
|
46
|
+
idx2 = SUPPORTED_UUIDS.index(actualUuid)
|
47
|
+
return idx2 >= idx1
|
48
|
+
end
|
49
|
+
|
50
|
+
def deserialize(data)
|
51
|
+
self.reset(data)
|
52
|
+
self.checkVersion()
|
53
|
+
self.checkUUID()
|
54
|
+
atn = self.readATN()
|
55
|
+
self.readStates(atn)
|
56
|
+
self.readRules(atn)
|
57
|
+
self.readModes(atn)
|
58
|
+
sets = self.readSets(atn)
|
59
|
+
self.readEdges(atn, sets)
|
60
|
+
self.readDecisions(atn)
|
61
|
+
self.readLexerActions(atn)
|
62
|
+
self.markPrecedenceDecisions(atn)
|
63
|
+
self.verifyATN(atn)
|
64
|
+
if self.deserializationOptions.generateRuleBypassTransitions \
|
65
|
+
and atn.grammarType == ATNType.PARSER
|
66
|
+
self.generateRuleBypassTransitions(atn)
|
67
|
+
# re-verify after modification
|
68
|
+
self.verifyATN(atn)
|
69
|
+
end
|
70
|
+
return atn
|
71
|
+
end
|
72
|
+
def reset(_data)
|
73
|
+
#for (int i = 1; i < data.length; i++) {
|
74
|
+
# data[i] = (char)(data[i] - 2);
|
75
|
+
# don't adjust the first value since that's the version number
|
76
|
+
temp = Array.new(_data.length)
|
77
|
+
temp = _data
|
78
|
+
ver = _data[1]
|
79
|
+
temp[1] = (_data[1].ord + 2)
|
80
|
+
self.data = temp
|
81
|
+
@pos = 0
|
82
|
+
#puts "idx =1288 <573>: #{temp[1288].inspect} : #{_data[1288].ord.inspect} | len #{temp.length}, #{_data.length}"
|
83
|
+
end
|
84
|
+
|
85
|
+
def checkVersion()
|
86
|
+
version = self.readInt()
|
87
|
+
if version != SERIALIZED_VERSION
|
88
|
+
raise Exception.new("Could not deserialize ATN with version #{version} (expected #{SERIALIZED_VERSION}).")
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def checkUUID()
|
93
|
+
uuid = self.readUUID()
|
94
|
+
if not SUPPORTED_UUIDS.member? uuid then
|
95
|
+
raise Exception.new("Could not deserialize ATN with UUID: #{uuid} (expected #{SERIALIZED_UUID} or a legacy UUID).")
|
96
|
+
end
|
97
|
+
self.uuid = uuid
|
98
|
+
end
|
99
|
+
def readATN()
|
100
|
+
idx = self.readInt()
|
101
|
+
grammarType = ATNType.fromOrdinal(idx)
|
102
|
+
maxTokenType = self.readInt()
|
103
|
+
#puts "(readATN: #{grammarType.inspect} #{maxTokenType.inspect} )"
|
104
|
+
return ATN.new(grammarType, maxTokenType)
|
105
|
+
end
|
106
|
+
|
107
|
+
def readStates(atn)
|
108
|
+
loopBackStateNumbers = Array.new
|
109
|
+
endStateNumbers = Array.new
|
110
|
+
nstates = self.readInt()
|
111
|
+
1.upto(nstates) do |i|
|
112
|
+
stype = self.readInt()
|
113
|
+
# ignore bad type of states
|
114
|
+
if stype==ATNState::INVALID_TYPE
|
115
|
+
atn.addState(nil)
|
116
|
+
next
|
117
|
+
end
|
118
|
+
ruleIndex = self.readInt()
|
119
|
+
if ruleIndex == 0xFFFF
|
120
|
+
ruleIndex = -1
|
121
|
+
end
|
122
|
+
s = self.stateFactory(stype, ruleIndex)
|
123
|
+
if stype == ATNState::LOOP_END # special case
|
124
|
+
loopBackStateNumber = self.readInt()
|
125
|
+
loopBackStateNumbers.push([s, loopBackStateNumber])
|
126
|
+
elsif s.kind_of? BlockStartState
|
127
|
+
endStateNumber = self.readInt()
|
128
|
+
endStateNumbers.push([s, endStateNumber])
|
129
|
+
end
|
130
|
+
|
131
|
+
atn.addState(s)
|
132
|
+
end
|
133
|
+
# delay the assignment of loop back and end states until we know all the state instances have been initialized
|
134
|
+
for pair in loopBackStateNumbers do
|
135
|
+
pair[0].loopBackState = atn.states[pair[1]]
|
136
|
+
end
|
137
|
+
for pair in endStateNumbers do
|
138
|
+
pair[0].endState = atn.states[pair[1]]
|
139
|
+
end
|
140
|
+
|
141
|
+
numNonGreedyStates = self.readInt()
|
142
|
+
1.upto(numNonGreedyStates) do |i|
|
143
|
+
stateNumber = self.readInt()
|
144
|
+
atn.states[stateNumber].nonGreedy = true
|
145
|
+
end
|
146
|
+
numPrecedenceStates = self.readInt()
|
147
|
+
1.upto(numPrecedenceStates) do |i|
|
148
|
+
stateNumber = self.readInt()
|
149
|
+
atn.states[stateNumber].isPrecedenceRule = true
|
150
|
+
end
|
151
|
+
end
|
152
|
+
def readRules(atn)
|
153
|
+
nrules = self.readInt()
|
154
|
+
if atn.grammarType == ATNType.LEXER
|
155
|
+
atn.ruleToTokenType = Array.new(nrules, 0) # [0] * nrules
|
156
|
+
end
|
157
|
+
|
158
|
+
atn.ruleToStartState = Array.new(nrules, 0) # [0] * nrules
|
159
|
+
0.upto(nrules - 1) do |i| #for i in range(0, nrules)
|
160
|
+
s = self.readInt()
|
161
|
+
startState = atn.states[s]
|
162
|
+
atn.ruleToStartState[i] = startState
|
163
|
+
if atn.grammarType == ATNType.LEXER
|
164
|
+
tokenType = self.readInt()
|
165
|
+
if tokenType == 0xFFFF
|
166
|
+
tokenType = Token::EOF
|
167
|
+
end
|
168
|
+
atn.ruleToTokenType[i] = tokenType
|
169
|
+
end
|
170
|
+
end
|
171
|
+
atn.ruleToStopState = Array.new(nrules, 0) #[0] * nrules
|
172
|
+
for state in atn.states do
|
173
|
+
if not state.kind_of? RuleStopState then
|
174
|
+
next
|
175
|
+
end
|
176
|
+
atn.ruleToStopState[state.ruleIndex] = state
|
177
|
+
atn.ruleToStartState[state.ruleIndex].stopState = state
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def readModes(atn)
|
182
|
+
nmodes = self.readInt()
|
183
|
+
1.upto(nmodes) do #for i in range(0, nmodes)
|
184
|
+
s = self.readInt()
|
185
|
+
atn.modeToStartState.push(atn.states[s])
|
186
|
+
end
|
187
|
+
end
|
188
|
+
#######
|
189
|
+
# List<IntervalSet> sets = new ArrayList<IntervalSet>();
|
190
|
+
# int nsets = toInt(data[p++]);
|
191
|
+
# for (int i=0; i<nsets; i++) {
|
192
|
+
# int nintervals = toInt(data[p]);
|
193
|
+
# p++;
|
194
|
+
# IntervalSet set = new IntervalSet();
|
195
|
+
# sets.add(set);
|
196
|
+
#
|
197
|
+
# boolean containsEof = toInt(data[p++]) != 0;
|
198
|
+
# if (containsEof) {
|
199
|
+
# set.add(-1);
|
200
|
+
# }
|
201
|
+
#
|
202
|
+
# for (int j=0; j<nintervals; j++) {
|
203
|
+
# set.add(toInt(data[p]), toInt(data[p + 1]));
|
204
|
+
# p += 2;
|
205
|
+
# }
|
206
|
+
# }
|
207
|
+
###############
|
208
|
+
|
209
|
+
def readSets(atn)
|
210
|
+
sets = Array.new
|
211
|
+
m = self.readInt()
|
212
|
+
1.upto(m) do |i| #for i in range(0, m)
|
213
|
+
iset = IntervalSet.new()
|
214
|
+
sets.push(iset)
|
215
|
+
n = self.readInt()
|
216
|
+
containsEof = self.readInt()
|
217
|
+
if containsEof !=0 then
|
218
|
+
iset.addOne(-1)
|
219
|
+
end
|
220
|
+
1.upto(n) do |j| # for j in range(0, n)
|
221
|
+
i1 = self.readInt()
|
222
|
+
i2 = self.readInt()
|
223
|
+
iset.addRange(i1..i2)
|
224
|
+
end
|
225
|
+
end
|
226
|
+
return sets
|
227
|
+
end
|
228
|
+
def readEdges(atn, sets)
|
229
|
+
#print "Current position: #{self.pos}, "
|
230
|
+
nedges = self.readInt()
|
231
|
+
#puts "readEdges n: #{nedges} #{self.pos}"
|
232
|
+
1.upto(nedges) do |i| #for i in range(0, nedges)
|
233
|
+
src = self.readInt()
|
234
|
+
trg = self.readInt()
|
235
|
+
ttype = self.readInt()
|
236
|
+
arg1 = self.readInt()
|
237
|
+
arg2 = self.readInt()
|
238
|
+
arg3 = self.readInt()
|
239
|
+
trans = edgeFactory(atn, ttype, src, trg, arg1, arg2, arg3, sets)
|
240
|
+
srcState = atn.states[src]
|
241
|
+
srcState.addTransition(trans)
|
242
|
+
end
|
243
|
+
# edges for rule stop states can be derived, so they aren't serialized
|
244
|
+
for state in atn.states do
|
245
|
+
state.transitions.each_index do |i| # for i in range(0, len(state.transitions))
|
246
|
+
t = state.transitions[i]
|
247
|
+
next if not t.kind_of? RuleTransition
|
248
|
+
atn.ruleToStopState[t.target.ruleIndex].addTransition(EpsilonTransition.new(t.followState))
|
249
|
+
end
|
250
|
+
end
|
251
|
+
for state in atn.states do
|
252
|
+
if state.kind_of? BlockStartState then
|
253
|
+
# we need to know the end state to set its start state
|
254
|
+
if state.endState.nil? then
|
255
|
+
raise Exception.new("IllegalState")
|
256
|
+
end
|
257
|
+
# block end states can only be associated to a single block start state
|
258
|
+
if state.endState.startState
|
259
|
+
raise Exception.new("IllegalState")
|
260
|
+
end
|
261
|
+
state.endState.startState = state
|
262
|
+
end
|
263
|
+
if state.kind_of? PlusLoopbackState then
|
264
|
+
state.transitions.each_index do |i| #for i in range(0, len(state.transitions))
|
265
|
+
target = state.transitions[i].target
|
266
|
+
if target.kind_of? PlusBlockStartState
|
267
|
+
target.loopBackState = state
|
268
|
+
end
|
269
|
+
end
|
270
|
+
elsif state.kind_of? StarLoopbackState
|
271
|
+
state.transitions.each_index do |i| #for i in range(0, len(state.transitions))
|
272
|
+
target = state.transitions[i].target
|
273
|
+
if target.kind_of? StarLoopEntryState
|
274
|
+
target.loopBackState = state
|
275
|
+
end
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
def readDecisions(atn)
|
281
|
+
ndecisions = self.readInt()
|
282
|
+
1.upto(ndecisions) do |i| # for i in range(0, ndecisions)
|
283
|
+
s = self.readInt()
|
284
|
+
decState = atn.states[s]
|
285
|
+
atn.decisionToState.push(decState)
|
286
|
+
decState.decision = i
|
287
|
+
end
|
288
|
+
end
|
289
|
+
def readLexerActions(atn)
|
290
|
+
if atn.grammarType == ATNType.LEXER
|
291
|
+
count = self.readInt()
|
292
|
+
atn.lexerActions = Array.new # [ nil ] * count
|
293
|
+
0.upto(count-1) do |i| # for i in range(0, count)
|
294
|
+
actionType = self.readInt()
|
295
|
+
data1 = self.readInt()
|
296
|
+
if data1 == 0xFFFF
|
297
|
+
data1 = -1
|
298
|
+
end
|
299
|
+
data2 = self.readInt()
|
300
|
+
if data2 == 0xFFFF
|
301
|
+
data2 = -1
|
302
|
+
end
|
303
|
+
lexerAction = self.lexerActionFactory(actionType, data1, data2)
|
304
|
+
atn.lexerActions[i] = lexerAction
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
def generateRuleBypassTransitions(atn)
|
309
|
+
count = atn.ruleToStartState.length()
|
310
|
+
atn.ruleToTokenType = Array.new(count, 0) #[ 0 ] * count
|
311
|
+
0.upto(count-1) do |i| # for i in range(0, count)
|
312
|
+
atn.ruleToTokenType[i] = atn.maxTokenType + i + 1
|
313
|
+
end
|
314
|
+
|
315
|
+
0.upto(count-1) do |i| # for i in range(0, count)
|
316
|
+
self.generateRuleBypassTransition(atn, i)
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
def generateRuleBypassTransition(atn, idx)
|
321
|
+
bypassStart = BasicBlockStartState.new()
|
322
|
+
bypassStart.ruleIndex = idx
|
323
|
+
atn.addState(bypassStart)
|
324
|
+
|
325
|
+
bypassStop = BlockEndState.new()
|
326
|
+
bypassStop.ruleIndex = idx
|
327
|
+
atn.addState(bypassStop)
|
328
|
+
|
329
|
+
bypassStart.endState = bypassStop
|
330
|
+
atn.defineDecisionState(bypassStart)
|
331
|
+
|
332
|
+
bypassStop.startState = bypassStart
|
333
|
+
|
334
|
+
excludeTransition = nil
|
335
|
+
|
336
|
+
if atn.ruleToStartState[idx].isPrecedenceRule
|
337
|
+
# wrap from the beginning of the rule to the StarLoopEntryState
|
338
|
+
endState = nil
|
339
|
+
for state in atn.states do
|
340
|
+
if self.stateIsEndStateFor(state, idx) then
|
341
|
+
endState = state
|
342
|
+
excludeTransition = state.loopBackState.transitions[0]
|
343
|
+
break
|
344
|
+
end
|
345
|
+
end
|
346
|
+
if excludeTransition.nil?
|
347
|
+
raise Exception.new("Couldn't identify final state of the precedence rule prefix section.")
|
348
|
+
end
|
349
|
+
else
|
350
|
+
endState = atn.ruleToStopState[idx]
|
351
|
+
end
|
352
|
+
|
353
|
+
# all non-excluded transitions that currently target end state need to target blockEnd instead
|
354
|
+
for state in atn.states
|
355
|
+
for transition in state.transitions
|
356
|
+
if transition == excludeTransition
|
357
|
+
next
|
358
|
+
end
|
359
|
+
if transition.target == endState
|
360
|
+
transition.target = bypassStop
|
361
|
+
end
|
362
|
+
end
|
363
|
+
end
|
364
|
+
# all transitions leaving the rule start state need to leave blockStart instead
|
365
|
+
ruleToStartState = atn.ruleToStartState[idx]
|
366
|
+
count = ruleToStartState.transitions.length()
|
367
|
+
0.upto(ruleToStartState.transitions.length() -1) do |i|
|
368
|
+
transition = ruleToStartState.removeTransition(i)
|
369
|
+
bypassStart.addTransition(transition)
|
370
|
+
end
|
371
|
+
# link the new states
|
372
|
+
atn.ruleToStartState[idx].addTransition(EpsilonTransition.new(bypassStart))
|
373
|
+
bypassStop.addTransition(EpsilonTransition.new(endState))
|
374
|
+
|
375
|
+
matchState = BasicState.new()
|
376
|
+
atn.addState(matchState)
|
377
|
+
matchState.addTransition(AtomTransition.new(bypassStop, atn.ruleToTokenType[idx]))
|
378
|
+
bypassStart.addTransition(EpsilonTransition.new(matchState))
|
379
|
+
end
|
380
|
+
|
381
|
+
def stateIsEndStateFor(state, idx)
|
382
|
+
return nil if state.ruleIndex != idx
|
383
|
+
return nil if not state.kind_of? StarLoopEntryState
|
384
|
+
|
385
|
+
maybeLoopEndState = state.transitions[-1].target
|
386
|
+
return nil if not maybeLoopEndState.kind_of? LoopEndState
|
387
|
+
|
388
|
+
if maybeLoopEndState.epsilonOnlyTransitions and maybeLoopEndState.transitions[0].target.kind_of? RuleStopState
|
389
|
+
return state
|
390
|
+
else
|
391
|
+
return nil
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
#
|
396
|
+
# Analyze the {@link StarLoopEntryState} states in the specified ATN to set
|
397
|
+
# the {@link StarLoopEntryState#precedenceRuleDecision} field to the
|
398
|
+
# correct value.
|
399
|
+
#
|
400
|
+
# @param atn The ATN.
|
401
|
+
#
|
402
|
+
def markPrecedenceDecisions(atn)
|
403
|
+
for state in atn.states do
|
404
|
+
next if not state.kind_of? StarLoopEntryState
|
405
|
+
|
406
|
+
# We analyze the ATN to determine if this ATN decision state is the
|
407
|
+
# decision for the closure block that determines whether a
|
408
|
+
# precedence rule should continue or complete.
|
409
|
+
#
|
410
|
+
if atn.ruleToStartState[state.ruleIndex].isPrecedenceRule
|
411
|
+
maybeLoopEndState = state.transitions[-1].target
|
412
|
+
if maybeLoopEndState.kind_of? LoopEndState
|
413
|
+
if maybeLoopEndState.epsilonOnlyTransitions and \
|
414
|
+
maybeLoopEndState.transitions[0].target.kind_of? RuleStopState
|
415
|
+
state.precedenceRuleDecision = true
|
416
|
+
end
|
417
|
+
end
|
418
|
+
end
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
def verifyATN(atn)
|
423
|
+
return if not self.deserializationOptions.verifyATN
|
424
|
+
# verify assumptions
|
425
|
+
for state in atn.states do
|
426
|
+
next if state.nil?
|
427
|
+
self.checkCondition((state.epsilonOnlyTransitions or state.transitions.length <= 1))
|
428
|
+
if state.kind_of? PlusBlockStartState then
|
429
|
+
self.checkCondition( !state.loopBackState.nil?)
|
430
|
+
|
431
|
+
if state.kind_of? StarLoopEntryState then
|
432
|
+
self.checkCondition( !state.loopBackState.nil? )
|
433
|
+
self.checkCondition(state.transitions.length() == 2)
|
434
|
+
|
435
|
+
if state.transitions[0].target.kind_of? StarBlockStartState then
|
436
|
+
self.checkCondition(state.transitions[1].target.kind_of?(LoopEndState))
|
437
|
+
self.checkCondition(!state.nonGreedy)
|
438
|
+
elsif state.transitions[0].target.kind_of? LoopEndState then
|
439
|
+
self.checkCondition(state.transitions[1].target.kind_of?(StarBlockStartState))
|
440
|
+
self.checkCondition(state.nonGreedy)
|
441
|
+
else
|
442
|
+
raise Exception.new("IllegalState")
|
443
|
+
end
|
444
|
+
end
|
445
|
+
if state.kind_of? StarLoopbackState then
|
446
|
+
self.checkCondition(state.transitions.length() == 1)
|
447
|
+
self.checkCondition(state.transitions[0].target.kind_of?(StarLoopEntryState))
|
448
|
+
end
|
449
|
+
if state.kind_of? LoopEndState then
|
450
|
+
self.checkCondition(! state.loopBackState.nil?)
|
451
|
+
end
|
452
|
+
|
453
|
+
if state.kind_of? RuleStartState then
|
454
|
+
self.checkCondition( !state.stopState.nil? )
|
455
|
+
end
|
456
|
+
|
457
|
+
if state.kind_of? BlockStartState then
|
458
|
+
self.checkCondition(!state.endState.nil? )
|
459
|
+
end
|
460
|
+
|
461
|
+
if state.kind_of? BlockEndState then
|
462
|
+
self.checkCondition(!state.startState.nil? )
|
463
|
+
end
|
464
|
+
|
465
|
+
if state.kind_of? DecisionState then
|
466
|
+
self.checkCondition( (( state.transitions.length() <= 1) || ( state.decision >= 0)))
|
467
|
+
else
|
468
|
+
self.checkCondition(((state.transitions.lenth() <= 1) || (state.kind_of?(RuleStopState) )))
|
469
|
+
end
|
470
|
+
end
|
471
|
+
end
|
472
|
+
end
|
473
|
+
def checkCondition(condition, message=nil)
|
474
|
+
unless condition then
|
475
|
+
if message.nil?
|
476
|
+
message = "IllegalState"
|
477
|
+
end
|
478
|
+
raise Exception.new(message)
|
479
|
+
end
|
480
|
+
end
|
481
|
+
def readByte()
|
482
|
+
i = @data[self.pos]
|
483
|
+
begin
|
484
|
+
ii = i.ord
|
485
|
+
rescue
|
486
|
+
puts "#{i.class} #{i} #{i.encoding}"
|
487
|
+
end
|
488
|
+
@pos = @pos + 1
|
489
|
+
return ii
|
490
|
+
end
|
491
|
+
def readInt()
|
492
|
+
high = readByte()
|
493
|
+
low = readByte()
|
494
|
+
return (low | (high << 8)) - 2
|
495
|
+
end
|
496
|
+
|
497
|
+
def readInt32()
|
498
|
+
low = self.readInt()
|
499
|
+
high = self.readInt()
|
500
|
+
return low | (high << 16)
|
501
|
+
end
|
502
|
+
def readLong()
|
503
|
+
low = self.readInt32()
|
504
|
+
high = self.readInt32()
|
505
|
+
return (low & 0x00000000FFFFFFFF) | (high << 32)
|
506
|
+
end
|
507
|
+
|
508
|
+
def readUUID()
|
509
|
+
low = self.readLong()
|
510
|
+
high = self.readLong()
|
511
|
+
allBits = (low & 0xFFFFFFFFFFFFFFFF) | (high << 64)
|
512
|
+
return UUID.create_from_bytes(allBits)
|
513
|
+
end
|
514
|
+
|
515
|
+
def edgeFactory(atn, type, src, trg, arg1, arg2, arg3, sets)
|
516
|
+
target = atn.states[trg]
|
517
|
+
if self.edgeFactories.nil? then
|
518
|
+
ef = Array.new(11) # [nil] * 11
|
519
|
+
ef[0] = lambda {|atn, src, trg, arg1, arg2, arg3, sets, target| raise Exception.new("The specified state type 0 is not valid.") }
|
520
|
+
ef[Transition::EPSILON] = lambda {|atn, src, trg, arg1, arg2, arg3, sets, target| EpsilonTransition.new(target) }
|
521
|
+
ef[Transition::RANGE] = lambda {|atn, src, trg, arg1, arg2, arg3, sets, target|
|
522
|
+
if arg3 != 0
|
523
|
+
RangeTransition.new(target, Token::EOF, arg2)
|
524
|
+
else
|
525
|
+
RangeTransition.new(target, arg1, arg2)
|
526
|
+
end
|
527
|
+
}
|
528
|
+
ef[Transition::RULE] = lambda {|atn, src, trg, arg1, arg2, arg3, sets, target |
|
529
|
+
RuleTransition.new(atn.states[arg1], arg2, arg3, target) }
|
530
|
+
ef[Transition::PREDICATE] = lambda {|atn, src, trg, arg1, arg2, arg3, sets, target |
|
531
|
+
PredicateTransition.new(target, arg1, arg2, arg3 != 0) }
|
532
|
+
ef[Transition::PRECEDENCE] = lambda {|atn, src, trg, arg1, arg2, arg3, sets, target |
|
533
|
+
PrecedencePredicateTransition.new(target, arg1) }
|
534
|
+
ef[Transition::ATOM] = lambda {|atn, src, trg, arg1, arg2, arg3, sets, target |
|
535
|
+
if arg3 != 0
|
536
|
+
AtomTransition.new(target, Token::EOF)
|
537
|
+
else
|
538
|
+
AtomTransition.new(target, arg1)
|
539
|
+
end
|
540
|
+
}
|
541
|
+
ef[Transition::ACTION] = lambda {|atn, src, trg, arg1, arg2, arg3, sets, target |
|
542
|
+
ActionTransition.new(target, arg1, arg2, arg3 != 0) }
|
543
|
+
ef[Transition::SET] = lambda {|atn, src, trg, arg1, arg2, arg3, sets, target|
|
544
|
+
SetTransition.new(target, sets[arg1]) }
|
545
|
+
ef[Transition::NOT_SET] = lambda {|atn, src, trg, arg1, arg2, arg3, sets, target |
|
546
|
+
NotSetTransition.new(target, sets[arg1]) }
|
547
|
+
ef[Transition::WILDCARD] = lambda {|atn, src, trg, arg1, arg2, arg3, sets, target |
|
548
|
+
WildcardTransition.new(target) }
|
549
|
+
self.edgeFactories = ef
|
550
|
+
end
|
551
|
+
if type > self.edgeFactories.length() or self.edgeFactories[type].nil? then
|
552
|
+
raise Exception.new("The specified transition type: #{type} is not valid.")
|
553
|
+
else
|
554
|
+
return self.edgeFactories[type].call(atn, src, trg, arg1, arg2, arg3, sets, target)
|
555
|
+
end
|
556
|
+
end
|
557
|
+
def stateFactory(type, ruleIndex)
|
558
|
+
if self.stateFactories.nil?
|
559
|
+
sf = Array.new(13) # [nil] * 13
|
560
|
+
sf[ATNState::INVALID_TYPE] = lambda { nil }
|
561
|
+
sf[ATNState::BASIC] = lambda { BasicState.new }
|
562
|
+
sf[ATNState::RULE_START] = lambda { RuleStartState.new }
|
563
|
+
sf[ATNState::BLOCK_START] = lambda { BasicBlockStartState.new }
|
564
|
+
sf[ATNState::PLUS_BLOCK_START] = lambda { PlusBlockStartState.new }
|
565
|
+
sf[ATNState::STAR_BLOCK_START] = lambda { StarBlockStartState.new }
|
566
|
+
sf[ATNState::TOKEN_START] = lambda { TokensStartState.new }
|
567
|
+
sf[ATNState::RULE_STOP] = lambda { RuleStopState.new }
|
568
|
+
sf[ATNState::BLOCK_END] = lambda { BlockEndState.new }
|
569
|
+
sf[ATNState::STAR_LOOP_BACK] = lambda { StarLoopbackState.new }
|
570
|
+
sf[ATNState::STAR_LOOP_ENTRY] = lambda { StarLoopEntryState.new }
|
571
|
+
sf[ATNState::PLUS_LOOP_BACK] = lambda { PlusLoopbackState.new }
|
572
|
+
sf[ATNState::LOOP_END] = lambda { LoopEndState.new }
|
573
|
+
self.stateFactories = sf
|
574
|
+
end
|
575
|
+
if type> self.stateFactories.length() or self.stateFactories[type].nil?
|
576
|
+
raise Exceptionn.new("The specified state type #{type} is not valid.")
|
577
|
+
else
|
578
|
+
s = self.stateFactories[type].call()
|
579
|
+
if s
|
580
|
+
s.ruleIndex = ruleIndex
|
581
|
+
end
|
582
|
+
end
|
583
|
+
return s
|
584
|
+
end
|
585
|
+
def lexerActionFactory(type, data1, data2)
|
586
|
+
if self.actionFactories.nil? then
|
587
|
+
af = Array.new(8) #[ nil ] * 8
|
588
|
+
af[LexerActionType::CHANNEL] = lambda {|data1, data2| LexerChannelAction.new(data1) }
|
589
|
+
af[LexerActionType::CUSTOM] = lambda {|data1, data2| LexerCustomAction.new(data1, data2) }
|
590
|
+
af[LexerActionType::MODE] = lambda {|data1, data2| LexerModeAction.new(data1) }
|
591
|
+
af[LexerActionType::MORE] = lambda {|data1, data2| LexerMoreAction.INSTANCE }
|
592
|
+
af[LexerActionType::POP_MODE] = lambda {|data1, data2| LexerPopModeAction.INSTANCE }
|
593
|
+
af[LexerActionType::PUSH_MODE] = lambda {|data1, data2| LexerPushModeAction.new(data1) }
|
594
|
+
af[LexerActionType::SKIP] = lambda {|data1, data2| LexerSkipAction.INSTANCE }
|
595
|
+
af[LexerActionType::TYPE] = lambda {|data1, data2| LexerTypeAction.new(data1) }
|
596
|
+
self.actionFactories = af
|
597
|
+
end
|
598
|
+
if type> self.actionFactories.length() or self.actionFactories[type].nil?
|
599
|
+
raise Exception("The specified lexer action type #{type} is not valid.")
|
600
|
+
else
|
601
|
+
return self.actionFactories[type].call(data1, data2)
|
602
|
+
end
|
603
|
+
end
|
604
|
+
end
|