use crate::query::match_ast;
use crate::syntax::builders::for_statement::build_for_statement_node;
use crate::syntax::fields::go::{for_clause, for_statement};
use crate::syntax::fields::Field;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::NodeId;
const RANGE_LEFT: Field<false> = Field::new("left_id");
const RANGE_RIGHT: Field<false> = Field::new("right_id");
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let body_id = args.required_field_alt(n_id, for_statement::BODY)?;
let childs = match_ast(
graph,
n_id,
&["for", "block", "for_clause", "range_clause"],
None,
);
if let Some(clause) = childs.get("range_clause").copied().flatten() {
let var_node = args.optional_field_alt(clause, RANGE_LEFT);
let condition_node = args.optional_field_alt(clause, RANGE_RIGHT);
return build_for_statement_node(args, n_id, var_node, condition_node, None, body_id)
.map(Some);
}
if let Some(clause) = childs.get("for_clause").copied().flatten() {
let init_id = args.optional_field_alt(clause, for_clause::INITIALIZER);
let condition_id = args.optional_field_alt(clause, for_clause::CONDITION);
let update_id = args.optional_field_alt(clause, for_clause::UPDATE);
return build_for_statement_node(args, n_id, init_id, condition_id, update_id, body_id)
.map(Some);
}
let condition_id = childs.get("__0__").copied().flatten();
build_for_statement_node(args, n_id, None, condition_id, None, body_id).map(Some)
}