@cparra/apex-reflection 0.1.0-alpha.0 → 0.1.1-alpha.10

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 (158) hide show
  1. package/__tests__/end-to-end.test.ts +256 -0
  2. package/coverage/clover.xml +12857 -0
  3. package/coverage/coverage-final.json +3 -0
  4. package/coverage/lcov-report/base.css +224 -0
  5. package/coverage/lcov-report/block-navigation.js +79 -0
  6. package/coverage/lcov-report/favicon.png +0 -0
  7. package/coverage/lcov-report/index.html +126 -0
  8. package/coverage/lcov-report/index.js.html +104 -0
  9. package/coverage/lcov-report/out.js.html +41126 -0
  10. package/coverage/lcov-report/prettify.css +1 -0
  11. package/coverage/lcov-report/prettify.js +2 -0
  12. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  13. package/coverage/lcov-report/sorter.js +170 -0
  14. package/coverage/lcov.info +24801 -0
  15. package/index.d.ts +118 -0
  16. package/index.js +8 -0
  17. package/index.ts +141 -0
  18. package/jest.config.js +11 -0
  19. package/out.js +13708 -0
  20. package/package.json +22 -17
  21. package/tsconfig.json +5 -3
  22. package/.idea/apexdocs-dart.iml +0 -18
  23. package/.idea/jsLibraryMappings.xml +0 -6
  24. package/.idea/libraries/Dart_Packages.xml +0 -556
  25. package/.idea/libraries/Dart_SDK.xml +0 -28
  26. package/.idea/misc.xml +0 -6
  27. package/.idea/modules.xml +0 -8
  28. package/.idea/runConfigurations/_template__of_Dart_Test.xml +0 -6
  29. package/.idea/vcs.xml +0 -6
  30. package/CHANGELOG.md +0 -3
  31. package/README.md +0 -26
  32. package/TODO.md +0 -5
  33. package/analysis_options.yaml +0 -30
  34. package/antlr4-4.9.2/CHANGELOG.md +0 -4
  35. package/antlr4-4.9.2/LICENSE +0 -52
  36. package/antlr4-4.9.2/README.md +0 -11
  37. package/antlr4-4.9.2/analysis_options.yaml +0 -1
  38. package/antlr4-4.9.2/lib/antlr4.dart +0 -21
  39. package/antlr4-4.9.2/lib/src/atn/atn.dart +0 -18
  40. package/antlr4-4.9.2/lib/src/atn/src/atn.dart +0 -170
  41. package/antlr4-4.9.2/lib/src/atn/src/atn_config.dart +0 -242
  42. package/antlr4-4.9.2/lib/src/atn/src/atn_config_set.dart +0 -283
  43. package/antlr4-4.9.2/lib/src/atn/src/atn_deserializer.dart +0 -809
  44. package/antlr4-4.9.2/lib/src/atn/src/atn_simulator.dart +0 -95
  45. package/antlr4-4.9.2/lib/src/atn/src/atn_state.dart +0 -296
  46. package/antlr4-4.9.2/lib/src/atn/src/atn_type.dart +0 -14
  47. package/antlr4-4.9.2/lib/src/atn/src/info.dart +0 -553
  48. package/antlr4-4.9.2/lib/src/atn/src/lexer_action.dart +0 -601
  49. package/antlr4-4.9.2/lib/src/atn/src/lexer_action_executor.dart +0 -167
  50. package/antlr4-4.9.2/lib/src/atn/src/lexer_atn_simulator.dart +0 -731
  51. package/antlr4-4.9.2/lib/src/atn/src/parser_atn_simulator.dart +0 -2630
  52. package/antlr4-4.9.2/lib/src/atn/src/profiling_atn_simulator.dart +0 -229
  53. package/antlr4-4.9.2/lib/src/atn/src/semantic_context.dart +0 -404
  54. package/antlr4-4.9.2/lib/src/atn/src/transition.dart +0 -305
  55. package/antlr4-4.9.2/lib/src/dfa/dfa.dart +0 -8
  56. package/antlr4-4.9.2/lib/src/dfa/src/dfa.dart +0 -138
  57. package/antlr4-4.9.2/lib/src/dfa/src/dfa_serializer.dart +0 -76
  58. package/antlr4-4.9.2/lib/src/dfa/src/dfa_state.dart +0 -151
  59. package/antlr4-4.9.2/lib/src/error/error.dart +0 -10
  60. package/antlr4-4.9.2/lib/src/error/src/diagnostic_error_listener.dart +0 -116
  61. package/antlr4-4.9.2/lib/src/error/src/error_listener.dart +0 -241
  62. package/antlr4-4.9.2/lib/src/error/src/error_strategy.dart +0 -902
  63. package/antlr4-4.9.2/lib/src/error/src/errors.dart +0 -204
  64. package/antlr4-4.9.2/lib/src/input_stream.dart +0 -335
  65. package/antlr4-4.9.2/lib/src/interval_set.dart +0 -735
  66. package/antlr4-4.9.2/lib/src/lexer.dart +0 -343
  67. package/antlr4-4.9.2/lib/src/ll1_analyzer.dart +0 -204
  68. package/antlr4-4.9.2/lib/src/misc/multi_map.dart +0 -32
  69. package/antlr4-4.9.2/lib/src/misc/pair.dart +0 -34
  70. package/antlr4-4.9.2/lib/src/parser.dart +0 -777
  71. package/antlr4-4.9.2/lib/src/parser_interpreter.dart +0 -393
  72. package/antlr4-4.9.2/lib/src/parser_rule_context.dart +0 -275
  73. package/antlr4-4.9.2/lib/src/prediction_context.dart +0 -877
  74. package/antlr4-4.9.2/lib/src/recognizer.dart +0 -182
  75. package/antlr4-4.9.2/lib/src/rule_context.dart +0 -192
  76. package/antlr4-4.9.2/lib/src/runtime_meta_data.dart +0 -188
  77. package/antlr4-4.9.2/lib/src/token.dart +0 -431
  78. package/antlr4-4.9.2/lib/src/token_factory.dart +0 -88
  79. package/antlr4-4.9.2/lib/src/token_source.dart +0 -241
  80. package/antlr4-4.9.2/lib/src/token_stream.dart +0 -627
  81. package/antlr4-4.9.2/lib/src/tree/src/pattern/chunk.dart +0 -90
  82. package/antlr4-4.9.2/lib/src/tree/src/pattern/parse_tree_match.dart +0 -635
  83. package/antlr4-4.9.2/lib/src/tree/src/tree.dart +0 -370
  84. package/antlr4-4.9.2/lib/src/tree/src/trees.dart +0 -226
  85. package/antlr4-4.9.2/lib/src/tree/tree.dart +0 -10
  86. package/antlr4-4.9.2/lib/src/util/bit_set.dart +0 -308
  87. package/antlr4-4.9.2/lib/src/util/murmur_hash.dart +0 -77
  88. package/antlr4-4.9.2/lib/src/util/utils.dart +0 -31
  89. package/antlr4-4.9.2/lib/src/vocabulary.dart +0 -254
  90. package/antlr4-4.9.2/pubspec.yaml +0 -13
  91. package/example/node_example/index.js +0 -8
  92. package/example/node_example/package.json +0 -12
  93. package/example/node_example_ts/package-lock.json +0 -70
  94. package/example/node_example_ts/package.json +0 -19
  95. package/example/node_example_ts/src/index.js +0 -5
  96. package/example/node_example_ts/src/index.ts +0 -9
  97. package/example/node_example_ts/tsconfig.json +0 -79
  98. package/js/dart2jsout.js +0 -25898
  99. package/js/dart2jsout.js.map +0 -16
  100. package/js/index.d.ts +0 -1
  101. package/js/index.js +0 -4
  102. package/js/out.js +0 -26023
  103. package/js/out.js.map +0 -16
  104. package/js/package-lock.json +0 -57
  105. package/js/preamble.js +0 -125
  106. package/lib/apexdocs_dart.dart +0 -28
  107. package/lib/src/antlr/grammars/Apexdoc/ApexdocLexer.g4 +0 -120
  108. package/lib/src/antlr/grammars/Apexdoc/ApexdocParser.g4 +0 -158
  109. package/lib/src/antlr/grammars/Apexdoc/gen/ApexdocLexer.interp +0 -95
  110. package/lib/src/antlr/grammars/Apexdoc/gen/ApexdocLexer.java +0 -238
  111. package/lib/src/antlr/grammars/Apexdoc/gen/ApexdocLexer.tokens +0 -23
  112. package/lib/src/antlr/grammars/apex/ApexLexer.g4 +0 -255
  113. package/lib/src/antlr/grammars/apex/ApexParser.g4 +0 -567
  114. package/lib/src/antlr/grammars/apex/examples/ApexClass.cls +0 -6
  115. package/lib/src/antlr/lib/apex/ApexLexer.dart +0 -1223
  116. package/lib/src/antlr/lib/apex/ApexLexer.interp +0 -393
  117. package/lib/src/antlr/lib/apex/ApexLexer.tokens +0 -212
  118. package/lib/src/antlr/lib/apex/ApexParser.dart +0 -9349
  119. package/lib/src/antlr/lib/apex/ApexParser.interp +0 -326
  120. package/lib/src/antlr/lib/apex/ApexParser.tokens +0 -212
  121. package/lib/src/antlr/lib/apex/ApexParserBaseListener.dart +0 -1036
  122. package/lib/src/antlr/lib/apex/ApexParserListener.dart +0 -975
  123. package/lib/src/antlr/lib/apexdoc/ApexdocLexer.dart +0 -373
  124. package/lib/src/antlr/lib/apexdoc/ApexdocLexer.interp +0 -95
  125. package/lib/src/antlr/lib/apexdoc/ApexdocLexer.tokens +0 -23
  126. package/lib/src/antlr/lib/apexdoc/ApexdocParser.dart +0 -2471
  127. package/lib/src/antlr/lib/apexdoc/ApexdocParser.interp +0 -69
  128. package/lib/src/antlr/lib/apexdoc/ApexdocParser.tokens +0 -23
  129. package/lib/src/antlr/lib/apexdoc/ApexdocParserBaseListener.dart +0 -252
  130. package/lib/src/antlr/lib/apexdoc/ApexdocParserListener.dart +0 -215
  131. package/lib/src/builders/builders.dart +0 -32
  132. package/lib/src/model/apex_file_manifest.dart +0 -37
  133. package/lib/src/model/apex_file_manifest.g.dart +0 -18
  134. package/lib/src/model/declaration.dart +0 -50
  135. package/lib/src/model/doc_comment.dart +0 -117
  136. package/lib/src/model/doc_comment.g.dart +0 -118
  137. package/lib/src/model/members.dart +0 -143
  138. package/lib/src/model/members.g.dart +0 -105
  139. package/lib/src/model/types.dart +0 -159
  140. package/lib/src/model/types.g.dart +0 -111
  141. package/lib/src/service/apex_listener.dart +0 -226
  142. package/lib/src/service/apexdoc_listener.dart +0 -82
  143. package/lib/src/service/parsers.dart +0 -33
  144. package/lib/src/service/utils/parsing/access_modifiers_parser.dart +0 -33
  145. package/lib/src/service/utils/parsing/parameters_parser.dart +0 -18
  146. package/lib/src/service/utils/parsing/parsing_utils.dart +0 -2
  147. package/lib/src/service/walker.dart +0 -82
  148. package/pubspec.yaml +0 -19
  149. package/test/apex_file_manifest_test.dart +0 -16
  150. package/test/apex_listener_test.dart +0 -703
  151. package/test/apexdoc_parser_test.dart +0 -179
  152. package/test/doc_comment_test.dart +0 -89
  153. package/test/members_serialization_test.dart +0 -158
  154. package/test/members_test.dart +0 -178
  155. package/test/types_serialization_test.dart +0 -191
  156. package/test/types_test.dart +0 -311
  157. package/test/walker_test.dart +0 -58
  158. package/tool/grind.dart +0 -20
