use crate::node::{Ancestors, Node, Tree};
use crate::traits::{LanguageInfo, ParserTrait, Search};
pub fn owners_covered(
seen: &std::collections::BTreeSet<u16>,
owners: &[&[u16]],
defensive: &[u16],
) {
for &k in defensive {
assert!(
owners.iter().copied().flatten().any(|&o| o == k),
"defensive kind {k} is no longer declared by any row"
);
assert!(
!seen.contains(&k),
"defensive kind {k} is now emitted; its row needs a look"
);
}
for &k in owners.iter().copied().flatten() {
if !defensive.contains(&k) {
assert!(seen.contains(&k), "fixture never emits kind {k}");
}
}
}
#[cfg(test)]
#[derive(PartialEq)]
pub(crate) struct Labelled<const N: usize>(
pub(crate) [bool; N],
pub(crate) &'static [&'static str; N],
);
#[cfg(test)]
impl<const N: usize> std::fmt::Debug for Labelled<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut set = f.debug_set();
for (name, on) in self.1.iter().zip(self.0) {
if on {
set.entry(&format_args!("{name}"));
}
}
set.finish()
}
}
#[cfg(test)]
#[cfg_attr(not(feature = "all-languages"), allow(unused_macros))]
macro_rules! slots {
($($name:ident),+ $(,)?) => {
pub(super) const NAMES: [&str; [$(stringify!($name)),+].len()] =
[$(stringify!($name)),+];
$crate::test_support::slots!(@at 0; $($name),+);
};
(@at $index:expr; $name:ident $(, $rest:ident)*) => {
pub(super) const $name: usize = $index;
$crate::test_support::slots!(@at $index + 1; $($rest),*);
};
(@at $index:expr;) => {};
}
#[cfg(test)]
pub(crate) use slots;
#[cfg(test)]
#[test]
fn labelled_prints_the_true_slots_by_name() {
const NAMES: [&str; 3] = ["FIRST", "SECOND", "THIRD"];
assert_eq!(
format!("{:?}", Labelled([true, false, true], &NAMES)),
"{FIRST, THIRD}"
);
assert_eq!(format!("{:?}", Labelled([false; 3], &NAMES)), "{}");
}
#[cfg(all(test, any(feature = "kotlin", feature = "python")))]
pub(crate) fn nesting_among(
node_types: &str,
skipped: fn(&str) -> bool,
) -> std::collections::BTreeMap<String, std::collections::BTreeSet<String>> {
let node_types: serde_json::Value =
serde_json::from_str(node_types).expect("the grammar crate ships valid node-types.json");
let types: Vec<&serde_json::Value> = node_types.as_array().into_iter().flatten().collect();
let names = |listed: &serde_json::Value| -> Vec<String> {
let listed = listed.as_array().into_iter().flatten();
listed
.filter_map(|entry| entry["type"].as_str().map(str::to_owned))
.collect()
};
let subtypes: std::collections::BTreeMap<&str, Vec<String>> = types
.iter()
.filter(|node| node["subtypes"].is_array())
.filter_map(|node| Some((node["type"].as_str()?, names(&node["subtypes"]))))
.collect();
let mut nesting = std::collections::BTreeMap::new();
for node in &types {
let Some(parent) = node["type"].as_str().filter(|kind| skipped(kind)) else {
continue;
};
let mut pending = names(&node["children"]["types"]);
for (_, field) in node["fields"].as_object().into_iter().flatten() {
pending.extend(names(&field["types"]));
}
let mut held = std::collections::BTreeSet::new();
while let Some(kind) = pending.pop() {
match subtypes.get(kind.as_str()) {
Some(concrete) => pending.extend(concrete.iter().cloned()),
None if skipped(&kind) => drop(held.insert(kind)),
None => {}
}
}
if !held.is_empty() {
nesting.insert(parent.to_owned(), held);
}
}
nesting
}
pub fn for_each_node_with_chain<L: LanguageInfo>(
code: &[u8],
check: impl FnMut(&Node<'_>, &[Node<'_>]),
) -> usize {
walk_with_chain(&clean_tree::<L>(code), check)
}
pub fn for_each_node_with_chain_tolerating_errors<L: LanguageInfo>(
code: &[u8],
check: impl FnMut(&Node<'_>, &[Node<'_>]),
) -> usize {
walk_with_chain(&Tree::new::<L>(code), check)
}
pub fn for_each_node_with_sibling_cache<L: LanguageInfo>(
code: &[u8],
mut check: impl for<'t, 'c> FnMut(&Node<'t>, Ancestors<'t, 'c>),
) -> usize {
let tree = clean_tree::<L>(code);
let mut visited = 0;
tree.get_root().act_on_node(&mut |node, ancestors| {
visited += 1;
check(node, ancestors);
});
visited
}
fn clean_tree<L: LanguageInfo>(code: &[u8]) -> Tree {
let tree = Tree::new::<L>(code);
assert!(
!tree.get_root().has_error(),
"fixture must parse cleanly, else the walk covers error recovery"
);
tree
}
pub fn for_each_node_with_sibling_cache_tolerating_errors<L: LanguageInfo>(
code: &[u8],
mut check: impl for<'t, 'c> FnMut(&Node<'t>, Ancestors<'t, 'c>),
) -> (usize, bool) {
let tree = Tree::new::<L>(code);
let recovered = tree.get_root().has_error();
let mut visited = 0;
tree.get_root().act_on_node(&mut |node, ancestors| {
visited += 1;
check(node, ancestors);
});
(visited, recovered)
}
#[cfg(all(test, feature = "rust"))]
#[test]
fn the_tolerant_walker_labels_recovery_and_lends_the_same_chain() {
use crate::RustCode;
let clean = b"fn f() { let x = 1; }\n";
let broken = b"fn f() { let x = ; }\n";
let mut parents_clean = Vec::new();
let clean_count = for_each_node_with_sibling_cache::<RustCode>(clean, |node, ancestors| {
parents_clean.push(ancestors.parent(node).map(|p| p.kind()));
});
let mut parents_tolerant = Vec::new();
let (tolerant_count, recovered) =
for_each_node_with_sibling_cache_tolerating_errors::<RustCode>(clean, |node, ancestors| {
assert!(
ancestors.depth().is_some(),
"the wrapper lends the walk's chain"
);
parents_tolerant.push(ancestors.parent(node).map(|p| p.kind()));
});
assert!(!recovered, "a clean parse is labelled clean");
assert_eq!(tolerant_count, clean_count);
assert_eq!(
parents_tolerant, parents_clean,
"the same chain under both walkers"
);
assert!(parents_clean.contains(&Some("let_declaration")));
let (count, recovered) =
for_each_node_with_sibling_cache_tolerating_errors::<RustCode>(broken, |_, _| {});
assert!(recovered, "a parse with an ERROR is labelled recovered");
assert!(count > 1);
}
fn walk_with_chain(tree: &Tree, mut check: impl FnMut(&Node<'_>, &[Node<'_>])) -> usize {
let mut chain: Vec<Node<'_>> = Vec::new();
let mut stack = vec![(tree.get_root(), 0_usize)];
let mut visited = 0;
while let Some((node, depth)) = stack.pop() {
chain.truncate(depth);
check(&node, &chain);
visited += 1;
chain.push(node);
let first = stack.len();
stack.extend(node.children().map(|child| (child, depth + 1)));
stack[first..].reverse();
}
visited
}
fn output_submodule() -> std::path::PathBuf {
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("the ast crate sits under the workspace root")
.join("tests/repositories/big-code-analysis-output")
}
#[must_use]
pub fn control_flow_corpus_present() -> bool {
let path = output_submodule();
match std::fs::read_dir(&path) {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => false,
Err(e) => panic!(
"{}: cannot enumerate the output submodule: {e}",
path.display()
),
Ok(mut entries) => entries
.next()
.map(|entry| {
entry.unwrap_or_else(|e| {
panic!(
"{}: unreadable entry in the output submodule: {e}",
path.display()
)
});
})
.is_some(),
}
}
#[must_use]
pub fn control_flow_sources(lang: crate::LANG) -> Option<Vec<(String, Vec<u8>)>> {
use crate::LANG;
let dir = match lang {
LANG::Ccomment | LANG::Preproc => return Some(Vec::new()),
LANG::Mozcpp => "cpp".to_owned(),
LANG::Mozjs => "javascript".to_owned(),
other => format!("{other:?}").to_lowercase(),
};
if !control_flow_corpus_present() {
return None;
}
let root = output_submodule().join("control_flow").join(&dir);
assert!(
root.is_dir(),
"{} missing; run `make worktree-setup` (the output submodule predates control_flow/)",
root.display()
);
let mut paths: Vec<std::path::PathBuf> = std::fs::read_dir(&root)
.expect("readable corpus directory")
.map(|e| e.expect("readable corpus entry").path())
.filter(|p| p.is_file())
.collect();
paths.sort();
assert!(!paths.is_empty(), "{} holds no fixture", root.display());
Some(
paths
.into_iter()
.map(|path| {
let file = path
.file_name()
.and_then(|n| n.to_str())
.expect("UTF-8 fixture name");
let bytes = crate::tools::read_file_with_eol(&path)
.expect("readable fixture")
.expect("a fixture is not a binary file");
(format!("control_flow/{dir}/{file}"), bytes)
})
.collect(),
)
}
pub fn ast_has_kind_id<P: ParserTrait>(parser: &P, target: u16) -> bool {
parser.root().preorder().any(|n| n.kind_id() == target)
}
#[must_use]
pub fn tcl_wide_erroring_eval(width: usize) -> String {
let half = "{a b} ".repeat(width / 2);
format!("eval {half}] {half}\n")
}
#[must_use]
pub fn rust_root_of_width(width: usize) -> String {
format!(
"{}{}",
"#[cfg(test)]\nfn a() {}\n".repeat(width / 2),
"fn z() {}\n".repeat(width % 2),
)
}
#[track_caller]
pub fn assert_classifies(predicate: fn(&[u8]) -> bool, yes: &[&[u8]], no: &[&[u8]]) {
for input in yes {
assert!(predicate(input), "{}", String::from_utf8_lossy(input));
}
for input in no {
assert!(!predicate(input), "{}", String::from_utf8_lossy(input));
}
}