use crate::query::{adj_ast, match_ast};
use crate::syntax::builders::else_clause::build_else_clause_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 mut body_id = match_ast(graph, n_id, &["else"], None)
.get("__0__")
.copied()
.flatten();
if let Some(id) = body_id {
if graph.label_type(id) == Some("expression_statement") {
body_id = match_ast(graph, id, &[], None)
.get("__0__")
.copied()
.flatten();
}
}
if let Some(id) = body_id {
if graph.label_type(id) == Some("parenthesized_expression") {
body_id = match_ast(graph, id, &[], None)
.get("__1__")
.copied()
.flatten();
}
}
let body_id = match body_id {
Some(id) => id,
None => *adj_ast(graph, n_id, None, &[])
.last()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?,
};
build_else_clause_node(args, n_id, body_id).map(Some)
}