Skip to content

Commit 190497c

Browse files
committed
Migrate LexFlags parsing to GrmtoolsSectionParser and Header.
1 parent 04ae392 commit 190497c

8 files changed

Lines changed: 552 additions & 438 deletions

File tree

cfgrammar/src/lib/markmap.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,48 @@ impl<K: Ord + Clone, V> MarkMap<K, V> {
409409
Ok(())
410410
}
411411

412+
/// Similar to `merge_from` except the `MergeBehavior` of self is used when merging into `*other*`.
413+
#[doc(hidden)]
414+
pub fn merge_into(mut self, other: &'_ mut Self) -> Result<(), MergeError<K, Box<V>>> {
415+
for (src_key, src_mark, src_val) in self.contents.drain(..) {
416+
let pos = other.contents.binary_search_by(|x| x.0.cmp(&src_key));
417+
match pos {
418+
Ok(pos) => {
419+
let (_, dest_mark, dest_val) = &mut other.contents[pos];
420+
let theirs_mark = Mark::MergeBehavior(MergeBehavior::Theirs).repr();
421+
let ours_mark = Mark::MergeBehavior(MergeBehavior::Ours).repr();
422+
let exclusive_mark =
423+
Mark::MergeBehavior(MergeBehavior::MutuallyExclusive).repr();
424+
let merge_behavior = (src_mark & exclusive_mark)
425+
| (src_mark & ours_mark)
426+
| (src_mark & theirs_mark);
427+
if merge_behavior == exclusive_mark || merge_behavior == 0 {
428+
// If only clippy could convince me and the borrow checker this is actually unnecessary.
429+
#[allow(clippy::unnecessary_unwrap)]
430+
if src_val.is_some() && dest_val.is_some() {
431+
return Err(MergeError::Exclusivity(
432+
src_key,
433+
Box::new(src_val.unwrap()),
434+
));
435+
} else if dest_val.is_none() {
436+
*dest_val = src_val;
437+
}
438+
} else if merge_behavior == ours_mark && src_val.is_some()
439+
|| merge_behavior == theirs_mark && dest_val.is_none()
440+
{
441+
*dest_mark = src_mark;
442+
*dest_val = src_val;
443+
}
444+
}
445+
Err(pos) => {
446+
other.contents
447+
.insert(pos, (src_key, src_mark, src_val));
448+
}
449+
}
450+
}
451+
Ok(())
452+
}
453+
412454
/// Returns whether `key` has been marked as used.
413455
#[doc(hidden)]
414456
pub fn is_used<Q>(&self, key: &Q) -> bool

cfgrammar/src/lib/yacc/parser.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@ pub enum YaccGrammarErrorKind {
9292
UnknownEPP(String),
9393
ExpectedInput(char),
9494
InvalidYaccKind,
95-
InvalidYaccKindNamespace,
96-
InvalidActionKind,
97-
InvalidActionKindNamespace,
98-
InvalidGrmtoolsSectionEntry,
99-
DuplicateGrmtoolsSectionEntry,
100-
MissingGrmtoolsSection,
10195
}
10296

10397
/// Any error from the Yacc parser returns an instance of this struct.
@@ -184,17 +178,7 @@ impl fmt::Display for YaccGrammarErrorKind {
184178
name
185179
)
186180
}
187-
YaccGrammarErrorKind::MissingGrmtoolsSection => "Missing '%grmtools' section",
188-
YaccGrammarErrorKind::DuplicateGrmtoolsSectionEntry => {
189-
"Duplicate entry in %grmtools section"
190-
}
191-
YaccGrammarErrorKind::InvalidGrmtoolsSectionEntry => {
192-
"Invalid entry in %grmtools section"
193-
}
194181
YaccGrammarErrorKind::InvalidYaccKind => "Invalid yacc kind",
195-
YaccGrammarErrorKind::InvalidYaccKindNamespace => "Invalid yacc kind namespace",
196-
YaccGrammarErrorKind::InvalidActionKind => "Invalid action kind",
197-
YaccGrammarErrorKind::InvalidActionKindNamespace => "Invalid action kind namespace",
198182
};
199183
write!(f, "{}", s)
200184
}
@@ -303,12 +287,7 @@ impl Spanned for YaccGrammarError {
303287
| YaccGrammarErrorKind::UnknownRuleRef(_)
304288
| YaccGrammarErrorKind::UnknownToken(_)
305289
| YaccGrammarErrorKind::NoPrecForToken(_)
306-
| YaccGrammarErrorKind::MissingGrmtoolsSection
307-
| YaccGrammarErrorKind::InvalidGrmtoolsSectionEntry
308290
| YaccGrammarErrorKind::InvalidYaccKind
309-
| YaccGrammarErrorKind::InvalidYaccKindNamespace
310-
| YaccGrammarErrorKind::InvalidActionKind
311-
| YaccGrammarErrorKind::InvalidActionKindNamespace
312291
| YaccGrammarErrorKind::ExpectedInput(_)
313292
| YaccGrammarErrorKind::UnknownEPP(_) => SpansKind::Error,
314293
YaccGrammarErrorKind::DuplicatePrecedence
@@ -318,7 +297,6 @@ impl Spanned for YaccGrammarError {
318297
| YaccGrammarErrorKind::DuplicateImplicitTokensDeclaration
319298
| YaccGrammarErrorKind::DuplicateStartDeclaration
320299
| YaccGrammarErrorKind::DuplicateActiontypeDeclaration
321-
| YaccGrammarErrorKind::DuplicateGrmtoolsSectionEntry
322300
| YaccGrammarErrorKind::DuplicateEPP => SpansKind::DuplicationError,
323301
}
324302
}

0 commit comments

Comments
 (0)