use alloc::borrow::ToOwned;
use alloc::string::String;
use crate::query::label_text;
use crate::syntax::builders::literal::build_literal_node;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::{CodeGraph, NodeId};
const INTEGER_TYPE_SUFFIXES: [char; 2] = ['l', 'L'];
const FLOAT_TYPE_SUFFIXES: [char; 4] = ['d', 'D', 'f', 'F'];
fn normalize_literal(value: &str, label_type: Option<&str>) -> String {
let suffixes: &[char] = if label_type.is_some_and(|kind| kind.ends_with("integer_literal")) {
&INTEGER_TYPE_SUFFIXES
} else {
&FLOAT_TYPE_SUFFIXES
};
value
.strip_suffix(suffixes)
.unwrap_or(value)
.replace('_', "")
}
#[expect(
clippy::unnecessary_wraps,
reason = "the signature must match the reader dispatch table"
)]
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let value = normalize_literal(
label_text(args.ast_graph, n_id).unwrap_or_default(),
args.ast_graph.label_type(n_id),
);
Ok(Some(build_literal_node(
args,
n_id,
value,
"number".to_owned(),
)))
}