Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions cfgrammar/src/lib/markmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ use std::fmt;
#[derive(Debug, PartialEq, Eq)]
#[doc(hidden)]
pub struct MarkMap<K, V> {
default_merge_behavior: MergeBehavior,
contents: Vec<(K, u16, Option<V>)>,
}

/// Defines the merge behavior for a single key in the markmap.
#[repr(u8)]
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[doc(hidden)]
pub enum MergeBehavior {
/// The value in `self` takes precedence.
Expand Down Expand Up @@ -216,7 +217,10 @@ impl<K: Ord + Clone, V> MarkMap<K, V> {
/// Returns a new `MarkMap`.
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
MarkMap { contents: vec![] }
MarkMap {
default_merge_behavior: MergeBehavior::MutuallyExclusive,
contents: vec![],
}
}

/// Inserts a `key` `value` pair.
Expand Down Expand Up @@ -286,6 +290,10 @@ impl<K: Ord + Clone, V> MarkMap<K, V> {
}
}

pub fn set_default_merge_behavior(&mut self, mb: MergeBehavior) {
self.default_merge_behavior = mb;
}

/// Sets the merge behavior for `key`.
pub fn set_merge_behavior(&mut self, key: &K, mb: MergeBehavior) {
let pos = self.contents.binary_search_by(|(k, _, _)| k.cmp(key));
Expand Down Expand Up @@ -387,10 +395,13 @@ impl<K: Ord + Clone, V> MarkMap<K, V> {
let ours_mark = Mark::MergeBehavior(MergeBehavior::Ours).repr();
let exclusive_mark =
Mark::MergeBehavior(MergeBehavior::MutuallyExclusive).repr();
let merge_behavior = (*my_mark & exclusive_mark)
let mut merge_behavior = (*my_mark & exclusive_mark)
| (*my_mark & ours_mark)
| (*my_mark & theirs_mark);
if merge_behavior == exclusive_mark || merge_behavior == 0 {
if merge_behavior == 0 {
merge_behavior = Mark::MergeBehavior(self.default_merge_behavior).repr();
}
if merge_behavior == exclusive_mark {
// If only clippy could convince me and the borrow checker this is actually unnecessary.
#[allow(clippy::unnecessary_unwrap)]
if my_val.is_some() && their_val.is_some() {
Expand Down Expand Up @@ -874,4 +885,24 @@ mod test {
vec!["a", "b", "x", "y"]
);
}

#[test]
fn test_default_merge_behavior() {
let mut mm = MarkMap::new();
mm.set_default_merge_behavior(MergeBehavior::Ours);
mm.insert("a", "mm");
mm.insert("b", "mm");
mm.set_merge_behavior(&"b", MergeBehavior::Theirs);

let mut mm2 = MarkMap::new();
mm2.insert("x", "mm2");
mm2.insert("b", "mm2");
mm2.insert("a", "mm2");

mm.merge_from(mm2).unwrap();
assert_eq!(
mm.into_iter().collect::<Vec<_>>(),
vec![(&"a", &"mm"), (&"b", &"mm2"), (&"x", &"mm2")]
);
}
}
22 changes: 0 additions & 22 deletions cfgrammar/src/lib/yacc/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ pub enum YaccGrammarErrorKind {
UnknownEPP(String),
ExpectedInput(char),
InvalidYaccKind,
InvalidYaccKindNamespace,
InvalidActionKind,
InvalidActionKindNamespace,
InvalidGrmtoolsSectionEntry,
DuplicateGrmtoolsSectionEntry,
MissingGrmtoolsSection,
Header(HeaderErrorKind, SpansKind),
}

Expand Down Expand Up @@ -157,17 +151,7 @@ impl fmt::Display for YaccGrammarErrorKind {
name
)
}
YaccGrammarErrorKind::MissingGrmtoolsSection => "Missing '%grmtools' section",
YaccGrammarErrorKind::DuplicateGrmtoolsSectionEntry => {
"Duplicate entry in %grmtools section"
}
YaccGrammarErrorKind::InvalidGrmtoolsSectionEntry => {
"Invalid entry in %grmtools section"
}
YaccGrammarErrorKind::InvalidYaccKind => "Invalid yacc kind",
YaccGrammarErrorKind::InvalidYaccKindNamespace => "Invalid yacc kind namespace",
YaccGrammarErrorKind::InvalidActionKind => "Invalid action kind",
YaccGrammarErrorKind::InvalidActionKindNamespace => "Invalid action kind namespace",
YaccGrammarErrorKind::Header(hk, _) => &format!("Error in '%grmtools' {}", hk),
};
write!(f, "{}", s)
Expand Down Expand Up @@ -278,12 +262,7 @@ impl Spanned for YaccGrammarError {
| YaccGrammarErrorKind::UnknownRuleRef(_)
| YaccGrammarErrorKind::UnknownToken(_)
| YaccGrammarErrorKind::NoPrecForToken(_)
| YaccGrammarErrorKind::MissingGrmtoolsSection
| YaccGrammarErrorKind::InvalidGrmtoolsSectionEntry
| YaccGrammarErrorKind::InvalidYaccKind
| YaccGrammarErrorKind::InvalidYaccKindNamespace
| YaccGrammarErrorKind::InvalidActionKind
| YaccGrammarErrorKind::InvalidActionKindNamespace
| YaccGrammarErrorKind::ExpectedInput(_)
| YaccGrammarErrorKind::UnknownEPP(_) => SpansKind::Error,
YaccGrammarErrorKind::DuplicatePrecedence
Expand All @@ -293,7 +272,6 @@ impl Spanned for YaccGrammarError {
| YaccGrammarErrorKind::DuplicateImplicitTokensDeclaration
| YaccGrammarErrorKind::DuplicateStartDeclaration
| YaccGrammarErrorKind::DuplicateActiontypeDeclaration
| YaccGrammarErrorKind::DuplicateGrmtoolsSectionEntry
| YaccGrammarErrorKind::DuplicateEPP => SpansKind::DuplicationError,
YaccGrammarErrorKind::Header(_, spanskind) => spanskind,
}
Expand Down
Loading