antlr4-runtime 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/antlr4-runtime.gemspec +30 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/rumourhash/Makefile +264 -0
- data/ext/rumourhash/extconf.rb +3 -0
- data/ext/rumourhash/rumourhash.c +59 -0
- data/lib/antlr4/runtime.rb +37 -0
- data/lib/antlr4/runtime/abstract_parse_tree_visitor.rb +43 -0
- data/lib/antlr4/runtime/abstract_predicate_transition.rb +11 -0
- data/lib/antlr4/runtime/action_transition.rb +29 -0
- data/lib/antlr4/runtime/ambiguity_info.rb +10 -0
- data/lib/antlr4/runtime/antlr_error_listener.rb +15 -0
- data/lib/antlr4/runtime/antlr_error_strategy.rb +24 -0
- data/lib/antlr4/runtime/antlr_file_stream.rb +17 -0
- data/lib/antlr4/runtime/antlr_input_stream.rb +6 -0
- data/lib/antlr4/runtime/array_2d_hash_set.rb +471 -0
- data/lib/antlr4/runtime/array_prediction_context.rb +76 -0
- data/lib/antlr4/runtime/atn.rb +100 -0
- data/lib/antlr4/runtime/atn_config.rb +140 -0
- data/lib/antlr4/runtime/atn_config_set.rb +150 -0
- data/lib/antlr4/runtime/atn_deserialization_options.rb +48 -0
- data/lib/antlr4/runtime/atn_deserializer.rb +737 -0
- data/lib/antlr4/runtime/atn_simulator.rb +69 -0
- data/lib/antlr4/runtime/atn_state.rb +118 -0
- data/lib/antlr4/runtime/atn_type.rb +8 -0
- data/lib/antlr4/runtime/atom_transition.rb +27 -0
- data/lib/antlr4/runtime/bail_error_strategy.rb +31 -0
- data/lib/antlr4/runtime/base_error_listener.rb +18 -0
- data/lib/antlr4/runtime/basic_block_start_state.rb +12 -0
- data/lib/antlr4/runtime/basic_state.rb +11 -0
- data/lib/antlr4/runtime/bit_set.rb +54 -0
- data/lib/antlr4/runtime/block_end_state.rb +15 -0
- data/lib/antlr4/runtime/block_start_state.rb +12 -0
- data/lib/antlr4/runtime/buffered_token_stream.rb +335 -0
- data/lib/antlr4/runtime/char_stream.rb +6 -0
- data/lib/antlr4/runtime/char_streams.rb +12 -0
- data/lib/antlr4/runtime/chunk.rb +4 -0
- data/lib/antlr4/runtime/code_point_char_stream.rb +83 -0
- data/lib/antlr4/runtime/common_token.rb +125 -0
- data/lib/antlr4/runtime/common_token_factory.rb +30 -0
- data/lib/antlr4/runtime/common_token_stream.rb +63 -0
- data/lib/antlr4/runtime/console_error_listener.rb +12 -0
- data/lib/antlr4/runtime/context_sensitivity_info.rb +7 -0
- data/lib/antlr4/runtime/decision_event_info.rb +19 -0
- data/lib/antlr4/runtime/decision_info.rb +36 -0
- data/lib/antlr4/runtime/decision_state.rb +15 -0
- data/lib/antlr4/runtime/default_error_strategy.rb +314 -0
- data/lib/antlr4/runtime/dfa.rb +97 -0
- data/lib/antlr4/runtime/dfa_serializer.rb +62 -0
- data/lib/antlr4/runtime/dfa_state.rb +109 -0
- data/lib/antlr4/runtime/diagnostic_error_listener.rb +58 -0
- data/lib/antlr4/runtime/double_key_map.rb +49 -0
- data/lib/antlr4/runtime/empty_prediction_context.rb +35 -0
- data/lib/antlr4/runtime/epsilon_transition.rb +27 -0
- data/lib/antlr4/runtime/equality_comparator.rb +4 -0
- data/lib/antlr4/runtime/error_info.rb +7 -0
- data/lib/antlr4/runtime/error_node.rb +5 -0
- data/lib/antlr4/runtime/error_node_impl.rb +12 -0
- data/lib/antlr4/runtime/failed_predicate_exception.rb +33 -0
- data/lib/antlr4/runtime/flexible_hash_map.rb +232 -0
- data/lib/antlr4/runtime/input_mismatch_exception.rb +20 -0
- data/lib/antlr4/runtime/int_stream.rb +31 -0
- data/lib/antlr4/runtime/integer.rb +14 -0
- data/lib/antlr4/runtime/interval.rb +111 -0
- data/lib/antlr4/runtime/interval_set.rb +540 -0
- data/lib/antlr4/runtime/lexer.rb +257 -0
- data/lib/antlr4/runtime/lexer_action.rb +12 -0
- data/lib/antlr4/runtime/lexer_action_executor.rb +75 -0
- data/lib/antlr4/runtime/lexer_action_type.rb +12 -0
- data/lib/antlr4/runtime/lexer_atn_config.rb +50 -0
- data/lib/antlr4/runtime/lexer_atn_simulator.rb +522 -0
- data/lib/antlr4/runtime/lexer_channel_action.rb +51 -0
- data/lib/antlr4/runtime/lexer_custom_action.rb +49 -0
- data/lib/antlr4/runtime/lexer_dfa_serializer.rb +12 -0
- data/lib/antlr4/runtime/lexer_indexed_custom_action.rb +49 -0
- data/lib/antlr4/runtime/lexer_mode_action.rb +51 -0
- data/lib/antlr4/runtime/lexer_more_action.rb +41 -0
- data/lib/antlr4/runtime/lexer_no_viable_alt_exception.rb +4 -0
- data/lib/antlr4/runtime/lexer_pop_mode_action.rb +41 -0
- data/lib/antlr4/runtime/lexer_push_mode_action.rb +51 -0
- data/lib/antlr4/runtime/lexer_skip_action.rb +43 -0
- data/lib/antlr4/runtime/lexer_type_action.rb +51 -0
- data/lib/antlr4/runtime/ll1_analyzer.rb +133 -0
- data/lib/antlr4/runtime/lookahead_event_info.rb +10 -0
- data/lib/antlr4/runtime/loop_end_state.rb +15 -0
- data/lib/antlr4/runtime/murmur_hash.rb +99 -0
- data/lib/antlr4/runtime/no_viable_alt_exception.rb +7 -0
- data/lib/antlr4/runtime/not_set_transition.rb +20 -0
- data/lib/antlr4/runtime/object_equality_comparator.rb +18 -0
- data/lib/antlr4/runtime/ordered_atn_config_set.rb +15 -0
- data/lib/antlr4/runtime/parse_cancellation_exception.rb +5 -0
- data/lib/antlr4/runtime/parse_tree.rb +7 -0
- data/lib/antlr4/runtime/parse_tree_listener.rb +4 -0
- data/lib/antlr4/runtime/parse_tree_visitor.rb +4 -0
- data/lib/antlr4/runtime/parser.rb +522 -0
- data/lib/antlr4/runtime/parser_atn_simulator.rb +1171 -0
- data/lib/antlr4/runtime/parser_rule_context.rb +186 -0
- data/lib/antlr4/runtime/plus_block_start_state.rb +11 -0
- data/lib/antlr4/runtime/plus_loopback_state.rb +12 -0
- data/lib/antlr4/runtime/precedence_predicate_transition.rb +31 -0
- data/lib/antlr4/runtime/predicate.rb +6 -0
- data/lib/antlr4/runtime/predicate_eval_info.rb +16 -0
- data/lib/antlr4/runtime/predicate_transition.rb +35 -0
- data/lib/antlr4/runtime/prediction_context.rb +103 -0
- data/lib/antlr4/runtime/prediction_context_cache.rb +28 -0
- data/lib/antlr4/runtime/prediction_context_utils.rb +407 -0
- data/lib/antlr4/runtime/prediction_mode.rb +213 -0
- data/lib/antlr4/runtime/profiling_atn_simulator.rb +149 -0
- data/lib/antlr4/runtime/proxy_error_listener.rb +33 -0
- data/lib/antlr4/runtime/range_transition.rb +29 -0
- data/lib/antlr4/runtime/recognition_exception.rb +17 -0
- data/lib/antlr4/runtime/recognizer.rb +136 -0
- data/lib/antlr4/runtime/rule_context.rb +131 -0
- data/lib/antlr4/runtime/rule_context_with_alt_num.rb +11 -0
- data/lib/antlr4/runtime/rule_node.rb +8 -0
- data/lib/antlr4/runtime/rule_start_state.rb +17 -0
- data/lib/antlr4/runtime/rule_stop_state.rb +12 -0
- data/lib/antlr4/runtime/rule_tag_token.rb +64 -0
- data/lib/antlr4/runtime/rule_transition.rb +29 -0
- data/lib/antlr4/runtime/semantic_context.rb +313 -0
- data/lib/antlr4/runtime/set_transition.rb +29 -0
- data/lib/antlr4/runtime/singleton_prediction_context.rb +56 -0
- data/lib/antlr4/runtime/star_block_start_state.rb +12 -0
- data/lib/antlr4/runtime/star_loop_entry_state.rb +17 -0
- data/lib/antlr4/runtime/star_loopback_state.rb +16 -0
- data/lib/antlr4/runtime/syntax_tree.rb +6 -0
- data/lib/antlr4/runtime/tag_chunk.rb +22 -0
- data/lib/antlr4/runtime/terminal_node.rb +5 -0
- data/lib/antlr4/runtime/terminal_node_impl.rb +50 -0
- data/lib/antlr4/runtime/text_chunk.rb +16 -0
- data/lib/antlr4/runtime/token.rb +13 -0
- data/lib/antlr4/runtime/token_stream.rb +13 -0
- data/lib/antlr4/runtime/token_tag_token.rb +22 -0
- data/lib/antlr4/runtime/tokens_start_state.rb +14 -0
- data/lib/antlr4/runtime/transition.rb +51 -0
- data/lib/antlr4/runtime/tree.rb +4 -0
- data/lib/antlr4/runtime/trees.rb +195 -0
- data/lib/antlr4/runtime/triple.rb +40 -0
- data/lib/antlr4/runtime/utils.rb +117 -0
- data/lib/antlr4/runtime/uuid.rb +46 -0
- data/lib/antlr4/runtime/version.rb +5 -0
- data/lib/antlr4/runtime/vocabulary.rb +12 -0
- data/lib/antlr4/runtime/vocabulary_impl.rb +82 -0
- data/lib/antlr4/runtime/wildcard_transition.rb +20 -0
- data/lib/antlr4/runtime/writable_token.rb +7 -0
- metadata +243 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require 'antlr4/runtime/char_stream'
|
|
2
|
+
|
|
3
|
+
module Antlr4::Runtime
|
|
4
|
+
|
|
5
|
+
class CodePointCharStream < CharStream
|
|
6
|
+
def initialize(position, remaining, name, byte_array)
|
|
7
|
+
@size = remaining
|
|
8
|
+
@name = name
|
|
9
|
+
@position = position
|
|
10
|
+
@byte_array = byte_array
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def text(interval)
|
|
14
|
+
start_idx = [interval.a, @size].min
|
|
15
|
+
len = [interval.b - interval.a + 1, @size - start_idx].min
|
|
16
|
+
|
|
17
|
+
# We know the maximum code point in byte_array is U+00FF,
|
|
18
|
+
# so we can treat this as if it were ISO-8859-1, aka Latin-1,
|
|
19
|
+
# which shares the same code points up to 0xFF.
|
|
20
|
+
chars = @byte_array.slice(start_idx, len)
|
|
21
|
+
result = ''
|
|
22
|
+
chars.each do |c|
|
|
23
|
+
result << c
|
|
24
|
+
end
|
|
25
|
+
result
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def la(i)
|
|
29
|
+
case Integer.signum(i)
|
|
30
|
+
when -1
|
|
31
|
+
offset = @position + i
|
|
32
|
+
return IntStream::EOF if offset < 0
|
|
33
|
+
|
|
34
|
+
return @byte_array[offset] & 0xFF
|
|
35
|
+
when 0
|
|
36
|
+
# Undefined
|
|
37
|
+
return 0
|
|
38
|
+
when 1
|
|
39
|
+
offset = @position + i - 1
|
|
40
|
+
return IntStream::EOF if offset >= @size
|
|
41
|
+
|
|
42
|
+
return @byte_array[offset] & 0xFF
|
|
43
|
+
else
|
|
44
|
+
# type code here
|
|
45
|
+
end
|
|
46
|
+
raise UnsupportedOperationException, 'Not reached'
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def internal_storage
|
|
50
|
+
@byte_array
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def consume
|
|
54
|
+
raise IllegalStateException, 'cannot consume EOF' if (@size - @position).zero?
|
|
55
|
+
|
|
56
|
+
@position += 1
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def index
|
|
60
|
+
@position
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
attr_reader :size
|
|
64
|
+
|
|
65
|
+
def mark
|
|
66
|
+
-1
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def release(marker)
|
|
70
|
+
;
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def seek(index)
|
|
74
|
+
@position = index
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def source_name
|
|
78
|
+
return UNKNOWN_SOURCE_NAME if @name.nil? || @name.empty?
|
|
79
|
+
|
|
80
|
+
@name
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
|
|
3
|
+
class CommonToken
|
|
4
|
+
|
|
5
|
+
class << self
|
|
6
|
+
@@EMPTY_SOURCE = OpenStruct.new
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
attr_accessor :type
|
|
10
|
+
attr_accessor :line
|
|
11
|
+
attr_accessor :char_position_in_line
|
|
12
|
+
attr_accessor :channel
|
|
13
|
+
attr_accessor :source
|
|
14
|
+
attr_accessor :index
|
|
15
|
+
attr_accessor :start
|
|
16
|
+
attr_accessor :stop
|
|
17
|
+
attr_accessor :_text
|
|
18
|
+
|
|
19
|
+
def initialize(type = nil)
|
|
20
|
+
@char_position_in_line = -1
|
|
21
|
+
@channel = Token::DEFAULT_CHANNEL
|
|
22
|
+
@index = -1
|
|
23
|
+
@type = type
|
|
24
|
+
@source = @@EMPTY_SOURCE
|
|
25
|
+
@_text = nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.create1(source, type, channel, start, stop)
|
|
29
|
+
result = CommonToken.new(type)
|
|
30
|
+
result.source = source
|
|
31
|
+
result.channel = channel
|
|
32
|
+
result.start = start
|
|
33
|
+
result.stop = stop
|
|
34
|
+
unless source.a.nil?
|
|
35
|
+
result.line = source.a.line
|
|
36
|
+
result.char_position_in_line = source.a.char_position_in_line
|
|
37
|
+
end
|
|
38
|
+
result
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.create2(type, text)
|
|
42
|
+
result = CommonToken.new(type)
|
|
43
|
+
result._text = text
|
|
44
|
+
result
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def create3(old_token)
|
|
48
|
+
result = CommonToken.new(old_token.type)
|
|
49
|
+
|
|
50
|
+
result.line = old_token.line
|
|
51
|
+
result.index = old_token.token_index
|
|
52
|
+
result.char_position_in_line = old_token.char_position_in_line
|
|
53
|
+
result.channel = old_token.channel
|
|
54
|
+
result.start = old_token.start_index
|
|
55
|
+
result.stop = old_token.stop_index
|
|
56
|
+
|
|
57
|
+
if old_token.is_a? CommonToken
|
|
58
|
+
result._text = old_token.text
|
|
59
|
+
result.source = old_token.source
|
|
60
|
+
else
|
|
61
|
+
result._text = old_token.text
|
|
62
|
+
result.source = OpenStruct.new
|
|
63
|
+
result.source.a = old_token.token_source
|
|
64
|
+
result.source.b = old_token.input_stream
|
|
65
|
+
end
|
|
66
|
+
result
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def input_stream
|
|
70
|
+
@source.b
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def text
|
|
74
|
+
return @_text unless @_text.nil?
|
|
75
|
+
|
|
76
|
+
input = input_stream
|
|
77
|
+
return nil if input.nil?
|
|
78
|
+
|
|
79
|
+
n = input.size
|
|
80
|
+
if @start < n && @stop < n
|
|
81
|
+
input.text(Interval.of(@start, @stop))
|
|
82
|
+
else
|
|
83
|
+
'<EOF>'
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def to_s_recog(r = nil)
|
|
88
|
+
channel_str = ''
|
|
89
|
+
channel_str = ',channel=' + @channel.to_s if @channel > 0
|
|
90
|
+
txt = @_text
|
|
91
|
+
if !txt.nil?
|
|
92
|
+
txt = txt.sub("\n", '\\n')
|
|
93
|
+
txt = txt.sub("\r", '\\r')
|
|
94
|
+
txt = txt.sub("\t", '\\t')
|
|
95
|
+
else
|
|
96
|
+
txt = '<no text>'
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
type_string = type.to_s
|
|
100
|
+
type_string = r.get_vocabulary.display_name(@type) unless r.nil?
|
|
101
|
+
'[@' << token_index.to_s << ',' << @start.to_s << ':' << @stop.to_s << "='" << txt << "',<" << type_string << '>' << channel_str << ',' << @line.to_s << ':' << char_position_in_line.to_s << ']'
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def to_s
|
|
105
|
+
'[@ ' << @start.to_s << ':' << @stop.to_s << ',' << @line.to_s << ':' << ']'
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def to_s_old
|
|
109
|
+
channel_str = ''
|
|
110
|
+
channel_str = ',channel=' + @channel.to_s if @channel > 0
|
|
111
|
+
txt = @_text
|
|
112
|
+
if !txt.nil?
|
|
113
|
+
txt = txt.sub("\n", '\\n')
|
|
114
|
+
txt = txt.sub("\r", '\\r')
|
|
115
|
+
txt = txt.sub("\t", '\\t')
|
|
116
|
+
else
|
|
117
|
+
txt = '<no text>'
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
type_string = type.to_s
|
|
121
|
+
|
|
122
|
+
'[@' << token_index.to_s << ',' << @start.to_s << ':' << @stop.to_s << "='" << txt << "',<" << type_string << '>' << channel_str << ',' << @line.to_s << ':' << char_position_in_line.to_s << ']'
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'antlr4/runtime/common_token'
|
|
2
|
+
|
|
3
|
+
module Antlr4::Runtime
|
|
4
|
+
|
|
5
|
+
class CommonTokenFactory
|
|
6
|
+
include Singleton
|
|
7
|
+
|
|
8
|
+
def initialize(copy_text = false)
|
|
9
|
+
@copy_text = false
|
|
10
|
+
@copy_text = copy_text
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def create(source, type, text, channel, start, stop, line, char_position_in_line)
|
|
14
|
+
t = CommonToken.create1(source, type, channel, start, stop)
|
|
15
|
+
t.line = line
|
|
16
|
+
t.char_position_in_line = char_position_in_line
|
|
17
|
+
if !text.nil?
|
|
18
|
+
t._text = text
|
|
19
|
+
elsif @copy_text && !source.b.nil?
|
|
20
|
+
t.set_text(source.b.text(Interval.of(start, stop)))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
t
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def create_simple(type, text)
|
|
27
|
+
CommonToken.create2(type, text)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require 'antlr4/runtime/buffered_token_stream'
|
|
2
|
+
|
|
3
|
+
module Antlr4::Runtime
|
|
4
|
+
|
|
5
|
+
class CommonTokenStream < BufferedTokenStream
|
|
6
|
+
def initialize(token_source, channel = nil)
|
|
7
|
+
super(token_source)
|
|
8
|
+
@channel = Token::DEFAULT_CHANNEL
|
|
9
|
+
@channel = channel unless channel.nil?
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def adjust_seek_index(i)
|
|
13
|
+
next_token_on_channel(i, @channel)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def lb(k)
|
|
17
|
+
return nil if k.zero? || (@ptr - k) < 0
|
|
18
|
+
|
|
19
|
+
i = @ptr
|
|
20
|
+
n = 1
|
|
21
|
+
# find k good tokens looking backwards
|
|
22
|
+
while n <= k && i > 0
|
|
23
|
+
# skip off-channel tokens
|
|
24
|
+
i = previous_token_on_channel(i - 1, @channel)
|
|
25
|
+
n += 1
|
|
26
|
+
end
|
|
27
|
+
return nil if i < 0
|
|
28
|
+
|
|
29
|
+
@tokens[i]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def lt(k)
|
|
33
|
+
lazy_init
|
|
34
|
+
return nil if k == 0
|
|
35
|
+
return lb(-k) if k < 0
|
|
36
|
+
|
|
37
|
+
i = @ptr
|
|
38
|
+
n = 1 # we know tokens[p] is a good one
|
|
39
|
+
# find k good tokens
|
|
40
|
+
while n < k
|
|
41
|
+
# skip off-channel tokens, but make sure to not look past EOF
|
|
42
|
+
i = next_token_on_channel(i + 1, @channel) if sync(i + 1)
|
|
43
|
+
n += 1
|
|
44
|
+
end
|
|
45
|
+
# if ( i>range ) range = i
|
|
46
|
+
@tokens[i]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def number_of_on_channel_tokens
|
|
50
|
+
n = 0
|
|
51
|
+
fill
|
|
52
|
+
i = 0
|
|
53
|
+
while i < @tokens.size
|
|
54
|
+
t = @tokens.get(i)
|
|
55
|
+
n += 1 if t.channel == @channel
|
|
56
|
+
break if t.type == Token::EOF
|
|
57
|
+
|
|
58
|
+
i += 1
|
|
59
|
+
end
|
|
60
|
+
n
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require 'antlr4/runtime/base_error_listener'
|
|
2
|
+
|
|
3
|
+
module Antlr4::Runtime
|
|
4
|
+
|
|
5
|
+
class ConsoleErrorListener < BaseErrorListener
|
|
6
|
+
include Singleton
|
|
7
|
+
|
|
8
|
+
def syntaxError(_recognizer, _offending_symbol, line, char_position_in_line, msg, _e)
|
|
9
|
+
STDERR.puts 'line ' << line.to_s << ':' << char_position_in_line.to_s << ' ' << msg.to_s << ''
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
class DecisionEventInfo
|
|
3
|
+
attr_accessor :decision
|
|
4
|
+
attr_accessor :configs
|
|
5
|
+
attr_accessor :input
|
|
6
|
+
attr_accessor :start_index
|
|
7
|
+
attr_accessor :stop_index
|
|
8
|
+
attr_accessor :full_ctx
|
|
9
|
+
|
|
10
|
+
def initialize(decision, configs, input, start_index, stop_index, full_ctx)
|
|
11
|
+
@decision = decision
|
|
12
|
+
@full_ctx = full_ctx
|
|
13
|
+
@stop_index = stop_index
|
|
14
|
+
@input = input
|
|
15
|
+
@start_index = start_index
|
|
16
|
+
@configs = configs
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
class DecisionInfo
|
|
3
|
+
attr_accessor :decision
|
|
4
|
+
attr_accessor :invocations
|
|
5
|
+
attr_accessor :timeInPrediction
|
|
6
|
+
attr_accessor :SLL_TotalLook
|
|
7
|
+
attr_accessor :SLL_MinLook
|
|
8
|
+
attr_accessor :SLL_MaxLook
|
|
9
|
+
attr_accessor :SLL_MaxLookEvent
|
|
10
|
+
attr_accessor :LL_TotalLook
|
|
11
|
+
attr_accessor :LL_MinLook
|
|
12
|
+
attr_accessor :LL_MaxLook
|
|
13
|
+
attr_accessor :LL_MaxLookEvent
|
|
14
|
+
attr_accessor :context_sensitivities
|
|
15
|
+
attr_accessor :errors
|
|
16
|
+
attr_accessor :ambiguities
|
|
17
|
+
attr_accessor :predicate_evals
|
|
18
|
+
attr_accessor :SLL_ATNTransitions
|
|
19
|
+
attr_accessor :SLL_DFATransitions
|
|
20
|
+
attr_accessor :LL_Fallback
|
|
21
|
+
attr_accessor :LL_ATNTransitions
|
|
22
|
+
attr_accessor :LL_DFATransitions
|
|
23
|
+
|
|
24
|
+
def initialize(decision)
|
|
25
|
+
@context_sensitivities = []
|
|
26
|
+
@errors = []
|
|
27
|
+
@ambiguities = []
|
|
28
|
+
@predicate_evals = []
|
|
29
|
+
@decision = decision
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def to_s
|
|
33
|
+
'' + 'decision=' + @decision + ', contextSensitivities=' + @context_sensitivities.size + ', errors=' + @errors.size + ', ambiguities=' + @ambiguities.size + ', SLL_lookahead=' + SLL_TotalLook + ', SLL_ATNTransitions=' + SLL_ATNTransitions + ', SLL_DFATransitions=' + SLL_DFATransitions + ', LL_Fallback=' + LL_Fallback + ', LL_lookahead=' + LL_TotalLook + ', LL_ATNTransitions=' + LL_ATNTransitions + 'end'
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
require 'antlr4/runtime/antlr_error_strategy'
|
|
2
|
+
require 'antlr4/runtime/input_mismatch_exception'
|
|
3
|
+
require 'antlr4/runtime/no_viable_alt_exception'
|
|
4
|
+
|
|
5
|
+
module Antlr4::Runtime
|
|
6
|
+
|
|
7
|
+
class DefaultErrorStrategy < ANTLRErrorStrategy
|
|
8
|
+
def initialize
|
|
9
|
+
@error_recovery_mode = false
|
|
10
|
+
@last_error_index = -1
|
|
11
|
+
@last_error_states = nil
|
|
12
|
+
@next_tokens_context = nil
|
|
13
|
+
@next_tokens_state = nil
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def reset(recognizer)
|
|
17
|
+
end_error_condition(recognizer)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def begin_error_condition(_recognizer)
|
|
21
|
+
@error_recovery_mode = true
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def error_recovery_mode?(_recognizer)
|
|
25
|
+
@error_recovery_mode
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def end_error_condition(_recognizer)
|
|
29
|
+
@error_recovery_mode = false
|
|
30
|
+
@last_error_states = nil
|
|
31
|
+
@last_error_index = -1
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def report_match(recognizer)
|
|
35
|
+
end_error_condition(recognizer)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def report_error(recognizer, e)
|
|
39
|
+
# if we've already reported an error and have not matched a token
|
|
40
|
+
# yet successfully, don't report any errors.
|
|
41
|
+
if error_recovery_mode?(recognizer)
|
|
42
|
+
# System.err.print("[SPURIOUS] ")
|
|
43
|
+
return # don't report spurious errors
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
begin_error_condition(recognizer)
|
|
47
|
+
if e.is_a? NoViableAltException
|
|
48
|
+
report_no_viable_alternative(recognizer, e)
|
|
49
|
+
elsif e.is_a? InputMismatchException
|
|
50
|
+
report_input_mismatch(recognizer, e)
|
|
51
|
+
elsif e.is_a? FailedPredicateException
|
|
52
|
+
report_failed_predicate(recognizer, e)
|
|
53
|
+
else
|
|
54
|
+
STDERR.puts 'unknown recognition error type: ' + e.getClass.getName
|
|
55
|
+
recognizer.notify_error_listeners(e.getOffendingToken, e.getMessage, e)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def recover(recognizer, _e)
|
|
60
|
+
if @last_error_index == recognizer._input.index && !@last_error_states.nil? && @last_error_states.include?(recognizer._state_number)
|
|
61
|
+
# uh oh, another error at same token index and previously-visited
|
|
62
|
+
# state in ATN must be a case where lt(1) is in the recovery
|
|
63
|
+
# token set so nothing got consumed. Consume a single token
|
|
64
|
+
# at least to prevent an infinite loop this is a failsafe.
|
|
65
|
+
# System.err.println("seen error condition before index="+
|
|
66
|
+
# lastErrorIndex+", states="+last_error_states)
|
|
67
|
+
# System.err.println("FAILSAFE consumes "+recognizer.getTokenNames()[recognizer.getInputStream().la(1)])
|
|
68
|
+
recognizer.consume
|
|
69
|
+
end
|
|
70
|
+
@last_error_index = recognizer._input.index
|
|
71
|
+
@last_error_states = IntervalSet.new if @last_error_states.nil?
|
|
72
|
+
@last_error_states.add(recognizer._state_number)
|
|
73
|
+
followSet = error_recovery_set(recognizer)
|
|
74
|
+
consume_until(recognizer, followSet)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def sync(recognizer)
|
|
78
|
+
s = recognizer._interp.atn.states[recognizer._state_number]
|
|
79
|
+
# System.err.println("sync @ "+s.stateNumber+"="+s.getClass().getSimpleName())
|
|
80
|
+
# If already recovering, don't try to sync
|
|
81
|
+
return if error_recovery_mode?(recognizer)
|
|
82
|
+
|
|
83
|
+
tokens = recognizer._input
|
|
84
|
+
la = tokens.la(1)
|
|
85
|
+
|
|
86
|
+
# try cheaper subset first might get lucky. seems to shave a wee bit off
|
|
87
|
+
next_tokens = recognizer.atn.next_tokens(s)
|
|
88
|
+
if next_tokens.contains(la)
|
|
89
|
+
# We are sure the token matches
|
|
90
|
+
@next_tokens_context = nil
|
|
91
|
+
@next_tokens_state = ATNState.invalid_state_number
|
|
92
|
+
return
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
if next_tokens.contains(Token::EPSILON)
|
|
96
|
+
if @next_tokens_context.nil?
|
|
97
|
+
# It's possible the next token won't match information tracked
|
|
98
|
+
# by sync is restricted for performance.
|
|
99
|
+
@next_tokens_context = recognizer._ctx
|
|
100
|
+
@next_tokens_state = recognizer._state_number
|
|
101
|
+
end
|
|
102
|
+
return
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
case s.state_type
|
|
106
|
+
when ATNState::BLOCK_START, ATNState::STAR_BLOCK_START, ATNState::PLUS_BLOCK_START, ATNState::STAR_LOOP_ENTRY
|
|
107
|
+
# report error and recover if possible
|
|
108
|
+
return unless single_token_deletion(recognizer).nil?
|
|
109
|
+
|
|
110
|
+
exc = InputMismatchException.create(recognizer)
|
|
111
|
+
raise exc
|
|
112
|
+
|
|
113
|
+
when ATNState::PLUS_LOOP_BACK, ATNState::STAR_LOOP_BACK
|
|
114
|
+
|
|
115
|
+
# System.err.println("at loop back: "+s.getClass().getSimpleName())
|
|
116
|
+
report_unwanted_token(recognizer)
|
|
117
|
+
expecting = recognizer.expected_tokens
|
|
118
|
+
what_follows_loop_iteration_or_rule = expecting.or(error_recovery_set(recognizer))
|
|
119
|
+
consume_until(recognizer, what_follows_loop_iteration_or_rule)
|
|
120
|
+
|
|
121
|
+
else # do nothing if we can't identify the exact kind of ATN state end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def report_no_viable_alternative(recognizer, e)
|
|
126
|
+
tokens = recognizer._input
|
|
127
|
+
input = if !tokens.nil?
|
|
128
|
+
if e.start_token.type == Token::EOF
|
|
129
|
+
'<EOF>'
|
|
130
|
+
else
|
|
131
|
+
tokens.text4(e.start_token, e.offending_token)
|
|
132
|
+
end
|
|
133
|
+
else
|
|
134
|
+
'<unknown input>'
|
|
135
|
+
end
|
|
136
|
+
msg = 'no viable alternative at input ' + escape_ws_and_quote(input)
|
|
137
|
+
recognizer.notify_error_listeners(e.offending_token, msg, e)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def report_input_mismatch(recognizer, e)
|
|
141
|
+
msg = 'mismatched input ' + token_error_display(e.offending_token) + ' expecting ' + e.expected_tokens.to_string_from_vocabulary(recognizer.get_vocabulary)
|
|
142
|
+
recognizer.notify_error_listeners(e.offending_token, msg, e)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def report_failed_predicate(recognizer, e)
|
|
146
|
+
rule_name = recognizer.rule_names[recognizer._ctx.rule_index]
|
|
147
|
+
msg = 'rule ' + rule_name + ' ' + e.getMessage
|
|
148
|
+
recognizer.notify_error_listeners(e.getOffendingToken, msg, e)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def report_unwanted_token(recognizer)
|
|
152
|
+
return if error_recovery_mode?(recognizer)
|
|
153
|
+
|
|
154
|
+
begin_error_condition(recognizer)
|
|
155
|
+
|
|
156
|
+
t = recognizer.current_token
|
|
157
|
+
token_name = token_error_display(t)
|
|
158
|
+
expecting = expected_tokens(recognizer)
|
|
159
|
+
msg = 'extraneous input ' + token_name + ' expecting ' + expecting.to_string_from_vocabulary(recognizer.get_vocabulary)
|
|
160
|
+
recognizer.notify_error_listeners(t, msg, nil)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def report_missing_token(recognizer)
|
|
164
|
+
return if error_recovery_mode?(recognizer)
|
|
165
|
+
|
|
166
|
+
begin_error_condition(recognizer)
|
|
167
|
+
|
|
168
|
+
t = recognizer.current_token
|
|
169
|
+
expecting = expected_tokens(recognizer)
|
|
170
|
+
msg = 'missing ' + expecting.to_string_from_vocabulary(recognizer.get_vocabulary) + ' at ' + token_error_display(t)
|
|
171
|
+
|
|
172
|
+
recognizer.notify_error_listeners(t, msg, nil)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def recover_in_line(recognizer)
|
|
176
|
+
# SINGLE TOKEN DELETION
|
|
177
|
+
matched_symbol = single_token_deletion(recognizer)
|
|
178
|
+
unless matched_symbol.nil?
|
|
179
|
+
# we have deleted the extra token.
|
|
180
|
+
# now, move past ttype token as if all were ok
|
|
181
|
+
recognizer.consume
|
|
182
|
+
return matched_symbol
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# SINGLE TOKEN INSERTION
|
|
186
|
+
return get_missing_symbol(recognizer) if single_token_insertion(recognizer)
|
|
187
|
+
|
|
188
|
+
# even that didn't work must throw the exception
|
|
189
|
+
exc = InputMismatchException.new
|
|
190
|
+
exc.recognizer = recognizer
|
|
191
|
+
if @next_tokens_context.nil?
|
|
192
|
+
raise exc
|
|
193
|
+
else
|
|
194
|
+
exc.offending_token = recognizer._input.lt(1)
|
|
195
|
+
exc.offending_state = @next_tokens_state
|
|
196
|
+
exc.context = @next_tokens_context
|
|
197
|
+
raise exc
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def single_token_insertion(recognizer)
|
|
202
|
+
current_symbol_type = recognizer._input.la(1)
|
|
203
|
+
# if current token is consistent with what could come after current
|
|
204
|
+
# ATN state, then we know we're missing a token error recovery
|
|
205
|
+
# is free to conjure up and insert the missing token
|
|
206
|
+
current_state = recognizer._interp.atn.states[recognizer._state_number]
|
|
207
|
+
next_t = current_state.transition(0).target
|
|
208
|
+
atn = recognizer._interp.atn
|
|
209
|
+
expecting_at_ll2 = atn.next_tokens_ctx(next_t, recognizer._ctx)
|
|
210
|
+
# System.out.println("lt(2) set="+expecting_at_ll2.to_s(recognizer.getTokenNames()))
|
|
211
|
+
if expecting_at_ll2.contains(current_symbol_type)
|
|
212
|
+
report_missing_token(recognizer)
|
|
213
|
+
return true
|
|
214
|
+
end
|
|
215
|
+
false
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def single_token_deletion(recognizer)
|
|
219
|
+
next_token_type = recognizer._input.la(2)
|
|
220
|
+
expecting = expected_tokens(recognizer)
|
|
221
|
+
if expecting.contains(next_token_type)
|
|
222
|
+
report_unwanted_token(recognizer)
|
|
223
|
+
|
|
224
|
+
recognizer.consume # simply delete extra token
|
|
225
|
+
# we want to return the token we're actually matching
|
|
226
|
+
matched_symbol = recognizer.current_token
|
|
227
|
+
report_match(recognizer) # we know current token is correct
|
|
228
|
+
return matched_symbol
|
|
229
|
+
end
|
|
230
|
+
nil
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def get_missing_symbol(recognizer)
|
|
234
|
+
current_symbol = recognizer.current_token
|
|
235
|
+
expecting = expected_tokens(recognizer)
|
|
236
|
+
expected_token_type = Token::INVALID_TYPE
|
|
237
|
+
unless expecting.is_nil
|
|
238
|
+
expected_token_type = expecting.min_element # get any element
|
|
239
|
+
end
|
|
240
|
+
if expected_token_type == Token::EOF
|
|
241
|
+
token_text = '<missing EOF>'
|
|
242
|
+
else
|
|
243
|
+
token_text = '<missing ' + recognizer.get_vocabulary.display_name(expected_token_type) + '>'
|
|
244
|
+
end
|
|
245
|
+
current = current_symbol
|
|
246
|
+
look_back = recognizer._input.lt(-1)
|
|
247
|
+
current = look_back if current.type == Token::EOF && !look_back.nil?
|
|
248
|
+
|
|
249
|
+
pair = OpenStruct.new
|
|
250
|
+
pair.a = current.source
|
|
251
|
+
pair.b = current.source._input
|
|
252
|
+
recognizer.token_factory.create(pair, expected_token_type, token_text, Token::DEFAULT_CHANNEL, -1, -1, current.line, current.char_position_in_line)
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def expected_tokens(recognizer)
|
|
256
|
+
recognizer.expected_tokens
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def token_error_display(t)
|
|
260
|
+
return '<no token>' if t.nil?
|
|
261
|
+
|
|
262
|
+
s = symbol_text(t)
|
|
263
|
+
if s.nil?
|
|
264
|
+
s = if symbol_type(t) == Token::EOF
|
|
265
|
+
'<EOF>'
|
|
266
|
+
else
|
|
267
|
+
'<' << symbol_type(t).to_s << '>'
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
escape_ws_and_quote(s)
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def symbol_text(symbol)
|
|
275
|
+
return symbol.text if symbol.respond_to? :text
|
|
276
|
+
symbol.to_s
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def symbol_type(symbol)
|
|
280
|
+
symbol.type
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def escape_ws_and_quote(s) # if ( s==nil ) return s
|
|
284
|
+
s = s.sub("\n", '\\n')
|
|
285
|
+
s = s.sub("\r", '\\r')
|
|
286
|
+
s = s.sub("\t", '\\t')
|
|
287
|
+
"'" + s + "'"
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def error_recovery_set(recognizer)
|
|
291
|
+
atn = recognizer._interp.atn
|
|
292
|
+
ctx = recognizer._ctx
|
|
293
|
+
recover_set = IntervalSet.new
|
|
294
|
+
while !ctx.nil? && ctx.invoking_state >= 0
|
|
295
|
+
# compute what follows who invoked us
|
|
296
|
+
invoking_state = atn.states[ctx.invoking_state]
|
|
297
|
+
rt = invoking_state.transition(0)
|
|
298
|
+
follow = atn.next_tokens(rt.follow_state)
|
|
299
|
+
recover_set.add_all(follow)
|
|
300
|
+
ctx = ctx.parent
|
|
301
|
+
end
|
|
302
|
+
recover_set.remove(Token::EPSILON)
|
|
303
|
+
recover_set
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def consume_until(recognizer, set)
|
|
307
|
+
ttype = recognizer._input.la(1)
|
|
308
|
+
while ttype != Token::EOF && !set.contains(ttype)
|
|
309
|
+
recognizer.consume
|
|
310
|
+
ttype = recognizer._input.la(1)
|
|
311
|
+
end
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
end
|