use crate::detail::container::{find_next_token, mark};
use crate::detail::element::element_from_token;
use crate::detail::keyword::KeywordKind;
use crate::detail::token::{is_not_delimiter_token, is_numeric_token, Token};
use crate::element::{Element, ElementKind};
fn is_part_keyword(token: &Token) -> bool {
token
.keyword
.is_some_and(|k| k.kind == KeywordKind::Part && (!k.ambiguous || token.is_enclosed))
}
pub(super) fn parse_part(tokens: &mut [Token]) -> Option<Element> {
let keyword_indices: Vec<usize> = tokens
.iter()
.enumerate()
.filter(|(_, t)| is_part_keyword(t))
.map(|(i, _)| i)
.collect();
for keyword_idx in keyword_indices {
let Some(next_idx) = find_next_token(tokens, keyword_idx, is_not_delimiter_token) else {
continue;
};
let Some(next) = tokens.get(next_idx) else {
continue;
};
if !is_numeric_token(next) {
continue;
}
mark(tokens, keyword_idx, ElementKind::Part);
mark(tokens, next_idx, ElementKind::Part);
let token = tokens.get(next_idx)?;
return Some(element_from_token(ElementKind::Part, token, None, None));
}
None
}