forked from Orchid/orchid
Added subscript lexer
This commit is contained in:
29
orchid-std/src/std/ops/subscript_lexer.rs
Normal file
29
orchid-std/src/std/ops/subscript_lexer.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use orchid_base::error::OrcRes;
|
||||
use orchid_base::interner::is;
|
||||
use orchid_base::parse::{name_char, name_start};
|
||||
use orchid_extension::conv::ToExpr;
|
||||
use orchid_extension::lexer::{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<char>] = &['.'..='.'];
|
||||
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(IntStrAtom(is(&tail[1..name_len + 1]).await).to_gen().await)
|
||||
.at(lctx.pos_tt(&tail[1..], new_tail)),
|
||||
]))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user