use crate::query::{adj_ast, match_ast};
use crate::syntax::builders::while_statement::build_while_statement_node;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::{CodeGraph, NodeId};
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let match_childs = match_ast(graph, n_id, &["parenthesized_expression", "block"], None);
let mut block_id = match match_childs.get("block").copied().flatten() {
Some(block_id) => block_id,
None => adj_ast(graph, n_id, None, &[])
.last()
.copied()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?,
};
if graph.label_type(block_id) == Some("expression_statement") {
block_id = adj_ast(graph, block_id, None, &[])
.first()
.copied()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
}
let conditional = match_childs
.get("parenthesized_expression")
.copied()
.flatten()
.and_then(|p_id| {
match_ast(graph, p_id, &[], None)
.get("__1__")
.copied()
.flatten()
});
build_while_statement_node(args, n_id, block_id, conditional).map(Some)
}