Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn tokenize(line: &str) -> StrResult<Line> {
Line::ModuleEnd
} else if let Some(rest) = head.strip_prefix('.') {
for part in rest.split('.') {
validate_ident(part)?;
validate_ident(part.strip_suffix('?').unwrap_or(part))?;
}
let value = decode_value(tail.ok_or("missing char")?)?;
Line::Variant(ModifierSet::from_raw_dotted(rest), value)
Expand Down
92 changes: 90 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Symbol {
/// Possible modifiers for this symbol.
pub fn modifiers(&self) -> impl Iterator<Item = &str> + '_ {
self.variants()
.flat_map(|(m, _, _)| m.into_iter())
.flat_map(|(m, _, _)| m.into_iter().map(|m| m.name()))
.collect::<std::collections::BTreeSet<_>>()
.into_iter()
}
Expand Down Expand Up @@ -170,14 +170,17 @@ mod test {
};
let variants = s
.variants()
.map(|(m, v, _)| (m.into_iter().collect::<BTreeSet<_>>(), v))
.map(|(m, v, _)| {
(m.into_iter().map(|m| m.as_str()).collect::<BTreeSet<_>>(), v)
})
.collect::<BTreeSet<_>>();
let control = control
.iter()
.map(|&(m, v)| {
(
ModifierSet::from_raw_dotted(m)
.into_iter()
.map(|m| m.as_str())
.collect::<BTreeSet<_>>(),
v,
)
Expand All @@ -187,4 +190,89 @@ mod test {
assert_eq!(variants, control);
}
}

#[test]
fn no_overlap() {
recur("", ROOT);

/// Iterate over all symbols in a module, recursing into submodules.
fn recur(prefix: &str, m: Module) {
for (name, b) in m.iter() {
match b.def {
Def::Module(m) => {
let new_prefix = if prefix.is_empty() {
name.to_string()
} else {
prefix.to_string() + "." + name
};
recur(&new_prefix, m);
}
Def::Symbol(s) => check_symbol(prefix, name, s),
}
}
}

/// Check the no overlap rule for a single symbol
fn check_symbol(prefix: &str, name: &str, sym: Symbol) {
// maximum number of modifiers per variant (we don't need to check more than this).
let max_modifs =
sym.variants().map(|(m, ..)| m.iter().count()).max().unwrap();
let modifs = sym.modifiers().collect::<Vec<_>>();
let max_index = modifs.len().saturating_sub(1);

for k in 0..=max_modifs {
let mut indices = (0..k).collect::<Vec<_>>();
loop {
let mset = indices.iter().map(|i| modifs[*i]).fold(
ModifierSet::<String>::default(),
|mut res, m| {
res.insert_raw(m);
res
},
);
Comment on lines +226 to +232
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should have an impl<S: Deref<Target = str>> FromIterator<S> for ModifierSet<String> instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't like that, see #46 (comment).
Maybe we could have an inherent method from_iter_raw that documents all of those requirements (S::default() must be empty, each item must be a single modifier). To use that like collect, we'd also need an iterator extension trait, which seems overkill, but maybe calling it directly on an iterator is already ergonomic enough?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh indeed you are right! I had forgotten I already suggested a similar thing before.

I think having from_iter_raw could be useful.


if sym.variants().filter(|(m, ..)| mset.is_candidate(*m)).count() > 1
{
panic!(
"Overlap in symbol {prefix}.{name} for modifiers {}",
mset.as_str()
);
}

if next_subseq(&mut indices, max_index).is_none() {
break;
}
}
}
}

/// Produce the next term in a sequence like
/// ```text
/// [0,1,2], [0,1,3], [0,1,4], [0,2,3], [0,2,4], [0,3,4], [1,2,3], [1,2,4], [1,3,4], [2,3,4]
/// ```
fn next_subseq(indices: &mut [usize], max_index: usize) -> Option<()> {
// invariant: indices.len() <= max_index + 1
match indices {
[] => None,
[single] => {
if *single < max_index {
*single += 1;
Some(())
} else {
None
}
}
[left @ .., last] => {
assert_ne!(max_index, 0);
if *last < max_index {
*last += 1;
} else {
next_subseq(left, max_index - 1)?;
*last = left.last().copied().map_or(*last, |x| x + 1);
}
Some(())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if *last < max_index {
*last += 1;
} else {
next_subseq(left, max_index - 1)?;
*last = left.last().copied().map_or(*last, |x| x + 1);
}
Some(())
if *last < max_index {
*last += 1;
} else {
*last = next_subseq(left, max_index - 1)?;
}
Some(*last)

This seems more idiomatic to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree. next_subseq shouldn't return anything since the "return value" is already its first argument. Adding a second return value makes the API harder to understand IMO.

}
}
}
}
}
Loading