From 0d4ba4a54ef0528a0a45b58e21a1e4aa7ed5eaf9 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Fri, 2 May 2025 19:16:28 +0300 Subject: [PATCH] dsl: add Str token --- dsl/src/dsl_iter.rs | 6 +++++ dsl/src/dsl_token.rs | 6 +++++ dsl/src/lib.rs | 56 ++++++++++++++++++++++++-------------------- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/dsl/src/dsl_iter.rs b/dsl/src/dsl_iter.rs index 0a94b3c..bc69f04 100644 --- a/dsl/src/dsl_iter.rs +++ b/dsl/src/dsl_iter.rs @@ -86,6 +86,8 @@ pub const fn peek_src <'a> (source: &'a str) -> Option> { token.grow(), '(' => Token::new(source, start, 1, Exp(1, TokenIter::new(str_range(source, start, start + 1)))), + '"' => + Token::new(source, start, 1, Str(str_range(source, start, start + 1))), ':'|'@' => Token::new(source, start, 1, Sym(str_range(source, start, start + 1))), '/'|'a'..='z' => @@ -97,6 +99,10 @@ pub const fn peek_src <'a> (source: &'a str) -> Option> { }), _ => token.error(Unexpected(c)) }, + Str(s) => match c { + '"' => return Some(token), + _ => token.grow_str(), + }, Num(n) => match c { '0'..='9' => token.grow_num(n, c), ' '|'\n'|'\r'|'\t'|')' => return Some(token), diff --git a/dsl/src/dsl_token.rs b/dsl/src/dsl_token.rs index 8949091..d58a60e 100644 --- a/dsl/src/dsl_token.rs +++ b/dsl/src/dsl_token.rs @@ -35,6 +35,7 @@ use crate::*; Num(usize), Sym(&'a str), Key(&'a str), + Str(&'a str), Exp(usize, TokenIter<'a>), } @@ -80,6 +81,11 @@ impl<'a> Token<'a> { token.value = Sym(token.slice_source(self.source)); token } + pub const fn grow_str (self) -> Self { + let mut token = self.grow(); + token.value = Str(token.slice_source(self.source)); + token + } pub const fn grow_exp (self) -> Self { let mut token = self.grow(); if let Exp(depth, _) = token.value { diff --git a/dsl/src/lib.rs b/dsl/src/lib.rs index 81e6aa2..ea0e477 100644 --- a/dsl/src/lib.rs +++ b/dsl/src/lib.rs @@ -14,31 +14,6 @@ mod dsl_iter; pub use self::dsl_iter::*; mod dsl_context; pub use self::dsl_context::*; mod dsl_macros; -//#[cfg(test)] #[test] fn test_examples () -> Result<(), ParseError> { - //// Let's pretend to render some view. - //let source = include_str!("../../tek/src/view_arranger.edn"); - //// The token iterator allows you to get the tokens represented by the source text. - //let mut view = TokenIter(source); - //// The token iterator wraps a const token+source iterator. - //assert_eq!(view.0.0, source); - //let mut expr = view.peek(); - //assert_eq!(view.0.0, source); - //assert_eq!(expr, Some(Token { - //source, start: 0, length: source.len() - 1, value: Exp(0, SourceIter(&source[1..])) - //})); - ////panic!("{view:?}"); - ////panic!("{:#?}", expr); - ////for example in [ - ////include_str!("../../tui/examples/edn01.edn"), - ////include_str!("../../tui/examples/edn02.edn"), - ////] { - //////let items = Dsl::read_all(example)?; - //////panic!("{layout:?}"); - //////let content = >::from(&layout); - ////} - //Ok(()) -//} - #[cfg(test)] mod test_token_iter { use crate::*; //use proptest::prelude::*; @@ -130,5 +105,36 @@ mod dsl_macros; let src = "foo/bar"; assert_eq!(Key("foo/bar"), SourceIter(src).next().unwrap().0.value); + let src = "\"foo/bar\""; + assert_eq!(Str("foo/bar"), SourceIter(src).next().unwrap().0.value); + + let src = " \"foo/bar\" "; + assert_eq!(Str("foo/bar"), SourceIter(src).next().unwrap().0.value); + Ok(()) } + +//#[cfg(test)] #[test] fn test_examples () -> Result<(), ParseError> { + //// Let's pretend to render some view. + //let source = include_str!("../../tek/src/view_arranger.edn"); + //// The token iterator allows you to get the tokens represented by the source text. + //let mut view = TokenIter(source); + //// The token iterator wraps a const token+source iterator. + //assert_eq!(view.0.0, source); + //let mut expr = view.peek(); + //assert_eq!(view.0.0, source); + //assert_eq!(expr, Some(Token { + //source, start: 0, length: source.len() - 1, value: Exp(0, SourceIter(&source[1..])) + //})); + ////panic!("{view:?}"); + ////panic!("{:#?}", expr); + ////for example in [ + ////include_str!("../../tui/examples/edn01.edn"), + ////include_str!("../../tui/examples/edn02.edn"), + ////] { + //////let items = Dsl::read_all(example)?; + //////panic!("{layout:?}"); + //////let content = >::from(&layout); + ////} + //Ok(()) +//}