Skip to content

Commit 3997be1

Browse files
authored
Merge pull request #556 from ratmice/lrlex_header_lexflags_on_pr554
Migrate `LexFlags` parsing to `GrmtoolsSectionParser` and `Header`.
2 parents 1627835 + 707bfd7 commit 3997be1

6 files changed

Lines changed: 419 additions & 419 deletions

File tree

cfgrammar/src/lib/markmap.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ use std::fmt;
2424
#[derive(Debug, PartialEq, Eq)]
2525
#[doc(hidden)]
2626
pub struct MarkMap<K, V> {
27+
default_merge_behavior: MergeBehavior,
2728
contents: Vec<(K, u16, Option<V>)>,
2829
}
2930

3031
/// Defines the merge behavior for a single key in the markmap.
3132
#[repr(u8)]
32-
#[derive(Clone, Copy)]
33+
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
3334
#[doc(hidden)]
3435
pub enum MergeBehavior {
3536
/// The value in `self` takes precedence.
@@ -216,7 +217,10 @@ impl<K: Ord + Clone, V> MarkMap<K, V> {
216217
/// Returns a new `MarkMap`.
217218
#[allow(clippy::new_without_default)]
218219
pub fn new() -> Self {
219-
MarkMap { contents: vec![] }
220+
MarkMap {
221+
default_merge_behavior: MergeBehavior::MutuallyExclusive,
222+
contents: vec![],
223+
}
220224
}
221225

222226
/// Inserts a `key` `value` pair.
@@ -286,6 +290,10 @@ impl<K: Ord + Clone, V> MarkMap<K, V> {
286290
}
287291
}
288292

293+
pub fn set_default_merge_behavior(&mut self, mb: MergeBehavior) {
294+
self.default_merge_behavior = mb;
295+
}
296+
289297
/// Sets the merge behavior for `key`.
290298
pub fn set_merge_behavior(&mut self, key: &K, mb: MergeBehavior) {
291299
let pos = self.contents.binary_search_by(|(k, _, _)| k.cmp(key));
@@ -387,10 +395,13 @@ impl<K: Ord + Clone, V> MarkMap<K, V> {
387395
let ours_mark = Mark::MergeBehavior(MergeBehavior::Ours).repr();
388396
let exclusive_mark =
389397
Mark::MergeBehavior(MergeBehavior::MutuallyExclusive).repr();
390-
let merge_behavior = (*my_mark & exclusive_mark)
398+
let mut merge_behavior = (*my_mark & exclusive_mark)
391399
| (*my_mark & ours_mark)
392400
| (*my_mark & theirs_mark);
393-
if merge_behavior == exclusive_mark || merge_behavior == 0 {
401+
if merge_behavior == 0 {
402+
merge_behavior = Mark::MergeBehavior(self.default_merge_behavior).repr();
403+
}
404+
if merge_behavior == exclusive_mark {
394405
// If only clippy could convince me and the borrow checker this is actually unnecessary.
395406
#[allow(clippy::unnecessary_unwrap)]
396407
if my_val.is_some() && their_val.is_some() {
@@ -874,4 +885,24 @@ mod test {
874885
vec!["a", "b", "x", "y"]
875886
);
876887
}
888+
889+
#[test]
890+
fn test_default_merge_behavior() {
891+
let mut mm = MarkMap::new();
892+
mm.set_default_merge_behavior(MergeBehavior::Ours);
893+
mm.insert("a", "mm");
894+
mm.insert("b", "mm");
895+
mm.set_merge_behavior(&"b", MergeBehavior::Theirs);
896+
897+
let mut mm2 = MarkMap::new();
898+
mm2.insert("x", "mm2");
899+
mm2.insert("b", "mm2");
900+
mm2.insert("a", "mm2");
901+
902+
mm.merge_from(mm2).unwrap();
903+
assert_eq!(
904+
mm.into_iter().collect::<Vec<_>>(),
905+
vec![(&"a", &"mm"), (&"b", &"mm2"), (&"x", &"mm2")]
906+
);
907+
}
877908
}

cfgrammar/src/lib/yacc/parser.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@ pub enum YaccGrammarErrorKind {
6464
UnknownEPP(String),
6565
ExpectedInput(char),
6666
InvalidYaccKind,
67-
InvalidYaccKindNamespace,
68-
InvalidActionKind,
69-
InvalidActionKindNamespace,
70-
InvalidGrmtoolsSectionEntry,
71-
DuplicateGrmtoolsSectionEntry,
72-
MissingGrmtoolsSection,
7367
Header(HeaderErrorKind, SpansKind),
7468
}
7569

@@ -157,17 +151,7 @@ impl fmt::Display for YaccGrammarErrorKind {
157151
name
158152
)
159153
}
160-
YaccGrammarErrorKind::MissingGrmtoolsSection => "Missing '%grmtools' section",
161-
YaccGrammarErrorKind::DuplicateGrmtoolsSectionEntry => {
162-
"Duplicate entry in %grmtools section"
163-
}
164-
YaccGrammarErrorKind::InvalidGrmtoolsSectionEntry => {
165-
"Invalid entry in %grmtools section"
166-
}
167154
YaccGrammarErrorKind::InvalidYaccKind => "Invalid yacc kind",
168-
YaccGrammarErrorKind::InvalidYaccKindNamespace => "Invalid yacc kind namespace",
169-
YaccGrammarErrorKind::InvalidActionKind => "Invalid action kind",
170-
YaccGrammarErrorKind::InvalidActionKindNamespace => "Invalid action kind namespace",
171155
YaccGrammarErrorKind::Header(hk, _) => &format!("Error in '%grmtools' {}", hk),
172156
};
173157
write!(f, "{}", s)
@@ -278,12 +262,7 @@ impl Spanned for YaccGrammarError {
278262
| YaccGrammarErrorKind::UnknownRuleRef(_)
279263
| YaccGrammarErrorKind::UnknownToken(_)
280264
| YaccGrammarErrorKind::NoPrecForToken(_)
281-
| YaccGrammarErrorKind::MissingGrmtoolsSection
282-
| YaccGrammarErrorKind::InvalidGrmtoolsSectionEntry
283265
| YaccGrammarErrorKind::InvalidYaccKind
284-
| YaccGrammarErrorKind::InvalidYaccKindNamespace
285-
| YaccGrammarErrorKind::InvalidActionKind
286-
| YaccGrammarErrorKind::InvalidActionKindNamespace
287266
| YaccGrammarErrorKind::ExpectedInput(_)
288267
| YaccGrammarErrorKind::UnknownEPP(_) => SpansKind::Error,
289268
YaccGrammarErrorKind::DuplicatePrecedence
@@ -293,7 +272,6 @@ impl Spanned for YaccGrammarError {
293272
| YaccGrammarErrorKind::DuplicateImplicitTokensDeclaration
294273
| YaccGrammarErrorKind::DuplicateStartDeclaration
295274
| YaccGrammarErrorKind::DuplicateActiontypeDeclaration
296-
| YaccGrammarErrorKind::DuplicateGrmtoolsSectionEntry
297275
| YaccGrammarErrorKind::DuplicateEPP => SpansKind::DuplicationError,
298276
YaccGrammarErrorKind::Header(_, spanskind) => spanskind,
299277
}

0 commit comments

Comments
 (0)