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.
Files changed (156) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +7 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +35 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +65 -0
  10. data/Rakefile +6 -0
  11. data/antlr4-runtime.gemspec +30 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/ext/rumourhash/Makefile +264 -0
  15. data/ext/rumourhash/extconf.rb +3 -0
  16. data/ext/rumourhash/rumourhash.c +59 -0
  17. data/lib/antlr4/runtime.rb +37 -0
  18. data/lib/antlr4/runtime/abstract_parse_tree_visitor.rb +43 -0
  19. data/lib/antlr4/runtime/abstract_predicate_transition.rb +11 -0
  20. data/lib/antlr4/runtime/action_transition.rb +29 -0
  21. data/lib/antlr4/runtime/ambiguity_info.rb +10 -0
  22. data/lib/antlr4/runtime/antlr_error_listener.rb +15 -0
  23. data/lib/antlr4/runtime/antlr_error_strategy.rb +24 -0
  24. data/lib/antlr4/runtime/antlr_file_stream.rb +17 -0
  25. data/lib/antlr4/runtime/antlr_input_stream.rb +6 -0
  26. data/lib/antlr4/runtime/array_2d_hash_set.rb +471 -0
  27. data/lib/antlr4/runtime/array_prediction_context.rb +76 -0
  28. data/lib/antlr4/runtime/atn.rb +100 -0
  29. data/lib/antlr4/runtime/atn_config.rb +140 -0
  30. data/lib/antlr4/runtime/atn_config_set.rb +150 -0
  31. data/lib/antlr4/runtime/atn_deserialization_options.rb +48 -0
  32. data/lib/antlr4/runtime/atn_deserializer.rb +737 -0
  33. data/lib/antlr4/runtime/atn_simulator.rb +69 -0
  34. data/lib/antlr4/runtime/atn_state.rb +118 -0
  35. data/lib/antlr4/runtime/atn_type.rb +8 -0
  36. data/lib/antlr4/runtime/atom_transition.rb +27 -0
  37. data/lib/antlr4/runtime/bail_error_strategy.rb +31 -0
  38. data/lib/antlr4/runtime/base_error_listener.rb +18 -0
  39. data/lib/antlr4/runtime/basic_block_start_state.rb +12 -0
  40. data/lib/antlr4/runtime/basic_state.rb +11 -0
  41. data/lib/antlr4/runtime/bit_set.rb +54 -0
  42. data/lib/antlr4/runtime/block_end_state.rb +15 -0
  43. data/lib/antlr4/runtime/block_start_state.rb +12 -0
  44. data/lib/antlr4/runtime/buffered_token_stream.rb +335 -0
  45. data/lib/antlr4/runtime/char_stream.rb +6 -0
  46. data/lib/antlr4/runtime/char_streams.rb +12 -0
  47. data/lib/antlr4/runtime/chunk.rb +4 -0
  48. data/lib/antlr4/runtime/code_point_char_stream.rb +83 -0
  49. data/lib/antlr4/runtime/common_token.rb +125 -0
  50. data/lib/antlr4/runtime/common_token_factory.rb +30 -0
  51. data/lib/antlr4/runtime/common_token_stream.rb +63 -0
  52. data/lib/antlr4/runtime/console_error_listener.rb +12 -0
  53. data/lib/antlr4/runtime/context_sensitivity_info.rb +7 -0
  54. data/lib/antlr4/runtime/decision_event_info.rb +19 -0
  55. data/lib/antlr4/runtime/decision_info.rb +36 -0
  56. data/lib/antlr4/runtime/decision_state.rb +15 -0
  57. data/lib/antlr4/runtime/default_error_strategy.rb +314 -0
  58. data/lib/antlr4/runtime/dfa.rb +97 -0
  59. data/lib/antlr4/runtime/dfa_serializer.rb +62 -0
  60. data/lib/antlr4/runtime/dfa_state.rb +109 -0
  61. data/lib/antlr4/runtime/diagnostic_error_listener.rb +58 -0
  62. data/lib/antlr4/runtime/double_key_map.rb +49 -0
  63. data/lib/antlr4/runtime/empty_prediction_context.rb +35 -0
  64. data/lib/antlr4/runtime/epsilon_transition.rb +27 -0
  65. data/lib/antlr4/runtime/equality_comparator.rb +4 -0
  66. data/lib/antlr4/runtime/error_info.rb +7 -0
  67. data/lib/antlr4/runtime/error_node.rb +5 -0
  68. data/lib/antlr4/runtime/error_node_impl.rb +12 -0
  69. data/lib/antlr4/runtime/failed_predicate_exception.rb +33 -0
  70. data/lib/antlr4/runtime/flexible_hash_map.rb +232 -0
  71. data/lib/antlr4/runtime/input_mismatch_exception.rb +20 -0
  72. data/lib/antlr4/runtime/int_stream.rb +31 -0
  73. data/lib/antlr4/runtime/integer.rb +14 -0
  74. data/lib/antlr4/runtime/interval.rb +111 -0
  75. data/lib/antlr4/runtime/interval_set.rb +540 -0
  76. data/lib/antlr4/runtime/lexer.rb +257 -0
  77. data/lib/antlr4/runtime/lexer_action.rb +12 -0
  78. data/lib/antlr4/runtime/lexer_action_executor.rb +75 -0
  79. data/lib/antlr4/runtime/lexer_action_type.rb +12 -0
  80. data/lib/antlr4/runtime/lexer_atn_config.rb +50 -0
  81. data/lib/antlr4/runtime/lexer_atn_simulator.rb +522 -0
  82. data/lib/antlr4/runtime/lexer_channel_action.rb +51 -0
  83. data/lib/antlr4/runtime/lexer_custom_action.rb +49 -0
  84. data/lib/antlr4/runtime/lexer_dfa_serializer.rb +12 -0
  85. data/lib/antlr4/runtime/lexer_indexed_custom_action.rb +49 -0
  86. data/lib/antlr4/runtime/lexer_mode_action.rb +51 -0
  87. data/lib/antlr4/runtime/lexer_more_action.rb +41 -0
  88. data/lib/antlr4/runtime/lexer_no_viable_alt_exception.rb +4 -0
  89. data/lib/antlr4/runtime/lexer_pop_mode_action.rb +41 -0
  90. data/lib/antlr4/runtime/lexer_push_mode_action.rb +51 -0
  91. data/lib/antlr4/runtime/lexer_skip_action.rb +43 -0
  92. data/lib/antlr4/runtime/lexer_type_action.rb +51 -0
  93. data/lib/antlr4/runtime/ll1_analyzer.rb +133 -0
  94. data/lib/antlr4/runtime/lookahead_event_info.rb +10 -0
  95. data/lib/antlr4/runtime/loop_end_state.rb +15 -0
  96. data/lib/antlr4/runtime/murmur_hash.rb +99 -0
  97. data/lib/antlr4/runtime/no_viable_alt_exception.rb +7 -0
  98. data/lib/antlr4/runtime/not_set_transition.rb +20 -0
  99. data/lib/antlr4/runtime/object_equality_comparator.rb +18 -0
  100. data/lib/antlr4/runtime/ordered_atn_config_set.rb +15 -0
  101. data/lib/antlr4/runtime/parse_cancellation_exception.rb +5 -0
  102. data/lib/antlr4/runtime/parse_tree.rb +7 -0
  103. data/lib/antlr4/runtime/parse_tree_listener.rb +4 -0
  104. data/lib/antlr4/runtime/parse_tree_visitor.rb +4 -0
  105. data/lib/antlr4/runtime/parser.rb +522 -0
  106. data/lib/antlr4/runtime/parser_atn_simulator.rb +1171 -0
  107. data/lib/antlr4/runtime/parser_rule_context.rb +186 -0
  108. data/lib/antlr4/runtime/plus_block_start_state.rb +11 -0
  109. data/lib/antlr4/runtime/plus_loopback_state.rb +12 -0
  110. data/lib/antlr4/runtime/precedence_predicate_transition.rb +31 -0
  111. data/lib/antlr4/runtime/predicate.rb +6 -0
  112. data/lib/antlr4/runtime/predicate_eval_info.rb +16 -0
  113. data/lib/antlr4/runtime/predicate_transition.rb +35 -0
  114. data/lib/antlr4/runtime/prediction_context.rb +103 -0
  115. data/lib/antlr4/runtime/prediction_context_cache.rb +28 -0
  116. data/lib/antlr4/runtime/prediction_context_utils.rb +407 -0
  117. data/lib/antlr4/runtime/prediction_mode.rb +213 -0
  118. data/lib/antlr4/runtime/profiling_atn_simulator.rb +149 -0
  119. data/lib/antlr4/runtime/proxy_error_listener.rb +33 -0
  120. data/lib/antlr4/runtime/range_transition.rb +29 -0
  121. data/lib/antlr4/runtime/recognition_exception.rb +17 -0
  122. data/lib/antlr4/runtime/recognizer.rb +136 -0
  123. data/lib/antlr4/runtime/rule_context.rb +131 -0
  124. data/lib/antlr4/runtime/rule_context_with_alt_num.rb +11 -0
  125. data/lib/antlr4/runtime/rule_node.rb +8 -0
  126. data/lib/antlr4/runtime/rule_start_state.rb +17 -0
  127. data/lib/antlr4/runtime/rule_stop_state.rb +12 -0
  128. data/lib/antlr4/runtime/rule_tag_token.rb +64 -0
  129. data/lib/antlr4/runtime/rule_transition.rb +29 -0
  130. data/lib/antlr4/runtime/semantic_context.rb +313 -0
  131. data/lib/antlr4/runtime/set_transition.rb +29 -0
  132. data/lib/antlr4/runtime/singleton_prediction_context.rb +56 -0
  133. data/lib/antlr4/runtime/star_block_start_state.rb +12 -0
  134. data/lib/antlr4/runtime/star_loop_entry_state.rb +17 -0
  135. data/lib/antlr4/runtime/star_loopback_state.rb +16 -0
  136. data/lib/antlr4/runtime/syntax_tree.rb +6 -0
  137. data/lib/antlr4/runtime/tag_chunk.rb +22 -0
  138. data/lib/antlr4/runtime/terminal_node.rb +5 -0
  139. data/lib/antlr4/runtime/terminal_node_impl.rb +50 -0
  140. data/lib/antlr4/runtime/text_chunk.rb +16 -0
  141. data/lib/antlr4/runtime/token.rb +13 -0
  142. data/lib/antlr4/runtime/token_stream.rb +13 -0
  143. data/lib/antlr4/runtime/token_tag_token.rb +22 -0
  144. data/lib/antlr4/runtime/tokens_start_state.rb +14 -0
  145. data/lib/antlr4/runtime/transition.rb +51 -0
  146. data/lib/antlr4/runtime/tree.rb +4 -0
  147. data/lib/antlr4/runtime/trees.rb +195 -0
  148. data/lib/antlr4/runtime/triple.rb +40 -0
  149. data/lib/antlr4/runtime/utils.rb +117 -0
  150. data/lib/antlr4/runtime/uuid.rb +46 -0
  151. data/lib/antlr4/runtime/version.rb +5 -0
  152. data/lib/antlr4/runtime/vocabulary.rb +12 -0
  153. data/lib/antlr4/runtime/vocabulary_impl.rb +82 -0
  154. data/lib/antlr4/runtime/wildcard_transition.rb +20 -0
  155. data/lib/antlr4/runtime/writable_token.rb +7 -0
  156. metadata +243 -0
