antlr3 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ANTLR-LICENSE.txt +26 -0
- data/History.txt +66 -0
- data/README.txt +139 -0
- data/bin/antlr4ruby +33 -0
- data/java/RubyTarget.java +524 -0
- data/java/antlr-full-3.2.1.jar +0 -0
- data/lib/antlr3.rb +176 -0
- data/lib/antlr3/constants.rb +88 -0
- data/lib/antlr3/debug.rb +701 -0
- data/lib/antlr3/debug/event-hub.rb +210 -0
- data/lib/antlr3/debug/record-event-listener.rb +25 -0
- data/lib/antlr3/debug/rule-tracer.rb +55 -0
- data/lib/antlr3/debug/socket.rb +360 -0
- data/lib/antlr3/debug/trace-event-listener.rb +92 -0
- data/lib/antlr3/dfa.rb +247 -0
- data/lib/antlr3/dot.rb +174 -0
- data/lib/antlr3/error.rb +657 -0
- data/lib/antlr3/main.rb +561 -0
- data/lib/antlr3/modes/ast-builder.rb +41 -0
- data/lib/antlr3/modes/filter.rb +56 -0
- data/lib/antlr3/profile.rb +322 -0
- data/lib/antlr3/recognizers.rb +1280 -0
- data/lib/antlr3/streams.rb +985 -0
- data/lib/antlr3/streams/interactive.rb +91 -0
- data/lib/antlr3/streams/rewrite.rb +412 -0
- data/lib/antlr3/test/call-stack.rb +57 -0
- data/lib/antlr3/test/config.rb +23 -0
- data/lib/antlr3/test/core-extensions.rb +269 -0
- data/lib/antlr3/test/diff.rb +165 -0
- data/lib/antlr3/test/functional.rb +207 -0
- data/lib/antlr3/test/grammar.rb +371 -0
- data/lib/antlr3/token.rb +592 -0
- data/lib/antlr3/tree.rb +1415 -0
- data/lib/antlr3/tree/debug.rb +163 -0
- data/lib/antlr3/tree/visitor.rb +84 -0
- data/lib/antlr3/tree/wizard.rb +481 -0
- data/lib/antlr3/util.rb +149 -0
- data/lib/antlr3/version.rb +27 -0
- data/samples/ANTLRv3Grammar.g +621 -0
- data/samples/Cpp.g +749 -0
- data/templates/AST.stg +335 -0
- data/templates/ASTDbg.stg +40 -0
- data/templates/ASTParser.stg +153 -0
- data/templates/ASTTreeParser.stg +272 -0
- data/templates/Dbg.stg +192 -0
- data/templates/Ruby.stg +1514 -0
- data/test/functional/ast-output/auto-ast.rb +797 -0
- data/test/functional/ast-output/construction.rb +555 -0
- data/test/functional/ast-output/hetero-nodes.rb +753 -0
- data/test/functional/ast-output/rewrites.rb +1327 -0
- data/test/functional/ast-output/tree-rewrite.rb +1662 -0
- data/test/functional/debugging/debug-mode.rb +689 -0
- data/test/functional/debugging/profile-mode.rb +165 -0
- data/test/functional/debugging/rule-tracing.rb +74 -0
- data/test/functional/delegation/import.rb +379 -0
- data/test/functional/lexer/basic.rb +559 -0
- data/test/functional/lexer/filter-mode.rb +245 -0
- data/test/functional/lexer/nuances.rb +47 -0
- data/test/functional/lexer/properties.rb +104 -0
- data/test/functional/lexer/syn-pred.rb +32 -0
- data/test/functional/lexer/xml.rb +206 -0
- data/test/functional/main/main-scripts.rb +245 -0
- data/test/functional/parser/actions.rb +224 -0
- data/test/functional/parser/backtracking.rb +244 -0
- data/test/functional/parser/basic.rb +282 -0
- data/test/functional/parser/calc.rb +98 -0
- data/test/functional/parser/ll-star.rb +143 -0
- data/test/functional/parser/nuances.rb +165 -0
- data/test/functional/parser/predicates.rb +103 -0
- data/test/functional/parser/properties.rb +242 -0
- data/test/functional/parser/rule-methods.rb +132 -0
- data/test/functional/parser/scopes.rb +274 -0
- data/test/functional/token-rewrite/basic.rb +318 -0
- data/test/functional/token-rewrite/via-parser.rb +100 -0
- data/test/functional/tree-parser/basic.rb +750 -0
- data/test/unit/sample-input/file-stream-1 +2 -0
- data/test/unit/sample-input/teststreams.input2 +2 -0
- data/test/unit/test-dfa.rb +52 -0
- data/test/unit/test-exceptions.rb +44 -0
- data/test/unit/test-recognizers.rb +55 -0
- data/test/unit/test-scheme.rb +62 -0
- data/test/unit/test-streams.rb +459 -0
- data/test/unit/test-tree-wizard.rb +535 -0
- data/test/unit/test-trees.rb +854 -0
- metadata +205 -0
data/lib/antlr3/error.rb
ADDED
@@ -0,0 +1,657 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
=begin LICENSE
|
5
|
+
|
6
|
+
[The "BSD licence"]
|
7
|
+
Copyright (c) 2009 Kyle Yetter
|
8
|
+
All rights reserved.
|
9
|
+
|
10
|
+
Redistribution and use in source and binary forms, with or without
|
11
|
+
modification, are permitted provided that the following conditions
|
12
|
+
are met:
|
13
|
+
|
14
|
+
1. Redistributions of source code must retain the above copyright
|
15
|
+
notice, this list of conditions and the following disclaimer.
|
16
|
+
2. Redistributions in binary form must reproduce the above copyright
|
17
|
+
notice, this list of conditions and the following disclaimer in the
|
18
|
+
documentation and/or other materials provided with the distribution.
|
19
|
+
3. The name of the author may not be used to endorse or promote products
|
20
|
+
derived from this software without specific prior written permission.
|
21
|
+
|
22
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
23
|
+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
24
|
+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
25
|
+
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
26
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
27
|
+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
28
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
29
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
30
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
31
|
+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
|
33
|
+
=end
|
34
|
+
|
35
|
+
# ANTLR3 exception hierarchy
|
36
|
+
# - ported from the ANTLR3 Python Runtime library by
|
37
|
+
# Kyle Yetter (kcy5b@yahoo.com)
|
38
|
+
module ANTLR3
|
39
|
+
module Error
|
40
|
+
|
41
|
+
=begin rdoc ANTLR3::Error::BacktrackingFailed
|
42
|
+
|
43
|
+
error:: BacktrackingFailed
|
44
|
+
used by:: all recognizers
|
45
|
+
occurs when::
|
46
|
+
recognizer is in backtracking mode (i.e. r.state.backtracking > 0)
|
47
|
+
and the decision path the recognizer is currently attempting
|
48
|
+
hit a point of failure
|
49
|
+
notes::
|
50
|
+
- functions more as an internal signal, simillar to exception
|
51
|
+
classes such as StopIteration and SystemExit
|
52
|
+
- used to inform the recognizer that it needs to rewind
|
53
|
+
the input to the point at which it started the decision
|
54
|
+
and then either try another possible decision path or
|
55
|
+
declare failure
|
56
|
+
- not a subclass of RecognitionError
|
57
|
+
|
58
|
+
=end
|
59
|
+
|
60
|
+
class BacktrackingFailed < StandardError; end
|
61
|
+
|
62
|
+
# To avoid English-only error messages and to generally make things
|
63
|
+
# as flexible as possible, these exceptions are not created with strings,
|
64
|
+
# but rather the information necessary to generate an error. Then
|
65
|
+
# the various reporting methods in Parser and Lexer can be overridden
|
66
|
+
# to generate a localized error message. For example, MismatchedToken
|
67
|
+
# exceptions are built with the expected token type.
|
68
|
+
# So, don't expect getMessage() to return anything.
|
69
|
+
#
|
70
|
+
# Note that as of Java 1.4, you can access the stack trace, which means
|
71
|
+
# that you can compute the complete trace of rules from the start symbol.
|
72
|
+
# This gives you considerable context information with which to generate
|
73
|
+
# useful error messages.
|
74
|
+
#
|
75
|
+
# ANTLR generates code that throws exceptions upon recognition error and
|
76
|
+
# also generates code to catch these exceptions in each rule. If you
|
77
|
+
# want to quit upon first error, you can turn off the automatic error
|
78
|
+
# handling mechanism using rulecatch action, but you still need to
|
79
|
+
# override methods mismatch and recoverFromMismatchSet.
|
80
|
+
#
|
81
|
+
# In general, the recognition exceptions can track where in a grammar a
|
82
|
+
# problem occurred and/or what was the expected input. While the parser
|
83
|
+
# knows its state (such as current input symbol and line info) that
|
84
|
+
# state can change before the exception is reported so current token index
|
85
|
+
# is computed and stored at exception time. From this info, you can
|
86
|
+
# perhaps print an entire line of input not just a single token, for example.
|
87
|
+
# Better to just say the recognizer had a problem and then let the parser
|
88
|
+
# figure out a fancy report.
|
89
|
+
|
90
|
+
=begin rdoc ANTLR3::Error::RecognitionError
|
91
|
+
|
92
|
+
The base class of the variety of syntax errors that can occur during the
|
93
|
+
recognition process. These errors all typically concern an expectation built in
|
94
|
+
to the recognizer by the rules of a grammar and an input symbol which failed to
|
95
|
+
fit the expectation.
|
96
|
+
|
97
|
+
=end
|
98
|
+
|
99
|
+
class RecognitionError < StandardError
|
100
|
+
include ANTLR3::Constants
|
101
|
+
attr_accessor :input, :index, :line, :column, :symbol, :token, :source_name
|
102
|
+
|
103
|
+
def initialize(input = nil)
|
104
|
+
@index = @line = @column = nil
|
105
|
+
@approximate_line_info = false
|
106
|
+
if @input = input
|
107
|
+
@index = input.index
|
108
|
+
@source_name = @input.source_name rescue nil
|
109
|
+
case @input
|
110
|
+
when TokenStream
|
111
|
+
@token = @symbol = input.look
|
112
|
+
@line = @symbol.line
|
113
|
+
@column = @symbol.column
|
114
|
+
when CharacterStream
|
115
|
+
@token = @symbol = input.peek || EOF
|
116
|
+
@line = @input.line
|
117
|
+
@column = @input.column
|
118
|
+
when AST::TreeNodeStream
|
119
|
+
@symbol = nodes.look
|
120
|
+
if @symbol.respond_to?(:line) and @symbol.respond_to?(:column)
|
121
|
+
@line, @column = @symbol.line, @symbol.column
|
122
|
+
else
|
123
|
+
extract_from_node_stream(@input)
|
124
|
+
end
|
125
|
+
else
|
126
|
+
@symbol = @input.look
|
127
|
+
if @symbol.respond_to?(:line) and @symbol.respond_to?(:column)
|
128
|
+
@line, @column = @symbol.line, @symbol.column
|
129
|
+
elsif @input.respond_to?(:line) and @input.respond_to?(:column)
|
130
|
+
@line, @column = @input.line, @input.column
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
super(message)
|
135
|
+
end
|
136
|
+
|
137
|
+
def approximate_line_info?
|
138
|
+
@approximate_line_info
|
139
|
+
end
|
140
|
+
|
141
|
+
def unexpected_type
|
142
|
+
case @input
|
143
|
+
when TokenStream
|
144
|
+
@symbol.type
|
145
|
+
when AST::TreeNodeStream
|
146
|
+
adaptor = @input.adaptor
|
147
|
+
return adaptor.type(@symbol)
|
148
|
+
else
|
149
|
+
return @symbol
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def location
|
154
|
+
if @source_name then "in #@source_name @ line #@line:#@column"
|
155
|
+
else "line #@line:#@column"
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
alias inspect message
|
160
|
+
|
161
|
+
private
|
162
|
+
|
163
|
+
def extract_from_node_stream(nodes)
|
164
|
+
adaptor = nodes.adaptor
|
165
|
+
payload = adaptor.token(@symbol)
|
166
|
+
|
167
|
+
if payload
|
168
|
+
@token = payload
|
169
|
+
if payload.line <= 0
|
170
|
+
i = -1
|
171
|
+
while prior_node = nodes.look(i)
|
172
|
+
prior_payload = adaptor.token(prior_node)
|
173
|
+
if prior_payload and prior_payload.line > 0
|
174
|
+
@line = prior_payload.line
|
175
|
+
@column = prior_payload.column
|
176
|
+
@approximate_line_info = true
|
177
|
+
break
|
178
|
+
end
|
179
|
+
i -= 1
|
180
|
+
end
|
181
|
+
else
|
182
|
+
@line = payload.line
|
183
|
+
@column = payload.column
|
184
|
+
end
|
185
|
+
elsif @symbol.is_a?(AST::Tree)
|
186
|
+
@line = @symbol.line
|
187
|
+
@column = @symbol.column
|
188
|
+
@symbol.is_a?(AST::CommonTree) and @token = @symbol.token
|
189
|
+
else
|
190
|
+
type = adaptor.type( @symbol )
|
191
|
+
text = adaptor.text( @symbol )
|
192
|
+
token_class = @input.token_class rescue CommonToken
|
193
|
+
@token = token_class.new
|
194
|
+
@token.type = type
|
195
|
+
@token.text = text
|
196
|
+
@token
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
=begin rdoc ANTLR3::Error::MismatchedToken
|
202
|
+
|
203
|
+
type:: MismatchedToken
|
204
|
+
used by:: lexers and parsers
|
205
|
+
occurs when::
|
206
|
+
The recognizer expected to match a symbol <tt>x</tt> at the current input
|
207
|
+
position, but it saw a different symbol <tt>y</tt> instead.
|
208
|
+
|
209
|
+
=end
|
210
|
+
|
211
|
+
class MismatchedToken < RecognitionError
|
212
|
+
attr_reader :expecting
|
213
|
+
def initialize(expecting, input)
|
214
|
+
@expecting = expecting
|
215
|
+
super(input)
|
216
|
+
end
|
217
|
+
|
218
|
+
def message
|
219
|
+
"%s: %p %p" % [self.class, unexpected_type, @expecting.inspect]
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
=begin rdoc ANTLR3::Error::UnwantedToken
|
224
|
+
|
225
|
+
TODO: this does not appear to be used by any code
|
226
|
+
|
227
|
+
=end
|
228
|
+
|
229
|
+
class UnwantedToken < MismatchedToken
|
230
|
+
def unexpected_token
|
231
|
+
return @token
|
232
|
+
end
|
233
|
+
|
234
|
+
def message
|
235
|
+
exp = @expecting == INVALID_TOKEN_TYPE ? '' : ", expected %p" % @expecting
|
236
|
+
text = @symbol.text rescue nil
|
237
|
+
"%s: found=%p%s" % [self.class, text, exp]
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
=begin rdoc ANTLR3::Error::MissingToken
|
242
|
+
|
243
|
+
error:: MissingToken
|
244
|
+
used by:: parsers and tree parsers
|
245
|
+
occurs when::
|
246
|
+
The recognizer expected to match some symbol, but it sees a different symbol.
|
247
|
+
The symbol it sees is actually what the recognizer expected to match next.
|
248
|
+
|
249
|
+
=== Example
|
250
|
+
|
251
|
+
grammar:
|
252
|
+
|
253
|
+
grammar MissingTokenExample;
|
254
|
+
|
255
|
+
options { language = Ruby; }
|
256
|
+
|
257
|
+
@members {
|
258
|
+
def report_error(e)
|
259
|
+
raise e
|
260
|
+
end
|
261
|
+
}
|
262
|
+
|
263
|
+
missing: A B C;
|
264
|
+
|
265
|
+
A: 'a';
|
266
|
+
B: 'b';
|
267
|
+
C: 'c';
|
268
|
+
|
269
|
+
in ruby:
|
270
|
+
|
271
|
+
require 'MissingTokenExampleLexer'
|
272
|
+
require 'MissingTokenExampleParser'
|
273
|
+
|
274
|
+
lexer = MissingTokenExample::Lexer.new( "ac" ) # <= notice the missing 'b'
|
275
|
+
tokens = ANTLR3::CommonTokenStream.new( lexer )
|
276
|
+
parser = MissingTokenExample::Parser.new( tokens )
|
277
|
+
|
278
|
+
parser.missing
|
279
|
+
# raises ANTLR3::Error::MissingToken: at "c"
|
280
|
+
|
281
|
+
=end
|
282
|
+
|
283
|
+
class MissingToken < MismatchedToken
|
284
|
+
attr_accessor :inserted
|
285
|
+
def initialize(expecting, input, inserted)
|
286
|
+
super(expecting, input)
|
287
|
+
@inserted = inserted
|
288
|
+
end
|
289
|
+
|
290
|
+
def missing_type
|
291
|
+
return @expecting
|
292
|
+
end
|
293
|
+
|
294
|
+
def message
|
295
|
+
if @inserted and @symbol
|
296
|
+
"%s: inserted %p at %p" %
|
297
|
+
[self.class, @inserted, @symbol.text]
|
298
|
+
else
|
299
|
+
msg = self.class.to_s
|
300
|
+
msg << ': at %p' % token.text unless @token.nil?
|
301
|
+
return msg
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
=begin rdoc ANTLR3::Error::MismatchedRange
|
307
|
+
|
308
|
+
error:: MismatchedRange
|
309
|
+
used by:: all recognizers
|
310
|
+
occurs when::
|
311
|
+
A recognizer expected to match an input symbol (either a character value or
|
312
|
+
an integer token type value) that falls into a range of possible values, but
|
313
|
+
instead it saw a symbol that falls outside the expected range.
|
314
|
+
|
315
|
+
=end
|
316
|
+
|
317
|
+
class MismatchedRange < RecognitionError
|
318
|
+
attr_accessor :min, :max
|
319
|
+
def initialize(min, max, input)
|
320
|
+
@min = min
|
321
|
+
@max = max
|
322
|
+
super(input)
|
323
|
+
end
|
324
|
+
|
325
|
+
def message
|
326
|
+
"%s: %p not in %p..%p" %
|
327
|
+
[self.class, unexpected_type, @min, @max]
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
=begin rdoc ANTLR3::Error::MismatchedSet
|
332
|
+
|
333
|
+
error:: MismatchedSet
|
334
|
+
used by:: all recognizers
|
335
|
+
occurs when::
|
336
|
+
A recognizer expects the current input symbol to be a member of a set of
|
337
|
+
possible symbol values, but the current symbol does not match.
|
338
|
+
|
339
|
+
=end
|
340
|
+
|
341
|
+
class MismatchedSet < RecognitionError
|
342
|
+
attr_accessor :expecting
|
343
|
+
def initialize(expecting, input)
|
344
|
+
super(input)
|
345
|
+
@expecting = expecting
|
346
|
+
end
|
347
|
+
|
348
|
+
def message
|
349
|
+
"%s: %p not in %p" %
|
350
|
+
[self.class, unexpected_type, @expecting]
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
=begin rdoc ANTLR3::Error::MismatchedNotSet
|
355
|
+
|
356
|
+
error:: MismatchedNotSet
|
357
|
+
used by:: all recognizers
|
358
|
+
occurs when::
|
359
|
+
A recognizer expected to match symbol that is not in some set of symbols but
|
360
|
+
failed.
|
361
|
+
|
362
|
+
=end
|
363
|
+
|
364
|
+
class MismatchedNotSet < MismatchedSet
|
365
|
+
def message
|
366
|
+
'%s: %p != %p' %
|
367
|
+
[self.class, unexpected_type, @expecting]
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
=begin rdoc ANTLR3::Error::NoViableAlternative
|
372
|
+
|
373
|
+
error:: NoViableAlternative
|
374
|
+
used by:: all recognizers
|
375
|
+
occurs when::
|
376
|
+
A recognizer must choose between multiple possible recognition paths based
|
377
|
+
upon the current and future input symbols, but it has determined that
|
378
|
+
the input does not suit any of the possible recognition alternatives.
|
379
|
+
|
380
|
+
In ANTLR terminology, a rule is composed of one or more _alternatives_,
|
381
|
+
specifications seperated by <tt>|</tt> characters. An alternative is composed of
|
382
|
+
a series of elements, including _subrules_ -- rule specifications enclosed
|
383
|
+
within parentheses. When recognition code enters a rule method (or a subrule
|
384
|
+
block) that has multiple alternatives, the recognizer must decide which one of
|
385
|
+
the multiple possible paths to follow by checking a number of future input
|
386
|
+
symbols. Thus, NoViableAlternative errors indicate that the current input does
|
387
|
+
not fit any of the possible paths.
|
388
|
+
|
389
|
+
In lexers, this error is often raised by the main +tokens!+ rule, which must
|
390
|
+
choose between all possible token rules. If raised by +tokens+, it means the
|
391
|
+
current input does not appear to be part of any token specification.
|
392
|
+
|
393
|
+
=end
|
394
|
+
|
395
|
+
class NoViableAlternative < RecognitionError
|
396
|
+
attr_accessor :grammar_decision_description, :decision_number, :state_number
|
397
|
+
def initialize(grammar_decision_description, decision_number, state_number, input)
|
398
|
+
@grammar_decision_description = grammar_decision_description
|
399
|
+
@decision_number = decision_number
|
400
|
+
@state_number = state_number
|
401
|
+
super(input)
|
402
|
+
end
|
403
|
+
|
404
|
+
def message
|
405
|
+
'%s: %p != [%p]' %
|
406
|
+
[self.class, unexpected_type, @grammar_decision_description]
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
=begin rdoc ANTLR3::Error::EarlyExit
|
411
|
+
|
412
|
+
error:: EarlyExit
|
413
|
+
used by:: all recognizers
|
414
|
+
occurs when::
|
415
|
+
The recognizer is in a <tt>(..)+</tt> subrule, meaning the recognizer must
|
416
|
+
match the body of the subrule one or more times. If it fails to match at least
|
417
|
+
one occurence of the subrule, the recognizer will raise an EarlyExit
|
418
|
+
exception.
|
419
|
+
|
420
|
+
== Example
|
421
|
+
|
422
|
+
consider a grammar like:
|
423
|
+
lexer grammar EarlyExitDemo;
|
424
|
+
...
|
425
|
+
ID: 'a'..'z' ('0'..'9')+;
|
426
|
+
|
427
|
+
now in ruby
|
428
|
+
|
429
|
+
require 'EarlyExitDemo'
|
430
|
+
|
431
|
+
input = ANTLR3::StringStream.new( "ab" )
|
432
|
+
lexer = EarlyExitDemo::Lexer.new( input )
|
433
|
+
lexer.next_token
|
434
|
+
# -> raises EarlyExit: line 1:1 required (...)+ loop did not match
|
435
|
+
# anything at character "b"
|
436
|
+
|
437
|
+
=end
|
438
|
+
|
439
|
+
class EarlyExit < RecognitionError
|
440
|
+
attr_accessor :decision_number
|
441
|
+
def initialize(decision_number, input)
|
442
|
+
@decision_number = decision_number
|
443
|
+
super(input)
|
444
|
+
end
|
445
|
+
def message
|
446
|
+
"The recognizer did not match anything for a (..)+ loop."
|
447
|
+
end
|
448
|
+
end
|
449
|
+
|
450
|
+
=begin rdoc ANTLR3::Error::FailedPredicate
|
451
|
+
|
452
|
+
error:: FailedPredicate
|
453
|
+
used by:: all recognizers
|
454
|
+
occurs when::
|
455
|
+
A recognizer is in a rule with a predicate action element, and the predicating
|
456
|
+
action code evaluated to a +false+ value.
|
457
|
+
|
458
|
+
=end
|
459
|
+
|
460
|
+
class FailedPredicate < RecognitionError
|
461
|
+
attr_accessor :input, :rule_name, :predicate_text
|
462
|
+
def initialize(input, rule_name, predicate_text)
|
463
|
+
@rule_name = rule_name
|
464
|
+
@predicate_text = predicate_text
|
465
|
+
super(input)
|
466
|
+
end
|
467
|
+
|
468
|
+
def message
|
469
|
+
'%s(%s, { %s }?)' % [self.class.name, @rule_name, @predicate_text]
|
470
|
+
end
|
471
|
+
end
|
472
|
+
|
473
|
+
=begin rdoc ANTLR3::Error::MismatchedTreeNode
|
474
|
+
|
475
|
+
error:: MismatchedTreeNode
|
476
|
+
used by:: tree parsers
|
477
|
+
occurs when::
|
478
|
+
A tree parser expects to match a tree node containing a specific type of
|
479
|
+
token, but the current tree node's token type does not match. It's essentially
|
480
|
+
the same as MismatchedToken, but used specifically for tree nodes.
|
481
|
+
|
482
|
+
=end
|
483
|
+
|
484
|
+
class MismatchedTreeNode < RecognitionError
|
485
|
+
attr_accessor :expecting, :input
|
486
|
+
def initialize(expecting, input)
|
487
|
+
@expecting = expecting
|
488
|
+
super(input)
|
489
|
+
end
|
490
|
+
|
491
|
+
def message
|
492
|
+
'%s: %p != %p' %
|
493
|
+
[self.class, unexpected_type, @expecting]
|
494
|
+
end
|
495
|
+
end
|
496
|
+
|
497
|
+
=begin rdoc ANTLR3::Error::RewriteCardinalityError
|
498
|
+
|
499
|
+
error:: RewriteCardinalityError
|
500
|
+
used by:: tree-rewriting parsers and tree parsers
|
501
|
+
occurs when::
|
502
|
+
There is an inconsistency between the number of appearances of some symbol
|
503
|
+
on the left side of a rewrite rule and the number of the same symbol
|
504
|
+
seen on the right side of a rewrite rule
|
505
|
+
|
506
|
+
=end
|
507
|
+
|
508
|
+
class RewriteCardinalityError < StandardError
|
509
|
+
attr_accessor :element_description
|
510
|
+
def initialize(element_description)
|
511
|
+
@element_description = element_description
|
512
|
+
super(message)
|
513
|
+
end
|
514
|
+
|
515
|
+
def message
|
516
|
+
"%s: %s" % [self.class, @element_description]
|
517
|
+
end
|
518
|
+
end
|
519
|
+
|
520
|
+
=begin rdoc ANTLR3::Error::RewriteEarlyExit
|
521
|
+
|
522
|
+
error:: RewriteEarlyExit
|
523
|
+
used by:: tree-rewriting parsers and tree parsers
|
524
|
+
occurs when::
|
525
|
+
A tree-rewrite rule requires one or more occurence of a symbol, but none
|
526
|
+
have been seen.
|
527
|
+
|
528
|
+
=end
|
529
|
+
|
530
|
+
class RewriteEarlyExit < RewriteCardinalityError
|
531
|
+
attr_accessor :element_description
|
532
|
+
def initialize(element_description = nil)
|
533
|
+
super(element_description)
|
534
|
+
end
|
535
|
+
end
|
536
|
+
|
537
|
+
=begin rdoc ANTLR3::Error::RewriteEmptyStream
|
538
|
+
|
539
|
+
error:: RewriteEmptyStream
|
540
|
+
used by:: tree-rewriting parsers and tree parsers
|
541
|
+
|
542
|
+
=end
|
543
|
+
|
544
|
+
class RewriteEmptyStream < RewriteCardinalityError; end
|
545
|
+
|
546
|
+
=begin rdoc ANTLR3::Error::TreeInconsistency
|
547
|
+
|
548
|
+
error:: TreeInconsistency
|
549
|
+
used by:: classes that deal with tree structures
|
550
|
+
occurs when::
|
551
|
+
A tree node's data is inconsistent with the overall structure to which it
|
552
|
+
belongs.
|
553
|
+
|
554
|
+
situations that result in tree inconsistencies:
|
555
|
+
|
556
|
+
1. A node has a child node with a +@parent+ attribute different than the node.
|
557
|
+
2. A node has a child at index +n+, but the child's +@child_index+ value is not
|
558
|
+
+n+
|
559
|
+
3. An adaptor encountered a situation where multiple tree nodes have been
|
560
|
+
simultaneously requested as a new tree root.
|
561
|
+
|
562
|
+
=end
|
563
|
+
|
564
|
+
class TreeInconsistency < StandardError
|
565
|
+
def self.failed_index_check!(expected, real)
|
566
|
+
new(
|
567
|
+
"%s: child indexes don't match -> expected %d found %d" %
|
568
|
+
[self, expected, real]
|
569
|
+
)
|
570
|
+
end
|
571
|
+
|
572
|
+
def self.failed_parent_check!(expected, real)
|
573
|
+
new(
|
574
|
+
"%s: parents don't match; expected %p found %p" %
|
575
|
+
[self, expected, real]
|
576
|
+
)
|
577
|
+
end
|
578
|
+
|
579
|
+
def self.multiple_roots!
|
580
|
+
new "%s: attempted to change more than one node to root" % self
|
581
|
+
end
|
582
|
+
end
|
583
|
+
|
584
|
+
module_function
|
585
|
+
|
586
|
+
def MismatchedToken(expecting, input = @input)
|
587
|
+
MismatchedToken.new(expecting, input)
|
588
|
+
end
|
589
|
+
|
590
|
+
def UnwantedToken(expecting, input = @input)
|
591
|
+
UnwantedToken.new(expecting, input)
|
592
|
+
end
|
593
|
+
|
594
|
+
def MissingToken(expecting, inserted, input = @input)
|
595
|
+
MissingToken.new(expecting, input, inserted)
|
596
|
+
end
|
597
|
+
|
598
|
+
def MismatchedRange(min, max, input = @input)
|
599
|
+
MismatchedRange.new(min, max, input)
|
600
|
+
end
|
601
|
+
|
602
|
+
def MismatchedSet(expecting, input = @input)
|
603
|
+
MismatchedSet.new(expecting, input)
|
604
|
+
end
|
605
|
+
|
606
|
+
def MismatchedNotSet(expecting, input = @input)
|
607
|
+
MismatchedNotSet.new(expecting, input)
|
608
|
+
end
|
609
|
+
|
610
|
+
def NoViableAlternative(description, decision_number, state_number, input = @input)
|
611
|
+
NoViableAlternative.new(description, decision_number, state_number, input)
|
612
|
+
end
|
613
|
+
|
614
|
+
def EarlyExit(decision_number, input = @input)
|
615
|
+
EarlyExit.new(decision_number, input)
|
616
|
+
end
|
617
|
+
|
618
|
+
def FailedPredicate(rule_name, predicate_text, input = @input)
|
619
|
+
FailedPredicate.new(input, rule_name, predicate_text)
|
620
|
+
end
|
621
|
+
|
622
|
+
def MismatchedTreeNode(expecting, input = @input)
|
623
|
+
MismatchedTreeNode.new(expecting, input)
|
624
|
+
end
|
625
|
+
|
626
|
+
def RewriteCardinalityError(element_description)
|
627
|
+
RewriteCardinalityError.new(element_description)
|
628
|
+
end
|
629
|
+
|
630
|
+
def RewriteEarlyExit(element_description = nil)
|
631
|
+
RewriteEarlyExit.new(element_description)
|
632
|
+
end
|
633
|
+
|
634
|
+
def RewriteEmptyStream(element_description)
|
635
|
+
RewriteEmptyStream.new(element_description)
|
636
|
+
end
|
637
|
+
|
638
|
+
end
|
639
|
+
|
640
|
+
include Error
|
641
|
+
|
642
|
+
=begin rdoc ANTLR3::Bug
|
643
|
+
|
644
|
+
|
645
|
+
|
646
|
+
=end
|
647
|
+
|
648
|
+
class Bug < StandardError
|
649
|
+
def initialize(message = nil, *args)
|
650
|
+
message = "something occurred that should not occur within unmodified, " <<
|
651
|
+
"ANTLR-generated source code: #{message}"
|
652
|
+
super(message, *args)
|
653
|
+
end
|
654
|
+
end
|
655
|
+
|
656
|
+
end
|
657
|
+
|