diff --git a/dsl/src/dsl.rs b/dsl/src/dsl.rs index ce30384..e7463ae 100644 --- a/dsl/src/dsl.rs +++ b/dsl/src/dsl.rs @@ -57,16 +57,16 @@ pub const fn no_trailing_non_space ( })) } pub const fn peek (src: &str) -> DslPerhaps<&str> { - Ok(Some(if let Ok(Some(expr)) = expr_peek(src) { expr } else - if let Ok(Some(word)) = word_peek(src) { word } else + Ok(Some(if let Ok(Some(expr)) = expr_peek(src) { expr } else + if let Ok(Some(word)) = word_peek(src) { word } else if let Ok(Some(num)) = num_peek(src) { num } else if let Ok(Some(text)) = text_peek(src) { text } else if let Err(e) = no_trailing_non_space(src, 0, Some("peek")) { return Err(e) } else { return Ok(None) })) } pub const fn seek (src: &str) -> DslPerhaps<(usize, usize)> { - Ok(Some(if let Ok(Some(expr)) = expr_seek(src) { expr } else - if let Ok(Some(word)) = word_seek(src) { word } else + Ok(Some(if let Ok(Some(expr)) = expr_seek(src) { expr } else + if let Ok(Some(word)) = word_seek(src) { word } else if let Ok(Some(num)) = num_seek(src) { num } else if let Ok(Some(text)) = text_seek(src) { text } else if let Err(e) = no_trailing_non_space(src, 0, Some("seek")) { return Err(e) } @@ -242,6 +242,29 @@ dsl_type!(DslText { } }); +pub const fn is_key_start (c: char) -> bool { matches!(c, '/'|('a'..='z')) } +pub const fn is_key_char (c: char) -> bool { is_key_start(c) || matches!(c, '0'..='9'|'-') } +pub const fn is_key_end (c: char) -> bool { !is_key_char(c) } +dsl_type!(DslKey { + fn key (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(key_peek_only)) } +} { + pub const fn key_peek [generated]; + pub const fn key_peek_only [generated]; + pub const fn key_seek [generated]; + pub const fn key_seek_start (src) { + for_each!((i, c) in char_indices(src) => + if is_key_start(c) { return Ok(Some(i)) } else + if !is_space(c) { return Err(Unexpected(c, Some(i), None)) }); + Ok(None) + } + pub const fn key_seek_length (src) { + for_each!((i, c) in char_indices(src) => + if is_key_end(c) { return Ok(Some(i)) } else + if !is_key_char(c) { return Err(Unexpected(c, Some(i), None)) }); + Ok(Some(src.len())) + } +}); + dsl_type!(DslNum { fn num (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(num_peek_only))} } { diff --git a/dsl/src/dsl_ns.rs b/dsl/src/dsl_ns.rs index b2776aa..7f91c46 100644 --- a/dsl/src/dsl_ns.rs +++ b/dsl/src/dsl_ns.rs @@ -10,18 +10,18 @@ use crate::*; // Special form for numeric types (num |$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => { impl<'t> DslNs<'t, $Type> for $State { - const WORDS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> = + const SYMS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> = DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]); - const EXPRS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> = + const EXPS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> = DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]); fn from (&'t self, dsl: D) -> Perhaps<$Type> { if let Ok(Some(src)) = dsl.src() { - if let Ok(num) = to_number(src) { - Ok(Some(num as $Type)) + if let Ok(Some(num)) = src.num() { + Ok(Some(to_number(num)? as $Type)) } else if let Ok(Some(src)) = src.word() { self.from_word(src) } else { - self.from_expr(src) + self.from_exp(src) } } else { Ok(None) @@ -36,9 +36,9 @@ use crate::*; // Regular form for single type (|$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => { impl<'t> DslNs<'t, $Type> for $State { - const WORDS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> = + const SYMS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> = DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]); - const EXPRS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> = + const EXPS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> = DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]); } }; @@ -54,16 +54,15 @@ use crate::*; }) => { ($head, |$state, tail: &str|{ $( let head = tail.head()?.unwrap_or_default(); - let tail = tail.tail()?.unwrap_or_default(); let $arg: $ty = if let Some(arg) = $state.from(&head)? { arg } else { - return Err(format!("{}: missing argument: {} ({}); got: {tail}", - $head, - stringify!($arg), - stringify!($Type), + return Err(format!("{}: missing argument: {}", + stringify!($head), + stringify!($arg) ).into()) }; + let tail = tail.tail()?.unwrap_or_default(); )* Ok(Some($body)) }) }; @@ -85,7 +84,7 @@ pub trait DslNs<'t, T: 't>: 't { if let Ok(Some(src)) = src.word() { self.from_word(src) } else { - self.from_expr(src) + self.from_exp(src) } } else { Ok(None) @@ -94,25 +93,26 @@ pub trait DslNs<'t, T: 't>: 't { /// Resolve a symbol if known. fn from_word (&'t self, dsl: D) -> Perhaps { if let Some(dsl) = dsl.word()? { - for (word, get) in Self::WORDS.0 { if dsl == *word { return get(self) } } + for (word, get) in Self::SYMS.0 { if dsl == *word { return get(self) } } } return Ok(None) } /// Resolve an expression if known. - fn from_expr (&'t self, dsl: D) -> Perhaps { + fn from_exp (&'t self, dsl: D) -> Perhaps { if let Some(head) = dsl.expr().head()? { - for (key, value) in Self::EXPRS.0.iter() { + for (key, value) in Self::EXPS.0.iter() { if head == *key { return value(self, dsl.expr().tail()?.unwrap_or("")) } } } + panic!("{dsl:?}"); return Ok(None) } /// Known symbols. - const WORDS: DslNsMap<'t, fn (&'t Self)->Perhaps> = DslNsMap::new(&[]); + const SYMS: DslNsMap<'t, fn (&'t Self)->Perhaps> = DslNsMap::new(&[]); /// Known expressions. - const EXPRS: DslNsMap<'t, fn (&'t Self, &str)->Perhaps> = DslNsMap::new(&[]); + const EXPS: DslNsMap<'t, fn (&'t Self, &str)->Perhaps> = DslNsMap::new(&[]); } /// Namespace mapping. #[derive(Debug)] diff --git a/proc/src/proc_expose.rs b/proc/src/proc_expose.rs index bb6794b..fa2707c 100644 --- a/proc/src/proc_expose.rs +++ b/proc/src/proc_expose.rs @@ -87,7 +87,7 @@ impl ExposeImpl { /// Generated by [tengri_proc::expose]. impl ::tengri::dsl::FromDsl<#state> for #t { fn from_dsl (state: &#state, dsl: &impl Dsl) -> Perhaps { - match dsl.word()? { + match dsl.key()? { Some("true") => Ok(Some(true)), Some("false") => Ok(Some(false)), _ => match dsl.word()? { #(#variants)* _ => Ok(None) }