use crate::{
query::{adj_ast, match_ast_d},
syntax::{
builders::{
for_each_statement::build_for_each_statement_node,
for_statement::build_for_statement_node,
},
fields::{swift::for_statement, Field},
SyntaxGraphArgs, SyntaxGraphError,
},
NodeId,
};
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
const COLLECTION: Field<false> = Field::new("collection_id");
let graph = args.ast_graph;
let childs = adj_ast(graph, n_id, Some(1), &[]);
let body_id = match_ast_d(graph, n_id, "statements", Some(1))
.or_else(|| childs.iter().rev().nth(1).copied())
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
let var_node = args.required_field_alt(n_id, for_statement::ITEM)?;
if let Some(it_item) = args.optional_field_alt(n_id, COLLECTION) {
return build_for_each_statement_node(args, n_id, var_node, it_item, Some(body_id))
.map(Some);
}
build_for_statement_node(args, n_id, None, None, None, body_id).map(Some)
}