@@ -0,0 +1,6 @@
1
+ require 'antlr4/runtime/int_stream'
2
+
3
+ module Antlr4::Runtime
4
+ class CharStream < IntStream
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ require 'antlr4/runtime/code_point_char_stream'
2
+
3
+ module Antlr4::Runtime
4
+
5
+ class CharStreams
6
+ DEFAULT_BUFFER_SIZE = 4096
7
+
8
+ def self.from_string(s, source_name)
9
+ CodePointCharStream.new(0, s.length, source_name, s.bytes)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ module Antlr4::Runtime
2
+ class Chunk
3
+ end
4
+ end
@@ -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,7 @@
1
+ module Antlr4::Runtime
2
+ class ContextSensitivityInfo < DecisionEventInfo
3
+ def initialize(decision, configs, input, start_index, stop_index)
4
+ super(decision, configs, input, start_index, stop_index, true)
5
+ end
6
+ end
7
+ 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,15 @@
1
+ require 'antlr4/runtime/atn_state'
2
+
3
+ module Antlr4::Runtime
4
+
5
+ class DecisionState < ATNState
6
+ attr_accessor :decision
7
+ attr_accessor :non_greedy
8
+
9
+ def initialize
10
+ super
11
+ @decision = -1
12
+ @non_greedy = false
13
+ end
14
+ end
15
+ 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