use orchid_base::{name_char, name_start}; use orchid_base::{OrcRes, is}; use orchid_extension::gen_expr::new_atom; use orchid_extension::{LexContext, LexedData, Lexer, err_not_applicable}; use orchid_extension::tree::GenTok; use crate::std::string::str_atom::IntStrAtom; #[derive(Default, Debug)] pub struct SubscriptLexer; impl Lexer for SubscriptLexer { const CHAR_FILTER: &'static [std::ops::RangeInclusive] = &['.'..='.']; async fn lex<'a>(tail: &'a str, lctx: &'a LexContext<'a>) -> OrcRes<(&'a str, impl LexedData)> { if tail.len() <= 1 || !name_start(tail.chars().nth(1).unwrap()) { return Err(err_not_applicable().await); } let name_len = match tail[1..].char_indices().find(|(_, c)| !name_char(*c)) { None => tail.len() - 1, Some((pos, _)) => pos, }; let new_tail = &tail[name_len + 1..]; Ok((new_tail, [ GenTok::Name(is(".").await).at(lctx.pos_lt(1, &tail[1..])), GenTok::NewExpr(new_atom(IntStrAtom(is(&tail[1..name_len + 1]).await))) .at(lctx.pos_tt(&tail[1..], new_tail)), ])) } }