forked from Orchid/orchid
Added subscript lexer
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
pub mod subscript_lexer;
|
||||
|
||||
use orchid_extension::tree::{GenMember, comments, prefix};
|
||||
|
||||
use crate::proto;
|
||||
|
||||
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)),
|
||||
]))
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ use super::string::str_atom::{IntStrAtom, StrAtom};
|
||||
use super::string::str_lib::gen_str_lib;
|
||||
use crate::std::number::num_lexer::NumLexer;
|
||||
use crate::std::ops::gen_ops_lib;
|
||||
use crate::std::ops::subscript_lexer::SubscriptLexer;
|
||||
use crate::std::option::{OptAtom, gen_option_lib};
|
||||
use crate::std::protocol::proto_parser::{AsProtoParser, ProtoParser};
|
||||
use crate::std::protocol::type_parser::{AsTypeParser, TypeParser};
|
||||
@@ -105,7 +106,7 @@ impl System for StdSystem {
|
||||
},
|
||||
}
|
||||
}
|
||||
fn lexers() -> Vec<LexerObj> { vec![&StringLexer, &NumLexer] }
|
||||
fn lexers() -> Vec<LexerObj> { vec![&StringLexer, &NumLexer, &SubscriptLexer] }
|
||||
fn parsers() -> Vec<ParserObj> { vec![&AsTypeParser, &TypeParser, &AsProtoParser, &ProtoParser] }
|
||||
async fn env() -> Vec<GenMember> {
|
||||
merge_trivial([
|
||||
|
||||
Reference in New Issue
Block a user