diff --git a/examples/hello-world/main.orc b/examples/hello-world/main.orc index 1427f9b..afc4bc4 100644 --- a/examples/hello-world/main.orc +++ b/examples/hello-world/main.orc @@ -1,5 +1,2 @@ -let foo = 1 + 2 -let ffmain = "hello $foo" - let user = r[ "foo" 1, "bar" t[3, 4] ] -let main = user."bar".1 +let main = user.bar.1 diff --git a/orchid-extension/src/lexer.rs b/orchid-extension/src/lexer.rs index 0c66437..7633a40 100644 --- a/orchid-extension/src/lexer.rs +++ b/orchid-extension/src/lexer.rs @@ -81,6 +81,9 @@ impl LexedData for GenTokTree { impl LexedData for Vec { fn into_vec(self) -> Vec { self } } +impl LexedData for [GenTokTree; N] { + fn into_vec(self) -> Vec { self.to_vec() } +} pub trait Lexer: Debug + Send + Sync + Sized + Default + 'static { const CHAR_FILTER: &'static [RangeInclusive]; diff --git a/orchid-std/src/std/ops/mod.rs b/orchid-std/src/std/ops/mod.rs index cd2f36b..8e13cbc 100644 --- a/orchid-std/src/std/ops/mod.rs +++ b/orchid-std/src/std/ops/mod.rs @@ -1,3 +1,5 @@ +pub mod subscript_lexer; + use orchid_extension::tree::{GenMember, comments, prefix}; use crate::proto; diff --git a/orchid-std/src/std/ops/subscript_lexer.rs b/orchid-std/src/std/ops/subscript_lexer.rs new file mode 100644 index 0000000..f1dca23 --- /dev/null +++ b/orchid-std/src/std/ops/subscript_lexer.rs @@ -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] = &['.'..='.']; + 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)), + ])) + } +} diff --git a/orchid-std/src/std/std_system.rs b/orchid-std/src/std/std_system.rs index a1cb11d..f4fe1de 100644 --- a/orchid-std/src/std/std_system.rs +++ b/orchid-std/src/std/std_system.rs @@ -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 { vec![&StringLexer, &NumLexer] } + fn lexers() -> Vec { vec![&StringLexer, &NumLexer, &SubscriptLexer] } fn parsers() -> Vec { vec![&AsTypeParser, &TypeParser, &AsProtoParser, &ProtoParser] } async fn env() -> Vec { merge_trivial([