use crate::query::{match_ast, match_ast_group_d};
use crate::syntax::builders::try_statement::build_try_statement_node;
use crate::syntax::fields::java::{try_statement, try_with_resources_statement};
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::{CodeGraph, NodeId};
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let block_node = match args.ast_graph.label_type(n_id) {
Some("try_statement") => args.required_field_alt(n_id, try_statement::BODY)?,
Some("try_with_resources_statement") => {
args.required_field_alt(n_id, try_with_resources_statement::BODY)?
}
_ => return Err(SyntaxGraphError::UnexpectedAstShape),
};
let childs = match_ast(
args.ast_graph,
n_id,
&["catch_clause", "finally_clause", "resource_specification"],
None,
);
let catch_blocks = match_ast_group_d(args.ast_graph, n_id, "catch_clause", None);
let try_block = childs.get("finally_clause").copied().flatten();
let resource_specification = childs.get("resource_specification").copied().flatten();
build_try_statement_node(
args,
n_id,
block_node,
&catch_blocks,
try_block,
resource_specification,
)
.map(Some)
}