#![allow(
clippy::enum_glob_use,
clippy::too_many_lines,
clippy::wildcard_imports
)]
#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
use super::{
Abc, Stats, count_boolean_slot, count_each_operand, count_field_operands,
count_negated_operand, peel, wrapped_operand,
};
use crate::*;
fn java_wrapper_operand<'a>(node: &Node<'a>) -> Option<(Node<'a>, bool)> {
use Java::*;
match node.kind_id().into() {
ParenthesizedExpression => wrapped_operand(node).map(|o| (o, false)),
CastExpression => node.child_by_field_name("value").map(|o| (o, false)),
UnaryExpression if node.child(0).is_some_and(|c| c.kind_id() == BANG as u16) => {
node.child_by_field_name("operand").map(|o| (o, true))
}
_ => None,
}
}
fn java_condition_scores_itself(expr: &Node) -> bool {
use Java::*;
match expr.kind_id().into() {
TernaryExpression | InstanceofExpression => true,
BinaryExpression => expr.child_by_field_name("operator").is_some_and(|op| {
matches!(
op.kind_id().into(),
EQEQ | BANGEQ | LT | GT | LTEQ | GTEQ | AMPAMP | PIPEPIPE
)
}),
AssignmentExpression => expr.child_by_field_name("right").is_some_and(|right| {
let (value, proves_boolean) = peel(&right, java_wrapper_operand);
proves_boolean || java_condition_scores_itself(&value)
}),
_ => false,
}
}
fn java_count_condition(condition: &Node, conditions: &mut f64) {
count_boolean_slot(
condition,
java_wrapper_operand,
java_condition_scores_itself,
conditions,
);
}
fn java_count_negated(operand: &Node, conditions: &mut f64) {
count_negated_operand(
operand,
java_wrapper_operand,
java_condition_scores_itself,
conditions,
);
}
fn java_count_slot(slot: Option<Node>, conditions: &mut f64) {
if let Some(condition) = slot {
java_count_condition(&condition, conditions);
}
}
fn java_count_negated_slot(slot: Option<Node>, conditions: &mut f64) {
if let Some(operand) = slot {
java_count_negated(&operand, conditions);
}
}
fn java_eq_initializes_final_binding<'a>(eq_node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> bool {
use Java::*;
eq_node.parent_grandparent_match(
ancestors,
|parent| parent.kind_id() == VariableDeclarator,
|declaration| {
matches!(
declaration.kind_id().into(),
LocalVariableDeclaration | FieldDeclaration
) && declaration
.children()
.take_while(|child| child.kind_id() != VariableDeclarator)
.any(|child| child.kind_id() == Modifiers && child.is_child(Final as u16))
},
)
}
fn java_count_token_assignment<'a>(
node: &Node<'a>,
ancestors: Ancestors<'a, '_>,
stats: &mut Stats,
) -> bool {
use Java::*;
match node.kind_id().into() {
STAREQ | SLASHEQ | PERCENTEQ | DASHEQ | PLUSEQ | LTLTEQ | GTGTEQ | AMPEQ | PIPEEQ
| CARETEQ | GTGTGTEQ | PLUSPLUS | DASHDASH => {
stats.assignments += 1.;
}
EQ => {
if !java_eq_initializes_final_binding(node, ancestors) {
stats.assignments += 1.;
}
}
_ => return false,
}
true
}
fn java_count_token_branch(node: &Node, stats: &mut Stats) -> bool {
use Java::*;
let is_branch = match node.kind_id().into() {
MethodInvocation | New | ExplicitConstructorInvocation => true,
EnumConstant => node.is_child(ArgumentList as u16),
_ => false,
};
if is_branch {
stats.branches += 1.;
}
is_branch
}
fn java_count_token_condition<'a>(
node: &Node<'a>,
ancestors: Ancestors<'a, '_>,
stats: &mut Stats,
) -> bool {
use Java::*;
match node.kind_id().into() {
GTEQ | LTEQ | EQEQ | BANGEQ | Else | Case | Try | Catch | InstanceofExpression => {
stats.conditions += 1.;
}
QMARK => {
if ancestors
.parent(node)
.is_some_and(|parent| matches!(parent.kind_id().into(), TernaryExpression))
{
stats.conditions += 1.;
}
}
GT | LT => {
if ancestors
.parent(node)
.is_some_and(|parent| matches!(parent.kind_id().into(), BinaryExpression))
{
stats.conditions += 1.;
}
}
_ => return false,
}
true
}
fn java_walk_for_conditions<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>, stats: &mut Stats) {
use Java::*;
let conds = &mut stats.conditions;
match node.kind_id().into() {
AMPAMP | PIPEPIPE => {
if let Some(chain) = ancestors
.parent(node)
.filter(|p| p.kind_id() == BinaryExpression)
{
count_field_operands(&chain, java_count_condition, conds);
}
}
ArgumentList => count_each_operand(node, java_count_negated, conds),
IfStatement | WhileStatement | DoStatement => {
java_count_slot(node.child_by_field_name("condition"), conds);
}
ReturnStatement => java_count_negated_slot(wrapped_operand(node), conds),
Guard => java_count_slot(wrapped_operand(node), conds),
VariableDeclarator => java_count_negated_slot(node.child_by_field_name("value"), conds),
AssignmentExpression => java_count_negated_slot(node.child_by_field_name("right"), conds),
LambdaExpression => java_count_negated_slot(node.child_by_field_name("body"), conds),
TernaryExpression => java_walk_ternary(node, stats),
ForStatement => java_walk_for_statement(node, stats),
_ => {}
}
}
fn java_walk_ternary(node: &Node, stats: &mut Stats) {
let conds = &mut stats.conditions;
if let Some(condition) = node.child_by_field_name("condition") {
java_count_condition(&condition, conds);
}
for field in ["consequence", "alternative"] {
java_count_negated_slot(node.child_by_field_name(field), conds);
}
}
fn java_walk_for_statement(node: &Node, stats: &mut Stats) {
if let Some(condition) = node.child_by_field_name("condition") {
java_count_condition(&condition, &mut stats.conditions);
}
}
impl Abc for JavaCode {
fn compute<'a>(
node: &Node<'a>,
_code: &'a [u8],
ancestors: Ancestors<'a, '_>,
stats: &mut Stats,
) {
if java_count_token_assignment(node, ancestors, stats) {
return;
}
if java_count_token_branch(node, stats) {
return;
}
if java_count_token_condition(node, ancestors, stats) {
return;
}
java_walk_for_conditions(node, ancestors, stats);
}
}