pub(crate) mod assemble;
pub(crate) mod authorship_parser;
pub(crate) mod authorship_split;
pub(crate) mod blacklisted_epithets;
pub(crate) mod cjk_names;
pub(crate) mod code_inference;
pub(crate) mod context;
pub(crate) mod culture_collections;
pub(crate) mod double_surnames;
pub(crate) mod name_tokens;
pub(crate) mod preflight;
pub(crate) mod rank_markers;
pub(crate) mod stripandstash;
pub(crate) use context::ParseContext;
use std::sync::LazyLock;
use regex::Regex;
use crate::model::{
warnings, Authorship, CombinedAuthorship, NameType, NomCode, ParseError, ParsedName, Rank,
State,
};
use crate::pipeline::authorship_parser::AuthState;
use crate::token::tokenize;
use crate::unicode::{is_fullwidth, java_trim, normalize_input, normalize_quotes};
const MAX_LENGTH: usize = 1000;
const LONG_NAME_LENGTH: usize = 250;
static GLUED_PHRASE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^([\p{Lu}][\p{Ll}]+)([\p{Lu}]{2,}[\p{Lu}\d_]*)$").unwrap());
fn append_authorship_to_phrase(authorship: &str, name: &mut ParsedName) -> bool {
let a = java_trim(authorship);
let Some(phrase) = name.phrase.as_ref() else {
return false;
};
let is_citation = a.starts_with("[of ") && a.ends_with(']');
if name.type_ != NameType::Informal || (name.specific_epithet.is_some() && !is_citation) {
return false;
}
if !contains_ignoring_punctuation(phrase, a) {
name.phrase = Some(format!("{phrase} {a}"));
}
true
}
fn contains_ignoring_punctuation(haystack: &str, needle: &str) -> bool {
let needle = letters(needle);
needle.is_empty() || letters(haystack).contains(&needle)
}
pub fn run(
name: &str,
authorship: Option<&str>,
rank: Option<Rank>,
code: Option<NomCode>,
) -> Result<ParsedName, ParseError> {
let spaced = normalize_input(name);
let trimmed = java_trim(&spaced);
if trimmed.is_empty() {
return Err(ParseError::new(NameType::Other, None, name));
}
if trimmed.chars().count() > MAX_LENGTH {
return Err(ParseError::new(NameType::Other, None, name));
}
if let Some(a) = authorship {
if java_trim(a).chars().count() > MAX_LENGTH {
return Err(ParseError::new(NameType::Other, None, name));
}
}
let trimmed = normalize_quotes(trimmed);
let fullwidth = name
.chars()
.chain(authorship.unwrap_or_default().chars())
.any(is_fullwidth);
let authorship = authorship.map(|a| normalize_quotes(&normalize_input(a)));
let full_length = trimmed.chars().count()
+ authorship
.as_deref()
.map(java_trim)
.filter(|a| !a.is_empty() && !contains_ignoring_punctuation(&trimmed, a))
.map_or(0, |a| a.chars().count() + 1);
let mut ctx = ParseContext::new(trimmed.clone(), authorship, rank, code);
if fullwidth {
ctx.name.add_warning(warnings::HOMOGLYHPS);
}
if full_length > LONG_NAME_LENGTH {
ctx.name.add_warning(warnings::LONG_NAME);
}
split_glued_phrase_name(&mut ctx);
preflight::run(name, &mut ctx)?;
if ctx.preflight_complete {
return Ok(ctx.name);
}
stripandstash::run(&mut ctx);
if !has_letter(&ctx.working) {
return Err(ParseError::new(NameType::Other, None, name));
}
ctx.tokens = tokenize(&ctx.working);
let boundary = authorship_split::find_boundary(&ctx.tokens, &ctx);
name_tokens::classify(&mut ctx, boundary);
if ctx.qualified_genus && ctx.name.specific_epithet.is_some() {
ctx.name.type_ = NameType::Informal;
}
let mut auth_state: Option<AuthState> = None;
if boundary < ctx.tokens.len() {
let st = authorship_parser::parse(&ctx.tokens, boundary);
apply_authorship(&mut ctx.name, &st);
if st.unparsed_from >= 0 {
ctx.name.state = State::Partial;
ctx.name.unparsed = st.unparsed_text.clone();
}
auth_state = Some(st);
}
let mut autonym_state: Option<AuthState> = None;
if ctx.mid_author_from >= 0 && ctx.name.is_autonym() && !ctx.name.has_authorship() {
let from = ctx.mid_author_from as usize;
let to = ctx.mid_author_to as usize;
let st = authorship_parser::parse(&ctx.tokens[from..to], 0);
apply_authorship(&mut ctx.name, &st);
autonym_state = Some(st);
}
let mut species_state: Option<AuthState> = None;
if ctx.mid_author_from >= 0 && autonym_state.is_none() && ctx.name.specific_authorship.is_none()
{
let from = ctx.mid_author_from as usize;
let to = ctx.mid_author_to as usize;
let st = authorship_parser::parse(&ctx.tokens[from..to], 0);
if st.combination.exists() || st.basionym.exists() {
ctx.name.specific_authorship = Some(CombinedAuthorship {
combination_authorship: st.combination.clone(),
basionym_authorship: st.basionym.clone(),
});
species_state = Some(st);
}
}
let phrase_after_author = ctx.name.rank.is_infraspecific()
&& ctx.name.specific_epithet.is_some()
&& ctx.name.infraspecific_epithet.is_none()
&& ctx
.name
.combination_authorship
.authors
.first()
.is_some_and(|author| {
let phrase = ctx.name.phrase.as_deref().unwrap_or_default();
let text = letters(&trimmed);
match (text.find(&letters(author)), text.find(&letters(phrase))) {
(Some(a), Some(p)) => !phrase.is_empty() && a < p,
_ => false,
}
});
if phrase_after_author && ctx.name.specific_authorship.is_none() {
ctx.name.specific_authorship = Some(CombinedAuthorship {
combination_authorship: std::mem::take(&mut ctx.name.combination_authorship),
basionym_authorship: std::mem::take(&mut ctx.name.basionym_authorship),
});
}
let ends_in_cultivar = ctx
.name
.cultivar_epithet
.as_deref()
.is_some_and(|cv| letters(&trimmed).ends_with(&letters(cv)));
if ends_in_cultivar
&& ctx.name.has_authorship()
&& ctx.name.specific_authorship.is_none()
&& ctx.pending_specific_author.is_none()
{
ctx.name.specific_authorship = Some(CombinedAuthorship {
combination_authorship: std::mem::take(&mut ctx.name.combination_authorship),
basionym_authorship: std::mem::take(&mut ctx.name.basionym_authorship),
});
}
let mut extra_state: Option<AuthState> = None;
if let Some(authorship) = ctx.authorship_input.clone() {
let own = auth_state
.as_ref()
.or(autonym_state.as_ref())
.filter(|st| st.combination.exists() || st.basionym.exists());
let repeated = own.is_some()
&& !letters(&authorship).is_empty()
&& contains_ignoring_punctuation(&trimmed, &authorship);
match own.filter(|_| repeated) {
Some(own) => {
let mut column = ctx.clone();
if let Some(st) = parse_separate_authorship(&mut column, authorship) {
if same_authorship(own, &st) {
apply_authorship(&mut ctx.name, &st);
extra_state = Some(st);
}
}
}
None => {
let notes = (
ctx.name.nomenclatural_note.clone(),
ctx.name.taxonomic_note.clone(),
);
extra_state = parse_separate_authorship(&mut ctx, authorship);
drop_repeated_note(&mut ctx.name.nomenclatural_note, notes.0);
drop_repeated_note(&mut ctx.name.taxonomic_note, notes.1);
}
}
}
let has_signal = |st: &&AuthState| !code_state_needs_fallback(Some(st));
let code_state: Option<&AuthState> = extra_state
.as_ref()
.filter(has_signal)
.or_else(|| auth_state.as_ref().filter(has_signal))
.or(autonym_state.as_ref())
.or_else(|| species_state.as_ref().filter(has_signal))
.or(auth_state.as_ref())
.or(extra_state.as_ref());
if !code_state.is_some_and(|c| species_state.as_ref().is_some_and(|s| std::ptr::eq(c, s))) {
ctx.species_code_state = species_state.clone();
}
if ctx.pending_year.is_some()
&& !ctx.pending_year_from_publication
&& ctx.name.combination_authorship.exists()
&& ctx.name.combination_authorship.year.is_none()
{
ctx.name.combination_authorship.year = ctx.pending_year.clone();
}
assemble::finish(&mut ctx, code_state);
if ctx.pending_year.is_some()
&& ctx.pending_year_from_publication
&& ctx.name.combination_authorship.exists()
&& ctx.name.combination_authorship.year.is_none()
{
ctx.name.combination_authorship.year = ctx.pending_year.clone();
}
if ctx.pending_imprint_year.is_some() && ctx.name.combination_authorship.imprint_year.is_none()
{
ctx.name.combination_authorship.imprint_year = ctx.pending_imprint_year.clone();
}
if let Some(specific_author) = ctx.pending_specific_author.clone() {
let already = ctx
.name
.specific_authorship
.as_ref()
.is_some_and(CombinedAuthorship::has_authorship);
if !already {
if let Some(ca) = parse_combined_authorship(&specific_author) {
ctx.name.specific_authorship = Some(ca);
}
}
}
if let Some(generic_author) = ctx.pending_generic_author.clone() {
let already = ctx
.name
.generic_authorship
.as_ref()
.is_some_and(CombinedAuthorship::has_authorship);
if !already {
if let Some(ca) = parse_combined_authorship(&generic_author) {
ctx.name.generic_authorship = Some(ca);
}
}
}
Ok(ctx.name)
}
fn parse_separate_authorship(ctx: &mut ParseContext, authorship: String) -> Option<AuthState> {
let authorship = stripandstash::strip_authorship_placeholder(&authorship, &mut ctx.name);
if !authorship.chars().all(crate::token::is_whitespace_java)
&& !stripandstash::stash_bracketed_family_group_authorship(&authorship, &mut ctx.name)
&& !append_authorship_to_phrase(&authorship, &mut ctx.name)
{
let auth_clean = stripandstash::strip_authorship_leading_steps(ctx, authorship);
let auth_clean = stripandstash::strip_authorship_markers(&auth_clean, &mut ctx.name);
let auth_clean = stripandstash::strip_authorship_trailing_steps(ctx, auth_clean);
let embedded_reference = ctx
.name
.published_in
.clone()
.map(|r| (r, ctx.name.published_in_year));
let auth_clean = stripandstash::strip_in_author_citations(ctx, auth_clean);
let auth_clean = stripandstash::strip_authorship_reference_steps(ctx, auth_clean);
let auth_clean =
stripandstash::strip_trailing_manuscript_marker(&auth_clean, &mut ctx.name);
if let Some((reference, year)) = embedded_reference {
ctx.name.published_in = Some(reference);
ctx.name.published_in_year = year;
}
let aux = tokenize(&auth_clean);
let st = authorship_parser::parse(&aux, 0);
apply_authorship(&mut ctx.name, &st);
if st.unparsed_from >= 0 {
ctx.name.state = State::Partial;
ctx.name.unparsed = st.unparsed_text.clone();
}
return Some(st);
}
None
}
fn drop_repeated_note(note: &mut Option<String>, before: Option<String>) {
if let (Some(now), Some(before)) = (note.as_deref(), before) {
if let Some(added) = now.strip_prefix(before.as_str()) {
if contains_ignoring_punctuation(&before, added) {
*note = Some(before);
}
}
}
}
fn same_authorship(a: &AuthState, b: &AuthState) -> bool {
let key = |x: &Authorship| {
(
letters(&x.authors.concat()),
letters(&x.ex_authors.concat()),
x.year.clone(),
x.anonymous,
)
};
key(&a.combination) == key(&b.combination) && key(&a.basionym) == key(&b.basionym)
}
fn letters(s: &str) -> String {
s.chars().filter(|c| c.is_alphanumeric()).collect()
}
fn apply_authorship(name: &mut ParsedName, st: &AuthState) {
if st.combination.exists() {
name.combination_authorship = st.combination.clone();
}
if st.basionym.exists() {
name.basionym_authorship = st.basionym.clone();
}
}
fn code_state_needs_fallback(state: Option<&AuthState>) -> bool {
match state {
None => true,
Some(s) => !s.combination.exists() && !s.basionym_present,
}
}
fn parse_combined_authorship(authors: &str) -> Option<CombinedAuthorship> {
let tokens = tokenize(authors);
let st = authorship_parser::parse(&tokens, 0);
let mut ca = CombinedAuthorship::default();
if st.combination.exists() {
ca.combination_authorship = st.combination;
}
if st.basionym.exists() {
ca.basionym_authorship = st.basionym;
}
ca.has_authorship().then_some(ca)
}
fn has_letter(s: &str) -> bool {
s.chars().any(|c| c.is_alphabetic())
}
fn split_glued_phrase_name(ctx: &mut ParseContext) {
if ctx.working.contains(' ') {
return;
}
let Some(caps) = GLUED_PHRASE.captures(&ctx.working) else {
return;
};
let prefix = caps.get(1).unwrap().as_str().to_string();
let suffix = caps.get(2).unwrap().as_str().to_string();
ctx.name.phrase = Some(suffix);
ctx.name.type_ = NameType::Informal;
ctx.working = prefix;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_input_is_rejected_as_other() {
let err = run("", None, None, None).unwrap_err();
assert_eq!(err.type_, NameType::Other);
assert_eq!(err.code, None);
assert_eq!(err.name, "");
}
#[test]
fn whitespace_only_input_is_rejected_as_other() {
let err = run(" ", None, None, None).unwrap_err();
assert_eq!(err.type_, NameType::Other);
}
#[test]
fn name_over_max_length_is_rejected_as_other() {
let long = "a".repeat(1001);
let err = run(&long, None, None, None).unwrap_err();
assert_eq!(err.type_, NameType::Other);
assert_eq!(err.code, None);
assert_eq!(err.name, long);
}
#[test]
fn name_at_max_length_is_not_rejected_by_the_length_guard() {
let at_limit = "a".repeat(1000);
assert!(run(&at_limit, None, None, None).is_ok());
}
#[test]
fn authorship_over_max_length_is_rejected_as_other_with_name_field() {
let long_authorship = "a".repeat(1001);
let err = run("Abies alba", Some(&long_authorship), None, None).unwrap_err();
assert_eq!(err.type_, NameType::Other);
assert_eq!(err.name, "Abies alba");
}
#[test]
fn normal_binomial_returns_ok_with_a_seeded_name() {
let pn = run("Abies alba", None, None, None).expect("should parse");
assert_eq!(pn.genus, Some("Abies".to_string()));
assert_eq!(pn.specific_epithet, Some("alba".to_string()));
assert_eq!(pn.rank, Rank::Species);
assert_eq!(pn.code, None);
assert_eq!(pn.type_, NameType::Scientific);
assert_eq!(pn.state, crate::model::State::Complete);
assert!(pn.warnings.is_empty());
}
#[test]
fn requested_rank_and_code_are_seeded_onto_the_returned_name() {
let pn = run(
"Abies alba",
None,
Some(Rank::Species),
Some(NomCode::Botanical),
)
.expect("should parse");
assert_eq!(pn.rank, Rank::Species);
assert_eq!(pn.code, Some(NomCode::Botanical));
}
#[test]
fn long_name_over_250_chars_gets_the_long_name_warning() {
let long = "Abies ".repeat(42);
let pn = run(long.trim(), None, None, None).expect("should parse");
assert!(pn.warnings.contains(&warnings::LONG_NAME.to_string()));
}
#[test]
fn short_name_does_not_get_the_long_name_warning() {
let pn = run("Abies alba", None, None, None).expect("should parse");
assert!(!pn.warnings.contains(&warnings::LONG_NAME.to_string()));
}
#[test]
fn quotes_are_normalized_before_preflight_and_storage() {
assert!(run("Ceylonesmus vector Cham\u{2019}s, 1941", None, None, None).is_ok());
}
#[test]
fn glued_phrase_name_is_split_into_prefix_and_phrase() {
let pn = run("OdontellidaeGEN", None, None, None).expect("should parse");
assert_eq!(pn.phrase, Some("GEN".to_string()));
assert_eq!(pn.type_, NameType::Informal);
}
#[test]
fn glued_phrase_pattern_does_not_fire_when_working_has_a_space() {
let pn = run("Odontellidae GEN", None, None, None).expect("should parse");
assert_eq!(pn.phrase, None);
assert_eq!(pn.type_, NameType::Scientific);
}
#[test]
fn input_with_no_letters_at_all_is_rejected_as_other() {
let err = run("-,.#", None, None, None).unwrap_err();
assert_eq!(err.type_, NameType::Other);
assert_eq!(err.code, None);
assert_eq!(err.name, "-,.#");
}
#[test]
fn single_letter_abbreviation_survives_the_has_letter_guard() {
assert!(run("B.", None, None, None).is_ok());
}
#[test]
fn embedded_authorship_is_applied_to_combination_authorship() {
let pn = run("Abies alba Mill.", None, None, None).expect("should parse");
assert_eq!(pn.combination_authorship.authors, vec!["Mill.".to_string()]);
}
#[test]
fn separately_supplied_authorship_is_applied_to_combination_authorship() {
let pn = run("Abies alba", Some("Mill."), None, None).expect("should parse");
assert_eq!(pn.combination_authorship.authors, vec!["Mill.".to_string()]);
assert_eq!(pn.genus, Some("Abies".to_string()));
assert_eq!(pn.specific_epithet, Some("alba".to_string()));
}
#[test]
fn separately_supplied_authorship_overwrites_embedded_authorship_when_both_are_given() {
let pn = run("Abies alba Mill.", Some("L."), None, None).expect("should parse");
assert_eq!(pn.combination_authorship.authors, vec!["L.".to_string()]);
}
#[test]
fn separately_supplied_authorship_runs_through_strip_authorship_markers_first() {
let pn = run("Abies alba", Some("Mill. nom. illeg."), None, None).expect("should parse");
assert_eq!(pn.combination_authorship.authors, vec!["Mill.".to_string()]);
assert_eq!(pn.nomenclatural_note, Some("nom. illeg.".to_string()));
}
#[test]
fn standalone_manuscript_marker_as_the_whole_separate_authorship_sets_manuscript() {
let pn = run("Abies alba", Some("ined."), None, None).expect("should parse");
assert!(pn.manuscript);
assert!(!pn.combination_authorship.exists());
}
#[test]
fn sanctioning_author_goes_with_the_authorship_that_wins() {
let pn =
run("Boletus versicolor L. : Fr.", Some("X. : Y."), None, None).expect("should parse");
assert_eq!(pn.combination_authorship.authors, vec!["X.".to_string()]);
assert_eq!(
pn.combination_authorship.sanctioning_author,
Some("Y.".to_string())
);
}
#[test]
fn code_state_falls_back_to_separately_supplied_authorship_when_embedded_has_none() {
let pn = run("Heptacyclus", Some("(Vasileyev, 1939)"), None, None).expect("should parse");
assert_eq!(pn.code, Some(NomCode::Zoological));
assert_eq!(
pn.basionym_authorship.authors,
vec!["Vasileyev".to_string()]
);
assert_eq!(pn.basionym_authorship.year, Some("1939".to_string()));
}
#[test]
fn autonym_species_author_drives_code_inference_when_no_other_authorship() {
let pn = run(
"Trimezia spathata (Klatt) Baker subsp. spathata",
None,
None,
None,
)
.expect("should parse");
assert!(pn.is_autonym());
assert_eq!(pn.code, Some(NomCode::Botanical));
assert_eq!(pn.combination_authorship.authors, vec!["Baker".to_string()]);
assert_eq!(pn.basionym_authorship.authors, vec!["Klatt".to_string()]);
}
}