| 30 |
30 |
** PMP 20261110 A custom prediction which finds no alternative for a mandatory block now falls back to
|
| 31 |
31 |
** the default prediction instead of matching nothing (an endless loop inside a loop body).
|
| 32 |
32 |
** PMP 20261111 Added custom prediction for the ON statement trigger.
|
|
33 |
** PMP 20261113 Matched the custom predictions to the decisions of the generated parser by rule, index in
|
|
34 |
** the rule and a fingerprint of the alternatives when the class is loaded, so that a shift
|
|
35 |
** of the decision numbers needs no change here. The constructor throws when a row no longer
|
|
36 |
** matches or its decision became LL(1), listing every such row and the rule's decisions.
|
|
37 |
** Added main(), which prints a rule's decisions as table rows.
|
| 33 |
38 |
*/
|
| 34 |
39 |
|
| 35 |
40 |
/*
|
| ... | ... | |
| 87 |
92 |
|
| 88 |
93 |
package com.goldencode.p2j.uast;
|
| 89 |
94 |
|
|
95 |
import java.util.*;
|
| 90 |
96 |
import org.antlr.v4.runtime.NoViableAltException;
|
| 91 |
97 |
import org.antlr.v4.runtime.ParserRuleContext;
|
| 92 |
98 |
import org.antlr.v4.runtime.Token;
|
| 93 |
99 |
import org.antlr.v4.runtime.TokenStream;
|
| 94 |
|
import org.antlr.v4.runtime.atn.ATN;
|
| 95 |
|
import org.antlr.v4.runtime.atn.BlockStartState;
|
| 96 |
|
import org.antlr.v4.runtime.atn.DecisionState;
|
| 97 |
|
import org.antlr.v4.runtime.atn.LL1Analyzer;
|
| 98 |
|
import org.antlr.v4.runtime.atn.ParserATNSimulator;
|
| 99 |
|
import org.antlr.v4.runtime.atn.PredictionContextCache;
|
|
100 |
import org.antlr.v4.runtime.atn.*;
|
| 100 |
101 |
import org.antlr.v4.runtime.dfa.DFA;
|
|
102 |
import org.antlr.v4.runtime.misc.IntervalSet;
|
| 101 |
103 |
|
| 102 |
104 |
public class ProgressParserATNSimulator
|
| 103 |
105 |
extends ParserATNSimulator
|
| 104 |
106 |
implements ProgressParserTokenTypes
|
| 105 |
107 |
{
|
| 106 |
|
// The progress parser
|
| 107 |
|
private final ProgressParser parser;
|
| 108 |
|
|
| 109 |
108 |
/**
|
| 110 |
|
* Signed adjustment applied to selected decision numbers before dispatching to the
|
| 111 |
|
* custom prediction handlers. Keep this value at {@code 0} when the generated
|
| 112 |
|
* ANTLR decision numbers are already aligned with the switch cases below.
|
|
109 |
* The decisions which have a custom prediction, one row per switch key of {@link #predictWorker}:
|
|
110 |
* the key, the rule which owns the decision, the index of the decision among the decisions of that
|
|
111 |
* rule (in ATN order, LL(1) decisions included) and the fingerprint of the decision (see
|
|
112 |
* {@link #fingerprint}). Every {@code case} of {@link #predictWorker} needs a row and every row a
|
|
113 |
* {@code case}: a {@code case} without a row is never reached, a row without a {@code case} is
|
|
114 |
* predicted by ANTLR.
|
|
115 |
* <p>
|
|
116 |
* A key is the decision number the generated parser had when the prediction was written, and the
|
|
117 |
* {@code Alt__<n>} suffix of the predictor names refers to it. The generated numbers shift when a
|
|
118 |
* decision is added or removed anywhere before in the grammar, so the keys are matched to the
|
|
119 |
* current decisions by rule and index, see {@link #mapDecisions}. The constructor throws when a row
|
|
120 |
* no longer matches: the rule has fewer decisions, the decision at that index has another
|
|
121 |
* fingerprint (another state type, number of alternatives, or first symbol of an alternative, for
|
|
122 |
* example after a decision was added before it in the same rule), two rows match the same decision,
|
|
123 |
* or the decision became LL(1) (the generated parser predicts it from the lookahead token and never
|
|
124 |
* calls {@link #adaptivePredict} for it). The predictor must then be reviewed, because the
|
|
125 |
* alternative numbers it returns belong to the decision. The error lists the current decisions of
|
|
126 |
* each affected rule, and {@link #main} prints them as rows of this table.
|
|
127 |
* <p>
|
|
128 |
* Not detected: a change of an alternative after its first symbol, and the LL(1) decisions which
|
|
129 |
* {@link LL1Analyzer} does not recognize as such (it can miss some which ANTLR generates as LL(1)).
|
|
130 |
*/
|
|
131 |
private static final int[][] CUSTOM_DECISIONS =
|
|
132 |
{
|
|
133 |
{ 1, ProgressParser.RULE_pre_scan_class, 1, 0x17BD2E4B },
|
|
134 |
{ 3, ProgressParser.RULE_pre_scan_class, 3, 0x5EA5CB06 },
|
|
135 |
{ 4, ProgressParser.RULE_pre_scan_class, 4, 0x28CDAE3F },
|
|
136 |
{ 8, ProgressParser.RULE_block, 0, 0x5E772712 },
|
|
137 |
{ 9, ProgressParser.RULE_single_block, 0, 0x17657236 },
|
|
138 |
{ 20, ProgressParser.RULE_procedure, 1, 0x024E27F7 },
|
|
139 |
{ 49, ProgressParser.RULE_using_stmt, 0, 0x172B60CF },
|
|
140 |
{ 53, ProgressParser.RULE_constructor, 0, 0xE621B83B },
|
|
141 |
{ 56, ProgressParser.RULE_user_defined_method, 0, 0xE621B83B },
|
|
142 |
{ 66, ProgressParser.RULE_function, 0, 0xE621B83B },
|
|
143 |
{ 87, ProgressParser.RULE_define_stmt, 6, 0x53C3E963 },
|
|
144 |
{ 98, ProgressParser.RULE_def_parm_stmt, 0, 0xAD558BB2 },
|
|
145 |
{ 101, ProgressParser.RULE_def_browse_stmt, 2, 0xCD1470D8 },
|
|
146 |
{ 102, ProgressParser.RULE_def_browse_stmt, 3, 0xE5261C8C },
|
|
147 |
{ 104, ProgressParser.RULE_def_browse_stmt, 5, 0x36447939 },
|
|
148 |
{ 106, ProgressParser.RULE_def_query_stmt, 1, 0x7C4C26A0 },
|
|
149 |
{ 107, ProgressParser.RULE_for_record_spec, 0, 0x840AC948 },
|
|
150 |
{ 108, ProgressParser.RULE_for_record_spec, 1, 0x840AC948 },
|
|
151 |
{ 109, ProgressParser.RULE_for_record_spec, 2, 0x85FA1C50 },
|
|
152 |
{ 110, ProgressParser.RULE_simple_for_record_spec, 0, 0x2E50908C },
|
|
153 |
{ 112, ProgressParser.RULE_def_buf_stmt, 1, 0xB2F8C2AB },
|
|
154 |
{ 113, ProgressParser.RULE_def_buf_stmt, 2, 0x08876AEA },
|
|
155 |
{ 119, ProgressParser.RULE_def_dataset_stmt, 3, 0x29E6B215 },
|
|
156 |
{ 128, ProgressParser.RULE_for_data_relation_spec, 1, 0x89B2C1D2 },
|
|
157 |
{ 132, ProgressParser.RULE_nested_clause, 0, 0x8146CFCB },
|
|
158 |
{ 139, ProgressParser.RULE_def_menu_stmt, 4, 0x9BBE6662 },
|
|
159 |
{ 142, ProgressParser.RULE_submenu_descriptor, 1, 0xEE51CAFE },
|
|
160 |
{ 144, ProgressParser.RULE_menu_item_phrase, 1, 0x50F48775 },
|
|
161 |
{ 147, ProgressParser.RULE_def_button_stmt, 2, 0x022A3C5F },
|
|
162 |
{ 149, ProgressParser.RULE_def_rect_stmt, 1, 0x494D0491 },
|
|
163 |
{ 151, ProgressParser.RULE_def_image_stmt, 1, 0xBE98C96E },
|
|
164 |
{ 154, ProgressParser.RULE_def_frame_stmt, 1, 0x3839D624 },
|
|
165 |
{ 155, ProgressParser.RULE_def_frame_stmt, 2, 0x119D161F },
|
|
166 |
{ 157, ProgressParser.RULE_form_stmt, 1, 0x2216A048 },
|
|
167 |
{ 161, ProgressParser.RULE_form_item, 0, 0x4E4BCC80 },
|
|
168 |
{ 173, ProgressParser.RULE_set_or_update_or_prompt_for_stmt, 3, 0xE3476A07 },
|
|
169 |
{ 192, ProgressParser.RULE_format_phrase, 4, 0x32828013 },
|
|
170 |
{ 195, ProgressParser.RULE_aggregate_phrase, 2, 0x20F901F8 },
|
|
171 |
{ 196, ProgressParser.RULE_aggregate_phrase, 3, 0xFEA2B4C1 },
|
|
172 |
{ 205, ProgressParser.RULE_frame_phrase, 5, 0x8A56F1B7 },
|
|
173 |
{ 206, ProgressParser.RULE_frame_phrase, 6, 0x73BCCD7B },
|
|
174 |
{ 212, ProgressParser.RULE_title_phrase, 0, 0x12C02DB8 },
|
|
175 |
{ 213, ProgressParser.RULE_title_phrase, 1, 0xB0495C87 },
|
|
176 |
{ 215, ProgressParser.RULE_color_stmt, 1, 0x627E2C4A },
|
|
177 |
{ 220, ProgressParser.RULE_display_color_clause, 0, 0xAC376778 },
|
|
178 |
{ 224, ProgressParser.RULE_color_phrase, 2, 0x551C8D28 },
|
|
179 |
{ 225, ProgressParser.RULE_column_spec, 0, 0x7CA23F6E },
|
|
180 |
{ 227, ProgressParser.RULE_column_spec, 2, 0x20C0DF3C },
|
|
181 |
{ 228, ProgressParser.RULE_column_spec, 3, 0x7B6041FE },
|
|
182 |
{ 229, ProgressParser.RULE_column_spec, 4, 0x323F21FE },
|
|
183 |
{ 230, ProgressParser.RULE_column_spec, 5, 0x9922794A },
|
|
184 |
{ 231, ProgressParser.RULE_browse_enable_phrase, 0, 0x8B6CDF8C },
|
|
185 |
{ 238, ProgressParser.RULE_column_format_phrase, 2, 0x1C7CC322 },
|
|
186 |
{ 242, ProgressParser.RULE_browse_options_phrase, 3, 0x7A0F06FB },
|
|
187 |
{ 243, ProgressParser.RULE_browse_options_phrase, 4, 0x1E89D7BD },
|
|
188 |
{ 246, ProgressParser.RULE_table_parm, 1, 0x3EAD0937 },
|
|
189 |
{ 249, ProgressParser.RULE_table_parm, 4, 0x8BFF2A14 },
|
|
190 |
{ 251, ProgressParser.RULE_regular_parm, 1, 0x7110BB10 },
|
|
191 |
{ 254, ProgressParser.RULE_inner_block, 1, 0xE621B83B },
|
|
192 |
{ 260, ProgressParser.RULE_stmt_list, 1, 0x2E4713DE },
|
|
193 |
{ 265, ProgressParser.RULE_common_block_options, 1, 0x55E37AA3 },
|
|
194 |
{ 271, ProgressParser.RULE_then_clause, 0, 0x880C89A6 },
|
|
195 |
{ 272, ProgressParser.RULE_else_clause, 0, 0x880C89A6 },
|
|
196 |
{ 274, ProgressParser.RULE_case_stmt, 1, 0x4D583592 },
|
|
197 |
{ 275, ProgressParser.RULE_case_stmt, 2, 0x430898FC },
|
|
198 |
{ 277, ProgressParser.RULE_end_stmt, 0, 0x6114103D },
|
|
199 |
{ 278, ProgressParser.RULE_end_stmt, 1, 0x630C5649 },
|
|
200 |
{ 409, ProgressParser.RULE_put_screen_stmt, 0, 0x96CC2358 },
|
|
201 |
{ 410, ProgressParser.RULE_put_screen_stmt, 1, 0xFAEDA274 },
|
|
202 |
{ 492, ProgressParser.RULE_on_stmt, 8, 0x853012D4 },
|
|
203 |
{ 493, ProgressParser.RULE_on_stmt, 9, 0x30C9E990 },
|
|
204 |
{ 494, ProgressParser.RULE_write_referencing_phrase, 0, 0x5537DF1D },
|
|
205 |
{ 505, ProgressParser.RULE_query_reference, 0, 0xC816A7D7 },
|
|
206 |
{ 517, ProgressParser.RULE_malformed_symbol, 0, 0x3BD76DC3 },
|
|
207 |
{ 548, ProgressParser.RULE_combo_box_phrase, 1, 0xB6AC150C },
|
|
208 |
{ 551, ProgressParser.RULE_simple_widget_phrase, 2, 0x044F5F45 },
|
|
209 |
{ 558, ProgressParser.RULE_radio_set_phrase, 3, 0x65DEC16B },
|
|
210 |
{ 561, ProgressParser.RULE_selection_list_phrase, 2, 0xF069D026 },
|
|
211 |
{ 565, ProgressParser.RULE_radio_buttons_clause, 0, 0xFFE3E15B },
|
|
212 |
{ 566, ProgressParser.RULE_radio_buttons_clause, 1, 0xFFE3E15B },
|
|
213 |
{ 567, ProgressParser.RULE_radio_buttons_clause, 2, 0x85FA1C50 },
|
|
214 |
{ 569, ProgressParser.RULE_list_items_clause, 0, 0x85FA1C50 },
|
|
215 |
{ 576, ProgressParser.RULE_def_var_stmt, 2, 0xDD2DB7D1 },
|
|
216 |
{ 590, ProgressParser.RULE_extent, 0, 0xC4CDBDD0 },
|
|
217 |
{ 598, ProgressParser.RULE_var_type, 2, 0xA3D25968 },
|
|
218 |
{ 600, ProgressParser.RULE_like_clause, 1, 0xF46A66FB },
|
|
219 |
{ 607, ProgressParser.RULE_no_focus_clause, 0, 0x74F47C75 },
|
|
220 |
{ 613, ProgressParser.RULE_label_reference, 0, 0x3740D9DC },
|
|
221 |
{ 628, ProgressParser.RULE_record_phrase, 3, 0x31A31610 },
|
|
222 |
{ 742, ProgressParser.RULE_def_temp_table_stmt, 2, 0xF3FBEE4A },
|
|
223 |
{ 747, ProgressParser.RULE_add_temp_table_field, 3, 0x8606874C },
|
|
224 |
{ 751, ProgressParser.RULE_temp_table_field_options, 1, 0x729C8ED3 },
|
|
225 |
{ 753, ProgressParser.RULE_index_clause, 1, 0x60F7A403 },
|
|
226 |
{ 756, ProgressParser.RULE_is_clause, 2, 0x5E3D15E9 },
|
|
227 |
{ 757, ProgressParser.RULE_index_field_clause, 0, 0x7D1AB91B },
|
|
228 |
{ 765, ProgressParser.RULE_field_list, 1, 0x2534BD7C },
|
|
229 |
{ 767, ProgressParser.RULE_where_clause, 0, 0x1B88C3C5 },
|
|
230 |
{ 773, ProgressParser.RULE_display_stmt, 0, 0x29DCD60E },
|
|
231 |
{ 775, ProgressParser.RULE_display_stmt, 2, 0x11980AAC },
|
|
232 |
{ 778, ProgressParser.RULE_display_stmt, 5, 0xE00A73A3 },
|
|
233 |
{ 779, ProgressParser.RULE_display_stmt, 6, 0xA144D52C },
|
|
234 |
{ 791, ProgressParser.RULE_put_stmt, 0, 0xDA004D66 },
|
|
235 |
{ 792, ProgressParser.RULE_put_stmt, 1, 0x93C6EB6A },
|
|
236 |
{ 811, ProgressParser.RULE_msg_stmt, 0, 0xDE3735B3 },
|
|
237 |
{ 825, ProgressParser.RULE_run_stmt, 0, 0xDA27A52E },
|
|
238 |
{ 827, ProgressParser.RULE_run_stmt, 2, 0xC25E7E85 },
|
|
239 |
{ 862, ProgressParser.RULE_filename, 0, 0xC38E6F74 },
|
|
240 |
{ 864, ProgressParser.RULE_return_core, 1, 0x1B88C3C5 },
|
|
241 |
{ 870, ProgressParser.RULE_expr, 0, 0x60763FF0 },
|
|
242 |
{ 871, ProgressParser.RULE_log_and_expr, 0, 0xE120265F },
|
|
243 |
{ 872, ProgressParser.RULE_bitwise_xor_expr, 0, 0x51B3B8E3 },
|
|
244 |
{ 873, ProgressParser.RULE_log_not_expr, 0, 0xB4F1521F },
|
|
245 |
{ 877, ProgressParser.RULE_compare_expr, 3, 0x8371EBF6 },
|
|
246 |
{ 878, ProgressParser.RULE_compare_expr, 4, 0xD56D06B3 },
|
|
247 |
{ 879, ProgressParser.RULE_compare_expr, 5, 0xE8E8A108 },
|
|
248 |
{ 880, ProgressParser.RULE_sum_expr, 0, 0x53C5F765 },
|
|
249 |
{ 881, ProgressParser.RULE_prod_expr, 0, 0xAC276FE9 },
|
|
250 |
{ 883, ProgressParser.RULE_un_type, 1, 0x79EFC24F },
|
|
251 |
{ 884, ProgressParser.RULE_chained_object_members, 0, 0xDFD215CA },
|
|
252 |
{ 885, ProgressParser.RULE_chained_object_members, 1, 0x65C0B688 },
|
|
253 |
{ 890, ProgressParser.RULE_chained_object_members, 6, 0x0F470D66 },
|
|
254 |
{ 893, ProgressParser.RULE_primary_expr, 1, 0xC0D25995 },
|
|
255 |
{ 894, ProgressParser.RULE_primary_expr, 2, 0x4EA34273 },
|
|
256 |
{ 921, ProgressParser.RULE_attribute_or_method, 7, 0xC5547940 },
|
|
257 |
{ 922, ProgressParser.RULE_attribute_or_method, 8, 0x419DD5C2 },
|
|
258 |
{ 927, ProgressParser.RULE_com_property_or_method, 4, 0x2534BD7C },
|
|
259 |
{ 932, ProgressParser.RULE_widget_qualifier, 0, 0xF4937A60 },
|
|
260 |
{ 937, ProgressParser.RULE_lvalue, 4, 0x651F119E },
|
|
261 |
{ 938, ProgressParser.RULE_lvalue, 5, 0x853F9E26 },
|
|
262 |
{ 939, ProgressParser.RULE_lvalue, 6, 0x0ABEF8BF },
|
|
263 |
{ 941, ProgressParser.RULE_subscript, 1, 0x074171FA },
|
|
264 |
{ 942, ProgressParser.RULE_likeSubscript, 0, 0x403FE8C1 },
|
|
265 |
{ 945, ProgressParser.RULE_super_function, 0, 0xB08CD4ED },
|
|
266 |
{ 948, ProgressParser.RULE_record_funcs, 2, 0x6CA2BE21 },
|
|
267 |
{ 953, ProgressParser.RULE_at_phrase, 0, 0x9A666002 },
|
|
268 |
{ 964, ProgressParser.RULE_command_token_list, 1, 0x0A25C9CC }
|
|
269 |
};
|
|
270 |
|
|
271 |
/** The decisions of each rule of the generated parser, indexed by rule, in ATN order. */
|
|
272 |
private static final List<List<DecisionState>> RULE_DECISIONS = decisionsByRule();
|
|
273 |
|
|
274 |
/**
|
|
275 |
* The switch key of each decision of the generated parser, or -1 for one without custom prediction.
|
|
276 |
* When {@link #CUSTOM_DECISIONS} does not match the grammar, only the rows which match are mapped.
|
|
277 |
*/
|
|
278 |
private static final int[] DECISION_KEYS = new int[ProgressParser._ATN.getNumberOfDecisions()];
|
|
279 |
|
|
280 |
/**
|
|
281 |
* Why {@link #CUSTOM_DECISIONS} does not match the grammar, or {@code null} when it does. The class
|
|
282 |
* stays usable on a mismatch, so that {@link #main} can still print the rows. The constructor throws.
|
| 113 |
283 |
*/
|
| 114 |
|
private static final int DECISION_OFFSET = 0;
|
|
284 |
private static final String MAPPING_ERROR = mapDecisions(DECISION_KEYS);
|
| 115 |
285 |
|
| 116 |
286 |
/** {@link #decisionKinds} value: not classified yet. */
|
| 117 |
287 |
private static final byte KIND_UNKNOWN = 0;
|
| ... | ... | |
| 128 |
298 |
*/
|
| 129 |
299 |
private static final byte[] decisionKinds = new byte[ProgressParser._ATN.getNumberOfDecisions()];
|
| 130 |
300 |
|
|
301 |
/** The progress parser. */
|
|
302 |
private final ProgressParser parser;
|
|
303 |
|
|
304 |
/**
|
|
305 |
* Creates the simulator.
|
|
306 |
*
|
|
307 |
* @param parser
|
|
308 |
* The progress parser.
|
|
309 |
* @param atn
|
|
310 |
* The ATN of the generated parser.
|
|
311 |
* @param decisionToDFA
|
|
312 |
* The DFA of each decision.
|
|
313 |
* @param sharedContextCache
|
|
314 |
* The prediction context cache.
|
|
315 |
*
|
|
316 |
* @throws IllegalStateException
|
|
317 |
* if {@link #CUSTOM_DECISIONS} does not match the decisions of the generated parser.
|
|
318 |
*/
|
| 131 |
319 |
public ProgressParserATNSimulator(ProgressParser parser,
|
| 132 |
320 |
ATN atn,
|
| 133 |
321 |
DFA[] decisionToDFA,
|
| 134 |
322 |
PredictionContextCache sharedContextCache)
|
| 135 |
323 |
{
|
| 136 |
324 |
super(parser, atn, decisionToDFA, sharedContextCache);
|
|
325 |
|
|
326 |
if (MAPPING_ERROR != null)
|
|
327 |
{
|
|
328 |
throw new IllegalStateException(MAPPING_ERROR);
|
|
329 |
}
|
|
330 |
|
| 137 |
331 |
this.parser = parser;
|
| 138 |
332 |
}
|
| 139 |
333 |
|
| ... | ... | |
| 183 |
377 |
}
|
| 184 |
378 |
|
| 185 |
379 |
/**
|
|
380 |
* Maps each decision of the generated parser to the switch key of its custom prediction, from
|
|
381 |
* {@link #CUSTOM_DECISIONS}, and checks every row against the grammar.
|
|
382 |
*
|
|
383 |
* @param keys
|
|
384 |
* Receives the switch key of each decision, -1 for a decision without custom prediction.
|
|
385 |
* A row which does not match the grammar is not mapped (except an LL(1) decision).
|
|
386 |
*
|
|
387 |
* @return {@code null} when every row matches the grammar, otherwise a message with every row
|
|
388 |
* which does not match and the current decisions of each affected rule.
|
|
389 |
*/
|
|
390 |
private static String mapDecisions(int[] keys)
|
|
391 |
{
|
|
392 |
Arrays.fill(keys, -1);
|
|
393 |
|
|
394 |
LL1Analyzer analyzer = new LL1Analyzer(ProgressParser._ATN);
|
|
395 |
StringBuilder problems = new StringBuilder();
|
|
396 |
Set<Integer> rules = new TreeSet<>();
|
|
397 |
|
|
398 |
for (int[] row : CUSTOM_DECISIONS)
|
|
399 |
{
|
|
400 |
int key = row[0];
|
|
401 |
int rule = row[1];
|
|
402 |
int index = row[2];
|
|
403 |
int expected = row[3];
|
|
404 |
|
|
405 |
List<DecisionState> decisions = RULE_DECISIONS.get(rule);
|
|
406 |
DecisionState state = index < decisions.size() ? decisions.get(index) : null;
|
|
407 |
|
|
408 |
String problem = null;
|
|
409 |
if (state == null)
|
|
410 |
{
|
|
411 |
problem = "the rule has " + decisions.size() + " decisions";
|
|
412 |
}
|
|
413 |
else if (fingerprint(state) != expected)
|
|
414 |
{
|
|
415 |
problem = "decision " + state.decision + " is " + describe(state);
|
|
416 |
}
|
|
417 |
else if (keys[state.decision] != -1)
|
|
418 |
{
|
|
419 |
problem = "decision " + state.decision + " is already mapped to key " + keys[state.decision];
|
|
420 |
}
|
|
421 |
else
|
|
422 |
{
|
|
423 |
keys[state.decision] = key;
|
|
424 |
|
|
425 |
if (isLL1(analyzer, state))
|
|
426 |
{
|
|
427 |
problem = "decision " + state.decision + " is LL(1), the parser never calls " +
|
|
428 |
"adaptivePredict() for it";
|
|
429 |
}
|
|
430 |
}
|
|
431 |
|
|
432 |
if (problem != null)
|
|
433 |
{
|
|
434 |
problems.append(String.format("%n custom prediction %d, expected decision index %d of " +
|
|
435 |
"rule %s with fingerprint 0x%08X: %s",
|
|
436 |
key,
|
|
437 |
index,
|
|
438 |
ProgressParser.ruleNames[rule],
|
|
439 |
expected,
|
|
440 |
problem));
|
|
441 |
rules.add(rule);
|
|
442 |
}
|
|
443 |
}
|
|
444 |
|
|
445 |
if (problems.length() == 0)
|
|
446 |
{
|
|
447 |
return null;
|
|
448 |
}
|
|
449 |
|
|
450 |
StringBuilder msg = new StringBuilder();
|
|
451 |
msg.append("CUSTOM_DECISIONS no longer matches the grammar. Review each predictor listed and " +
|
|
452 |
"update its row (ProgressParserATNSimulator.main() prints the rows of a rule):");
|
|
453 |
msg.append(problems);
|
|
454 |
for (int rule : rules)
|
|
455 |
{
|
|
456 |
List<DecisionState> decisions = RULE_DECISIONS.get(rule);
|
|
457 |
|
|
458 |
msg.append(String.format("%nDecisions of rule %s:", ProgressParser.ruleNames[rule]));
|
|
459 |
for (int i = 0; i < decisions.size(); i++)
|
|
460 |
{
|
|
461 |
DecisionState state = decisions.get(i);
|
|
462 |
msg.append(String.format("%n index %d: decision %d, %s", i, state.decision, describe(state)));
|
|
463 |
}
|
|
464 |
}
|
|
465 |
|
|
466 |
return msg.toString();
|
|
467 |
}
|
|
468 |
|
|
469 |
/**
|
|
470 |
* Collects the decisions of each rule of the generated parser.
|
|
471 |
*
|
|
472 |
* @return The decision states, indexed by rule, each list in ATN order.
|
|
473 |
*/
|
|
474 |
private static List<List<DecisionState>> decisionsByRule()
|
|
475 |
{
|
|
476 |
ATN atn = ProgressParser._ATN;
|
|
477 |
List<List<DecisionState>> ruleDecisions = new ArrayList<>();
|
|
478 |
for (int i = 0; i < atn.ruleToStartState.length; i++)
|
|
479 |
{
|
|
480 |
ruleDecisions.add(new ArrayList<>());
|
|
481 |
}
|
|
482 |
|
|
483 |
for (int d = 0; d < atn.getNumberOfDecisions(); d++)
|
|
484 |
{
|
|
485 |
DecisionState state = atn.getDecisionState(d);
|
|
486 |
ruleDecisions.get(state.ruleIndex).add(state);
|
|
487 |
}
|
|
488 |
|
|
489 |
return ruleDecisions;
|
|
490 |
}
|
|
491 |
|
|
492 |
/**
|
|
493 |
* Computes the fingerprint of a decision: a hash of its state type and, for each alternative in
|
|
494 |
* order, the set of symbols the alternative can start with inside the rule (see
|
|
495 |
* {@link #firstSymbols}). Only names enter the hash (token and rule names, not their numbers), so
|
|
496 |
* the fingerprint does not change when tokens, rules or decisions are added elsewhere in the
|
|
497 |
* grammar.
|
|
498 |
*
|
|
499 |
* @param state
|
|
500 |
* The decision state.
|
|
501 |
*
|
|
502 |
* @return The fingerprint.
|
|
503 |
*/
|
|
504 |
private static int fingerprint(DecisionState state)
|
|
505 |
{
|
|
506 |
StringBuilder text = new StringBuilder(ATNState.serializationNames.get(state.getStateType()));
|
|
507 |
for (int i = 0; i < state.getNumberOfTransitions(); i++)
|
|
508 |
{
|
|
509 |
Set<String> symbols = new TreeSet<>();
|
|
510 |
firstSymbols(state.transition(i).target, symbols, new HashSet<>());
|
|
511 |
text.append(" | ").append(String.join(" ", symbols));
|
|
512 |
}
|
|
513 |
|
|
514 |
return text.toString().hashCode();
|
|
515 |
}
|
|
516 |
|
|
517 |
/**
|
|
518 |
* Collects the symbols which can be matched first from the given ATN state, without leaving its
|
|
519 |
* rule: the display name of a token (prefixed by {@code ~} for a negated set), {@code .} for a
|
|
520 |
* wildcard, the name of a referenced rule (which is not entered), {@code ?} for a semantic
|
|
521 |
* predicate and {@code <end>} when the end of the rule can be reached. Actions are skipped.
|
|
522 |
*
|
|
523 |
* @param state
|
|
524 |
* The ATN state to start from.
|
|
525 |
* @param symbols
|
|
526 |
* Receives the symbols.
|
|
527 |
* @param visited
|
|
528 |
* The states already visited, to stop at loops.
|
|
529 |
*/
|
|
530 |
private static void firstSymbols(ATNState state, Set<String> symbols, Set<ATNState> visited)
|
|
531 |
{
|
|
532 |
if (!visited.add(state))
|
|
533 |
{
|
|
534 |
return;
|
|
535 |
}
|
|
536 |
|
|
537 |
if (state instanceof RuleStopState)
|
|
538 |
{
|
|
539 |
symbols.add("<end>");
|
|
540 |
return;
|
|
541 |
}
|
|
542 |
|
|
543 |
for (int i = 0; i < state.getNumberOfTransitions(); i++)
|
|
544 |
{
|
|
545 |
Transition trans = state.transition(i);
|
|
546 |
if (trans instanceof RuleTransition)
|
|
547 |
{
|
|
548 |
symbols.add(ProgressParser.ruleNames[trans.target.ruleIndex]);
|
|
549 |
}
|
|
550 |
else if (trans.isEpsilon())
|
|
551 |
{
|
|
552 |
if (trans instanceof AbstractPredicateTransition)
|
|
553 |
{
|
|
554 |
symbols.add("?");
|
|
555 |
}
|
|
556 |
|
|
557 |
firstSymbols(trans.target, symbols, visited);
|
|
558 |
}
|
|
559 |
else if (trans instanceof WildcardTransition)
|
|
560 |
{
|
|
561 |
symbols.add(".");
|
|
562 |
}
|
|
563 |
else
|
|
564 |
{
|
|
565 |
String prefix = trans instanceof NotSetTransition ? "~" : "";
|
|
566 |
for (int type : trans.label().toList())
|
|
567 |
{
|
|
568 |
symbols.add(prefix + ProgressParser.VOCABULARY.getDisplayName(type));
|
|
569 |
}
|
|
570 |
}
|
|
571 |
}
|
|
572 |
}
|
|
573 |
|
|
574 |
/**
|
|
575 |
* Checks whether ANTLR generates a decision as LL(1), predicted from the lookahead token without
|
|
576 |
* {@link #adaptivePredict}: the lookahead sets of its alternatives are disjoint and none of them
|
|
577 |
* depends on a predicate. This is the test of the ANTLR tool, run on the runtime ATN, so it can miss
|
|
578 |
* a decision which the tool generates as LL(1).
|
|
579 |
*
|
|
580 |
* @param analyzer
|
|
581 |
* The analyzer of the parser's ATN.
|
|
582 |
* @param state
|
|
583 |
* The decision state.
|
|
584 |
*
|
|
585 |
* @return {@code true} if the decision is LL(1).
|
|
586 |
*/
|
|
587 |
private static boolean isLL1(LL1Analyzer analyzer, DecisionState state)
|
|
588 |
{
|
|
589 |
IntervalSet[] look = analyzer.getDecisionLookahead(state);
|
|
590 |
IntervalSet seen = new IntervalSet();
|
|
591 |
for (IntervalSet alt : look)
|
|
592 |
{
|
|
593 |
if (alt == null || !alt.and(seen).isNil())
|
|
594 |
{
|
|
595 |
return false;
|
|
596 |
}
|
|
597 |
|
|
598 |
seen.addAll(alt);
|
|
599 |
}
|
|
600 |
|
|
601 |
return true;
|
|
602 |
}
|
|
603 |
|
|
604 |
/**
|
|
605 |
* Describes a decision state by its type, number of alternatives and fingerprint.
|
|
606 |
*
|
|
607 |
* @param state
|
|
608 |
* The decision state.
|
|
609 |
*
|
|
610 |
* @return The description, for example
|
|
611 |
* {@code "STAR_BLOCK_START with 12 alternatives, fingerprint 0x1A2B3C4D"}.
|
|
612 |
*/
|
|
613 |
private static String describe(DecisionState state)
|
|
614 |
{
|
|
615 |
return String.format("%s with %d alternatives, fingerprint 0x%08X",
|
|
616 |
ATNState.serializationNames.get(state.getStateType()),
|
|
617 |
state.getNumberOfTransitions(),
|
|
618 |
fingerprint(state));
|
|
619 |
}
|
|
620 |
|
|
621 |
/**
|
|
622 |
* Builds the rule column of a {@link #CUSTOM_DECISIONS} row, as printed by {@link #main}.
|
|
623 |
*
|
|
624 |
* @param rule
|
|
625 |
* The rule index.
|
|
626 |
*
|
|
627 |
* @return The column text, for example {@code "ProgressParser.RULE_at_phrase,"}.
|
|
628 |
*/
|
|
629 |
private static String ruleColumn(int rule)
|
|
630 |
{
|
|
631 |
return "ProgressParser.RULE_" + ProgressParser.ruleNames[rule] + ",";
|
|
632 |
}
|
|
633 |
|
|
634 |
/**
|
| 186 |
635 |
* Checks whether a decision is a block which must match one of its alternatives: none of them can reach
|
| 187 |
636 |
* the end of the block without matching a token. Loop decisions and optional blocks are not mandatory.
|
| 188 |
637 |
*
|
| ... | ... | |
| 234 |
683 |
* This method contains the custom prediction dispatch used by this simulator. For selected
|
| 235 |
684 |
* decisions, it applies specialized fast-path logic which avoids expensive generic ATN
|
| 236 |
685 |
* prediction in known grammar hotspots. Decisions which are not handled explicitly are
|
| 237 |
|
* delegated to the superclass implementation.
|
|
686 |
* delegated to the superclass implementation. The switch is on the key the decision has in
|
|
687 |
* {@link #CUSTOM_DECISIONS}, which can differ from the decision number, while the superclass
|
|
688 |
* always gets the decision number.
|
| 238 |
689 |
*
|
| 239 |
690 |
* @param input
|
| 240 |
691 |
* The token stream used by the parser.
|
| ... | ... | |
| 248 |
699 |
private int predictWorker(TokenStream input, int decision, ParserRuleContext outerContext)
|
| 249 |
700 |
{
|
| 250 |
701 |
int alt = -1;
|
| 251 |
|
int minAdjustedDecision = 0;
|
| 252 |
|
int maxAdjustedDecision = Integer.MAX_VALUE;
|
| 253 |
|
decision = adjustDecision(decision, minAdjustedDecision, maxAdjustedDecision);
|
| 254 |
|
|
| 255 |
|
switch(decision)
|
|
702 |
|
|
703 |
switch (DECISION_KEYS[decision])
|
| 256 |
704 |
{
|
| 257 |
705 |
case 1:
|
| 258 |
706 |
return parser.predictPreScanClassAlt__1(input);
|
| ... | ... | |
| 731 |
1179 |
return super.adaptivePredict(input, decision, outerContext);
|
| 732 |
1180 |
}
|
| 733 |
1181 |
}
|
| 734 |
|
|
| 735 |
|
/**
|
| 736 |
|
* Adjusts a generated ANTLR decision number before it is used for custom prediction dispatch.
|
| 737 |
|
* <p>
|
| 738 |
|
* The superclass must still be called with the original decision number, because it indexes the
|
| 739 |
|
* generated ATN/DFA tables. The adjusted value is used only to match the stable switch cases in
|
| 740 |
|
* this class.
|
| 741 |
|
*
|
| 742 |
|
* @param decision
|
| 743 |
|
* The original generated ANTLR decision number.
|
| 744 |
|
* @param minAdjustedDecision
|
| 745 |
|
* The lowest decision number to which the configured offset should be applied.
|
| 746 |
|
* @param maxAdjustedDecision
|
| 747 |
|
* The highest decision number to which the configured offset should be applied.
|
| 748 |
|
*
|
| 749 |
|
* @return The adjusted decision number when it is inside the configured range, otherwise the
|
| 750 |
|
* original decision number.
|
| 751 |
|
*/
|
| 752 |
|
private int adjustDecision(int decision, int minAdjustedDecision, int maxAdjustedDecision)
|
| 753 |
|
{
|
| 754 |
|
if (decision >= minAdjustedDecision && decision <= maxAdjustedDecision)
|
| 755 |
|
{
|
| 756 |
|
return decision + DECISION_OFFSET;
|
| 757 |
|
}
|
| 758 |
1182 |
|
| 759 |
|
return decision;
|
| 760 |
|
}
|
|
1183 |
/**
|
|
1184 |
* Prints the decisions of the given rules as rows of {@link #CUSTOM_DECISIONS}, aligned as the table,
|
|
1185 |
* to add a custom prediction or to repair the table after a grammar change. A decision which has a
|
|
1186 |
* custom prediction (its row still matches) is printed with its key, any other with its current
|
|
1187 |
* decision number as the key, and a comment after each row describes the decision. Also prints why the
|
|
1188 |
* table does not match the grammar, if it does not. The exit status is 1 for a usage error, an
|
|
1189 |
* unknown rule or a table which does not match the grammar.
|
|
1190 |
* <p>
|
|
1191 |
* Usage: {@code java -cp "build/lib/*" com.goldencode.p2j.uast.ProgressParserATNSimulator <rule>...}
|
|
1192 |
*
|
|
1193 |
* @param args
|
|
1194 |
* The names of the parser rules, as in {@code ProgressParser.g4}.
|
|
1195 |
*/
|
|
1196 |
public static void main(String[] args)
|
|
1197 |
{
|
|
1198 |
boolean failed = false;
|
|
1199 |
|
|
1200 |
if (MAPPING_ERROR != null)
|
|
1201 |
{
|
|
1202 |
System.err.println(MAPPING_ERROR);
|
|
1203 |
System.err.println();
|
|
1204 |
failed = true;
|
|
1205 |
}
|
|
1206 |
|
|
1207 |
if (args.length == 0)
|
|
1208 |
{
|
|
1209 |
System.err.println("Usage: java -cp \"build/lib/*\" " + ProgressParserATNSimulator.class.getName() +
|
|
1210 |
" <rule>...");
|
|
1211 |
System.exit(1);
|
|
1212 |
}
|
|
1213 |
|
|
1214 |
// the width of the rule column, as in the table
|
|
1215 |
int width = 0;
|
|
1216 |
for (int[] row : CUSTOM_DECISIONS)
|
|
1217 |
{
|
|
1218 |
width = Math.max(width, ruleColumn(row[1]).length());
|
|
1219 |
}
|
|
1220 |
|
|
1221 |
List<String> ruleNames = Arrays.asList(ProgressParser.ruleNames);
|
|
1222 |
for (String name : args)
|
|
1223 |
{
|
|
1224 |
int rule = ruleNames.indexOf(name);
|
|
1225 |
if (rule < 0)
|
|
1226 |
{
|
|
1227 |
System.err.println("Unknown rule: " + name);
|
|
1228 |
failed = true;
|
|
1229 |
continue;
|
|
1230 |
}
|
|
1231 |
|
|
1232 |
List<DecisionState> decisions = RULE_DECISIONS.get(rule);
|
|
1233 |
if (decisions.isEmpty())
|
|
1234 |
{
|
|
1235 |
System.out.println("Rule " + name + " has no decisions.");
|
|
1236 |
continue;
|
|
1237 |
}
|
|
1238 |
|
|
1239 |
String column = String.format("%-" + Math.max(width, ruleColumn(rule).length()) + "s",
|
|
1240 |
ruleColumn(rule));
|
|
1241 |
for (int i = 0; i < decisions.size(); i++)
|
|
1242 |
{
|
|
1243 |
DecisionState state = decisions.get(i);
|
|
1244 |
boolean custom = DECISION_KEYS[state.decision] != -1;
|
|
1245 |
int key = custom ? DECISION_KEYS[state.decision] : state.decision;
|
|
1246 |
|
|
1247 |
System.out.printf(" { %3d, %s %2d, 0x%08X }, // decision %d, %s%s%n",
|
|
1248 |
key,
|
|
1249 |
column,
|
|
1250 |
i,
|
|
1251 |
fingerprint(state),
|
|
1252 |
state.decision,
|
|
1253 |
describe(state),
|
|
1254 |
custom ? ", custom prediction" : "");
|
|
1255 |
}
|
|
1256 |
}
|
|
1257 |
|
|
1258 |
if (failed)
|
|
1259 |
{
|
|
1260 |
System.exit(1);
|
|
1261 |
}
|
|
1262 |
}
|
| 761 |
1263 |
}
|