use crate::syntax::fields::php::binary_expression;
use crate::{
query::{adj_ast, label_text},
syntax::{
builders::binary_operation::build_binary_operation_node, SyntaxGraphArgs, SyntaxGraphError,
},
CodeGraph, NodeId,
};
use alloc::borrow::ToOwned;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let operator_id = args.required_field_alt(n_id, binary_expression::OPERATOR)?;
let operator = label_text(graph, operator_id).ok_or(SyntaxGraphError::UnexpectedAstShape)?;
let mut left_id = args.required_field_alt(n_id, binary_expression::LEFT)?;
let mut right_id = args.required_field_alt(n_id, binary_expression::RIGHT)?;
if graph.label_type(left_id) == Some("parenthesized_expression") {
left_id = adj_ast(graph, left_id, Some(1), &[])
.get(1)
.copied()
.unwrap_or(left_id);
}
if graph.label_type(right_id) == Some("parenthesized_expression") {
right_id = adj_ast(graph, right_id, Some(1), &[])
.get(1)
.copied()
.unwrap_or(right_id);
}
build_binary_operation_node(
args,
n_id,
operator.to_owned(),
Some(left_id),
Some(right_id),
)
.map(Some)
}