#![allow(clippy::enum_glob_use, clippy::if_not_else, clippy::wildcard_imports)]
use std::ops::ControlFlow;
use serde::{Deserialize, Serialize};
use crate::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Span {
pub start_line: usize,
pub start_col: usize,
pub end_line: usize,
pub end_col: usize,
#[serde(default)]
pub start_byte: usize,
#[serde(default)]
pub end_byte: usize,
}
impl Span {
#[must_use]
pub fn new(
start_line: usize,
start_col: usize,
end_line: usize,
end_col: usize,
start_byte: usize,
end_byte: usize,
) -> Self {
Self {
start_line,
start_col,
end_line,
end_col,
start_byte,
end_byte,
}
}
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct AstPayload {
#[serde(default)]
pub id: String,
pub file_name: String,
pub code: String,
#[serde(default)]
pub comment: bool,
#[serde(default)]
pub span: bool,
}
#[derive(Debug, Serialize)]
pub struct AstResponse {
pub id: String,
pub language: String,
pub root: Option<AstNode>,
}
pub const MAX_AST_SERIALIZE_DEPTH: usize = 512;
fn serialize_children<S: serde::Serializer>(
children: &[AstNode],
serializer: S,
) -> Result<S::Ok, S::Error> {
crate::recursion::serialize_bounded(children, MAX_AST_SERIALIZE_DEPTH, "AstNode", serializer)
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub struct AstNode {
pub r#type: &'static str,
pub value: String,
pub span: Option<Span>,
pub field_name: Option<&'static str>,
#[serde(serialize_with = "serialize_children")]
pub children: Vec<AstNode>,
}
crate::recursion::impl_iterative_drop!(AstNode, children);
impl AstNode {
#[must_use]
pub fn new(
r#type: &'static str,
value: String,
span: Option<Span>,
children: Vec<AstNode>,
) -> Self {
Self::with_field_name(r#type, value, span, None, children)
}
#[must_use]
pub fn with_field_name(
r#type: &'static str,
value: String,
span: Option<Span>,
field_name: Option<&'static str>,
children: Vec<AstNode>,
) -> Self {
Self {
r#type,
value,
span,
field_name,
children,
}
}
}
crate::observation::counter!(cursor_builds, cfg(any(test, feature = "test-support")));
crate::observation::counter!(indexed_builds, cfg(any(test, feature = "test-support")));
struct Frame<'a> {
node: crate::Node<'a>,
field: Option<&'static str>,
children: Vec<AstNode>,
next_child: usize,
fields: bool,
}
const FIELD_PROBE_WIDTH: usize = 32;
fn has_field_child(node: &crate::Node<'_>) -> bool {
if node.is_error() {
return true;
}
(1..=node.as_tree_sitter().language().field_count())
.filter_map(|id| u16::try_from(id).ok())
.any(|id| node.child_by_field_id(id).is_some())
}
impl<'a> Frame<'a> {
fn new(node: crate::Node<'a>, field: Option<&'static str>) -> Self {
let width = node.child_count();
Self {
node,
field,
children: Vec::with_capacity(width),
next_child: 0,
fields: width < FIELD_PROBE_WIDTH || has_field_child(&node),
}
}
}
struct Export<'a, 'code> {
code: &'code [u8],
span: bool,
comment: bool,
stack: Vec<Frame<'a>>,
chain: Vec<crate::Node<'a>>,
folds: FoldCache,
}
impl<'a, 'code> Export<'a, 'code> {
fn new(root: crate::Node<'a>, code: &'code [u8], span: bool, comment: bool) -> Self {
Self {
code,
span,
comment,
stack: vec![Frame::new(root, None)],
chain: vec![root],
folds: FoldCache::default(),
}
}
fn enter(&mut self, child: crate::Node<'a>, field: Option<&'static str>) {
self.stack.push(Frame::new(child, field));
self.chain.push(child);
}
fn children_may_have_fields(&self) -> bool {
self.stack.last().is_some_and(|parent| parent.fields)
}
fn enter_at(&mut self, cursor: &crate::node::Cursor<'a>) {
let field = if self.children_may_have_fields() {
cursor.field_name()
} else {
None
};
self.enter(cursor.node(), field);
}
fn finish<T: ParserTrait>(&mut self) -> ControlFlow<Option<AstNode>> {
if crate::cancel::requested() {
return ControlFlow::Break(None);
}
let frame = self
.stack
.pop()
.expect("stack invariant: finish is called only while a frame is pending");
self.chain.pop();
let node = T::Checker::get_ast_node(
&frame.node,
self.code,
self.span,
self.comment,
frame.field,
frame.children,
Ancestors::for_walk_without_siblings(&self.chain, &frame.node, &self.folds),
);
match self.stack.last_mut() {
Some(parent) => {
parent.children.extend(node);
ControlFlow::Continue(())
}
None => ControlFlow::Break(node),
}
}
}
fn build_cursor<T: ParserTrait>(parser: &T, span: bool, comment: bool) -> Option<AstNode> {
cursor_builds::record();
let root = parser.root();
let mut export = Export::new(root, parser.code(), span, comment);
let mut cursor = root.cursor();
loop {
while cursor.goto_first_child() {
export.enter_at(&cursor);
}
loop {
if let ControlFlow::Break(root) = export.finish::<T>() {
return root;
}
if cursor.goto_next_sibling() {
export.enter_at(&cursor);
break;
}
let climbed = cursor.goto_parent();
debug_assert!(
climbed,
"the cursor must sit below the root while a frame is pending"
);
}
}
}
fn build_indexed<T: ParserTrait>(parser: &T, span: bool, comment: bool) -> Option<AstNode> {
indexed_builds::record();
let root = parser.root();
let mut export = Export::new(root, parser.code(), span, comment);
loop {
let frame = export
.stack
.last_mut()
.expect("stack invariant: loop only runs while a frame is pending");
let idx = frame.next_child;
if idx < frame.node.child_count() {
frame.next_child += 1;
let child = frame
.node
.child(idx)
.expect("stack invariant: idx < child_count so the child exists");
let field = if frame.fields {
frame.node.field_name_for_child(
u32::try_from(idx)
.expect("invariant: tree-sitter caps child indices at u32::MAX"),
)
} else {
None
};
export.enter(child, field);
} else if let ControlFlow::Break(root) = export.finish::<T>() {
return root;
}
}
}
#[derive(Debug)]
pub struct AstCfg {
pub id: String,
pub language: String,
pub comment: bool,
pub span: bool,
}
pub fn dump_inner<T: ParserTrait>(parser: &T, cfg: AstCfg) -> AstResponse {
dump_cursor_inner(parser, cfg)
}
pub fn dump_cursor_inner<T: ParserTrait>(parser: &T, cfg: AstCfg) -> AstResponse {
AstResponse {
id: cfg.id,
language: cfg.language,
root: build_cursor(parser, cfg.span, cfg.comment),
}
}
pub fn dump_indexed_inner<T: ParserTrait>(parser: &T, cfg: AstCfg) -> AstResponse {
AstResponse {
id: cfg.id,
language: cfg.language,
root: build_indexed(parser, cfg.span, cfg.comment),
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
#[cfg(any(feature = "cpp", feature = "python", feature = "rust", feature = "tcl"))]
type Dump<P> = fn(&P, AstCfg) -> AstResponse;
#[cfg(any(feature = "cpp", feature = "python", feature = "rust", feature = "tcl"))]
fn strategies<P: ParserTrait>() -> [(&'static str, Dump<P>); 2] {
[
("cursor", dump_cursor_inner),
("indexed", dump_indexed_inner),
]
}
#[cfg(any(feature = "cpp", feature = "python", feature = "rust", feature = "tcl"))]
fn cfg_with(span: bool, comment: bool) -> AstCfg {
AstCfg {
id: String::new(),
language: String::new(),
comment,
span,
}
}
#[cfg(any(feature = "cpp", feature = "python", feature = "rust", feature = "tcl"))]
fn build_ast_by<P: ParserTrait>(
dump: Dump<P>,
code: &[u8],
filename: &str,
span: bool,
comment: bool,
) -> AstNode {
let path = PathBuf::from(filename);
let parser = P::new(code.to_vec(), &path, None);
dump(&parser, cfg_with(span, comment))
.root
.expect("parser should produce a root AST node")
}
#[cfg(any(feature = "cpp", feature = "rust", feature = "tcl"))]
fn for_each_strategy<P: ParserTrait>(
code: &[u8],
filename: &str,
span: bool,
check: impl Fn(&str, &AstNode),
) {
for (name, dump) in strategies::<P>() {
check(name, &build_ast_by(dump, code, filename, span, false));
}
}
#[cfg(any(feature = "rust", feature = "tcl"))]
fn build_ast<P: ParserTrait>(code: &[u8], filename: &str) -> AstNode {
build_ast_by::<P>(dump_inner, code, filename, false, false)
}
#[cfg(feature = "rust")]
fn build_ast_with_span<P: ParserTrait>(code: &[u8], filename: &str) -> AstNode {
build_ast_by::<P>(dump_inner, code, filename, true, false)
}
#[cfg(any(feature = "python", feature = "rust"))]
fn kinds_and_fields(node: &AstNode) -> Vec<(&'static str, Option<&'static str>)> {
node.children
.iter()
.map(|c| (c.r#type, c.field_name))
.collect()
}
#[cfg(feature = "rust")]
fn count_kinds(root: &AstNode, kinds: &[&str]) -> usize {
let mut pending = vec![root];
let mut found = 0;
while let Some(node) = pending.pop() {
found += usize::from(kinds.contains(&node.r#type));
pending.extend(&node.children);
}
found
}
#[cfg(any(feature = "cpp", feature = "rust"))]
fn find_first<'a>(node: &'a AstNode, kind: &str) -> Option<&'a AstNode> {
if node.r#type == kind {
return Some(node);
}
node.children.iter().find_map(|c| find_first(c, kind))
}
#[cfg(any(feature = "cpp", feature = "rust"))]
fn find_child<'a>(parent: &'a AstNode, field: &str) -> Option<&'a AstNode> {
parent.children.iter().find(|c| c.field_name == Some(field))
}
#[cfg(feature = "rust")]
#[test]
fn a_wide_node_with_fields_keeps_them() {
const ELEMENTS: usize = 40;
const {
assert!(
2 * ELEMENTS + 1 >= FIELD_PROBE_WIDTH,
"the list must be wide enough to probe"
);
}
let code = format!("struct T({});\n", vec!["i32"; ELEMENTS].join(", "));
for_each_strategy::<crate::RustParser>(code.as_bytes(), "test.rs", false, |name, root| {
let list =
find_first(root, "ordered_field_declaration_list").expect("a tuple struct body");
let typed = list
.children
.iter()
.filter(|c| c.field_name == Some("type"))
.count();
assert_eq!(typed, ELEMENTS, "{name}");
assert!(
list.children
.iter()
.all(|c| (c.r#type == "primitive_type") == (c.field_name == Some("type"))),
"{name}"
);
});
}
#[cfg(feature = "rust")]
#[test]
fn a_wide_error_node_keeps_its_childrens_fields() {
for (elements, typed) in [(10, 9), (20, 19)] {
let code = format!("struct T({}", "i32, ".repeat(elements));
let path = PathBuf::from("test.rs");
let parser = crate::RustParser::new(code.as_bytes().to_vec(), &path, None);
let error = parser
.root()
.preorder()
.find(crate::Node::is_error)
.expect("an unfinished tuple struct recovers into an ERROR");
let raw = (0..error.child_count())
.filter_map(|i| u32::try_from(i).ok())
.filter(|&i| error.field_name_for_child(i) == Some("type"))
.count();
assert_eq!(raw, typed, "raw at {elements}");
assert_eq!(
error.child_count() >= FIELD_PROBE_WIDTH,
elements == 20,
"the two inputs must straddle the probe width"
);
for_each_strategy::<crate::RustParser>(
code.as_bytes(),
"test.rs",
false,
|name, root| {
let error = find_first(root, "ERROR").expect("the exported ERROR");
let exported = error
.children
.iter()
.filter(|c| c.field_name == Some("type"))
.count();
assert_eq!(exported, typed, "{name} at {elements}");
},
);
}
}
#[cfg(feature = "rust")]
#[test]
fn a_cancelled_export_yields_no_tree() {
let path = PathBuf::from("test.rs");
let parser = crate::RustParser::new(b"fn f() {}\n".to_vec(), &path, None);
let cancel = crate::cancel::ParseCancel::new();
let export = |dump: Dump<crate::RustParser>| {
cancel.run(|| dump(&parser, cfg_with(false, false)).root)
};
for (name, dump) in strategies::<crate::RustParser>() {
assert!(export(dump).is_some(), "{name} under a live flag");
}
cancel.cancel();
for (name, dump) in strategies::<crate::RustParser>() {
assert!(export(dump).is_none(), "{name} under a cancelled flag");
}
}
#[cfg(feature = "rust")]
#[test]
fn root_has_no_field_name() {
for_each_strategy::<crate::RustParser>(b"fn main() {}", "test.rs", false, |name, root| {
assert_eq!(root.field_name, None, "{name}");
});
}
#[cfg(feature = "rust")]
#[test]
fn rust_assignment_carries_left_and_right_field_names() {
let code = b"fn f() { let mut a = 0; a = a + 1; }";
for_each_strategy::<crate::RustParser>(code, "test.rs", false, |name, root| {
let assign = find_first(root, "assignment_expression")
.expect("expected an assignment_expression node");
let left = find_child(assign, "left").expect("expected a `left` child");
let right = find_child(assign, "right").expect("expected a `right` child");
assert_eq!(left.field_name, Some("left"), "{name}");
assert_eq!(right.field_name, Some("right"), "{name}");
assert_eq!(
kinds_and_fields(assign),
[
("identifier", Some("left")),
("=", None),
("binary_expression", Some("right")),
],
"{name}"
);
});
}
#[cfg(feature = "rust")]
#[test]
fn rust_function_carries_name_and_body_field_names() {
let code = b"fn greet(name: &str) -> &str { name }";
for_each_strategy::<crate::RustParser>(code, "test.rs", false, |name, root| {
let func = find_first(root, "function_item").expect("expected a function_item node");
let name_child =
find_child(func, "name").expect("function_item should have a name child");
assert_eq!(name_child.field_name, Some("name"), "{name}");
assert_eq!(name_child.r#type, "identifier", "{name}");
let params_child = find_child(func, "parameters")
.expect("function_item should have a parameters child");
assert_eq!(params_child.field_name, Some("parameters"), "{name}");
assert_eq!(params_child.r#type, "parameters", "{name}");
let body_child =
find_child(func, "body").expect("function_item should have a body child");
assert_eq!(body_child.field_name, Some("body"), "{name}");
assert_eq!(body_child.r#type, "block", "{name}");
});
}
#[cfg(feature = "cpp")]
#[test]
fn cpp_assignment_carries_left_and_right_field_names() {
let code = b"int main(){ int x = 0; x = x + 1; }";
for_each_strategy::<crate::CppParser>(code, "test.cpp", false, |name, root| {
let assign = find_first(root, "assignment_expression")
.expect("expected an assignment_expression node");
assert_eq!(
find_child(assign, "left").map(|n| n.r#type),
Some("identifier"),
"{name}"
);
assert_eq!(
find_child(assign, "right").map(|n| n.r#type),
Some("binary_expression"),
"{name}"
);
});
}
#[cfg(feature = "rust")]
#[test]
fn serialized_json_includes_field_name_key() {
let root = build_ast::<crate::RustParser>(b"fn f(){ let a = 1; }", "test.rs");
let json = serde_json::to_string(&root).expect("serialize");
assert!(
json.contains("\"field_name\""),
"field_name missing from JSON: {json}"
);
assert!(
json.contains("\"field_name\":\"pattern\""),
"expected pattern field name; got {json}"
);
assert!(
json.contains("\"field_name\":\"value\""),
"expected value field name; got {json}"
);
}
#[cfg(feature = "rust")]
#[test]
fn serialized_json_uses_snake_case_keys() {
let root = build_ast_with_span::<crate::RustParser>(b"fn f(){}", "test.rs");
let json = serde_json::to_string(&root).expect("serialize");
for key in [
"\"type\":",
"\"value\":",
"\"span\":",
"\"field_name\":",
"\"children\":",
] {
assert!(json.contains(key), "expected key {key}; got {json}");
}
for legacy in ["\"Type\"", "\"TextValue\"", "\"Span\"", "\"Children\""] {
assert!(
!json.contains(legacy),
"unexpected PascalCase key {legacy}; got {json}"
);
}
}
#[cfg(feature = "rust")]
#[test]
fn span_serializes_as_named_object() {
for_each_strategy::<crate::RustParser>(b"fn f(){}", "test.rs", true, |name, root| {
let span = root
.span
.expect("root span present when span tracking is on");
assert_eq!(span, Span::new(1, 1, 1, 9, 0, 8), "{name}");
let json = serde_json::to_string(&root.span).expect("serialize span");
assert!(
json.contains("\"start_line\":1")
&& json.contains("\"start_col\":1")
&& json.contains("\"end_line\":1")
&& json.contains("\"end_col\":9")
&& json.contains("\"start_byte\":0")
&& json.contains("\"end_byte\":8"),
"expected named span object with byte offsets; got {json}"
);
assert!(
!json.contains("start_row") && !json.contains("end_row"),
"unexpected pre-2.0 *_row span keys; got {json}"
);
});
}
#[test]
fn span_round_trips_through_serde() {
let span = Span::new(2, 3, 4, 5, 11, 42);
let json = serde_json::to_string(&span).expect("serialize");
let back: Span = serde_json::from_str(&json).expect("deserialize");
assert_eq!(span, back);
}
#[test]
fn span_deserializes_pre_byte_offsets_with_default() {
let legacy = r#"{"start_line":2,"start_col":3,"end_line":4,"end_col":5}"#;
let span: Span = serde_json::from_str(legacy).expect("deserialize legacy span");
assert_eq!(span, Span::new(2, 3, 4, 5, 0, 0));
}
fn ast_chain(depth: usize) -> AstNode {
let mut node = AstNode::new("leaf", String::new(), None, Vec::new());
for _ in 0..depth {
node = AstNode::new("branch", String::new(), None, vec![node]);
}
node
}
#[test]
fn ast_depth_at_the_serialize_limit_is_accepted_and_one_deeper_is_not() {
let at_limit = ast_chain(MAX_AST_SERIALIZE_DEPTH);
serde_json::to_string(&at_limit).expect("the limit itself must serialize");
let past_limit = ast_chain(MAX_AST_SERIALIZE_DEPTH + 1);
let err = serde_json::to_string(&past_limit).expect_err("one deeper must be refused");
assert!(
err.to_string().contains(&format!(
"AstNode nesting is deeper than the serialization limit of \
{MAX_AST_SERIALIZE_DEPTH} levels"
)),
"the error must name the type and the limit, got: {err}"
);
}
#[cfg(feature = "tcl")]
fn ast_depth(root: &AstNode) -> usize {
let mut deepest = 0;
let mut stack = vec![(root, 1usize)];
while let Some((node, depth)) = stack.pop() {
deepest = deepest.max(depth);
stack.extend(node.children.iter().map(|child| (child, depth + 1)));
}
deepest
}
#[cfg(feature = "tcl")]
#[test]
fn a_tcl_brace_level_costs_three_ast_levels() {
let levels = 64;
let code = format!("{}puts hi{}", "eval {".repeat(levels), "}".repeat(levels));
let root = build_ast::<crate::TclParser>(code.as_bytes(), "deep.tcl");
let depth = ast_depth(&root);
let per_level = depth / levels;
assert!(
(3..=4).contains(&per_level),
"a braced script level should cost 3 AstNode levels \
(braced_word -> command -> word_list); measured {depth} \
levels over {levels} braces = {per_level}"
);
let ceiling = MAX_AST_SERIALIZE_DEPTH / per_level;
assert!(
(120..=180).contains(&ceiling),
"the effective Tcl brace ceiling should sit near 170, got \
{ceiling} from {per_level} levels per brace"
);
}
#[cfg(feature = "rust")]
#[test]
fn each_entry_point_enters_exactly_its_own_builder() {
let parser = crate::RustParser::new(b"fn f() {}".to_vec(), &PathBuf::from("a.rs"), None);
let _ = dump_cursor_inner(&parser, cfg_with(false, false));
let _ = dump_indexed_inner(&parser, cfg_with(false, false));
let (cursor_seed, indexed_seed) = (cursor_builds::observed(), indexed_builds::observed());
assert!(
cursor_seed > 0 && indexed_seed > 0,
"the seed calls must be counted"
);
let entries: [(&str, Dump<crate::RustParser>, usize, usize); 3] = [
("dump_inner", dump_inner, 1, 0),
("dump_cursor_inner", dump_cursor_inner, 1, 0),
("dump_indexed_inner", dump_indexed_inner, 0, 1),
];
let (mut cursor_total, mut indexed_total) = (cursor_seed, indexed_seed);
for (name, dump, cursor_delta, indexed_delta) in entries {
let _ = dump(&parser, cfg_with(false, false));
cursor_total += cursor_delta;
indexed_total += indexed_delta;
assert_eq!(
(cursor_builds::observed(), indexed_builds::observed()),
(cursor_total, indexed_total),
"{name} must enter the cursor builder {cursor_delta} time(s) and the indexed \
builder {indexed_delta} time(s)"
);
}
}
#[cfg(feature = "rust")]
#[test]
fn a_cursor_reports_fields_below_its_start_and_cannot_climb_above_it() {
let parser = crate::RustParser::new(
b"fn f(a: u8) { let b = a; }".to_vec(),
&PathBuf::from("a.rs"),
None,
);
let func = parser
.root()
.children()
.find(|n| n.kind() == "function_item")
.expect("fixture has a function");
let mut cursor = func.cursor();
assert_eq!(cursor.node().id(), func.id());
assert_eq!(cursor.field_name(), None);
assert!(!cursor.goto_parent(), "the start node is the ceiling");
assert_eq!(
cursor.node().id(),
func.id(),
"a refused climb must not move"
);
assert!(cursor.goto_first_child());
let mut fields = Vec::new();
loop {
fields.push((cursor.node().kind(), cursor.field_name()));
if !cursor.goto_next_sibling() {
break;
}
}
assert_eq!(
fields,
[
("fn", None),
("identifier", Some("name")),
("parameters", Some("parameters")),
("block", Some("body")),
],
"fields read off the cursor must match the grammar's"
);
assert!(cursor.goto_parent());
assert_eq!(cursor.node().id(), func.id());
assert!(
!cursor.goto_parent(),
"back at the start node, the ceiling holds"
);
let let_decl = func
.preorder()
.find(|n| n.kind() == "let_declaration")
.expect("fixture has a let");
let mut from_parent = let_decl.cursor();
assert!(from_parent.goto_first_child() && from_parent.goto_next_sibling());
assert_eq!(from_parent.node().kind(), "identifier");
assert_eq!(from_parent.field_name(), Some("pattern"));
cursor.reset(&from_parent.node());
assert_eq!(
cursor.node().id(),
from_parent.node().id(),
"the reset must move the cursor before its answer means anything"
);
assert_eq!(
cursor.field_name(),
None,
"after a reset the new start node hides its field exactly as the original did"
);
}
#[cfg(feature = "python")]
#[test]
fn each_strategy_reports_a_field_inherited_through_a_hidden_rule() {
let code = b"a < b <= c\n";
for (name, dump) in strategies::<crate::PythonParser>() {
let root = build_ast_by(dump, code, "c.py", false, false);
let comparison = kinds_and_fields(&root.children[0].children[0]);
assert_eq!(
comparison,
[
("identifier", None),
("<", Some("operators")),
("identifier", None),
("<=", Some("operators")),
("identifier", None),
],
"{name}"
);
}
}
#[cfg(feature = "rust")]
#[test]
fn each_strategy_filters_comments_only_when_asked() {
let code = b"// lead\nfn f() {\n // inner\n let a = 1; /* trail */\n}\n";
for (name, dump) in strategies::<crate::RustParser>() {
for (comment, expected) in [(false, 3), (true, 0)] {
let root = build_ast_by(dump, code, "c.rs", true, comment);
let comments = count_kinds(&root, &["line_comment", "block_comment"]);
assert_eq!(comments, expected, "{name}, comment={comment}");
assert_eq!(
count_kinds(&root, &["let_declaration"]),
1,
"{name}, comment={comment}: the `let` must survive the filter"
);
}
}
}
#[cfg(feature = "rust")]
#[test]
fn each_strategy_echoes_the_request_metadata() {
let parser = crate::RustParser::new(b"fn f() {}".to_vec(), &PathBuf::from("a.rs"), None);
let default: (&str, Dump<crate::RustParser>) = ("default", dump_inner);
for (name, dump) in strategies().into_iter().chain([default]) {
let resp = dump(
&parser,
AstCfg {
id: "req-42".to_owned(),
language: "rust".to_owned(),
comment: false,
span: false,
},
);
assert_eq!(
(resp.id.as_str(), resp.language.as_str()),
("req-42", "rust"),
"{name}"
);
assert_eq!(resp.root.map(|r| r.r#type), Some("source_file"), "{name}");
}
}
#[cfg(feature = "tcl")]
#[test]
fn each_strategy_hands_the_alterator_the_real_parent() {
let code = b"{puts} hi\neval {puts a}\n";
for_each_strategy::<crate::TclParser>(code, "name.tcl", false, |name, root| {
let braced: Vec<(&str, Vec<&str>)> = root
.children
.iter()
.flat_map(|command| command.children.iter())
.flat_map(|word| {
std::iter::once(word).chain(word.children.iter())
})
.filter(|node| node.r#type == "braced_word")
.map(|node| {
let kinds: Vec<&str> = node.children.iter().map(|c| c.r#type).collect();
(node.value.as_str(), kinds)
})
.collect();
assert_eq!(
braced,
[("{puts}", vec![]), ("", vec!["{", "command", "}"])],
"{name}"
);
});
}
#[cfg(feature = "tcl")]
#[test]
fn each_strategy_scans_an_erroring_argument_list_once() {
use crate::lang_helpers::argument_slot_scans;
let width = 200;
let code = crate::test_support::tcl_wide_erroring_eval(width);
for (name, dump) in strategies::<crate::TclParser>() {
let before = argument_slot_scans::observed();
let root = build_ast_by(dump, code.as_bytes(), "wide.tcl", false, false);
assert_eq!(
argument_slot_scans::observed() - before,
1,
"{name}: the export lends one cache for the whole walk"
);
let kept = root
.children
.iter()
.flat_map(|command| command.children.iter())
.flat_map(|list| list.children.iter())
.filter(|word| word.r#type == "braced_word" && !word.children.is_empty())
.count();
assert_eq!(
kept, width,
"{name}: every `eval` argument keeps its children"
);
}
}
#[cfg(feature = "rust")]
#[test]
fn a_comment_only_source_exports_a_childless_root_under_either_strategy() {
let code = b"// only\n";
for (name, dump) in strategies::<crate::RustParser>() {
let keep = build_ast_by(dump, code, "a.rs", false, false);
assert_eq!(
keep.children.len(),
1,
"{name}: without the filter the root keeps its one comment child"
);
let root = build_ast_by(dump, code, "a.rs", false, true);
assert_eq!(
kinds_and_fields(&root),
[],
"{name}: the comment child must be filtered"
);
}
}
#[test]
fn a_pathologically_deep_ast_errors_and_tears_down_without_overflowing() {
const DEPTH: usize = 50_000;
const PRODUCTION_STACK: usize = 2 * 1024 * 1024;
let message = std::thread::Builder::new()
.stack_size(PRODUCTION_STACK)
.spawn(|| {
let root = ast_chain(DEPTH);
let message = serde_json::to_string(&root)
.expect_err("depth past the limit must fail, not serialize")
.to_string();
message
})
.expect("spawn bounded-stack thread")
.join()
.expect("bounded-stack thread must not overflow");
assert!(
message.contains("AstNode nesting is deeper than the serialization limit"),
"the error must explain the refusal, got: {message}"
);
}
}