@@ -1,2630 +0,0 @@
1
- /*
2
- * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3
- * Use of this file is governed by the BSD 3-clause license that
4
- * can be found in the LICENSE.txt file in the project root.
5
- */
6
-
7
- import 'dart:collection';
8
- import 'dart:developer';
9
-
10
- import 'package:logging/logging.dart';
11
-
12
- import '../../dfa/dfa.dart';
13
- import '../../error/error.dart';
14
- import '../../input_stream.dart';
15
- import '../../interval_set.dart';
16
- import '../../misc/pair.dart';
17
- import '../../parser.dart';
18
- import '../../parser_rule_context.dart';
19
- import '../../prediction_context.dart';
20
- import '../../rule_context.dart';
21
- import '../../token.dart';
22
- import '../../token_stream.dart';
23
- import '../../util/bit_set.dart';
24
- import '../../util/murmur_hash.dart';
25
- import '../../vocabulary.dart';
26
- import 'atn.dart';
27
- import 'atn_config.dart';
28
- import 'atn_config_set.dart';
29
- import 'atn_simulator.dart';
30
- import 'atn_state.dart';
31
- import 'semantic_context.dart';
32
- import 'transition.dart';
33
-
34
- /// The embodiment of the adaptive LL(*), ALL(*), parsing strategy.
35
- ///
36
- /// <p>
37
- /// The basic complexity of the adaptive strategy makes it harder to understand.
38
- /// We begin with ATN simulation to build paths in a DFA. Subsequent prediction
39
- /// requests go through the DFA first. If they reach a state without an edge for
40
- /// the current symbol, the algorithm fails over to the ATN simulation to
41
- /// complete the DFA path for the current input (until it finds a conflict state
42
- /// or uniquely predicting state).</p>
43
- ///
44
- /// <p>
45
- /// All of that is done without using the outer context because we want to create
46
- /// a DFA that is not dependent upon the rule invocation stack when we do a
47
- /// prediction. One DFA works in all contexts. We avoid using context not
48
- /// necessarily because it's slower, although it can be, but because of the DFA
49
- /// caching problem. The closure routine only considers the rule invocation stack
50
- /// created during prediction beginning in the decision rule. For example, if
51
- /// prediction occurs without invoking another rule's ATN, there are no context
52
- /// stacks in the configurations. When lack of context leads to a conflict, we
53
- /// don't know if it's an ambiguity or a weakness in the strong LL(*) parsing
54
- /// strategy (versus full LL(*)).</p>
55
- ///
56
- /// <p>
57
- /// When SLL yields a configuration set with conflict, we rewind the input and
58
- /// retry the ATN simulation, this time using full outer context without adding
59
- /// to the DFA. Configuration context stacks will be the full invocation stacks
60
- /// from the start rule. If we get a conflict using full context, then we can
61
- /// definitively say we have a true ambiguity for that input sequence. If we
62
- /// don't get a conflict, it implies that the decision is sensitive to the outer
63
- /// context. (It is not context-sensitive in the sense of context-sensitive
64
- /// grammars.)</p>
65
- ///
66
- /// <p>
67
- /// The next time we reach this DFA state with an SLL conflict, through DFA
68
- /// simulation, we will again retry the ATN simulation using full context mode.
69
- /// This is slow because we can't save the results and have to "interpret" the
70
- /// ATN each time we get that input.</p>
71
- ///
72
- /// <p>
73
- /// <strong>CACHING FULL CONTEXT PREDICTIONS</strong></p>
74
- ///
75
- /// <p>
76
- /// We could cache results from full context to predicted alternative easily and
77
- /// that saves a lot of time but doesn't work in presence of predicates. The set
78
- /// of visible predicates from the ATN start state changes depending on the
79
- /// context, because closure can fall off the end of a rule. I tried to cache
80
- /// tuples (stack context, semantic context, predicted alt) but it was slower
81
- /// than interpreting and much more complicated. Also required a huge amount of
82
- /// memory. The goal is not to create the world's fastest parser anyway. I'd like
83
- /// to keep this algorithm simple. By launching multiple threads, we can improve
84
- /// the speed of parsing across a large number of files.</p>
85
- ///
86
- /// <p>
87
- /// There is no strict ordering between the amount of input used by SLL vs LL,
88
- /// which makes it really hard to build a cache for full context. Let's say that
89
- /// we have input A B C that leads to an SLL conflict with full context X. That
90
- /// implies that using X we might only use A B but we could also use A B C D to
91
- /// resolve conflict. Input A B C D could predict alternative 1 in one position
92
- /// in the input and A B C E could predict alternative 2 in another position in
93
- /// input. The conflicting SLL configurations could still be non-unique in the
94
- /// full context prediction, which would lead us to requiring more input than the
95
- /// original A B C. To make a prediction cache work, we have to track the exact
96
- /// input used during the previous prediction. That amounts to a cache that maps
97
- /// X to a specific DFA for that context.</p>
98
- ///
99
- /// <p>
100
- /// Something should be done for left-recursive expression predictions. They are
101
- /// likely LL(1) + pred eval. Easier to do the whole SLL unless error and retry
102
- /// with full LL thing Sam does.</p>
103
- ///
104
- /// <p>
105
- /// <strong>AVOIDING FULL CONTEXT PREDICTION</strong></p>
106
- ///
107
- /// <p>
108
- /// We avoid doing full context retry when the outer context is empty, we did not
109
- /// dip into the outer context by falling off the end of the decision state rule,
110
- /// or when we force SLL mode.</p>
111
- ///
112
- /// <p>
113
- /// As an example of the not dip into outer context case, consider as super
114
- /// constructor calls versus function calls. One grammar might look like
115
- /// this:</p>
116
- ///
117
- /// <pre>
118
- /// ctorBody
119
- /// : '{' superCall? stat* '}'
120
- /// ;
121
- /// </pre>
122
- ///
123
- /// <p>
124
- /// Or, you might see something like</p>
125
- ///
126
- /// <pre>
127
- /// stat
128
- /// : superCall ';'
129
- /// | expression ';'
130
- /// | ...
131
- /// ;
132
- /// </pre>
133
- ///
134
- /// <p>
135
- /// In both cases I believe that no closure operations will dip into the outer
136
- /// context. In the first case ctorBody in the worst case will stop at the '}'.
137
- /// In the 2nd case it should stop at the ';'. Both cases should stay within the
138
- /// entry rule and not dip into the outer context.</p>
139
- ///
140
- /// <p>
141
- /// <strong>PREDICATES</strong></p>
142
- ///
143
- /// <p>
144
- /// Predicates are always evaluated if present in either SLL or LL both. SLL and
145
- /// LL simulation deals with predicates differently. SLL collects predicates as
146
- /// it performs closure operations like ANTLR v3 did. It delays predicate
147
- /// evaluation until it reaches and accept state. This allows us to cache the SLL
148
- /// ATN simulation whereas, if we had evaluated predicates on-the-fly during
149
- /// closure, the DFA state configuration sets would be different and we couldn't
150
- /// build up a suitable DFA.</p>
151
- ///
152
- /// <p>
153
- /// When building a DFA accept state during ATN simulation, we evaluate any
154
- /// predicates and return the sole semantically valid alternative. If there is
155
- /// more than 1 alternative, we report an ambiguity. If there are 0 alternatives,
156
- /// we throw an exception. Alternatives without predicates act like they have
157
- /// true predicates. The simple way to think about it is to strip away all
158
- /// alternatives with false predicates and choose the minimum alternative that
159
- /// remains.</p>
160
- ///
161
- /// <p>
162
- /// When we start in the DFA and reach an accept state that's predicated, we test
163
- /// those and return the minimum semantically viable alternative. If no
164
- /// alternatives are viable, we throw an exception.</p>
165
- ///
166
- /// <p>
167
- /// During full LL ATN simulation, closure always evaluates predicates and
168
- /// on-the-fly. This is crucial to reducing the configuration set size during
169
- /// closure. It hits a landmine when parsing with the Java grammar, for example,
170
- /// without this on-the-fly evaluation.</p>
171
- ///
172
- /// <p>
173
- /// <strong>SHARING DFA</strong></p>
174
- ///
175
- /// <p>
176
- /// All instances of the same parser share the same decision DFAs through a
177
- /// static field. Each instance gets its own ATN simulator but they share the
178
- /// same {@link #decisionToDFA} field. They also share a
179
- /// [PredictionContextCache] object that makes sure that all
180
- /// [PredictionContext] objects are shared among the DFA states. This makes
181
- /// a big size difference.</p>
182
- ///
183
- /// <p>
184
- /// <strong>THREAD SAFETY</strong></p>
185
- ///
186
- /// <p>
187
- /// The [ParserATNSimulator] locks on the {@link #decisionToDFA} field when
188
- /// it adds a new DFA object to that array. {@link #addDFAEdge}
189
- /// locks on the DFA for the current decision when setting the
190
- /// {@link DFAState#edges} field. {@link #addDFAState} locks on
191
- /// the DFA for the current decision when looking up a DFA state to see if it
192
- /// already exists. We must make sure that all requests to add DFA states that
193
- /// are equivalent result in the same shared DFA object. This is because lots of
194
- /// threads will be trying to update the DFA at once. The
195
- /// {@link #addDFAState} method also locks inside the DFA lock
196
- /// but this time on the shared context cache when it rebuilds the
197
- /// configurations' [PredictionContext] objects using cached
198
- /// subgraphs/nodes. No other locking occurs, even during DFA simulation. This is
199
- /// safe as long as we can guarantee that all threads referencing
200
- /// {@code s.edge[t]} get the same physical target [DFAState], or
201
- /// null. Once into the DFA, the DFA simulation does not reference the
202
- /// {@link DFA#states} map. It follows the {@link DFAState#edges} field to new
203
- /// targets. The DFA simulator will either find {@link DFAState#edges} to be
204
- /// null, to be non-null and {@code dfa.edges[t]} null, or
205
- /// {@code dfa.edges[t]} to be non-null. The
206
- /// {@link #addDFAEdge} method could be racing to set the field
207
- /// but in either case the DFA simulator works; if null, and requests ATN
208
- /// simulation. It could also race trying to get {@code dfa.edges[t]}, but either
209
- /// way it will work because it's not doing a test and set operation.</p>
210
- ///
211
- /// <p>
212
- /// <strong>Starting with SLL then failing to combined SLL/LL (Two-Stage
213
- /// Parsing)</strong></p>
214
- ///
215
- /// <p>
216
- /// Sam pointed out that if SLL does not give a syntax error, then there is no
217
- /// point in doing full LL, which is slower. We only have to try LL if we get a
218
- /// syntax error. For maximum speed, Sam starts the parser set to pure SLL
219
- /// mode with the [BailErrorStrategy]:</p>
220
- ///
221
- /// <pre>
222
- /// parser.{@link Parser#interpreter interpreter}.{@link #setPredictionMode setPredictionMode}{@code (}{@link PredictionMode#SLL}{@code )};
223
- /// parser.{@link Parser#setErrorHandler setErrorHandler}(new [BailErrorStrategy]());
224
- /// </pre>
225
- ///
226
- /// <p>
227
- /// If it does not get a syntax error, then we're done. If it does get a syntax
228
- /// error, we need to retry with the combined SLL/LL strategy.</p>
229
- ///
230
- /// <p>
231
- /// The reason this works is as follows. If there are no SLL conflicts, then the
232
- /// grammar is SLL (at least for that input set). If there is an SLL conflict,
233
- /// the full LL analysis must yield a set of viable alternatives which is a
234
- /// subset of the alternatives reported by SLL. If the LL set is a singleton,
235
- /// then the grammar is LL but not SLL. If the LL set is the same size as the SLL
236
- /// set, the decision is SLL. If the LL set has size &gt; 1, then that decision
237
- /// is truly ambiguous on the current input. If the LL set is smaller, then the
238
- /// SLL conflict resolution might choose an alternative that the full LL would
239
- /// rule out as a possibility based upon better context information. If that's
240
- /// the case, then the SLL parse will definitely get an error because the full LL
241
- /// analysis says it's not viable. If SLL conflict resolution chooses an
242
- /// alternative within the LL set, them both SLL and LL would choose the same
243
- /// alternative because they both choose the minimum of multiple conflicting
244
- /// alternatives.</p>
245
- ///
246
- /// <p>
247
- /// Let's say we have a set of SLL conflicting alternatives {@code {1, 2, 3}} and
248
- /// a smaller LL set called <em>s</em>. If <em>s</em> is {@code {2, 3}}, then SLL
249
- /// parsing will get an error because SLL will pursue alternative 1. If
250
- /// <em>s</em> is {@code {1, 2}} or {@code {1, 3}} then both SLL and LL will
251
- /// choose the same alternative because alternative one is the minimum of either
252
- /// set. If <em>s</em> is {@code {2}} or {@code {3}} then SLL will get a syntax
253
- /// error. If <em>s</em> is {@code {1}} then SLL will succeed.</p>
254
- ///
255
- /// <p>
256
- /// Of course, if the input is invalid, then we will get an error for sure in
257
- /// both SLL and LL parsing. Erroneous input will therefore require 2 passes over
258
- /// the input.</p>
259
- class ParserATNSimulator extends ATNSimulator {
260
- static final bool debug = false;
261
- static final bool debug_list_atn_decisions = false;
262
- static final bool dfa_debug = false;
263
- static final bool retry_debug = false;
264
-
265
- /// Just in case this optimization is bad, add an ENV variable to turn it off */
266
- static final bool TURN_OFF_LR_LOOP_ENTRY_BRANCH_OPT =
267
- bool.fromEnvironment('TURN_OFF_LR_LOOP_ENTRY_BRANCH_OPT');
268
-
269
- final Parser parser;
270
-
271
- final List<DFA> decisionToDFA;
272
-
273
- /// SLL, LL, or LL + exact ambig detection? */
274
-
275
- PredictionMode predictionMode = PredictionMode.LL;
276
-
277
- /// Each prediction operation uses a cache for merge of prediction contexts.
278
- /// Don't keep around as it wastes huge amounts of memory. DoubleKeyMap
279
- /// isn't synchronized but we're ok since two threads shouldn't reuse same
280
- /// parser/atnsim object because it can only handle one input at a time.
281
- /// This maps graphs a and b to merged result c. (a,b)&rarr;c. We can avoid
282
- /// the merge if we ever see a and b again. Note that (b,a)&rarr;c should
283
- /// also be examined during cache lookup.
284
- Map<Pair<PredictionContext, PredictionContext>, PredictionContext> mergeCache;
285
-
286
- // LAME globals to avoid parameters!!!!! I need these down deep in predTransition
287
- TokenStream input;
288
- int startIndex = 0;
289
- ParserRuleContext _outerContext;
290
- DFA _dfa;
291
-
292
- ParserATNSimulator(this.parser, ATN atn, this.decisionToDFA,
293
- PredictionContextCache sharedContextCache)
294
- : super(atn, sharedContextCache) {
295
- // DOTGenerator dot = new DOTGenerator(null);
296
- // log(dot.getDOT(atn.rules.get(0), parser.getRuleNames()));
297
- // log(dot.getDOT(atn.rules.get(1), parser.getRuleNames()));
298
- }
299
-
300
- @override
301
- void reset() {}
302
-
303
- @override
304
- void clearDFA() {
305
- for (var d = 0; d < decisionToDFA.length; d++) {
306
- decisionToDFA[d] = DFA(atn.getDecisionState(d), d);
307
- }
308
- }
309
-
310
- int adaptivePredict(
311
- TokenStream input_, int decision, ParserRuleContext outerContext) {
312
- if (debug || debug_list_atn_decisions) {
313
- log('adaptivePredict decision $decision' ' exec LA(1)==' +
314
- getLookaheadName(input_) +
315
- ' line ${input_.LT(1).line}:${input_.LT(1).charPositionInLine}');
316
- }
317
-
318
- input = input_;
319
- startIndex = input_.index;
320
- _outerContext = outerContext;
321
- final dfa = decisionToDFA[decision];
322
- _dfa = dfa;
323
-
324
- final m = input_.mark();
325
- final index = startIndex;
326
-
327
- // Now we are certain to have a specific decision's DFA
328
- // But, do we still need an initial state?
329
- try {
330
- DFAState s0;
331
- if (dfa.isPrecedenceDfa()) {
332
- // the start state for a precedence DFA depends on the current
333
- // parser precedence, and is provided by a DFA method.
334
- s0 = dfa.getPrecedenceStartState(parser.precedence);
335
- } else {
336
- // the start state for a "regular" DFA is just s0
337
- s0 = dfa.s0;
338
- }
339
-
340
- if (s0 == null) {
341
- outerContext ??= ParserRuleContext.EMPTY;
342
- if (debug || debug_list_atn_decisions) {
343
- log('predictATN decision ${dfa.decision}' ' exec LA(1)==' +
344
- getLookaheadName(input_) +
345
- ', outerContext=' +
346
- outerContext.toString(recog: parser));
347
- }
348
-
349
- final fullCtx = false;
350
- var s0_closure = computeStartState(
351
- dfa.atnStartState, ParserRuleContext.EMPTY, fullCtx);
352
-
353
- if (dfa.isPrecedenceDfa()) {
354
- /* If this is a precedence DFA, we use applyPrecedenceFilter
355
- * to convert the computed start state to a precedence start
356
- * state. We then use DFA.setPrecedenceStartState to set the
357
- * appropriate start state for the precedence level rather
358
- * than simply setting DFA.s0.
359
- */
360
- dfa.s0.configs =
361
- s0_closure; // not used for prediction but useful to know start configs anyway
362
- s0_closure = applyPrecedenceFilter(s0_closure);
363
- s0 = addDFAState(dfa, DFAState(configs: s0_closure));
364
- dfa.setPrecedenceStartState(parser.precedence, s0);
365
- } else {
366
- s0 = addDFAState(dfa, DFAState(configs: s0_closure));
367
- dfa.s0 = s0;
368
- }
369
- }
370
-
371
- final alt = execATN(dfa, s0, input_, index, outerContext);
372
- if (debug) {
373
- log('DFA after predictATN: ' + dfa.toString(parser.vocabulary));
374
- }
375
- return alt;
376
- } finally {
377
- mergeCache = null; // wack cache after each prediction
378
- _dfa = null;
379
- input_.seek(index);
380
- input_.release(m);
381
- }
382
- }
383
-
384
- /// Performs ATN simulation to compute a predicted alternative based
385
- /// upon the remaining input, but also updates the DFA cache to avoid
386
- /// having to traverse the ATN again for the same input sequence.
387
- ///
388
- /// There are some key conditions we're looking for after computing a new
389
- /// set of ATN configs (proposed DFA state):
390
- /// if the set is empty, there is no viable alternative for current symbol
391
- /// does the state uniquely predict an alternative?
392
- /// does the state have a conflict that would prevent us from
393
- /// putting it on the work list?
394
- ///
395
- /// We also have some key operations to do:
396
- /// add an edge from previous DFA state to potentially new DFA state, D,
397
- /// upon current symbol but only if adding to work list, which means in all
398
- /// cases except no viable alternative (and possibly non-greedy decisions?)
399
- /// collecting predicates and adding semantic context to DFA accept states
400
- /// adding rule context to context-sensitive DFA accept states
401
- /// consuming an input symbol
402
- /// reporting a conflict
403
- /// reporting an ambiguity
404
- /// reporting a context sensitivity
405
- /// reporting insufficient predicates
406
- ///
407
- /// cover these cases:
408
- /// dead end
409
- /// single alt
410
- /// single alt + preds
411
- /// conflict
412
- /// conflict + preds
413
- ///
414
- int execATN(DFA dfa, DFAState s0, TokenStream input, int startIndex,
415
- ParserRuleContext outerContext) {
416
- if (debug || debug_list_atn_decisions) {
417
- log('execATN decision ${dfa.decision}' ' exec LA(1)==' +
418
- getLookaheadName(input) +
419
- ' line ${input.LT(1).line}' +
420
- ':${input.LT(1).charPositionInLine}');
421
- }
422
-
423
- var previousD = s0;
424
-
425
- if (debug) log('s0 = $s0');
426
-
427
- var t = input.LA(1);
428
-
429
- while (true) {
430
- // while more work
431
- var D = getExistingTargetState(previousD, t);
432
- D ??= computeTargetState(dfa, previousD, t);
433
-
434
- if (D == ATNSimulator.ERROR) {
435
- // if any configs in previous dipped into outer context, that
436
- // means that input up to t actually finished entry rule
437
- // at least for SLL decision. Full LL doesn't dip into outer
438
- // so don't need special case.
439
- // We will get an error no matter what so delay until after
440
- // decision; better error message. Also, no reachable target
441
- // ATN states in SLL implies LL will also get nowhere.
442
- // If conflict in states that dip out, choose min since we
443
- // will get error no matter what.
444
- final e =
445
- noViableAlt(input, outerContext, previousD.configs, startIndex);
446
- input.seek(startIndex);
447
- final alt = getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(
448
- previousD.configs, outerContext);
449
- if (alt != ATN.INVALID_ALT_NUMBER) {
450
- return alt;
451
- }
452
- throw e;
453
- }
454
-
455
- if (D.requiresFullContext && predictionMode != PredictionMode.SLL) {
456
- // IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
457
- var conflictingAlts = D.configs.conflictingAlts;
458
- if (D.predicates != null) {
459
- if (debug) log('DFA state has preds in DFA sim LL failover');
460
- final conflictIndex = input.index;
461
- if (conflictIndex != startIndex) {
462
- input.seek(startIndex);
463
- }
464
-
465
- conflictingAlts =
466
- evalSemanticContext(D.predicates, outerContext, true);
467
- if (conflictingAlts.cardinality == 1) {
468
- if (debug) log('Full LL avoided');
469
- return conflictingAlts.nextset(0);
470
- }
471
-
472
- if (conflictIndex != startIndex) {
473
- // restore the index so reporting the fallback to full
474
- // context occurs with the index at the correct spot
475
- input.seek(conflictIndex);
476
- }
477
- }
478
-
479
- if (dfa_debug) log('ctx sensitive state ${outerContext} in $D');
480
- final fullCtx = true;
481
- final s0_closure =
482
- computeStartState(dfa.atnStartState, outerContext, fullCtx);
483
- reportAttemptingFullContext(
484
- dfa, conflictingAlts, D.configs, startIndex, input.index);
485
- final alt = execATNWithFullContext(
486
- dfa, D, s0_closure, input, startIndex, outerContext);
487
- return alt;
488
- }
489
-
490
- if (D.isAcceptState) {
491
- if (D.predicates == null) {
492
- return D.prediction;
493
- }
494
-
495
- final stopIndex = input.index;
496
- input.seek(startIndex);
497
- final alts = evalSemanticContext(D.predicates, outerContext, true);
498
- switch (alts.cardinality) {
499
- case 0:
500
- throw noViableAlt(input, outerContext, D.configs, startIndex);
501
-
502
- case 1:
503
- return alts.nextset(0);
504
-
505
- default:
506
- // report ambiguity after predicate evaluation to make sure the correct
507
- // set of ambig alts is reported.
508
- reportAmbiguity(
509
- dfa, D, startIndex, stopIndex, false, alts, D.configs);
510
- return alts.nextset(0);
511
- }
512
- }
513
-
514
- previousD = D;
515
-
516
- if (t != IntStream.EOF) {
517
- input.consume();
518
- t = input.LA(1);
519
- }
520
- }
521
- }
522
-
523
- /// Get an existing target state for an edge in the DFA. If the target state
524
- /// for the edge has not yet been computed or is otherwise not available,
525
- /// this method returns null.
526
- ///
527
- /// @param previousD The current DFA state
528
- /// @param t The next input symbol
529
- /// @return The existing target DFA state for the given input symbol
530
- /// [t], or null if the target state for this edge is not
531
- /// already cached
532
- DFAState getExistingTargetState(DFAState previousD, int t) {
533
- final edges = previousD.edges;
534
- if (edges == null || t + 1 < 0 || t + 1 >= edges.length) {
535
- return null;
536
- }
537
-
538
- return edges[t + 1];
539
- }
540
-
541
- /// Compute a target state for an edge in the DFA, and attempt to add the
542
- /// computed state and corresponding edge to the DFA.
543
- ///
544
- /// @param dfa The DFA
545
- /// @param previousD The current DFA state
546
- /// @param t The next input symbol
547
- ///
548
- /// @return The computed target DFA state for the given input symbol
549
- /// [t]. If [t] does not lead to a valid DFA state, this method
550
- /// returns {@link #ERROR}.
551
- DFAState computeTargetState(DFA dfa, DFAState previousD, int t) {
552
- final reach = computeReachSet(previousD.configs, t, false);
553
- if (reach == null) {
554
- addDFAEdge(dfa, previousD, t, ATNSimulator.ERROR);
555
- return ATNSimulator.ERROR;
556
- }
557
-
558
- // create new target state; we'll add to DFA after it's complete
559
- var D = DFAState(configs: reach);
560
-
561
- final predictedAlt = getUniqueAlt(reach);
562
-
563
- if (debug) {
564
- final altSubSets =
565
- PredictionModeExtension.getConflictingAltSubsets(reach);
566
- log(
567
- 'SLL altSubSets=$altSubSets' ', configs=$reach' ', predict=$predictedAlt, allSubsetsConflict=${PredictionModeExtension
568
- .allSubsetsConflict(
569
- altSubSets)}, conflictingAlts=${getConflictingAlts(reach)}');
570
- }
571
-
572
- if (predictedAlt != ATN.INVALID_ALT_NUMBER) {
573
- // NO CONFLICT, UNIQUELY PREDICTED ALT
574
- D.isAcceptState = true;
575
- D.configs.uniqueAlt = predictedAlt;
576
- D.prediction = predictedAlt;
577
- } else if (PredictionModeExtension.hasSLLConflictTerminatingPrediction(
578
- predictionMode, reach)) {
579
- // MORE THAN ONE VIABLE ALTERNATIVE
580
- D.configs.conflictingAlts = getConflictingAlts(reach);
581
- D.requiresFullContext = true;
582
- // in SLL-only mode, we will stop at this state and return the minimum alt
583
- D.isAcceptState = true;
584
- D.prediction = D.configs.conflictingAlts.nextset(0);
585
- }
586
-
587
- if (D.isAcceptState && D.configs.hasSemanticContext) {
588
- predicateDFAState(D, atn.getDecisionState(dfa.decision));
589
- if (D.predicates != null) {
590
- D.prediction = ATN.INVALID_ALT_NUMBER;
591
- }
592
- }
593
-
594
- // all adds to dfa are done after we've created full D state
595
- D = addDFAEdge(dfa, previousD, t, D);
596
- return D;
597
- }
598
-
599
- void predicateDFAState(DFAState dfaState, DecisionState decisionState) {
600
- // We need to test all predicates, even in DFA states that
601
- // uniquely predict alternative.
602
- final nalts = decisionState.numberOfTransitions;
603
- // Update DFA so reach becomes accept state with (predicate,alt)
604
- // pairs if preds found for conflicting alts
605
- final altsToCollectPredsFrom =
606
- getConflictingAltsOrUniqueAlt(dfaState.configs);
607
- final altToPred =
608
- getPredsForAmbigAlts(altsToCollectPredsFrom, dfaState.configs, nalts);
609
- if (altToPred != null) {
610
- dfaState.predicates =
611
- getPredicatePredictions(altsToCollectPredsFrom, altToPred);
612
- dfaState.prediction = ATN.INVALID_ALT_NUMBER; // make sure we use preds
613
- } else {
614
- // There are preds in configs but they might go away
615
- // when OR'd together like {p}? || NONE == NONE. If neither
616
- // alt has preds, resolve to min alt
617
- dfaState.prediction = altsToCollectPredsFrom.nextset(0);
618
- }
619
- }
620
-
621
- // comes back with reach.uniqueAlt set to a valid alt
622
- int execATNWithFullContext(
623
- DFA dfa,
624
- DFAState D, // how far we got in SLL DFA before failing over
625
- ATNConfigSet s0,
626
- TokenStream input,
627
- int startIndex,
628
- ParserRuleContext outerContext) {
629
- if (debug || debug_list_atn_decisions) {
630
- log('execATNWithFullContext $s0');
631
- }
632
- final fullCtx = true;
633
- var foundExactAmbig = false;
634
- ATNConfigSet reach;
635
- var previous = s0;
636
- input.seek(startIndex);
637
- var t = input.LA(1);
638
- int predictedAlt;
639
- while (true) {
640
- // while more work
641
- // log("LL REACH "+getLookaheadName(input)+
642
- // " from configs.size="+previous.length+
643
- // " line "+input.LT(1).getLine()+":"+input.LT(1).getCharPositionInLine());
644
- reach = computeReachSet(previous, t, fullCtx);
645
- if (reach == null) {
646
- // if any configs in previous dipped into outer context, that
647
- // means that input up to t actually finished entry rule
648
- // at least for LL decision. Full LL doesn't dip into outer
649
- // so don't need special case.
650
- // We will get an error no matter what so delay until after
651
- // decision; better error message. Also, no reachable target
652
- // ATN states in SLL implies LL will also get nowhere.
653
- // If conflict in states that dip out, choose min since we
654
- // will get error no matter what.
655
- final e =
656
- noViableAlt(input, outerContext, previous, startIndex);
657
- input.seek(startIndex);
658
- final alt = getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(
659
- previous, outerContext);
660
- if (alt != ATN.INVALID_ALT_NUMBER) {
661
- return alt;
662
- }
663
- throw e;
664
- }
665
-
666
- final altSubSets =
667
- PredictionModeExtension.getConflictingAltSubsets(reach);
668
- if (debug) {
669
- log('LL altSubSets=$altSubSets' ', predict=${PredictionModeExtension.getUniqueAlt(altSubSets)}' ', resolvesToJustOneViableAlt=${PredictionModeExtension.resolvesToJustOneViableAlt(altSubSets)}');
670
- }
671
-
672
- // log("altSubSets: "+altSubSets);
673
- // log("reach="+reach+", "+reach.conflictingAlts, level: Level.SEVERE.value);
674
- reach.uniqueAlt = getUniqueAlt(reach);
675
- // unique prediction?
676
- if (reach.uniqueAlt != ATN.INVALID_ALT_NUMBER) {
677
- predictedAlt = reach.uniqueAlt;
678
- break;
679
- }
680
- if (predictionMode != PredictionMode.LL_EXACT_AMBIG_DETECTION) {
681
- predictedAlt =
682
- PredictionModeExtension.resolvesToJustOneViableAlt(altSubSets);
683
- if (predictedAlt != ATN.INVALID_ALT_NUMBER) {
684
- break;
685
- }
686
- } else {
687
- // In exact ambiguity mode, we never try to terminate early.
688
- // Just keeps scarfing until we know what the conflict is
689
- if (PredictionModeExtension.allSubsetsConflict(altSubSets) &&
690
- PredictionModeExtension.allSubsetsEqual(altSubSets)) {
691
- foundExactAmbig = true;
692
- predictedAlt = PredictionModeExtension.getSingleViableAlt(altSubSets);
693
- break;
694
- }
695
- // else there are multiple non-conflicting subsets or
696
- // we're not sure what the ambiguity is yet.
697
- // So, keep going.
698
- }
699
-
700
- previous = reach;
701
- if (t != IntStream.EOF) {
702
- input.consume();
703
- t = input.LA(1);
704
- }
705
- }
706
-
707
- // If the configuration set uniquely predicts an alternative,
708
- // without conflict, then we know that it's a full LL decision
709
- // not SLL.
710
- if (reach.uniqueAlt != ATN.INVALID_ALT_NUMBER) {
711
- reportContextSensitivity(
712
- dfa, predictedAlt, reach, startIndex, input.index);
713
- return predictedAlt;
714
- }
715
-
716
- // We do not check predicates here because we have checked them
717
- // on-the-fly when doing full context prediction.
718
-
719
- /*
720
- In non-exact ambiguity detection mode, we might actually be able to
721
- detect an exact ambiguity, but I'm not going to spend the cycles
722
- needed to check. We only emit ambiguity warnings in exact ambiguity
723
- mode.
724
-
725
- For example, we might know that we have conflicting configurations.
726
- But, that does not mean that there is no way forward without a
727
- conflict. It's possible to have nonconflicting alt subsets as in:
728
-
729
- LL altSubSets=[{1, 2}, {1, 2}, {1}, {1, 2}]
730
-
731
- from
732
-
733
- [(17,1,[5 $]), (13,1,[5 10 $]), (21,1,[5 10 $]), (11,1,[$]),
734
- (13,2,[5 10 $]), (21,2,[5 10 $]), (11,2,[$])]
735
-
736
- In this case, (17,1,[5 $]) indicates there is some next sequence that
737
- would resolve this without conflict to alternative 1. Any other viable
738
- next sequence, however, is associated with a conflict. We stop
739
- looking for input because no amount of further lookahead will alter
740
- the fact that we should predict alternative 1. We just can't say for
741
- sure that there is an ambiguity without looking further.
742
- */
743
- reportAmbiguity(dfa, D, startIndex, input.index, foundExactAmbig,
744
- reach.alts, reach);
745
-
746
- return predictedAlt;
747
- }
748
-
749
- ATNConfigSet computeReachSet(ATNConfigSet config, int t, bool fullCtx) {
750
- if (debug) log('in computeReachSet, starting closure: $config');
751
-
752
- mergeCache ??= {};
753
-
754
- final intermediate = ATNConfigSet(fullCtx);
755
-
756
- /* Configurations already in a rule stop state indicate reaching the end
757
- * of the decision rule (local context) or end of the start rule (full
758
- * context). Once reached, these configurations are never updated by a
759
- * closure operation, so they are handled separately for the performance
760
- * advantage of having a smaller intermediate set when calling closure.
761
- *
762
- * For full-context reach operations, separate handling is required to
763
- * ensure that the alternative matching the longest overall sequence is
764
- * chosen when multiple such configurations can match the input.
765
- */
766
- List<ATNConfig> skippedStopStates;
767
-
768
- // First figure out where we can reach on input t
769
- for (var c in config) {
770
- if (debug) log('testing ' + getTokenName(t) + ' at ' + c.toString());
771
-
772
- if (c.state is RuleStopState) {
773
- assert(c.context.isEmpty);
774
- if (fullCtx || t == IntStream.EOF) {
775
- skippedStopStates ??= [];
776
-
777
- skippedStopStates.add(c);
778
- }
779
-
780
- continue;
781
- }
782
-
783
- final n = c.state.numberOfTransitions;
784
- for (var ti = 0; ti < n; ti++) {
785
- // for each transition
786
- final trans = c.state.transition(ti);
787
- final target = getReachableTarget(trans, t);
788
- if (target != null) {
789
- intermediate.add(ATNConfig.dup(c, state: target), mergeCache);
790
- }
791
- }
792
- }
793
-
794
- // Now figure out where the reach operation can take us...
795
-
796
- ATNConfigSet reach;
797
-
798
- /* This block optimizes the reach operation for intermediate sets which
799
- * trivially indicate a termination state for the overall
800
- * adaptivePredict operation.
801
- *
802
- * The conditions assume that intermediate
803
- * contains all configurations relevant to the reach set, but this
804
- * condition is not true when one or more configurations have been
805
- * withheld in skippedStopStates, or when the current symbol is EOF.
806
- */
807
- if (skippedStopStates == null && t != Token.EOF) {
808
- if (intermediate.length == 1) {
809
- // Don't pursue the closure if there is just one state.
810
- // It can only have one alternative; just add to result
811
- // Also don't pursue the closure if there is unique alternative
812
- // among the configurations.
813
- reach = intermediate;
814
- } else if (getUniqueAlt(intermediate) != ATN.INVALID_ALT_NUMBER) {
815
- // Also don't pursue the closure if there is unique alternative
816
- // among the configurations.
817
- reach = intermediate;
818
- }
819
- }
820
-
821
- /* If the reach set could not be trivially determined, perform a closure
822
- * operation on the intermediate set to compute its initial value.
823
- */
824
- if (reach == null) {
825
- reach = ATNConfigSet(fullCtx);
826
- final closureBusy = <ATNConfig>{};
827
- final treatEofAsEpsilon = t == Token.EOF;
828
- for (var c in intermediate) {
829
- closure(c, reach, closureBusy, false, fullCtx, treatEofAsEpsilon);
830
- }
831
- }
832
-
833
- if (t == IntStream.EOF) {
834
- /* After consuming EOF no additional input is possible, so we are
835
- * only interested in configurations which reached the end of the
836
- * decision rule (local context) or end of the start rule (full
837
- * context). Update reach to contain only these configurations. This
838
- * handles both explicit EOF transitions in the grammar and implicit
839
- * EOF transitions following the end of the decision or start rule.
840
- *
841
- * When reach==intermediate, no closure operation was performed. In
842
- * this case, removeAllConfigsNotInRuleStopState needs to check for
843
- * reachable rule stop states as well as configurations already in
844
- * a rule stop state.
845
- *
846
- * This is handled before the configurations in skippedStopStates,
847
- * because any configurations potentially added from that list are
848
- * already guaranteed to meet this condition whether or not it's
849
- * required.
850
- */
851
- reach = removeAllConfigsNotInRuleStopState(reach, reach == intermediate);
852
- }
853
-
854
- /* If skippedStopStates is not null, then it contains at least one
855
- * configuration. For full-context reach operations, these
856
- * configurations reached the end of the start rule, in which case we
857
- * only add them back to reach if no configuration during the current
858
- * closure operation reached such a state. This ensures adaptivePredict
859
- * chooses an alternative matching the longest overall sequence when
860
- * multiple alternatives are viable.
861
- */
862
- if (skippedStopStates != null &&
863
- (!fullCtx ||
864
- !PredictionModeExtension.hasConfigInRuleStopState(reach))) {
865
- assert(skippedStopStates.isNotEmpty);
866
- for (var c in skippedStopStates) {
867
- reach.add(c, mergeCache);
868
- }
869
- }
870
-
871
- if (reach.isEmpty) return null;
872
- return reach;
873
- }
874
-
875
- /// Return a configuration set containing only the configurations from
876
- /// [configs] which are in a [RuleStopState]. If all
877
- /// configurations in [configs] are already in a rule stop state, this
878
- /// method simply returns [configs].
879
- ///
880
- /// <p>When [lookToEndOfRule] is true, this method uses
881
- /// {@link ATN#nextTokens} for each configuration in [configs] which is
882
- /// not already in a rule stop state to see if a rule stop state is reachable
883
- /// from the configuration via epsilon-only transitions.</p>
884
- ///
885
- /// @param configs the configuration set to update
886
- /// @param lookToEndOfRule when true, this method checks for rule stop states
887
- /// reachable by epsilon-only transitions from each configuration in
888
- /// [configs].
889
- ///
890
- /// @return [configs] if all configurations in [configs] are in a
891
- /// rule stop state, otherwise return a new configuration set containing only
892
- /// the configurations from [configs] which are in a rule stop state
893
- ATNConfigSet removeAllConfigsNotInRuleStopState(
894
- ATNConfigSet configs, bool lookToEndOfRule) {
895
- if (PredictionModeExtension.allConfigsInRuleStopStates(configs)) {
896
- return configs;
897
- }
898
-
899
- final result = ATNConfigSet(configs.fullCtx);
900
- for (var config in configs) {
901
- if (config.state is RuleStopState) {
902
- result.add(config, mergeCache);
903
- continue;
904
- }
905
-
906
- if (lookToEndOfRule && config.state.onlyHasEpsilonTransitions()) {
907
- final nextTokens = atn.nextTokens(config.state);
908
- if (nextTokens.contains(Token.EPSILON)) {
909
- ATNState endOfRuleState = atn.ruleToStopState[config.state.ruleIndex];
910
- result.add(
911
- ATNConfig.dup(config, state: endOfRuleState), mergeCache);
912
- }
913
- }
914
- }
915
-
916
- return result;
917
- }
918
-
919
- ATNConfigSet computeStartState(ATNState p, RuleContext ctx, bool fullCtx) {
920
- // always at least the implicit call to start rule
921
- final initialContext =
922
- PredictionContext.fromRuleContext(atn, ctx);
923
- final configs = ATNConfigSet(fullCtx);
924
-
925
- for (var i = 0; i < p.numberOfTransitions; i++) {
926
- final target = p.transition(i).target;
927
- final c = ATNConfig(target, i + 1, initialContext);
928
- final closureBusy = <ATNConfig>{};
929
- closure(c, configs, closureBusy, true, fullCtx, false);
930
- }
931
-
932
- return configs;
933
- }
934
-
935
- /* parrt internal source braindump that doesn't mess up
936
- * external API spec.
937
- context-sensitive in that they can only be properly evaluated
938
- in the context of the proper prec argument. Without pruning,
939
- these predicates are normal predicates evaluated when we reach
940
- conflict state (or unique prediction). As we cannot evaluate
941
- these predicates out of context, the resulting conflict leads
942
- to full LL evaluation and nonlinear prediction which shows up
943
- very clearly with fairly large expressions.
944
-
945
- Example grammar:
946
-
947
- e : e '*' e
948
- | e '+' e
949
- | INT
950
- ;
951
-
952
- We convert that to the following:
953
-
954
- e[int prec]
955
- : INT
956
- ( {3>=prec}? '*' e[4]
957
- | {2>=prec}? '+' e[3]
958
- )*
959
- ;
960
-
961
- The (..)* loop has a decision for the inner block as well as
962
- an enter or exit decision, which is what concerns us here. At
963
- the 1st + of input 1+2+3, the loop entry sees both predicates
964
- and the loop exit also sees both predicates by falling off the
965
- edge of e. This is because we have no stack information with
966
- SLL and find the follow of e, which will hit the return states
967
- inside the loop after e[4] and e[3], which brings it back to
968
- the enter or exit decision. In this case, we know that we
969
- cannot evaluate those predicates because we have fallen off
970
- the edge of the stack and will in general not know which prec
971
- parameter is the right one to use in the predicate.
972
-
973
- Because we have special information, that these are precedence
974
- predicates, we can resolve them without failing over to full
975
- LL despite their context sensitive nature. We make an
976
- assumption that prec[-1] <= prec[0], meaning that the current
977
- precedence level is greater than or equal to the precedence
978
- level of recursive invocations above us in the stack. For
979
- example, if predicate {3>=prec}? is true of the current prec,
980
- then one option is to enter the loop to match it now. The
981
- other option is to exit the loop and the left recursive rule
982
- to match the current operator in rule invocation further up
983
- the stack. But, we know that all of those prec are lower or
984
- the same value and so we can decide to enter the loop instead
985
- of matching it later. That means we can strip out the other
986
- configuration for the exit branch.
987
-
988
- So imagine we have (14,1,$,{2>=prec}?) and then
989
- (14,2,$-dipsIntoOuterContext,{2>=prec}?). The optimization
990
- allows us to collapse these two configurations. We know that
991
- if {2>=prec}? is true for the current prec parameter, it will
992
- also be true for any prec from an invoking e call, indicated
993
- by dipsIntoOuterContext. As the predicates are both true, we
994
- have the option to evaluate them early in the decision start
995
- state. We do this by stripping both predicates and choosing to
996
- enter the loop as it is consistent with the notion of operator
997
- precedence. It's also how the full LL conflict resolution
998
- would work.
999
-
1000
- The solution requires a different DFA start state for each
1001
- precedence level.
1002
-
1003
- The basic filter mechanism is to remove configurations of the
1004
- form (p, 2, pi) if (p, 1, pi) exists for the same p and pi. In
1005
- other words, for the same ATN state and predicate context,
1006
- remove any configuration associated with an exit branch if
1007
- there is a configuration associated with the enter branch.
1008
-
1009
- It's also the case that the filter evaluates precedence
1010
- predicates and resolves conflicts according to precedence
1011
- levels. For example, for input 1+2+3 at the first +, we see
1012
- prediction filtering
1013
-
1014
- [(11,1,[$],{3>=prec}?), (14,1,[$],{2>=prec}?), (5,2,[$],up=1),
1015
- (11,2,[$],up=1), (14,2,[$],up=1)],hasSemanticContext=true,dipsIntoOuterContext
1016
-
1017
- to
1018
-
1019
- [(11,1,[$]), (14,1,[$]), (5,2,[$],up=1)],dipsIntoOuterContext
1020
-
1021
- This filters because {3>=prec}? evals to true and collapses
1022
- (11,1,[$],{3>=prec}?) and (11,2,[$],up=1) since early conflict
1023
- resolution based upon rules of operator precedence fits with
1024
- our usual match first alt upon conflict.
1025
-
1026
- We noticed a problem where a recursive call resets precedence
1027
- to 0. Sam's fix: each config has flag indicating if it has
1028
- returned from an expr[0] call. then just don't filter any
1029
- config with that flag set. flag is carried along in
1030
- closure(). so to avoid adding field, set bit just under sign
1031
- bit of dipsIntoOuterContext (SUPPRESS_PRECEDENCE_FILTER).
1032
- With the change you filter "unless (p, 2, pi) was reached
1033
- after leaving the rule stop state of the LR rule containing
1034
- state p, corresponding to a rule invocation with precedence
1035
- level 0"
1036
- */
1037
-
1038
- /// This method transforms the start state computed by
1039
- /// {@link #computeStartState} to the special start state used by a
1040
- /// precedence DFA for a particular precedence value. The transformation
1041
- /// process applies the following changes to the start state's configuration
1042
- /// set.
1043
- ///
1044
- /// <ol>
1045
- /// <li>Evaluate the precedence predicates for each configuration using
1046
- /// {@link SemanticContext#evalPrecedence}.</li>
1047
- /// <li>When {@link ATNConfig#isPrecedenceFilterSuppressed} is [false],
1048
- /// remove all configurations which predict an alternative greater than 1,
1049
- /// for which another configuration that predicts alternative 1 is in the
1050
- /// same ATN state with the same prediction context. This transformation is
1051
- /// valid for the following reasons:
1052
- /// <ul>
1053
- /// <li>The closure block cannot contain any epsilon transitions which bypass
1054
- /// the body of the closure, so all states reachable via alternative 1 are
1055
- /// part of the precedence alternatives of the transformed left-recursive
1056
- /// rule.</li>
1057
- /// <li>The "primary" portion of a left recursive rule cannot contain an
1058
- /// epsilon transition, so the only way an alternative other than 1 can exist
1059
- /// in a state that is also reachable via alternative 1 is by nesting calls
1060
- /// to the left-recursive rule, with the outer calls not being at the
1061
- /// preferred precedence level. The
1062
- /// {@link ATNConfig#isPrecedenceFilterSuppressed} property marks ATN
1063
- /// configurations which do not meet this condition, and therefore are not
1064
- /// eligible for elimination during the filtering process.</li>
1065
- /// </ul>
1066
- /// </li>
1067
- /// </ol>
1068
- ///
1069
- /// <p>
1070
- /// The prediction context must be considered by this filter to address
1071
- /// situations like the following.
1072
- /// </p>
1073
- /// <code>
1074
- /// <pre>
1075
- /// grammar TA;
1076
- /// prog: statement* EOF;
1077
- /// statement: letterA | statement letterA 'b' ;
1078
- /// letterA: 'a';
1079
- /// </pre>
1080
- /// </code>
1081
- /// <p>
1082
- /// If the above grammar, the ATN state immediately before the token
1083
- /// reference {@code 'a'} in [letterA] is reachable from the left edge
1084
- /// of both the primary and closure blocks of the left-recursive rule
1085
- /// [statement]. The prediction context associated with each of these
1086
- /// configurations distinguishes between them, and prevents the alternative
1087
- /// which stepped out to [prog] (and then back in to [statement]
1088
- /// from being eliminated by the filter.
1089
- /// </p>
1090
- ///
1091
- /// @param configs The configuration set computed by
1092
- /// {@link #computeStartState} as the start state for the DFA.
1093
- /// @return The transformed configuration set representing the start state
1094
- /// for a precedence DFA at a particular precedence level (determined by
1095
- /// calling {@link Parser#getPrecedence}).
1096
- ATNConfigSet applyPrecedenceFilter(ATNConfigSet configs) {
1097
- final statesFromAlt1 = <int, PredictionContext>{};
1098
- final configSet = ATNConfigSet(configs.fullCtx);
1099
- for (var config in configs) {
1100
- // handle alt 1 first
1101
- if (config.alt != 1) {
1102
- continue;
1103
- }
1104
-
1105
- final updatedContext =
1106
- config.semanticContext.evalPrecedence(parser, _outerContext);
1107
- if (updatedContext == null) {
1108
- // the configuration was eliminated
1109
- continue;
1110
- }
1111
-
1112
- statesFromAlt1[config.state.stateNumber] = config.context;
1113
- if (updatedContext != config.semanticContext) {
1114
- configSet.add(
1115
- ATNConfig.dup(config, semanticContext: updatedContext),
1116
- mergeCache);
1117
- } else {
1118
- configSet.add(config, mergeCache);
1119
- }
1120
- }
1121
-
1122
- for (var config in configs) {
1123
- if (config.alt == 1) {
1124
- // already handled
1125
- continue;
1126
- }
1127
-
1128
- if (!config.isPrecedenceFilterSuppressed()) {
1129
- /* In the future, this elimination step could be updated to also
1130
- * filter the prediction context for alternatives predicting alt>1
1131
- * (basically a graph subtraction algorithm).
1132
- */
1133
- final context = statesFromAlt1[config.state.stateNumber];
1134
- if (context != null && context == config.context) {
1135
- // eliminated
1136
- continue;
1137
- }
1138
- }
1139
-
1140
- configSet.add(config, mergeCache);
1141
- }
1142
-
1143
- return configSet;
1144
- }
1145
-
1146
- ATNState getReachableTarget(Transition trans, int ttype) {
1147
- if (trans.matches(ttype, 0, atn.maxTokenType)) {
1148
- return trans.target;
1149
- }
1150
-
1151
- return null;
1152
- }
1153
-
1154
- List<SemanticContext> getPredsForAmbigAlts(
1155
- BitSet ambigAlts, ATNConfigSet configs, int nalts) {
1156
- // REACH=[1|1|[]|0:0, 1|2|[]|0:1]
1157
- /* altToPred starts as an array of all null contexts. The entry at index i
1158
- * corresponds to alternative i. altToPred[i] may have one of three values:
1159
- * 1. null: no ATNConfig c is found such that c.alt==i
1160
- * 2. SemanticContext.NONE: At least one ATNConfig c exists such that
1161
- * c.alt==i and c.semanticContext==SemanticContext.NONE. In other words,
1162
- * alt i has at least one unpredicated config.
1163
- * 3. Non-NONE Semantic Context: There exists at least one, and for all
1164
- * ATNConfig c such that c.alt==i, c.semanticContext!=SemanticContext.NONE.
1165
- *
1166
- * From this, it is clear that NONE||anything==NONE.
1167
- */
1168
- var altToPred = List<SemanticContext>(nalts + 1);
1169
- for (var c in configs) {
1170
- if (ambigAlts[c.alt]) {
1171
- altToPred[c.alt] =
1172
- SemanticContext.or(altToPred[c.alt], c.semanticContext);
1173
- }
1174
- }
1175
-
1176
- var nPredAlts = 0;
1177
- for (var i = 1; i <= nalts; i++) {
1178
- if (altToPred[i] == null) {
1179
- altToPred[i] = SemanticContext.NONE;
1180
- } else if (altToPred[i] != SemanticContext.NONE) {
1181
- nPredAlts++;
1182
- }
1183
- }
1184
-
1185
- // // Optimize away p||p and p&&p TODO: optimize() was a no-op
1186
- // for (int i = 0; i < altToPred.length; i++) {
1187
- // altToPred[i] = altToPred[i].optimize();
1188
- // }
1189
-
1190
- // nonambig alts are null in altToPred
1191
- if (nPredAlts == 0) altToPred = null;
1192
- if (debug) log('getPredsForAmbigAlts result $altToPred');
1193
- return altToPred;
1194
- }
1195
-
1196
- List<PredPrediction> getPredicatePredictions(
1197
- BitSet ambigAlts, List<SemanticContext> altToPred) {
1198
- final pairs = <PredPrediction>[];
1199
- var containsPredicate = false;
1200
- for (var i = 1; i < altToPred.length; i++) {
1201
- final pred = altToPred[i];
1202
-
1203
- // unpredicated is indicated by SemanticContext.NONE
1204
- assert(pred != null);
1205
-
1206
- if (ambigAlts != null && ambigAlts[i]) {
1207
- pairs.add(PredPrediction(pred, i));
1208
- }
1209
- if (pred != SemanticContext.NONE) containsPredicate = true;
1210
- }
1211
-
1212
- if (!containsPredicate) {
1213
- return null;
1214
- }
1215
-
1216
- // log(Arrays.toString(altToPred)+"->"+pairs);
1217
- return pairs;
1218
- }
1219
-
1220
- /// This method is used to improve the localization of error messages by
1221
- /// choosing an alternative rather than throwing a
1222
- /// [NoViableAltException] in particular prediction scenarios where the
1223
- /// {@link #ERROR} state was reached during ATN simulation.
1224
- ///
1225
- /// <p>
1226
- /// The default implementation of this method uses the following
1227
- /// algorithm to identify an ATN configuration which successfully parsed the
1228
- /// decision entry rule. Choosing such an alternative ensures that the
1229
- /// [ParserRuleContext] returned by the calling rule will be complete
1230
- /// and valid, and the syntax error will be reported later at a more
1231
- /// localized location.</p>
1232
- ///
1233
- /// <ul>
1234
- /// <li>If a syntactically valid path or paths reach the end of the decision rule and
1235
- /// they are semantically valid if predicated, return the min associated alt.</li>
1236
- /// <li>Else, if a semantically invalid but syntactically valid path exist
1237
- /// or paths exist, return the minimum associated alt.
1238
- /// </li>
1239
- /// <li>Otherwise, return {@link ATN#INVALID_ALT_NUMBER}.</li>
1240
- /// </ul>
1241
- ///
1242
- /// <p>
1243
- /// In some scenarios, the algorithm described above could predict an
1244
- /// alternative which will result in a [FailedPredicateException] in
1245
- /// the parser. Specifically, this could occur if the <em>only</em> configuration
1246
- /// capable of successfully parsing to the end of the decision rule is
1247
- /// blocked by a semantic predicate. By choosing this alternative within
1248
- /// {@link #adaptivePredict} instead of throwing a
1249
- /// [NoViableAltException], the resulting
1250
- /// [FailedPredicateException] in the parser will identify the specific
1251
- /// predicate which is preventing the parser from successfully parsing the
1252
- /// decision rule, which helps developers identify and correct logic errors
1253
- /// in semantic predicates.
1254
- /// </p>
1255
- ///
1256
- /// @param configs The ATN configurations which were valid immediately before
1257
- /// the {@link #ERROR} state was reached
1258
- /// @param outerContext The is the \gamma_0 initial parser context from the paper
1259
- /// or the parser stack at the instant before prediction commences.
1260
- ///
1261
- /// @return The value to return from {@link #adaptivePredict}, or
1262
- /// {@link ATN#INVALID_ALT_NUMBER} if a suitable alternative was not
1263
- /// identified and {@link #adaptivePredict} should report an error instead.
1264
- int getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(
1265
- ATNConfigSet configs, ParserRuleContext outerContext) {
1266
- final sets =
1267
- splitAccordingToSemanticValidity(configs, outerContext);
1268
- final semValidConfigs = sets.a;
1269
- final semInvalidConfigs = sets.b;
1270
- var alt = getAltThatFinishedDecisionEntryRule(semValidConfigs);
1271
- if (alt != ATN.INVALID_ALT_NUMBER) {
1272
- // semantically/syntactically viable path exists
1273
- return alt;
1274
- }
1275
- // Is there a syntactically valid path with a failed pred?
1276
- if (semInvalidConfigs.isNotEmpty) {
1277
- alt = getAltThatFinishedDecisionEntryRule(semInvalidConfigs);
1278
- if (alt != ATN.INVALID_ALT_NUMBER) {
1279
- // syntactically viable path exists
1280
- return alt;
1281
- }
1282
- }
1283
- return ATN.INVALID_ALT_NUMBER;
1284
- }
1285
-
1286
- int getAltThatFinishedDecisionEntryRule(ATNConfigSet configs) {
1287
- final alts = IntervalSet();
1288
- for (var c in configs) {
1289
- if (c.outerContextDepth > 0 ||
1290
- (c.state is RuleStopState && c.context.hasEmptyPath())) {
1291
- alts.addOne(c.alt);
1292
- }
1293
- }
1294
- if (alts.length == 0) return ATN.INVALID_ALT_NUMBER;
1295
- return alts.minElement;
1296
- }
1297
-
1298
- /// Walk the list of configurations and split them according to
1299
- /// those that have preds evaluating to true/false. If no pred, assume
1300
- /// true pred and include in succeeded set. Returns Pair of sets.
1301
- ///
1302
- /// Create a new set so as not to alter the incoming parameter.
1303
- ///
1304
- /// Assumption: the input stream has been restored to the starting point
1305
- /// prediction, which is where predicates need to evaluate.
1306
- Pair<ATNConfigSet, ATNConfigSet> splitAccordingToSemanticValidity(
1307
- ATNConfigSet configs, ParserRuleContext outerContext) {
1308
- final succeeded = ATNConfigSet(configs.fullCtx);
1309
- final failed = ATNConfigSet(configs.fullCtx);
1310
- for (var c in configs) {
1311
- if (c.semanticContext != SemanticContext.NONE) {
1312
- final predicateEvaluationResult = evalSemanticContextOne(
1313
- c.semanticContext, outerContext, c.alt, configs.fullCtx);
1314
- if (predicateEvaluationResult) {
1315
- succeeded.add(c);
1316
- } else {
1317
- failed.add(c);
1318
- }
1319
- } else {
1320
- succeeded.add(c);
1321
- }
1322
- }
1323
- return Pair<ATNConfigSet, ATNConfigSet>(succeeded, failed);
1324
- }
1325
-
1326
- /// Look through a list of predicate/alt pairs, returning alts for the
1327
- /// pairs that win. A [NONE] predicate indicates an alt containing an
1328
- /// unpredicated config which behaves as "always true." If !complete
1329
- /// then we stop at the first predicate that evaluates to true. This
1330
- /// includes pairs with null predicates.
1331
- BitSet evalSemanticContext(List<PredPrediction> predPredictions,
1332
- ParserRuleContext outerContext, bool complete) {
1333
- final predictions = BitSet();
1334
- for (var pair in predPredictions) {
1335
- if (pair.pred == SemanticContext.NONE) {
1336
- predictions.set(pair.alt);
1337
- if (!complete) {
1338
- break;
1339
- }
1340
- continue;
1341
- }
1342
-
1343
- final fullCtx = false; // in dfa
1344
- final predicateEvaluationResult =
1345
- evalSemanticContextOne(pair.pred, outerContext, pair.alt, fullCtx);
1346
- if (debug || dfa_debug) {
1347
- log('eval pred $pair=$predicateEvaluationResult');
1348
- }
1349
-
1350
- if (predicateEvaluationResult) {
1351
- if (debug || dfa_debug) log('PREDICT ' + pair.alt);
1352
- predictions.set(pair.alt);
1353
- if (!complete) {
1354
- break;
1355
- }
1356
- }
1357
- }
1358
-
1359
- return predictions;
1360
- }
1361
-
1362
- /// Evaluate a semantic context within a specific parser context.
1363
- ///
1364
- /// <p>
1365
- /// This method might not be called for every semantic context evaluated
1366
- /// during the prediction process. In particular, we currently do not
1367
- /// evaluate the following but it may change in the future:</p>
1368
- ///
1369
- /// <ul>
1370
- /// <li>Precedence predicates (represented by
1371
- /// {@link SemanticContext.PrecedencePredicate}) are not currently evaluated
1372
- /// through this method.</li>
1373
- /// <li>Operator predicates (represented by {@link SemanticContext.AND} and
1374
- /// {@link SemanticContext.OR}) are evaluated as a single semantic
1375
- /// context, rather than evaluating the operands individually.
1376
- /// Implementations which require evaluation results from individual
1377
- /// predicates should override this method to explicitly handle evaluation of
1378
- /// the operands within operator predicates.</li>
1379
- /// </ul>
1380
- ///
1381
- /// @param pred The semantic context to evaluate
1382
- /// @param parserCallStack The parser context in which to evaluate the
1383
- /// semantic context
1384
- /// @param alt The alternative which is guarded by [pred]
1385
- /// @param fullCtx [true] if the evaluation is occurring during LL
1386
- /// prediction; otherwise, [false] if the evaluation is occurring
1387
- /// during SLL prediction
1388
- ///
1389
- /// @since 4.3
1390
- bool evalSemanticContextOne(SemanticContext pred,
1391
- ParserRuleContext parserCallStack, int alt, bool fullCtx) {
1392
- return pred.eval(parser, parserCallStack);
1393
- }
1394
-
1395
- /* TODO: If we are doing predicates, there is no point in pursuing
1396
- closure operations if we reach a DFA state that uniquely predicts
1397
- alternative. We will not be caching that DFA state and it is a
1398
- waste to pursue the closure. Might have to advance when we do
1399
- ambig detection thought :(
1400
- */
1401
-
1402
- void closure(
1403
- ATNConfig config,
1404
- ATNConfigSet configs,
1405
- Set<ATNConfig> closureBusy,
1406
- bool collectPredicates,
1407
- bool fullCtx,
1408
- bool treatEofAsEpsilon) {
1409
- final initialDepth = 0;
1410
- closureCheckingStopState(config, configs, closureBusy, collectPredicates,
1411
- fullCtx, initialDepth, treatEofAsEpsilon);
1412
- assert(!fullCtx || !configs.dipsIntoOuterContext);
1413
- }
1414
-
1415
- void closureCheckingStopState(
1416
- ATNConfig config,
1417
- ATNConfigSet configs,
1418
- Set<ATNConfig> closureBusy,
1419
- bool collectPredicates,
1420
- bool fullCtx,
1421
- int depth,
1422
- bool treatEofAsEpsilon) {
1423
- if (debug) log('closure(' + config.toString(parser, true) + ')');
1424
-
1425
- if (config.state is RuleStopState) {
1426
- // We hit rule end. If we have context info, use it
1427
- // run thru all possible stack tops in ctx
1428
- if (!config.context.isEmpty) {
1429
- for (var i = 0; i < config.context.length; i++) {
1430
- if (config.context.getReturnState(i) ==
1431
- PredictionContext.EMPTY_RETURN_STATE) {
1432
- if (fullCtx) {
1433
- configs.add(
1434
- ATNConfig.dup(config,
1435
- state: config.state, context: PredictionContext.EMPTY),
1436
- mergeCache);
1437
- continue;
1438
- } else {
1439
- // we have no context info, just chase follow links (if greedy)
1440
- if (debug) {
1441
- log('FALLING off rule ' + getRuleName(config.state.ruleIndex));
1442
- }
1443
- closure_(config, configs, closureBusy, collectPredicates, fullCtx,
1444
- depth, treatEofAsEpsilon);
1445
- }
1446
- continue;
1447
- }
1448
- final returnState = atn.states[config.context.getReturnState(i)];
1449
- final newContext =
1450
- config.context.getParent(i); // "pop" return state
1451
- final c = ATNConfig(
1452
- returnState, config.alt, newContext, config.semanticContext);
1453
- // While we have context to pop back from, we may have
1454
- // gotten that context AFTER having falling off a rule.
1455
- // Make sure we track that we are now out of context.
1456
- //
1457
- // This assignment also propagates the
1458
- // isPrecedenceFilterSuppressed() value to the new
1459
- // configuration.
1460
- c.reachesIntoOuterContext = config.reachesIntoOuterContext;
1461
- // assert(depth > int.MIN_VALUE);
1462
- closureCheckingStopState(c, configs, closureBusy, collectPredicates,
1463
- fullCtx, depth - 1, treatEofAsEpsilon);
1464
- }
1465
- return;
1466
- } else if (fullCtx) {
1467
- // reached end of start rule
1468
- configs.add(config, mergeCache);
1469
- return;
1470
- } else {
1471
- // else if we have no context info, just chase follow links (if greedy)
1472
- if (debug) {
1473
- log('FALLING off rule ' + getRuleName(config.state.ruleIndex));
1474
- }
1475
- }
1476
- }
1477
-
1478
- closure_(config, configs, closureBusy, collectPredicates, fullCtx, depth,
1479
- treatEofAsEpsilon);
1480
- }
1481
-
1482
- /// Do the actual work of walking epsilon edges */
1483
- void closure_(
1484
- ATNConfig config,
1485
- ATNConfigSet configs,
1486
- Set<ATNConfig> closureBusy,
1487
- bool collectPredicates,
1488
- bool fullCtx,
1489
- int depth,
1490
- bool treatEofAsEpsilon) {
1491
- final p = config.state;
1492
- // optimization
1493
- if (!p.onlyHasEpsilonTransitions()) {
1494
- configs.add(config, mergeCache);
1495
- // make sure to not return here, because EOF transitions can act as
1496
- // both epsilon transitions and non-epsilon transitions.
1497
- // if ( debug ) log("added config "+configs);
1498
- }
1499
-
1500
- for (var i = 0; i < p.numberOfTransitions; i++) {
1501
- if (i == 0 && canDropLoopEntryEdgeInLeftRecursiveRule(config)) continue;
1502
-
1503
- final t = p.transition(i);
1504
- final continueCollecting = !(t is ActionTransition) && collectPredicates;
1505
- final c = getEpsilonTarget(config, t, continueCollecting, depth == 0,
1506
- fullCtx, treatEofAsEpsilon);
1507
- if (c != null) {
1508
- var newDepth = depth;
1509
- if (config.state is RuleStopState) {
1510
- assert(!fullCtx);
1511
- // target fell off end of rule; mark resulting c as having dipped into outer context
1512
- // We can't get here if incoming config was rule stop and we had context
1513
- // track how far we dip into outer context. Might
1514
- // come in handy and we avoid evaluating context dependent
1515
- // preds if this is > 0.
1516
-
1517
- if (_dfa != null && _dfa.isPrecedenceDfa()) {
1518
- final outermostPrecedenceReturn =
1519
- (t as EpsilonTransition).outermostPrecedenceReturn;
1520
- if (outermostPrecedenceReturn == _dfa.atnStartState.ruleIndex) {
1521
- c.setPrecedenceFilterSuppressed(true);
1522
- }
1523
- }
1524
-
1525
- c.reachesIntoOuterContext++;
1526
-
1527
- if (!closureBusy.add(c)) {
1528
- // avoid infinite recursion for right-recursive rules
1529
- continue;
1530
- }
1531
-
1532
- // TODO: can remove? only care when we add to set per middle of this method
1533
- configs.dipsIntoOuterContext = true;
1534
- // assert(newDepth > int.MIN_VALUE);
1535
- newDepth--;
1536
- if (debug) log('dips into outer ctx: $c');
1537
- } else {
1538
- if (!t.isEpsilon && !closureBusy.add(c)) {
1539
- // avoid infinite recursion for EOF* and EOF+
1540
- continue;
1541
- }
1542
-
1543
- if (t is RuleTransition) {
1544
- // latch when newDepth goes negative - once we step out of the entry context we can't return
1545
- if (newDepth >= 0) {
1546
- newDepth++;
1547
- }
1548
- }
1549
- }
1550
-
1551
- closureCheckingStopState(c, configs, closureBusy, continueCollecting,
1552
- fullCtx, newDepth, treatEofAsEpsilon);
1553
- }
1554
- }
1555
- }
1556
-
1557
- /// Implements first-edge (loop entry) elimination as an optimization
1558
- /// during closure operations. See antlr/antlr4#1398.
1559
- ///
1560
- /// The optimization is to avoid adding the loop entry config when
1561
- /// the exit path can only lead back to the same
1562
- /// StarLoopEntryState after popping context at the rule end state
1563
- /// (traversing only epsilon edges, so we're still in closure, in
1564
- /// this same rule).
1565
- ///
1566
- /// We need to detect any state that can reach loop entry on
1567
- /// epsilon w/o exiting rule. We don't have to look at FOLLOW
1568
- /// links, just ensure that all stack tops for config refer to key
1569
- /// states in LR rule.
1570
- ///
1571
- /// To verify we are in the right situation we must first check
1572
- /// closure is at a StarLoopEntryState generated during LR removal.
1573
- /// Then we check that each stack top of context is a return state
1574
- /// from one of these cases:
1575
- ///
1576
- /// 1. 'not' expr, '(' type ')' expr. The return state points at loop entry state
1577
- /// 2. expr op expr. The return state is the block end of internal block of (...)*
1578
- /// 3. 'between' expr 'and' expr. The return state of 2nd expr reference.
1579
- /// That state points at block end of internal block of (...)*.
1580
- /// 4. expr '?' expr ':' expr. The return state points at block end,
1581
- /// which points at loop entry state.
1582
- ///
1583
- /// If any is true for each stack top, then closure does not add a
1584
- /// config to the current config set for edge[0], the loop entry branch.
1585
- ///
1586
- /// Conditions fail if any context for the current config is:
1587
- ///
1588
- /// a. empty (we'd fall out of expr to do a global FOLLOW which could
1589
- /// even be to some weird spot in expr) or,
1590
- /// b. lies outside of expr or,
1591
- /// c. lies within expr but at a state not the BlockEndState
1592
- /// generated during LR removal
1593
- ///
1594
- /// Do we need to evaluate predicates ever in closure for this case?
1595
- ///
1596
- /// No. Predicates, including precedence predicates, are only
1597
- /// evaluated when computing a DFA start state. I.e., only before
1598
- /// the lookahead (but not parser) consumes a token.
1599
- ///
1600
- /// There are no epsilon edges allowed in LR rule alt blocks or in
1601
- /// the "primary" part (ID here). If closure is in
1602
- /// StarLoopEntryState any lookahead operation will have consumed a
1603
- /// token as there are no epsilon-paths that lead to
1604
- /// StarLoopEntryState. We do not have to evaluate predicates
1605
- /// therefore if we are in the generated StarLoopEntryState of a LR
1606
- /// rule. Note that when making a prediction starting at that
1607
- /// decision point, decision d=2, compute-start-state performs
1608
- /// closure starting at edges[0], edges[1] emanating from
1609
- /// StarLoopEntryState. That means it is not performing closure on
1610
- /// StarLoopEntryState during compute-start-state.
1611
- ///
1612
- /// How do we know this always gives same prediction answer?
1613
- ///
1614
- /// Without predicates, loop entry and exit paths are ambiguous
1615
- /// upon remaining input +b (in, say, a+b). Either paths lead to
1616
- /// valid parses. Closure can lead to consuming + immediately or by
1617
- /// falling out of this call to expr back into expr and loop back
1618
- /// again to StarLoopEntryState to match +b. In this special case,
1619
- /// we choose the more efficient path, which is to take the bypass
1620
- /// path.
1621
- ///
1622
- /// The lookahead language has not changed because closure chooses
1623
- /// one path over the other. Both paths lead to consuming the same
1624
- /// remaining input during a lookahead operation. If the next token
1625
- /// is an operator, lookahead will enter the choice block with
1626
- /// operators. If it is not, lookahead will exit expr. Same as if
1627
- /// closure had chosen to enter the choice block immediately.
1628
- ///
1629
- /// Closure is examining one config (some loopentrystate, some alt,
1630
- /// context) which means it is considering exactly one alt. Closure
1631
- /// always copies the same alt to any derived configs.
1632
- ///
1633
- /// How do we know this optimization doesn't mess up precedence in
1634
- /// our parse trees?
1635
- ///
1636
- /// Looking through expr from left edge of stat only has to confirm
1637
- /// that an input, say, a+b+c; begins with any valid interpretation
1638
- /// of an expression. The precedence actually doesn't matter when
1639
- /// making a decision in stat seeing through expr. It is only when
1640
- /// parsing rule expr that we must use the precedence to get the
1641
- /// right interpretation and, hence, parse tree.
1642
- ///
1643
- /// @since 4.6
1644
- bool canDropLoopEntryEdgeInLeftRecursiveRule(ATNConfig config) {
1645
- if (TURN_OFF_LR_LOOP_ENTRY_BRANCH_OPT) return false;
1646
- final p = config.state;
1647
- // First check to see if we are in StarLoopEntryState generated during
1648
- // left-recursion elimination. For efficiency, also check if
1649
- // the context has an empty stack case. If so, it would mean
1650
- // global FOLLOW so we can't perform optimization
1651
- if (p.stateType != StateType.STAR_LOOP_ENTRY ||
1652
- !(p as StarLoopEntryState)
1653
- .isPrecedenceDecision || // Are we the special loop entry/exit state?
1654
- config.context.isEmpty || // If SLL wildcard
1655
- config.context.hasEmptyPath()) {
1656
- return false;
1657
- }
1658
-
1659
- // Require all return states to return back to the same rule
1660
- // that p is in.
1661
- final numCtxs = config.context.length;
1662
- for (var i = 0; i < numCtxs; i++) {
1663
- // for each stack context
1664
- final returnState = atn.states[config.context.getReturnState(i)];
1665
- if (returnState.ruleIndex != p.ruleIndex) return false;
1666
- }
1667
-
1668
- BlockStartState decisionStartState = p.transition(0).target;
1669
- final blockEndStateNum = decisionStartState.endState.stateNumber;
1670
- BlockEndState blockEndState = atn.states[blockEndStateNum];
1671
-
1672
- // Verify that the top of each stack context leads to loop entry/exit
1673
- // state through epsilon edges and w/o leaving rule.
1674
- for (var i = 0; i < numCtxs; i++) {
1675
- // for each stack context
1676
- final returnStateNumber = config.context.getReturnState(i);
1677
- final returnState = atn.states[returnStateNumber];
1678
- // all states must have single outgoing epsilon edge
1679
- if (returnState.numberOfTransitions != 1 ||
1680
- !returnState.transition(0).isEpsilon) {
1681
- return false;
1682
- }
1683
- // Look for prefix op case like 'not expr', (' type ')' expr
1684
- final returnStateTarget = returnState.transition(0).target;
1685
- if (returnState.stateType == StateType.BLOCK_END &&
1686
- returnStateTarget == p) {
1687
- continue;
1688
- }
1689
- // Look for 'expr op expr' or case where expr's return state is block end
1690
- // of (...)* internal block; the block end points to loop back
1691
- // which points to p but we don't need to check that
1692
- if (returnState == blockEndState) {
1693
- continue;
1694
- }
1695
- // Look for ternary expr ? expr : expr. The return state points at block end,
1696
- // which points at loop entry state
1697
- if (returnStateTarget == blockEndState) {
1698
- continue;
1699
- }
1700
- // Look for complex prefix 'between expr and expr' case where 2nd expr's
1701
- // return state points at block end state of (...)* internal block
1702
- if (returnStateTarget.stateType == StateType.BLOCK_END &&
1703
- returnStateTarget.numberOfTransitions == 1 &&
1704
- returnStateTarget.transition(0).isEpsilon &&
1705
- returnStateTarget.transition(0).target == p) {
1706
- continue;
1707
- }
1708
-
1709
- // anything else ain't conforming
1710
- return false;
1711
- }
1712
-
1713
- return true;
1714
- }
1715
-
1716
- String getRuleName(int index) {
1717
- if (parser != null && index >= 0) return parser.ruleNames[index];
1718
- return '<rule $index>';
1719
- }
1720
-
1721
- ATNConfig getEpsilonTarget(
1722
- ATNConfig config,
1723
- Transition t,
1724
- bool collectPredicates,
1725
- bool inContext,
1726
- bool fullCtx,
1727
- bool treatEofAsEpsilon) {
1728
- switch (t.type) {
1729
- case TransitionType.RULE:
1730
- return ruleTransition(config, t);
1731
-
1732
- case TransitionType.PRECEDENCE:
1733
- return precedenceTransition(
1734
- config, t, collectPredicates, inContext, fullCtx);
1735
-
1736
- case TransitionType.PREDICATE:
1737
- return predTransition(config, t, collectPredicates, inContext, fullCtx);
1738
- case TransitionType.ACTION:
1739
- return actionTransition(config, t);
1740
-
1741
- case TransitionType.EPSILON:
1742
- return ATNConfig.dup(config, state: t.target);
1743
-
1744
- case TransitionType.ATOM:
1745
- case TransitionType.RANGE:
1746
- case TransitionType.SET:
1747
- // EOF transitions act like epsilon transitions after the first EOF
1748
- // transition is traversed
1749
- if (treatEofAsEpsilon) {
1750
- if (t.matches(Token.EOF, 0, 1)) {
1751
- return ATNConfig.dup(config, state: t.target);
1752
- }
1753
- }
1754
-
1755
- return null;
1756
-
1757
- default:
1758
- return null;
1759
- }
1760
- }
1761
-
1762
- ATNConfig actionTransition(ATNConfig config, ActionTransition t) {
1763
- if (debug) log('ACTION edge ${t.ruleIndex}:${t.actionIndex}');
1764
- return ATNConfig.dup(config, state: t.target);
1765
- }
1766
-
1767
- ATNConfig precedenceTransition(
1768
- ATNConfig config,
1769
- PrecedencePredicateTransition pt,
1770
- bool collectPredicates,
1771
- bool inContext,
1772
- bool fullCtx) {
1773
- if (debug) {
1774
- log('PRED (collectPredicates=$collectPredicates) ${pt.precedence}>=_p, ctx dependent=true');
1775
- if (parser != null) {
1776
- log('context surrounding pred is ${parser.getRuleInvocationStack()}');
1777
- }
1778
- }
1779
-
1780
- ATNConfig c;
1781
- if (collectPredicates && inContext) {
1782
- if (fullCtx) {
1783
- // In full context mode, we can evaluate predicates on-the-fly
1784
- // during closure, which dramatically reduces the size of
1785
- // the config sets. It also obviates the need to test predicates
1786
- // later during conflict resolution.
1787
- final currentPosition = input.index;
1788
- input.seek(startIndex);
1789
- final predSucceeds = evalSemanticContextOne(
1790
- pt.predicate, _outerContext, config.alt, fullCtx);
1791
- input.seek(currentPosition);
1792
- if (predSucceeds) {
1793
- c = ATNConfig.dup(config, state: pt.target); // no pred context
1794
- }
1795
- } else {
1796
- final newSemCtx =
1797
- SemanticContext.and(config.semanticContext, pt.predicate);
1798
- c = ATNConfig.dup(config,
1799
- state: pt.target, semanticContext: newSemCtx);
1800
- }
1801
- } else {
1802
- c = ATNConfig.dup(config, state: pt.target);
1803
- }
1804
-
1805
- if (debug) log('config from pred transition=$c');
1806
- return c;
1807
- }
1808
-
1809
- ATNConfig predTransition(ATNConfig config, PredicateTransition pt,
1810
- bool collectPredicates, bool inContext, bool fullCtx) {
1811
- if (debug) {
1812
- log('PRED (collectPredicates=$collectPredicates) ' '${pt.ruleIndex}:${pt.predIndex}' ', ctx dependent=${pt.isCtxDependent}');
1813
- if (parser != null) {
1814
- log('context surrounding pred is ${parser.getRuleInvocationStack()}');
1815
- }
1816
- }
1817
-
1818
- ATNConfig c;
1819
- if (collectPredicates &&
1820
- (!pt.isCtxDependent || (pt.isCtxDependent && inContext))) {
1821
- if (fullCtx) {
1822
- // In full context mode, we can evaluate predicates on-the-fly
1823
- // during closure, which dramatically reduces the size of
1824
- // the config sets. It also obviates the need to test predicates
1825
- // later during conflict resolution.
1826
- final currentPosition = input.index;
1827
- input.seek(startIndex);
1828
- final predSucceeds = evalSemanticContextOne(
1829
- pt.predicate, _outerContext, config.alt, fullCtx);
1830
- input.seek(currentPosition);
1831
- if (predSucceeds) {
1832
- c = ATNConfig.dup(config, state: pt.target); // no pred context
1833
- }
1834
- } else {
1835
- final newSemCtx =
1836
- SemanticContext.and(config.semanticContext, pt.predicate);
1837
- c = ATNConfig.dup(config,
1838
- state: pt.target, semanticContext: newSemCtx);
1839
- }
1840
- } else {
1841
- c = ATNConfig.dup(config, state: pt.target);
1842
- }
1843
-
1844
- if (debug) log('config from pred transition=$c');
1845
- return c;
1846
- }
1847
-
1848
- ATNConfig ruleTransition(ATNConfig config, RuleTransition t) {
1849
- if (debug) {
1850
- log('CALL rule ' +
1851
- getRuleName(t.target.ruleIndex) +
1852
- ', ctx=${config.context}');
1853
- }
1854
-
1855
- final returnState = t.followState;
1856
- PredictionContext newContext = SingletonPredictionContext.create(
1857
- config.context, returnState.stateNumber);
1858
- return ATNConfig.dup(config, state: t.target, context: newContext);
1859
- }
1860
-
1861
- /// Gets a [BitSet] containing the alternatives in [configs]
1862
- /// which are part of one or more conflicting alternative subsets.
1863
- ///
1864
- /// @param configs The [ATNConfigSet] to analyze.
1865
- /// @return The alternatives in [configs] which are part of one or more
1866
- /// conflicting alternative subsets. If [configs] does not contain any
1867
- /// conflicting subsets, this method returns an empty [BitSet].
1868
- BitSet getConflictingAlts(ATNConfigSet configs) {
1869
- final altsets =
1870
- PredictionModeExtension.getConflictingAltSubsets(configs);
1871
- return PredictionModeExtension.getAlts(altsets);
1872
- }
1873
-
1874
- /// Sam pointed out a problem with the previous definition, v3, of
1875
- /// ambiguous states. If we have another state associated with conflicting
1876
- /// alternatives, we should keep going. For example, the following grammar
1877
- ///
1878
- /// s : (ID | ID ID?) ';' ;
1879
- ///
1880
- /// When the ATN simulation reaches the state before ';', it has a DFA
1881
- /// state that looks like: [12|1|[], 6|2|[], 12|2|[]]. Naturally
1882
- /// 12|1|[] and 12|2|[] conflict, but we cannot stop processing this node
1883
- /// because alternative to has another way to continue, via [6|2|[]].
1884
- /// The key is that we have a single state that has config's only associated
1885
- /// with a single alternative, 2, and crucially the state transitions
1886
- /// among the configurations are all non-epsilon transitions. That means
1887
- /// we don't consider any conflicts that include alternative 2. So, we
1888
- /// ignore the conflict between alts 1 and 2. We ignore a set of
1889
- /// conflicting alts when there is an intersection with an alternative
1890
- /// associated with a single alt state in the state&rarr;config-list map.
1891
- ///
1892
- /// It's also the case that we might have two conflicting configurations but
1893
- /// also a 3rd nonconflicting configuration for a different alternative:
1894
- /// [1|1|[], 1|2|[], 8|3|[]]. This can come about from grammar:
1895
- ///
1896
- /// a : A | A | A B ;
1897
- ///
1898
- /// After matching input A, we reach the stop state for rule A, state 1.
1899
- /// State 8 is the state right before B. Clearly alternatives 1 and 2
1900
- /// conflict and no amount of further lookahead will separate the two.
1901
- /// However, alternative 3 will be able to continue and so we do not
1902
- /// stop working on this state. In the previous example, we're concerned
1903
- /// with states associated with the conflicting alternatives. Here alt
1904
- /// 3 is not associated with the conflicting configs, but since we can continue
1905
- /// looking for input reasonably, I don't declare the state done. We
1906
- /// ignore a set of conflicting alts when we have an alternative
1907
- /// that we still need to pursue.
1908
- BitSet getConflictingAltsOrUniqueAlt(ATNConfigSet configs) {
1909
- BitSet conflictingAlts;
1910
- if (configs.uniqueAlt != ATN.INVALID_ALT_NUMBER) {
1911
- conflictingAlts = BitSet();
1912
- conflictingAlts.set(configs.uniqueAlt);
1913
- } else {
1914
- conflictingAlts = configs.conflictingAlts;
1915
- }
1916
- return conflictingAlts;
1917
- }
1918
-
1919
- String getTokenName(int t) {
1920
- if (t == Token.EOF) {
1921
- return 'EOF';
1922
- }
1923
-
1924
- final vocabulary = parser != null
1925
- ? parser.vocabulary
1926
- : VocabularyImpl.EMPTY_VOCABULARY;
1927
- final displayName = vocabulary.getDisplayName(t);
1928
- if (displayName == t.toString()) {
1929
- return displayName;
1930
- }
1931
-
1932
- return displayName + '<$t>';
1933
- }
1934
-
1935
- String getLookaheadName(TokenStream input) {
1936
- return getTokenName(input.LA(1));
1937
- }
1938
-
1939
- /// Used for debugging in adaptivePredict around execATN but I cut
1940
- /// it out for clarity now that alg. works well. We can leave this
1941
- /// "dead" code for a bit.
1942
- void dumpDeadEndConfigs(NoViableAltException nvae) {
1943
- log('dead end configs: ', level: Level.SEVERE.value);
1944
- for (var c in nvae.deadEndConfigs) {
1945
- var trans = 'no edges';
1946
- if (c.state.numberOfTransitions > 0) {
1947
- final t = c.state.transition(0);
1948
- if (t is AtomTransition) {
1949
- final at = t;
1950
- trans = 'Atom ' + getTokenName(at.atomLabel);
1951
- } else if (t is SetTransition) {
1952
- final st = t;
1953
- final not = st is NotSetTransition;
1954
- trans = (not ? '~' : '') + 'Set ' + st.label.toString();
1955
- }
1956
- }
1957
- log(c.toString(parser, true) + ':' + trans, level: Level.SEVERE.value);
1958
- }
1959
- }
1960
-
1961
- NoViableAltException noViableAlt(TokenStream input,
1962
- ParserRuleContext outerContext, ATNConfigSet configs, int startIndex) {
1963
- return NoViableAltException(parser, input, input.get(startIndex),
1964
- input.LT(1), configs, outerContext);
1965
- }
1966
-
1967
- static int getUniqueAlt(ATNConfigSet configs) {
1968
- var alt = ATN.INVALID_ALT_NUMBER;
1969
- for (var c in configs) {
1970
- if (alt == ATN.INVALID_ALT_NUMBER) {
1971
- alt = c.alt; // found first alt
1972
- } else if (c.alt != alt) {
1973
- return ATN.INVALID_ALT_NUMBER;
1974
- }
1975
- }
1976
- return alt;
1977
- }
1978
-
1979
- /// Add an edge to the DFA, if possible. This method calls
1980
- /// {@link #addDFAState} to ensure the [to] state is present in the
1981
- /// DFA. If [from] is null, or if [t] is outside the
1982
- /// range of edges that can be represented in the DFA tables, this method
1983
- /// returns without adding the edge to the DFA.
1984
- ///
1985
- /// <p>If [to] is null, this method returns null.
1986
- /// Otherwise, this method returns the [DFAState] returned by calling
1987
- /// {@link #addDFAState} for the [to] state.</p>
1988
- ///
1989
- /// @param dfa The DFA
1990
- /// @param from The source state for the edge
1991
- /// @param t The input symbol
1992
- /// @param to The target state for the edge
1993
- ///
1994
- /// @return If [to] is null, this method returns null;
1995
- /// otherwise this method returns the result of calling {@link #addDFAState}
1996
- /// on [to]
1997
- DFAState addDFAEdge(DFA dfa, DFAState from, int t, DFAState to) {
1998
- if (debug) {
1999
- log('EDGE $from -> $to upon ' + getTokenName(t));
2000
- }
2001
-
2002
- if (to == null) {
2003
- return null;
2004
- }
2005
-
2006
- to = addDFAState(dfa, to); // used existing if possible not incoming
2007
- if (from == null || t < -1 || t > atn.maxTokenType) {
2008
- return to;
2009
- }
2010
-
2011
- from.edges ??= List(atn.maxTokenType + 1 + 1);
2012
-
2013
- from.edges[t + 1] = to; // connect
2014
-
2015
- if (debug) {
2016
- log('DFA=\n' +
2017
- dfa.toString(parser != null
2018
- ? parser.vocabulary
2019
- : VocabularyImpl.EMPTY_VOCABULARY));
2020
- }
2021
-
2022
- return to;
2023
- }
2024
-
2025
- /// Add state [D] to the DFA if it is not already present, and return
2026
- /// the actual instance stored in the DFA. If a state equivalent to [D]
2027
- /// is already in the DFA, the existing state is returned. Otherwise this
2028
- /// method returns [D] after adding it to the DFA.
2029
- ///
2030
- /// <p>If [D] is {@link #ERROR}, this method returns {@link #ERROR} and
2031
- /// does not change the DFA.</p>
2032
- ///
2033
- /// @param dfa The dfa
2034
- /// @param D The DFA state to add
2035
- /// @return The state stored in the DFA. This will be either the existing
2036
- /// state if [D] is already in the DFA, or [D] itself if the
2037
- /// state was not already present.
2038
- DFAState addDFAState(DFA dfa, DFAState D) {
2039
- if (D == ATNSimulator.ERROR) {
2040
- return D;
2041
- }
2042
-
2043
- final existing = dfa.states[D];
2044
- if (existing != null) return existing;
2045
-
2046
- D.stateNumber = dfa.states.length;
2047
- if (!D.configs.readOnly) {
2048
- D.configs.optimizeConfigs(this);
2049
- D.configs.readOnly = true;
2050
- }
2051
- dfa.states[D] = D;
2052
- if (debug) log('adding new DFA state: $D');
2053
- return D;
2054
- }
2055
-
2056
- void reportAttemptingFullContext(DFA dfa, BitSet conflictingAlts,
2057
- ATNConfigSet configs, int startIndex, int stopIndex) {
2058
- if (debug || retry_debug) {
2059
- final interval = Interval.of(startIndex, stopIndex);
2060
- log('reportAttemptingFullContext decision=${dfa.decision}:$configs' ', input=' +
2061
- parser.tokenStream.getText(interval));
2062
- }
2063
- if (parser != null) {
2064
- parser.errorListenerDispatch.reportAttemptingFullContext(
2065
- parser, dfa, startIndex, stopIndex, conflictingAlts, configs);
2066
- }
2067
- }
2068
-
2069
- void reportContextSensitivity(DFA dfa, int prediction, ATNConfigSet configs,
2070
- int startIndex, int stopIndex) {
2071
- if (debug || retry_debug) {
2072
- final interval = Interval.of(startIndex, stopIndex);
2073
- log('reportContextSensitivity decision=${dfa.decision}:$configs' ', input=' +
2074
- parser.tokenStream.getText(interval));
2075
- }
2076
- if (parser != null) {
2077
- parser.errorListenerDispatch.reportContextSensitivity(
2078
- parser, dfa, startIndex, stopIndex, prediction, configs);
2079
- }
2080
- }
2081
-
2082
- /// If context sensitive parsing, we know it's ambiguity not conflict */
2083
- void reportAmbiguity(
2084
- DFA dfa,
2085
- DFAState D, // the DFA state from execATN() that had SLL conflicts
2086
- int startIndex,
2087
- int stopIndex,
2088
- bool exact,
2089
- BitSet ambigAlts,
2090
- ATNConfigSet configs) // configs that LL not SLL considered conflicting
2091
- {
2092
- if (debug || retry_debug) {
2093
- final interval = Interval.of(startIndex, stopIndex);
2094
- log('reportAmbiguity $ambigAlts:$configs' ', input=' +
2095
- parser.tokenStream.getText(interval));
2096
- }
2097
- if (parser != null) {
2098
- parser.errorListenerDispatch.reportAmbiguity(
2099
- parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs);
2100
- }
2101
- }
2102
- }
2103
-
2104
- /// This enumeration defines the prediction modes available in ANTLR 4 along with
2105
- /// utility methods for analyzing configuration sets for conflicts and/or
2106
- /// ambiguities.
2107
- enum PredictionMode {
2108
- /// The SLL(*) prediction mode. This prediction mode ignores the current
2109
- /// parser context when making predictions. This is the fastest prediction
2110
- /// mode, and provides correct results for many grammars. This prediction
2111
- /// mode is more powerful than the prediction mode provided by ANTLR 3, but
2112
- /// may result in syntax errors for grammar and input combinations which are
2113
- /// not SLL.
2114
- ///
2115
- /// <p>
2116
- /// When using this prediction mode, the parser will either return a correct
2117
- /// parse tree (i.e. the same parse tree that would be returned with the
2118
- /// {@link #LL} prediction mode), or it will report a syntax error. If a
2119
- /// syntax error is encountered when using the {@link #SLL} prediction mode,
2120
- /// it may be due to either an actual syntax error in the input or indicate
2121
- /// that the particular combination of grammar and input requires the more
2122
- /// powerful {@link #LL} prediction abilities to complete successfully.</p>
2123
- ///
2124
- /// <p>
2125
- /// This prediction mode does not provide any guarantees for prediction
2126
- /// behavior for syntactically-incorrect inputs.</p>
2127
- SLL,
2128
- /// The LL(*) prediction mode. This prediction mode allows the current parser
2129
- /// context to be used for resolving SLL conflicts that occur during
2130
- /// prediction. This is the fastest prediction mode that guarantees correct
2131
- /// parse results for all combinations of grammars with syntactically correct
2132
- /// inputs.
2133
- ///
2134
- /// <p>
2135
- /// When using this prediction mode, the parser will make correct decisions
2136
- /// for all syntactically-correct grammar and input combinations. However, in
2137
- /// cases where the grammar is truly ambiguous this prediction mode might not
2138
- /// report a precise answer for <em>exactly which</em> alternatives are
2139
- /// ambiguous.</p>
2140
- ///
2141
- /// <p>
2142
- /// This prediction mode does not provide any guarantees for prediction
2143
- /// behavior for syntactically-incorrect inputs.</p>
2144
- LL,
2145
- /// The LL(*) prediction mode with exact ambiguity detection. In addition to
2146
- /// the correctness guarantees provided by the {@link #LL} prediction mode,
2147
- /// this prediction mode instructs the prediction algorithm to determine the
2148
- /// complete and exact set of ambiguous alternatives for every ambiguous
2149
- /// decision encountered while parsing.
2150
- ///
2151
- /// <p>
2152
- /// This prediction mode may be used for diagnosing ambiguities during
2153
- /// grammar development. Due to the performance overhead of calculating sets
2154
- /// of ambiguous alternatives, this prediction mode should be avoided when
2155
- /// the exact results are not necessary.</p>
2156
- ///
2157
- /// <p>
2158
- /// This prediction mode does not provide any guarantees for prediction
2159
- /// behavior for syntactically-incorrect inputs.</p>
2160
- LL_EXACT_AMBIG_DETECTION,
2161
- }
2162
-
2163
- extension PredictionModeExtension on PredictionMode {
2164
- /// Computes the SLL prediction termination condition.
2165
- ///
2166
- /// <p>
2167
- /// This method computes the SLL prediction termination condition for both of
2168
- /// the following cases.</p>
2169
- ///
2170
- /// <ul>
2171
- /// <li>The usual SLL+LL fallback upon SLL conflict</li>
2172
- /// <li>Pure SLL without LL fallback</li>
2173
- /// </ul>
2174
- ///
2175
- /// <p><strong>COMBINED SLL+LL PARSING</strong></p>
2176
- ///
2177
- /// <p>When LL-fallback is enabled upon SLL conflict, correct predictions are
2178
- /// ensured regardless of how the termination condition is computed by this
2179
- /// method. Due to the substantially higher cost of LL prediction, the
2180
- /// prediction should only fall back to LL when the additional lookahead
2181
- /// cannot lead to a unique SLL prediction.</p>
2182
- ///
2183
- /// <p>Assuming combined SLL+LL parsing, an SLL configuration set with only
2184
- /// conflicting subsets should fall back to full LL, even if the
2185
- /// configuration sets don't resolve to the same alternative (e.g.
2186
- /// {@code {1,2}} and {@code {3,4}}. If there is at least one non-conflicting
2187
- /// configuration, SLL could continue with the hopes that more lookahead will
2188
- /// resolve via one of those non-conflicting configurations.</p>
2189
- ///
2190
- /// <p>Here's the prediction termination rule them: SLL (for SLL+LL parsing)
2191
- /// stops when it sees only conflicting configuration subsets. In contrast,
2192
- /// full LL keeps going when there is uncertainty.</p>
2193
- ///
2194
- /// <p><strong>HEURISTIC</strong></p>
2195
- ///
2196
- /// <p>As a heuristic, we stop prediction when we see any conflicting subset
2197
- /// unless we see a state that only has one alternative associated with it.
2198
- /// The single-alt-state thing lets prediction continue upon rules like
2199
- /// (otherwise, it would admit defeat too soon):</p>
2200
- ///
2201
- /// <p>{@code [12|1|[], 6|2|[], 12|2|[]]. s : (ID | ID ID?) ';' ;}</p>
2202
- ///
2203
- /// <p>When the ATN simulation reaches the state before {@code ';'}, it has a
2204
- /// DFA state that looks like: {@code [12|1|[], 6|2|[], 12|2|[]]}. Naturally
2205
- /// {@code 12|1|[]} and {@code 12|2|[]} conflict, but we cannot stop
2206
- /// processing this node because alternative to has another way to continue,
2207
- /// via {@code [6|2|[]]}.</p>
2208
- ///
2209
- /// <p>It also let's us continue for this rule:</p>
2210
- ///
2211
- /// <p>{@code [1|1|[], 1|2|[], 8|3|[]] a : A | A | A B ;}</p>
2212
- ///
2213
- /// <p>After matching input A, we reach the stop state for rule A, state 1.
2214
- /// State 8 is the state right before B. Clearly alternatives 1 and 2
2215
- /// conflict and no amount of further lookahead will separate the two.
2216
- /// However, alternative 3 will be able to continue and so we do not stop
2217
- /// working on this state. In the previous example, we're concerned with
2218
- /// states associated with the conflicting alternatives. Here alt 3 is not
2219
- /// associated with the conflicting configs, but since we can continue
2220
- /// looking for input reasonably, don't declare the state done.</p>
2221
- ///
2222
- /// <p><strong>PURE SLL PARSING</strong></p>
2223
- ///
2224
- /// <p>To handle pure SLL parsing, all we have to do is make sure that we
2225
- /// combine stack contexts for configurations that differ only by semantic
2226
- /// predicate. From there, we can do the usual SLL termination heuristic.</p>
2227
- ///
2228
- /// <p><strong>PREDICATES IN SLL+LL PARSING</strong></p>
2229
- ///
2230
- /// <p>SLL decisions don't evaluate predicates until after they reach DFA stop
2231
- /// states because they need to create the DFA cache that works in all
2232
- /// semantic situations. In contrast, full LL evaluates predicates collected
2233
- /// during start state computation so it can ignore predicates thereafter.
2234
- /// This means that SLL termination detection can totally ignore semantic
2235
- /// predicates.</p>
2236
- ///
2237
- /// <p>Implementation-wise, [ATNConfigSet] combines stack contexts but not
2238
- /// semantic predicate contexts so we might see two configurations like the
2239
- /// following.</p>
2240
- ///
2241
- /// <p>{@code (s, 1, x, {}), (s, 1, x', {p})}</p>
2242
- ///
2243
- /// <p>Before testing these configurations against others, we have to merge
2244
- /// [x] and {@code x'} (without modifying the existing configurations).
2245
- /// For example, we test {@code (x+x')==x''} when looking for conflicts in
2246
- /// the following configurations.</p>
2247
- ///
2248
- /// <p>{@code (s, 1, x, {}), (s, 1, x', {p}), (s, 2, x'', {})}</p>
2249
- ///
2250
- /// <p>If the configuration set has predicates (as indicated by
2251
- /// {@link ATNConfigSet#hasSemanticContext}), this algorithm makes a copy of
2252
- /// the configurations to strip out all of the predicates so that a standard
2253
- /// [ATNConfigSet] will merge everything ignoring predicates.</p>
2254
- static bool hasSLLConflictTerminatingPrediction(
2255
- PredictionMode mode, ATNConfigSet configs) {
2256
- /* Configs in rule stop states indicate reaching the end of the decision
2257
- * rule (local context) or end of start rule (full context). If all
2258
- * configs meet this condition, then none of the configurations is able
2259
- * to match additional input so we terminate prediction.
2260
- */
2261
- if (allConfigsInRuleStopStates(configs)) {
2262
- return true;
2263
- }
2264
-
2265
- // pure SLL mode parsing
2266
- if (mode == PredictionMode.SLL) {
2267
- // Don't bother with combining configs from different semantic
2268
- // contexts if we can fail over to full LL; costs more time
2269
- // since we'll often fail over anyway.
2270
- if (configs.hasSemanticContext) {
2271
- // dup configs, tossing out semantic predicates
2272
- final dup = ATNConfigSet();
2273
- for (var c in configs) {
2274
- c = ATNConfig.dup(c, semanticContext: SemanticContext.NONE);
2275
- dup.add(c);
2276
- }
2277
- configs = dup;
2278
- }
2279
- // now we have combined contexts for configs with dissimilar preds
2280
- }
2281
-
2282
- // pure SLL or combined SLL+LL mode parsing
2283
-
2284
- final altsets = getConflictingAltSubsets(configs);
2285
- final heuristic =
2286
- hasConflictingAltSet(altsets) && !hasStateAssociatedWithOneAlt(configs);
2287
- return heuristic;
2288
- }
2289
-
2290
- /// Checks if any configuration in [configs] is in a
2291
- /// [RuleStopState]. Configurations meeting this condition have reached
2292
- /// the end of the decision rule (local context) or end of start rule (full
2293
- /// context).
2294
- ///
2295
- /// @param configs the configuration set to test
2296
- /// @return [true] if any configuration in [configs] is in a
2297
- /// [RuleStopState], otherwise [false]
2298
- static bool hasConfigInRuleStopState(ATNConfigSet configs) {
2299
- for (var c in configs) {
2300
- if (c.state is RuleStopState) {
2301
- return true;
2302
- }
2303
- }
2304
-
2305
- return false;
2306
- }
2307
-
2308
- /// Checks if all configurations in [configs] are in a
2309
- /// [RuleStopState]. Configurations meeting this condition have reached
2310
- /// the end of the decision rule (local context) or end of start rule (full
2311
- /// context).
2312
- ///
2313
- /// @param configs the configuration set to test
2314
- /// @return [true] if all configurations in [configs] are in a
2315
- /// [RuleStopState], otherwise [false]
2316
- static bool allConfigsInRuleStopStates(ATNConfigSet configs) {
2317
- for (var config in configs) {
2318
- if (!(config.state is RuleStopState)) {
2319
- return false;
2320
- }
2321
- }
2322
-
2323
- return true;
2324
- }
2325
-
2326
- /// Full LL prediction termination.
2327
- ///
2328
- /// <p>Can we stop looking ahead during ATN simulation or is there some
2329
- /// uncertainty as to which alternative we will ultimately pick, after
2330
- /// consuming more input? Even if there are partial conflicts, we might know
2331
- /// that everything is going to resolve to the same minimum alternative. That
2332
- /// means we can stop since no more lookahead will change that fact. On the
2333
- /// other hand, there might be multiple conflicts that resolve to different
2334
- /// minimums. That means we need more look ahead to decide which of those
2335
- /// alternatives we should predict.</p>
2336
- ///
2337
- /// <p>The basic idea is to split the set of configurations [C], into
2338
- /// conflicting subsets {@code (s, _, ctx, _)} and singleton subsets with
2339
- /// non-conflicting configurations. Two configurations conflict if they have
2340
- /// identical {@link ATNConfig#state} and {@link ATNConfig#context} values
2341
- /// but different {@link ATNConfig#alt} value, e.g. {@code (s, i, ctx, _)}
2342
- /// and {@code (s, j, ctx, _)} for {@code i!=j}.</p>
2343
- ///
2344
- /// <p>Reduce these configuration subsets to the set of possible alternatives.
2345
- /// You can compute the alternative subsets in one pass as follows:</p>
2346
- ///
2347
- /// <p>{@code A_s,ctx = {i | (s, i, ctx, _)}} for each configuration in
2348
- /// [C] holding [s] and [ctx] fixed.</p>
2349
- ///
2350
- /// <p>Or in pseudo-code, for each configuration [c] in [C]:</p>
2351
- ///
2352
- /// <pre>
2353
- /// map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
2354
- /// alt and not pred
2355
- /// </pre>
2356
- ///
2357
- /// <p>The values in [map] are the set of {@code A_s,ctx} sets.</p>
2358
- ///
2359
- /// <p>If {@code |A_s,ctx|=1} then there is no conflict associated with
2360
- /// [s] and [ctx].</p>
2361
- ///
2362
- /// <p>Reduce the subsets to singletons by choosing a minimum of each subset. If
2363
- /// the union of these alternative subsets is a singleton, then no amount of
2364
- /// more lookahead will help us. We will always pick that alternative. If,
2365
- /// however, there is more than one alternative, then we are uncertain which
2366
- /// alternative to predict and must continue looking for resolution. We may
2367
- /// or may not discover an ambiguity in the future, even if there are no
2368
- /// conflicting subsets this round.</p>
2369
- ///
2370
- /// <p>The biggest sin is to terminate early because it means we've made a
2371
- /// decision but were uncertain as to the eventual outcome. We haven't used
2372
- /// enough lookahead. On the other hand, announcing a conflict too late is no
2373
- /// big deal; you will still have the conflict. It's just inefficient. It
2374
- /// might even look until the end of file.</p>
2375
- ///
2376
- /// <p>No special consideration for semantic predicates is required because
2377
- /// predicates are evaluated on-the-fly for full LL prediction, ensuring that
2378
- /// no configuration contains a semantic context during the termination
2379
- /// check.</p>
2380
- ///
2381
- /// <p><strong>CONFLICTING CONFIGS</strong></p>
2382
- ///
2383
- /// <p>Two configurations {@code (s, i, x)} and {@code (s, j, x')}, conflict
2384
- /// when {@code i!=j} but {@code x=x'}. Because we merge all
2385
- /// {@code (s, i, _)} configurations together, that means that there are at
2386
- /// most [n] configurations associated with state [s] for
2387
- /// [n] possible alternatives in the decision. The merged stacks
2388
- /// complicate the comparison of configuration contexts [x] and
2389
- /// {@code x'}. Sam checks to see if one is a subset of the other by calling
2390
- /// merge and checking to see if the merged result is either [x] or
2391
- /// {@code x'}. If the [x] associated with lowest alternative [i]
2392
- /// is the superset, then [i] is the only possible prediction since the
2393
- /// others resolve to {@code min(i)} as well. However, if [x] is
2394
- /// associated with {@code j>i} then at least one stack configuration for
2395
- /// [j] is not in conflict with alternative [i]. The algorithm
2396
- /// should keep going, looking for more lookahead due to the uncertainty.</p>
2397
- ///
2398
- /// <p>For simplicity, I'm doing a equality check between [x] and
2399
- /// {@code x'} that lets the algorithm continue to consume lookahead longer
2400
- /// than necessary. The reason I like the equality is of course the
2401
- /// simplicity but also because that is the test you need to detect the
2402
- /// alternatives that are actually in conflict.</p>
2403
- ///
2404
- /// <p><strong>CONTINUE/STOP RULE</strong></p>
2405
- ///
2406
- /// <p>Continue if union of resolved alternative sets from non-conflicting and
2407
- /// conflicting alternative subsets has more than one alternative. We are
2408
- /// uncertain about which alternative to predict.</p>
2409
- ///
2410
- /// <p>The complete set of alternatives, {@code [i for (_,i,_)]}, tells us which
2411
- /// alternatives are still in the running for the amount of input we've
2412
- /// consumed at this point. The conflicting sets let us to strip away
2413
- /// configurations that won't lead to more states because we resolve
2414
- /// conflicts to the configuration with a minimum alternate for the
2415
- /// conflicting set.</p>
2416
- ///
2417
- /// <p><strong>CASES</strong></p>
2418
- ///
2419
- /// <ul>
2420
- ///
2421
- /// <li>no conflicts and more than 1 alternative in set =&gt; continue</li>
2422
- ///
2423
- /// <li> {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s, 3, z)},
2424
- /// {@code (s', 1, y)}, {@code (s', 2, y)} yields non-conflicting set
2425
- /// {@code {3}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
2426
- /// {@code {1,3}} =&gt; continue
2427
- /// </li>
2428
- ///
2429
- /// <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
2430
- /// {@code (s', 2, y)}, {@code (s'', 1, z)} yields non-conflicting set
2431
- /// {@code {1}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
2432
- /// {@code {1}} =&gt; stop and predict 1</li>
2433
- ///
2434
- /// <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
2435
- /// {@code (s', 2, y)} yields conflicting, reduced sets {@code {1}} U
2436
- /// {@code {1}} = {@code {1}} =&gt; stop and predict 1, can announce
2437
- /// ambiguity {@code {1,2}}</li>
2438
- ///
2439
- /// <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 2, y)},
2440
- /// {@code (s', 3, y)} yields conflicting, reduced sets {@code {1}} U
2441
- /// {@code {2}} = {@code {1,2}} =&gt; continue</li>
2442
- ///
2443
- /// <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 3, y)},
2444
- /// {@code (s', 4, y)} yields conflicting, reduced sets {@code {1}} U
2445
- /// {@code {3}} = {@code {1,3}} =&gt; continue</li>
2446
- ///
2447
- /// </ul>
2448
- ///
2449
- /// <p><strong>EXACT AMBIGUITY DETECTION</strong></p>
2450
- ///
2451
- /// <p>If all states report the same conflicting set of alternatives, then we
2452
- /// know we have the exact ambiguity set.</p>
2453
- ///
2454
- /// <p><code>|A_<em>i</em>|&gt;1</code> and
2455
- /// <code>A_<em>i</em> = A_<em>j</em></code> for all <em>i</em>, <em>j</em>.</p>
2456
- ///
2457
- /// <p>In other words, we continue examining lookahead until all {@code A_i}
2458
- /// have more than one alternative and all {@code A_i} are the same. If
2459
- /// {@code A={{1,2}, {1,3}}}, then regular LL prediction would terminate
2460
- /// because the resolved set is {@code {1}}. To determine what the real
2461
- /// ambiguity is, we have to know whether the ambiguity is between one and
2462
- /// two or one and three so we keep going. We can only stop prediction when
2463
- /// we need exact ambiguity detection when the sets look like
2464
- /// {@code A={{1,2}}} or {@code {{1,2},{1,2}}}, etc...</p>
2465
- static int resolvesToJustOneViableAlt(List<BitSet> altsets) {
2466
- return getSingleViableAlt(altsets);
2467
- }
2468
-
2469
- /// Determines if every alternative subset in [altsets] contains more
2470
- /// than one alternative.
2471
- ///
2472
- /// @param altsets a collection of alternative subsets
2473
- /// @return [true] if every [BitSet] in [altsets] has
2474
- /// {@link BitSet#cardinality cardinality} &gt; 1, otherwise [false]
2475
- static bool allSubsetsConflict(List<BitSet> altsets) {
2476
- return !hasNonConflictingAltSet(altsets);
2477
- }
2478
-
2479
- /// Determines if any single alternative subset in [altsets] contains
2480
- /// exactly one alternative.
2481
- ///
2482
- /// @param altsets a collection of alternative subsets
2483
- /// @return [true] if [altsets] contains a [BitSet] with
2484
- /// {@link BitSet#cardinality cardinality} 1, otherwise [false]
2485
- static bool hasNonConflictingAltSet(List<BitSet> altsets) {
2486
- for (var alts in altsets) {
2487
- if (alts.cardinality == 1) {
2488
- return true;
2489
- }
2490
- }
2491
- return false;
2492
- }
2493
-
2494
- /// Determines if any single alternative subset in [altsets] contains
2495
- /// more than one alternative.
2496
- ///
2497
- /// @param altsets a collection of alternative subsets
2498
- /// @return [true] if [altsets] contains a [BitSet] with
2499
- /// {@link BitSet#cardinality cardinality} &gt; 1, otherwise [false]
2500
- static bool hasConflictingAltSet(List<BitSet> altsets) {
2501
- for (var alts in altsets) {
2502
- if (alts.cardinality > 1) {
2503
- return true;
2504
- }
2505
- }
2506
- return false;
2507
- }
2508
-
2509
- /// Determines if every alternative subset in [altsets] is equivalent.
2510
- ///
2511
- /// @param altsets a collection of alternative subsets
2512
- /// @return [true] if every member of [altsets] is equal to the
2513
- /// others, otherwise [false]
2514
- static bool allSubsetsEqual(List<BitSet> altsets) {
2515
- final first = altsets.first;
2516
- return altsets.every((e) => e == first);
2517
- }
2518
-
2519
- /// Returns the unique alternative predicted by all alternative subsets in
2520
- /// [altsets]. If no such alternative exists, this method returns
2521
- /// {@link ATN#INVALID_ALT_NUMBER}.
2522
- ///
2523
- /// @param altsets a collection of alternative subsets
2524
- static int getUniqueAlt(List<BitSet> altsets) {
2525
- final all = getAlts(altsets);
2526
- if (all.cardinality == 1) return all.nextset(0);
2527
- return ATN.INVALID_ALT_NUMBER;
2528
- }
2529
-
2530
- /// Gets the complete set of represented alternatives for a collection of
2531
- /// alternative subsets. This method returns the union of each [BitSet]
2532
- /// in [altsets].
2533
- ///
2534
- /// @param altsets a collection of alternative subsets
2535
- /// @return the set of represented alternatives in [altsets]
2536
- static BitSet getAlts(List<BitSet> altsets) {
2537
- final all = BitSet();
2538
- for (var alts in altsets) {
2539
- all.or(alts);
2540
- }
2541
- return all;
2542
- }
2543
-
2544
- /// Get union of all alts from configs.
2545
- ///
2546
- /// @since 4.5.1
2547
- static BitSet getAltsFromConfigs(ATNConfigSet configs) {
2548
- final alts = BitSet();
2549
- for (var config in configs) {
2550
- alts.set(config.alt);
2551
- }
2552
- return alts;
2553
- }
2554
-
2555
- /// This function gets the conflicting alt subsets from a configuration set.
2556
- /// For each configuration [c] in [configs]:
2557
- ///
2558
- /// <pre>
2559
- /// map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
2560
- /// alt and not pred
2561
- /// </pre>
2562
- static List<BitSet> getConflictingAltSubsets(ATNConfigSet configs) {
2563
- final configToAlts =
2564
- HashMap<ATNConfig, BitSet>(equals: (ATNConfig a, ATNConfig b) {
2565
- if (identical(a, b)) return true;
2566
- if (a == null || b == null) return false;
2567
- return a.state.stateNumber == b.state.stateNumber &&
2568
- a.context == b.context;
2569
- }, hashCode: (ATNConfig o) {
2570
- /**
2571
- * The hash code is only a function of the {@link ATNState#stateNumber}
2572
- * and {@link ATNConfig#context}.
2573
- */
2574
- var hashCode = MurmurHash.initialize(7);
2575
- hashCode = MurmurHash.update(hashCode, o.state.stateNumber);
2576
- hashCode = MurmurHash.update(hashCode, o.context);
2577
- hashCode = MurmurHash.finish(hashCode, 2);
2578
- return hashCode;
2579
- });
2580
- for (var c in configs) {
2581
- var alts = configToAlts[c];
2582
- if (alts == null) {
2583
- alts = BitSet();
2584
- configToAlts[c] = alts;
2585
- }
2586
- alts.set(c.alt);
2587
- }
2588
- return configToAlts.values.toList();
2589
- }
2590
-
2591
- /// Get a map from state to alt subset from a configuration set. For each
2592
- /// configuration [c] in [configs]:
2593
- ///
2594
- /// <pre>
2595
- /// map[c.{@link ATNConfig#state state}] U= c.{@link ATNConfig#alt alt}
2596
- /// </pre>
2597
- static Map<ATNState, BitSet> getStateToAltMap(ATNConfigSet configs) {
2598
- final m = <ATNState, BitSet>{};
2599
- for (var c in configs) {
2600
- var alts = m[c.state];
2601
- if (alts == null) {
2602
- alts = BitSet();
2603
- m[c.state] = alts;
2604
- }
2605
- alts.set(c.alt);
2606
- }
2607
- return m;
2608
- }
2609
-
2610
- static bool hasStateAssociatedWithOneAlt(ATNConfigSet configs) {
2611
- final x = getStateToAltMap(configs);
2612
- for (var alts in x.values) {
2613
- if (alts.cardinality == 1) return true;
2614
- }
2615
- return false;
2616
- }
2617
-
2618
- static int getSingleViableAlt(List<BitSet> altsets) {
2619
- final viableAlts = BitSet();
2620
- for (var alts in altsets) {
2621
- final minAlt = alts.nextset(0);
2622
- viableAlts.set(minAlt);
2623
- if (viableAlts.cardinality > 1) {
2624
- // more than 1 viable alt
2625
- return ATN.INVALID_ALT_NUMBER;
2626
- }
2627
- }
2628
- return viableAlts.nextset(0);
2629
- }
2630
- }