From 6490b1e4f235b4fdb22ca9a252252aaaf038d7a3 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 29 Mar 2019 17:36:19 -0700 Subject: [PATCH] Fix non-deterministic conflict message order --- .../build_tables/build_parse_table.rs | 37 ++++++++++--------- .../expected_error.txt | 4 +- .../expected_error.txt | 4 +- .../expected_error.txt | 4 +- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/cli/src/generate/build_tables/build_parse_table.rs b/cli/src/generate/build_tables/build_parse_table.rs index 04ab3aae..fa42953a 100644 --- a/cli/src/generate/build_tables/build_parse_table.rs +++ b/cli/src/generate/build_tables/build_parse_table.rs @@ -520,12 +520,17 @@ impl<'a> ParseTableBuilder<'a> { let mut resolution_count = 0; write!(&mut msg, "\nPossible resolutions:\n\n").unwrap(); - let mut shift_items = conflicting_items - .iter() - .filter(|i| !i.is_done()) - .cloned() - .collect::>(); + let mut shift_items = Vec::new(); + let mut reduce_items = Vec::new(); + for item in conflicting_items { + if item.is_done() { + reduce_items.push(item); + } else { + shift_items.push(item); + } + } shift_items.sort_unstable(); + reduce_items.sort_unstable(); if actual_conflict.len() > 1 { if shift_items.len() > 0 { resolution_count += 1; @@ -549,17 +554,15 @@ impl<'a> ParseTableBuilder<'a> { write!(&mut msg, " than in the other rules.\n").unwrap(); } - for item in &conflicting_items { - if item.is_done() { - resolution_count += 1; - write!( - &mut msg, - " {}: Specify a higher precedence in `{}` than in the other rules.\n", - resolution_count, - self.symbol_name(&Symbol::non_terminal(item.variable_index as usize)) - ) - .unwrap(); - } + for item in &reduce_items { + resolution_count += 1; + write!( + &mut msg, + " {}: Specify a higher precedence in `{}` than in the other rules.\n", + resolution_count, + self.symbol_name(&Symbol::non_terminal(item.variable_index as usize)) + ) + .unwrap(); } } @@ -571,7 +574,7 @@ impl<'a> ParseTableBuilder<'a> { resolution_count ) .unwrap(); - for (i, item) in conflicting_items.iter().filter(|i| i.is_done()).enumerate() { + for (i, item) in reduce_items.iter().enumerate() { if i > 0 { write!(&mut msg, " and ").unwrap(); } diff --git a/test/fixtures/test_grammars/conflict_in_repeat_rule/expected_error.txt b/test/fixtures/test_grammars/conflict_in_repeat_rule/expected_error.txt index 4357c5cf..934f91c7 100644 --- a/test/fixtures/test_grammars/conflict_in_repeat_rule/expected_error.txt +++ b/test/fixtures/test_grammars/conflict_in_repeat_rule/expected_error.txt @@ -9,6 +9,6 @@ Possible interpretations: Possible resolutions: - 1: Specify a higher precedence in `array_type_repeat1` than in the other rules. - 2: Specify a higher precedence in `array_repeat1` than in the other rules. + 1: Specify a higher precedence in `array_repeat1` than in the other rules. + 2: Specify a higher precedence in `array_type_repeat1` than in the other rules. 3: Add a conflict for these rules: `array`, `array_type` diff --git a/test/fixtures/test_grammars/conflict_in_repeat_rule_after_external_token/expected_error.txt b/test/fixtures/test_grammars/conflict_in_repeat_rule_after_external_token/expected_error.txt index 4d0bdebc..2aabe1e4 100644 --- a/test/fixtures/test_grammars/conflict_in_repeat_rule_after_external_token/expected_error.txt +++ b/test/fixtures/test_grammars/conflict_in_repeat_rule_after_external_token/expected_error.txt @@ -9,6 +9,6 @@ Possible interpretations: Possible resolutions: - 1: Specify a higher precedence in `array_type_repeat1` than in the other rules. - 2: Specify a higher precedence in `array_repeat1` than in the other rules. + 1: Specify a higher precedence in `array_repeat1` than in the other rules. + 2: Specify a higher precedence in `array_type_repeat1` than in the other rules. 3: Add a conflict for these rules: `array`, `array_type` diff --git a/test/fixtures/test_grammars/partially_resolved_conflict/expected_error.txt b/test/fixtures/test_grammars/partially_resolved_conflict/expected_error.txt index d5d2c1bf..dda779f8 100644 --- a/test/fixtures/test_grammars/partially_resolved_conflict/expected_error.txt +++ b/test/fixtures/test_grammars/partially_resolved_conflict/expected_error.txt @@ -9,6 +9,6 @@ Possible interpretations: Possible resolutions: - 1: Specify a higher precedence in `unary_b` than in the other rules. - 2: Specify a higher precedence in `unary_a` than in the other rules. + 1: Specify a higher precedence in `unary_a` than in the other rules. + 2: Specify a higher precedence in `unary_b` than in the other rules. 3: Add a conflict for these rules: `unary_a`, `unary_b`