From d99b20c99da0ed6ec523a81ccb404c0479bbc228 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Thu, 17 Jul 2025 18:50:42 +0300 Subject: [PATCH 1/2] wip: fix(dsl): kinda patch it up --- dsl/src/lib.rs | 425 +++++++++++++++++++++-------------------- input/src/input_dsl.rs | 61 +++--- output/src/ops.rs | 6 +- 3 files changed, 253 insertions(+), 239 deletions(-) diff --git a/dsl/src/lib.rs b/dsl/src/lib.rs index 3e26227..416db82 100644 --- a/dsl/src/lib.rs +++ b/dsl/src/lib.rs @@ -14,72 +14,135 @@ pub(crate) use konst::string::{split_at, str_range, char_indices}; pub(crate) use thiserror::Error; pub(crate) use self::DslError::*; #[cfg(test)] mod test; - -pub type DslUsually = Result; -pub type DslPerhaps = Result, DslError>; - -/// Pronounced dizzle. +/// A DSL expression. Generic over string and expression storage. pub trait Dsl: Clone + Debug { /// The string representation for a dizzle. type Str: DslStr; /// The expression representation for a dizzle. type Exp: DslExp; - /// Return a token iterator for this dizzle. - fn dsl (&self) -> DslUsually<&Val>; + /// Request the top-level DSL [Val]ue. + /// May perform cloning or parsing. + fn dsl (&self) -> Val; + fn err (&self) -> Option {self.dsl().err()} + fn nil (&self) -> bool {self.dsl().nil()} + fn num (&self) -> Option {self.dsl().num()} + fn sym (&self) -> Option {self.dsl().sym()} + fn key (&self) -> Option {self.dsl().key()} + fn str (&self) -> Option {self.dsl().str()} + fn exp (&self) -> Option {self.dsl().exp()} } - -/// Enumeration of values representable by a DSL [Token]s. -/// Generic over string and expression storage. +/// Enumeration of values that may figure in an expression. +/// Generic over [Dsl] implementation. #[derive(Clone, Debug, PartialEq, Default)] pub enum Val { - #[default] - Nil, + /// Empty expression + #[default] Nil, /// Unsigned integer literal Num(usize), - /// Tokens that start with `:` + /// An identifier that starts with `.` Sym(D::Str), - /// Tokens that don't start with `:` + /// An identifier that doesn't start with `:` Key(D::Str), - /// Quoted string literals + /// A quoted string literal Str(D::Str), - /// Expressions. + /// A sub-expression. Exp( /// Expression depth checksum. Must be 0, otherwise you have an unclosed delimiter. - usize, + isize, /// Expression content. D::Exp ), + /// An error. Error(DslError), } - -impl> Copy for Val {} - impl Val { - pub fn convert (&self) -> Val where - B::Str: for<'a> From<&'a D::Str>, - B::Exp: for<'a> From<&'a D::Exp> - { - match self { Val::Nil => Val::Nil, - Val::Num(u) => Val::Num(*u), - Val::Sym(s) => Val::Sym(s.into()), - Val::Key(s) => Val::Key(s.into()), - Val::Str(s) => Val::Str(s.into()), - Val::Exp(d, x) => Val::Exp(*d, x.into()), - Val::Error(e) => Val::Error(*e) } } - pub fn is_nil (&self) -> bool { matches!(self, Self::Nil) } - pub fn as_error (&self) -> Option<&DslError> { if let Self::Error(e) = self { Some(e) } else { None } } - pub fn as_num (&self) -> Option {match self{Self::Num(n)=>Some(*n),_=>None}} - pub fn as_sym (&self) -> Option<&str> {match self{Self::Sym(s )=>Some(s.as_ref()),_=>None}} - pub fn as_key (&self) -> Option<&str> {match self{Self::Key(k )=>Some(k.as_ref()),_=>None}} - pub fn as_str (&self) -> Option<&str> {match self{Self::Str(s )=>Some(s.as_ref()),_=>None}} - pub fn as_exp (&self) -> Option<&D::Exp> {match self{Self::Exp(_, x)=>Some(x),_=>None}} - pub fn exp_depth (&self) -> Option { todo!() } + pub fn err (&self) -> Option {match self{Val::Error(e)=>Some(*e), _=>None}} + pub fn nil (&self) -> bool {match self{Val::Nil=>true, _=>false}} + pub fn num (&self) -> Option {match self{Val::Num(n)=>Some(*n), _=>None}} + pub fn sym (&self) -> Option {match self{Val::Sym(s)=>Some(s.clone()), _=>None}} + pub fn key (&self) -> Option {match self{Val::Key(k)=>Some(k.clone()), _=>None}} + pub fn str (&self) -> Option {match self{Val::Str(s)=>Some(s.clone()), _=>None}} + pub fn exp (&self) -> Option {match self{Val::Exp(_, x)=>Some(x.clone()),_=>None}} +} +/// The abstract syntax tree (AST) can be produced from the CST +/// by cloning source slices into owned ([Arc]) string slices. +#[derive(Debug, Clone, Default, PartialEq)] +pub struct Ast(Arc>>>); +impl Dsl for Ast { + type Str = Arc; + type Exp = Arc>>>; + fn dsl (&self) -> Val { Val::Exp(0, self.0.clone()) } +} +/// The concrete syntax tree (CST) implements zero-copy +/// parsing of the DSL from a string reference. CST items +/// preserve info about their location in the source. +/// CST stores strings as source references and expressions as [CstIter] instances. +#[derive(Debug, Copy, Clone, Default, PartialEq)] +pub struct Cst<'s>(CstConstIter<'s>); +impl<'s> Dsl for Cst<'s> { + type Str = &'s str; + type Exp = CstConstIter<'s>; + fn dsl (&self) -> Val { Val::Exp(0, self.0) } +} +impl<'s> From<&'s str> for Cst<'s> { + fn from (source: &'s str) -> Self { + Self(CstConstIter(source)) + } +} +/// The string representation for a [Dsl] implementation. +/// [Cst] uses `&'s str`. [Ast] uses `Arc`. +pub trait DslStr: PartialEq + Clone + Default + Debug + AsRef + std::ops::Deref {} +impl + std::ops::Deref> DslStr for T {} +/// The expression representation for a [Dsl] implementation. +/// [Cst] uses [CstIter]. [Ast] uses [VecDeque]. +pub trait DslExp: PartialEq + Clone + Default + Debug { + fn head (&self) -> Val; + fn tail (&self) -> Self; +} +impl DslExp for Arc>>> { + fn head (&self) -> Val { + self.get(0).cloned().unwrap_or_else(||Arc::new(Default::default())).value.into() + } + fn tail (&self) -> Self { + Self::new(self.iter().skip(1).cloned().collect()) + } +} +impl<'s> DslExp for CstConstIter<'s> { + fn head (&self) -> Val { + peek(self.0).value.into() + } + fn tail (&self) -> Self { + let Token { span: Span { start, length, source }, .. } = peek(self.0); + Self(&source[(start+length)..]) + } +} +impl + Copy> Copy for Val {} +impl Val { + pub fn is_nil (&self) -> bool { matches!(self, Self::Nil) } + pub fn as_error (&self) -> Option<&DslError> { if let Self::Error(e) = self { Some(e) } else { None } } + pub fn as_num (&self) -> Option {match self{Self::Num(n)=>Some(*n),_=>None}} + pub fn as_sym (&self) -> Option<&str> {match self{Self::Sym(s )=>Some(s.as_ref()),_=>None}} + pub fn as_key (&self) -> Option<&str> {match self{Self::Key(k )=>Some(k.as_ref()),_=>None}} + pub fn as_str (&self) -> Option<&str> {match self{Self::Str(s )=>Some(s.as_ref()),_=>None}} + //pub fn as_exp (&self) -> Option<&D::Exp> {match self{Self::Exp(_, x)=>Some(x),_=>None}} + pub fn exp_depth (&self) -> Option { todo!() } pub fn exp_head_tail (&self) -> (Option<&Self>, Option<&D::Exp>) { (self.exp_head(), self.exp_tail()) } - pub fn exp_head (&self) -> Option<&Self> { todo!() } // TODO - pub fn exp_tail (&self) -> Option<&D::Exp> { todo!() } - pub fn peek (&self) -> Option { todo!() } - pub fn next (&mut self) -> Option { todo!() } - pub fn rest (self) -> Vec { todo!() } + pub fn exp_head (&self) -> Option<&Self> { todo!() } // TODO + pub fn exp_tail (&self) -> Option<&D::Exp> { todo!() } + pub fn peek (&self) -> Option { todo!() } + pub fn next (&mut self) -> Option { todo!() } + pub fn rest (self) -> Vec { todo!() } + //pub fn convert (&self) -> Val where + //T::Str: for<'a> From<&'a D::Str>, + //T::Exp: for<'a> From<&'a D::Exp> + //{ + //match self { Val::Nil => Val::Nil, + //Val::Num(u) => Val::Num(*u), + //Val::Sym(s) => Val::Sym(s.into()), + //Val::Key(s) => Val::Key(s.into()), + //Val::Str(s) => Val::Str(s.into()), + //Val::Exp(d, x) => Val::Exp(*d, x.into()), + //Val::Error(e) => Val::Error(*e) } } //pub fn exp_match (&self, namespace: &str, cb: F) -> DslPerhaps //where F: Fn(&str, &Exp)-> DslPerhaps { //if let Some(Self::Key(key)) = self.exp_head() @@ -92,61 +155,6 @@ impl Val { //} } -/// The string representation for a [Dsl] implementation. -/// [Cst] uses `&'s str`. [Ast] uses `Arc`. -pub trait DslStr: PartialEq + Clone + Default + Debug + AsRef + std::ops::Deref {} -impl + std::ops::Deref> DslStr for T {} - -/// The expression representation for a [Dsl] implementation. -/// [Cst] uses [CstIter]. [Ast] uses [VecDeque]. -pub trait DslExp: PartialEq + Clone + Default + Debug {} -impl DslExp for T {} - -/// The abstract syntax tree (AST) can be produced from the CST -/// by cloning source slices into owned ([Arc]) string slices. -#[derive(Debug, Clone, Default, PartialEq)] -pub struct Ast(Token); -impl Dsl for Ast { - type Str = Arc; - type Exp = VecDeque>>; - fn dsl (&self) -> DslUsually<&Val> { - Ok(self.0.value()) - } -} - -/// The concrete syntax tree (CST) implements zero-copy -/// parsing of the DSL from a string reference. CST items -/// preserve info about their location in the source. -/// CST stores strings as source references and expressions as [CstIter] instances. -#[derive(Debug, Clone, Default, PartialEq)] -pub struct Cst<'s>(Token>); -impl<'s> Dsl for Cst<'s> { - type Str = &'s str; - type Exp = CstConstIter<'s>; - fn dsl (&self) -> DslUsually<&Val> { - Ok(self.0.value()) - } -} - -/// `State` + [Dsl] -> `Self`. -pub trait FromDsl: Sized { - fn try_from_dsl (state: &State, dsl: &impl Dsl) -> Perhaps; - fn from_dsl (state: &State, dsl: &impl Dsl, err: impl Fn()->Box) -> Usually { - match Self::try_from_dsl(state, dsl)? { Some(dsl) => Ok(dsl), _ => Err(err()) } } } - -/// `self` + `Options` -> [Dsl] -pub trait IntoDsl { /*TODO*/ } - -/// `self` + [Dsl] -> `Item` -pub trait DslInto { - fn try_dsl_into (&self, dsl: &impl Dsl) -> Perhaps; - fn dsl_into (&self, dsl: &impl Dsl, err: impl Fn()->Box) -> Usually { - match Self::try_dsl_into(self, dsl)? { Some(dsl) => Ok(dsl), _ => Err(err()) } } } - -/// `self` + `Item` -> [Dsl] -pub trait DslFrom { /*TODO*/ } -/// Standard result type for DSL-specific operations. -pub type DslResult = Result; /// DSL-specific error codes. #[derive(Error, Debug, Copy, Clone, PartialEq, PanicFmt)] pub enum DslError { #[error("parse failed: not implemented")] @@ -175,9 +183,8 @@ impl<'s> Iterator for CstIter<'s> { type Item = Token>; fn next (&mut self) -> Option { match self.0.advance() { - Ok(Some((item, rest))) => { self.0 = rest; item.into() }, - Ok(None) => None, - Err(e) => panic!("{e:?}") + Some((item, rest)) => { self.0 = rest; item.into() }, + None => None, } } } @@ -193,7 +200,7 @@ impl<'s> From <&'s str> for CstConstIter<'s> { } impl<'s> Iterator for CstConstIter<'s> { type Item = Token>; - fn next (&mut self) -> Option>> { self.advance().unwrap().map(|x|x.0) } + fn next (&mut self) -> Option>> { self.advance().map(|x|x.0) } } impl<'s> ConstIntoIter for CstConstIter<'s> { type Kind = IsIteratorKind; @@ -203,21 +210,98 @@ impl<'s> ConstIntoIter for CstConstIter<'s> { impl<'s> CstConstIter<'s> { pub const fn new (source: &'s str) -> Self { Self(source) } pub const fn chomp (&self, index: usize) -> Self { Self(split_at(self.0, index).1) } - pub const fn peek (&self) -> DslPerhaps>> { Token::peek(self.0) } - //pub const fn next (mut self) -> Option<(Token>, Self)> { - //Self::advance(&mut self).unwrap() } - pub const fn advance (&mut self) -> DslPerhaps<(Token>, Self)> { - match self.peek() { - Ok(Some(token)) => { + pub const fn advance (&mut self) -> Option<(Token>, Self)> { + match peek(self.0) { + Token { value: Val::Nil, .. } => None, + token => { let end = self.chomp(token.span.end()); - Ok(Some((token.copy(), end))) + Some((token.copy(), end)) }, - Ok(None) => Ok(None), - Err(e) => Err(e) } } } +const fn is_whitespace (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t') } +const fn is_digit (c: char) -> bool { matches!(c, '0'..='9') } +const fn is_num_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') } +const fn is_key_start (c: char) -> bool { matches!(c, '/'|'a'..='z') } +const fn is_key_char (c: char) -> bool { matches!(c, 'a'..='z'|'0'..='9'|'-'|'/') } +const fn is_key_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') } +const fn is_sym_start (c: char) -> bool { matches!(c, ':'|'@') } +const fn is_sym_char (c: char) -> bool { matches!(c, 'a'..='z'|'A'..='Z'|'0'..='9'|'-') } +const fn is_sym_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') } +const fn is_str_start (c: char) -> bool { matches!(c, '"') } +const fn is_str_end (c: char) -> bool { matches!(c, '"') } +const fn is_exp_start (c: char) -> bool { matches!(c, '(') } + +pub const fn peek <'s> (src: &'s str) -> Token> { + use Val::*; + let mut t = Token { value: Val::Nil, span: Span { source: src, start: 0, length: 0 } }; + let mut iter = char_indices(src); + while let Some(((i, c), next)) = iter.next() { + t = match (t.value(), c) { + (Error(_), _) => return t, + + (Nil, _) if is_exp_start(c) => Token::new(src, i, 1, Exp(1, CstConstIter(str_range(src, i, i+1)))), + (Nil, _) if is_str_start(c) => Token::new(src, i, 1, Str(str_range(src, i, i+1))), + (Nil, _) if is_sym_start(c) => Token::new(src, i, 1, Sym(str_range(src, i, i+1))), + (Nil, _) if is_key_start(c) => Token::new(src, i, 1, Key(str_range(src, i, i+1))), + (Nil, _) if is_digit(c) => Token::new(src, i, 1, match to_digit(c) { Ok(c) => Num(c), Err(e) => Error(e) }), + (Nil, _) if is_whitespace(c) => t.grown(), + (Nil, _) => { t.value = Val::Error(Unexpected(c)); t }, + + (Str(_), _) if is_str_end(c) => return t, + (Str(_), _) => { t.value = Str(t.span.grow().slice()); t }, + + (Sym(_), _) if is_sym_end(c) => return t, + (Sym(_), _) if is_sym_char(c) => { t.value = Sym(t.span.grow().slice()); t }, + (Sym(_), _) => { t.value = Error(Unexpected(c)); t }, + + (Key(_), _) if is_key_end(c) => return t, + (Key(_), _) if is_key_char(c) => { t.value = Key(t.span.grow().slice()); t }, + (Key(_), _) => { t.value = Error(Unexpected(c)); t }, + + (Exp(0, _), _) => { t.value = Exp(0, CstConstIter(t.span.grow().slice_exp())); return t }, + (Exp(d, _), ')') => { t.value = Exp((*d)-1, CstConstIter(t.span.grow().slice_exp())); t }, + (Exp(d, _), '(') => { t.value = Exp((*d)+1, CstConstIter(t.span.grow().slice_exp())); t }, + (Exp(d, _), _ ) => { t.value = Exp(*d, CstConstIter(t.span.grow().slice_exp())); t }, + + (Num(m), _) if is_num_end(c) => return t, + (Num(m), _) => match to_digit(c) { + Ok(n) => { let m = *m; t.span.grow(); t.value = Num(n+10*m); t }, + Err(e) => { t.span.grow(); t.value = Error(e); t } }, + }; + iter = next; + } + t +} +pub const fn to_number (digits: &str) -> Result { + let mut iter = char_indices(digits); + let mut value = 0; + while let Some(((_, c), next)) = iter.next() { + match to_digit(c) { + Ok(digit) => value = 10 * value + digit, + Err(e) => return Err(e), + } + iter = next; + } + Ok(value) +} +pub const fn to_digit (c: char) -> Result { + Ok(match c { + '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, + '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, + _ => return Err(Unexpected(c)) + }) +} +/// Parsed substring with range and value. +#[derive(Debug, Clone, Default, PartialEq)] +pub struct Token { + /// Source span of token. + span: Span, + /// Meaning of token. + value: Val, +} #[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct Span { /// Reference to source text. @@ -235,20 +319,13 @@ impl<'s, D: Dsl> Span { str_range(self.source, self.start, self.end()) } pub const fn slice_exp (&self) -> &'s str { str_range(self.source, self.start.saturating_add(1), self.end()) } - pub const fn grow (&mut self) -> DslUsually<&mut Self> { - if self.length + self.start >= self.source.len() { return Err(End) } - self.length = self.length.saturating_add(1); - Ok(self) } + pub const fn grow (&mut self) -> &mut Self { + let max_length = self.source.len().saturating_sub(self.start); + self.length = self.length + 1; + if self.length > max_length { self.length = max_length } + self } } -/// Parsed substring with range and value. -#[derive(Debug, Clone, Default, PartialEq)] -pub struct Token { - /// Source span of token. - span: Span, - /// Meaning of token. - value: Val, -} impl Token { pub const fn value (&self) -> &Val { &self.value } pub const fn span (&self) -> &Span { &self.span } @@ -256,76 +333,14 @@ impl Token { if let Val::Error(e) = self.value { Some(e) } else { None } } pub const fn new (source: D::Str, start: usize, length: usize, value: Val) -> Self { Self { value, span: Span { source, start, length } } } - pub const fn copy (&self) -> Self where D::Str: Copy, D::Exp: Copy { + pub const fn copy (&self) -> Self where D::Str: Copy, D::Exp: Copy, Val: Copy { Self { span: Span { ..self.span }, value: self.value } } } -const fn or_panic (result: DslUsually) -> T { - match result { Ok(t) => t, Err(e) => const_panic::concat_panic!(e) } -} -impl<'s, D: Dsl> Token { - pub const fn peek (src: D::Str) -> DslPerhaps where D::Exp: From<&'s str> { - use Val::*; - let mut t = Self::new(src, 0, 0, Nil); - let mut iter = char_indices(src); - while let Some(((i, c), next)) = iter.next() { - t = match (t.value(), c) { - (Error(_), _) => - return Ok(Some(t)), - (Nil, ' '|'\n'|'\r'|'\t') => - *or_panic(t.grow()), - (Nil, '(') => - Self::new(src, i, 1, Exp(1, D::Exp::from(str_range(src, i, i + 1)))), - (Nil, '"') => - Self::new(src, i, 1, Str(str_range(src, i, i + 1))), - (Nil, ':'|'@') => - Self::new(src, i, 1, Sym(str_range(src, i, i + 1))), - (Nil, '/'|'a'..='z') => - Self::new(src, i, 1, Key(str_range(src, i, i + 1))), - (Nil, '0'..='9') => - Self::new(src, i, 1, match to_digit(c) { Ok(c) => Num(c), Err(e) => Error(e) }), - (Nil, _) => - { t.value = Val::Error(Unexpected(c)); t }, - (Str(_), '"') => - return Ok(Some(t)), - (Str(_), _) => - { or_panic(t.grow()); t.value = Str(t.span.slice()); t }, - (Num(m), ' '|'\n'|'\r'|'\t'|')') => - return Ok(Some(t)), - (Num(m), _) => match to_digit(c) { - Ok(n) => { t.grow()?; t.value = Num(10*m+n); t }, - Err(e) => { t.grow()?; t.value = Error(e); t } }, - (Sym(_), ' '|'\n'|'\r'|'\t'|')') => - return Ok(Some(t)), - (Sym(_), 'a'..='z'|'A'..='Z'|'0'..='9'|'-') => { - t.grow()?; t.value = Sym(t.span.slice()); t }, - (Sym(_), _) => - { t.value = Error(Unexpected(c)); t }, - (Key(_), ' '|'\n'|'\r'|'\t'|')') => - return Ok(Some(t)), - (Key(_), 'a'..='z'|'0'..='9'|'-'|'/') => - { t.grow()?; t.value = Key(t.span.slice()); t }, - (Key(_), _ ) => - { t.value = Error(Unexpected(c)); t }, - (Exp(0, _), _) => - { t.grow()?; t.value = Exp(0, D::Exp::from(t.span.slice_exp())); return Ok(Some(t)) }, - (Exp(d, _), ')') => - { t.grow()?; t.value = Exp(d-1, D::Exp::from(t.span.slice_exp())); t }, - (Exp(d, _), '(') => - { t.grow()?; t.value = Exp(d+1, D::Exp::from(t.span.slice_exp())); t }, - (Exp(d, _), _ ) => - { t.grow()?; t.value = Exp(*d, D::Exp::from(t.span.slice_exp())); t }, - }; - iter = next; - } - Ok(match t.value() { - Nil => None, - _ => Some(t) - }) - } - pub const fn grow (&mut self) -> DslUsually<&mut Self> { self.span.grow()?; Ok(self) } +impl<'s, D: Dsl>> Token { + pub const fn grown (mut self) -> Self { self.span.grow(); self } pub const fn grow_exp (&mut self, d: isize) -> &mut Self where D::Exp: From<&'s str> { if let Val::Exp(depth, _) = self.value() { - self.value = Val::Exp((*depth as isize + d) as usize, D::Exp::from(self.span.slice_exp())); + self.value = Val::Exp(*depth as isize + d, CstConstIter(self.span.slice_exp())); self } else { unreachable!() @@ -333,20 +348,22 @@ impl<'s, D: Dsl> Token { } } -pub const fn to_digit (c: char) -> DslResult { - Ok(match c { - '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, - '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, - _ => return Result::Err(Unexpected(c)) }) } - -macro_rules! iterate(($expr:expr => $arg: pat => $body:expr) => { - let mut iter = $expr; while let Some(($arg, next)) = iter.next() { $body; iter = next; } }); -pub const fn to_number (digits: &str) -> DslResult { - let mut value = 0; - iterate!(char_indices(digits) => (_, c) => match to_digit(c) { - Ok(digit) => value = 10 * value + digit, - Result::Err(e) => return Result::Err(e) }); - Ok(value) } +/// `State` + [Dsl] -> `Self`. +pub trait FromDsl: Sized { + fn try_from_dsl (state: &State, dsl: &impl Dsl) -> Perhaps; + fn from_dsl (state: &State, dsl: &impl Dsl, err: impl Fn()->Box) -> Usually { + match Self::try_from_dsl(state, dsl)? { Some(dsl) => Ok(dsl), _ => Err(err()) } } +} +/// `self` + `Options` -> [Dsl] +pub trait IntoDsl { /*TODO*/ } +/// `self` + [Dsl] -> `Item` +pub trait DslInto { + fn try_dsl_into (&self, dsl: &impl Dsl) -> Perhaps; + fn dsl_into (&self, dsl: &impl Dsl, err: impl Fn()->Box) -> Usually { + match Self::try_dsl_into(self, dsl)? { Some(dsl) => Ok(dsl), _ => Err(err()) } } +} +/// `self` + `Item` -> [Dsl] +pub trait DslFrom { /*TODO*/ } /// Implement type conversions. macro_rules! from(($($Struct:ty { $( diff --git a/input/src/input_dsl.rs b/input/src/input_dsl.rs index 7ac0644..882612d 100644 --- a/input/src/input_dsl.rs +++ b/input/src/input_dsl.rs @@ -4,24 +4,24 @@ use crate::*; /// Each contained layer defines a mapping from input event to command invocation /// over a given state. Furthermore, each layer may have an associated cond, /// so that only certain layers are active at a given time depending on state. -#[derive(Debug)] pub struct InputMap( +#[derive(Debug)] pub struct InputMap( /// Map of input event (key combination) to /// all command expressions bound to it by /// all loaded input layers. - pub BTreeMap>> + pub BTreeMap>> ); -impl Default for InputMap { +impl Default for InputMap { fn default () -> Self { Self(Default::default()) } } -#[derive(Debug, Default)] pub struct InputBinding { - condition: Option, - command: T, +#[derive(Debug, Default)] pub struct InputBinding { + condition: Option, + command: D, description: Option>, source: Option>, } -impl InputMap { +impl<'s, I: Debug + Ord, D: Dsl + From>> InputMap { /// Create input layer collection from path to text file. pub fn from_path > (path: P) -> Usually { if !exists(path.as_ref())? { @@ -30,22 +30,19 @@ impl InputMap { Self::from_source(read_and_leak(path)?) } /// Create input layer collection from string. - pub fn from_source > (source: S) -> Usually { - Self::from_dsl(CstIter::from(source.as_ref())) + pub fn from_source (source: impl AsRef) -> Usually { + Self::from_dsl(D::from(Cst::from(source.as_ref()))) } /// Create input layer collection from DSL. - pub fn from_dsl (dsl: D) -> Usually { - use DslVal::*; - let mut input_map: BTreeMap>> = Default::default(); - let mut index = 0; - while let Some(Exp(_, mut exp)) = dsl.nth(index) { - let val = exp.nth(0).map(|x|x.val()); - match val { + pub fn from_dsl (dsl: D) -> Usually { + use Val::*; + let mut input_map: BTreeMap>> = Default::default(); + match dsl.exp() { + Some(exp) => match exp.head() { Some(Str(path)) => { - let path = PathBuf::from(path.as_ref()); - let module = InputMap::::from_path(&path)?; - for (key, val) in module.0.into_iter() { - todo!("import {exp:?} {key:?} {val:?} {path:?}"); + let path = PathBuf::from(path.as_ref()); + for (key, val) in InputMap::::from_path(&path)?.0.into_iter() { + todo!("import {path:?} {key:?} {val:?}"); if !input_map.contains_key(&key) { input_map.insert(key, vec![]); } @@ -56,14 +53,14 @@ impl InputMap { //if !input_map.contains_key(&key) { //input_map.insert(key, vec![]); //} - todo!("binding {exp:?} {sym:?}"); + todo!("binding {sym:?} {:?}", exp.tail()); }, - Some(Key(key)) if key.as_ref() == "if" => { - todo!("conditional binding {exp:?} {key:?}"); + Some(Key("if")) => { + todo!("conditional binding {:?}", exp.tail()); }, - _ => return Result::Err(format!("invalid token in keymap: {val:?}").into()), - } - index += 1; + _ => return Err(format!("invalid form in keymap: {exp:?}").into()) + }, + _ => return Err(format!("not an expression: {dsl:?}").into()) } Ok(Self(input_map)) } @@ -92,23 +89,23 @@ impl InputMap { /* /// Create an input map with a single non-condal layer. /// (Use [Default::default] to get an empty map.) - pub fn new (layer: DslVal) -> Self { + pub fn new (layer: Val) -> Self { Self::default().layer(layer) } /// Add layer, return `Self`. - pub fn layer (mut self, layer: DslVal) -> Self { + pub fn layer (mut self, layer: Val) -> Self { self.add_layer(layer); self } /// Add condal layer, return `Self`. - pub fn layer_if (mut self, cond: DslVal, layer: DslVal) -> Self { + pub fn layer_if (mut self, cond: Val, layer: Val) -> Self { self.add_layer_if(Some(cond), layer); self } /// Add layer, return `&mut Self`. - pub fn add_layer (&mut self, layer: DslVal) -> &mut Self { + pub fn add_layer (&mut self, layer: Val) -> &mut Self { self.add_layer_if(None, layer.into()); self } /// Add condal layer, return `&mut Self`. - pub fn add_layer_if (&mut self, cond: Option>, bind: DslVal) -> &mut Self { + pub fn add_layer_if (&mut self, cond: Option>, bind: Val) -> &mut Self { self.0.push(InputLayer { cond, bind }); self } @@ -182,7 +179,7 @@ impl InputMap { //} //fn from (source: &'s str) -> Self { //// this should be for single layer: - //use DslVal::*; + //use Val::*; //let mut layers = vec![]; //let mut source = CstIter::from(source); //while let Some(Exp(_, mut iter)) = source.next().map(|x|x.value) { diff --git a/output/src/ops.rs b/output/src/ops.rs index 13ade31..976ab51 100644 --- a/output/src/ops.rs +++ b/output/src/ops.rs @@ -392,7 +392,7 @@ transform_xy_unit!("padding/x" "padding/y" "padding/xy"|self: Padding, area|{ //)*) => { //$( //impl FromDsl for $Struct$(<$($A),+>)? { - //fn try_dsl_from ( + //fn try_from_dsl ( //state: &S, dsl: &impl Dsl //) -> Perhaps { //todo!() @@ -408,7 +408,7 @@ transform_xy_unit!("padding/x" "padding/y" "padding/xy"|self: Padding, area|{ $op:literal $(/)? [$head: ident, $tail: ident] $expr:expr ) => { impl FromDsl for $Struct$(<$($A),+>)? { - fn try_dsl_from ( + fn try_from_dsl ( _state: &S, _dsl: &impl Dsl ) -> Perhaps { todo!() @@ -423,7 +423,7 @@ transform_xy_unit!("padding/x" "padding/y" "padding/xy"|self: Padding, area|{ $op:literal $(/)? [$head: ident, $tail: ident] $expr:expr ) => { impl FromDsl for $Struct$(<$($A),+>)? { - fn try_dsl_from ( + fn try_from_dsl ( _state: &S, _dsl: &impl Dsl ) -> Perhaps { todo!() From a145e332dec73ebea1fa3576ae48be13e3ad104c Mon Sep 17 00:00:00 2001 From: unspeaker Date: Thu, 17 Jul 2025 21:18:17 +0300 Subject: [PATCH 2/2] 1 file, 300 lines, many worries --- dsl/src/lib.rs | 437 ++++++++++++++++++++--------------------- input/src/input_dsl.rs | 45 ++--- 2 files changed, 239 insertions(+), 243 deletions(-) diff --git a/dsl/src/lib.rs b/dsl/src/lib.rs index 416db82..437f4de 100644 --- a/dsl/src/lib.rs +++ b/dsl/src/lib.rs @@ -14,7 +14,110 @@ pub(crate) use konst::string::{split_at, str_range, char_indices}; pub(crate) use thiserror::Error; pub(crate) use self::DslError::*; #[cfg(test)] mod test; -/// A DSL expression. Generic over string and expression storage. +/// Enumeration of values that may figure in an expression. +/// Generic over string and expression storage. +#[derive(Clone, Debug, PartialEq, Default)] +pub enum Val { + /// Empty expression + #[default] Nil, + /// Unsigned integer literal + Num(usize), + /// An identifier that starts with `.` + Sym(Str), + /// An identifier that doesn't start with `:` + Key(Str), + /// A quoted string literal + Str(Str), + /// A DSL expression. + Exp( + /// Number of unclosed parentheses. Must be 0 to be valid. + isize, + /// Expression content. + Exp + ), + /// An error. + Error(DslError), +} +impl Copy for Val {} +impl Val { + pub fn convert (&self) -> Val where + T::Str: for<'a> From<&'a Str>, + T::Exp: for<'a> From<&'a Exp> + { + match self { Val::Nil => Val::Nil, + Val::Num(u) => Val::Num(*u), + Val::Sym(s) => Val::Sym(s.into()), + Val::Key(s) => Val::Key(s.into()), + Val::Str(s) => Val::Str(s.into()), + Val::Exp(d, x) => Val::Exp(*d, x.into()), + Val::Error(e) => Val::Error(*e) } } +} +/// The expression representation for a [Dsl] implementation. +/// [Cst] uses [CstIter]. [Ast] uses [VecDeque]. +pub trait DslExp: PartialEq + Clone + Default + Debug + Dsl {} +impl DslExp for T {} +/// The string representation for a [Dsl] implementation. +/// [Cst] uses `&'s str`. [Ast] uses `Arc`. +pub trait DslStr: PartialEq + Clone + Default + Debug + AsRef + std::ops::Deref {} +impl + std::ops::Deref> DslStr for T {} +impl> Val { + pub const fn err (&self) -> Option {match self{Val::Error(e)=>Some(*e), _=>None}} + pub const fn nil (&self) -> bool {match self{Val::Nil=>true, _=>false}} + pub const fn num (&self) -> Option {match self{Val::Num(n)=>Some(*n), _=>None}} + pub const fn sym (&self) -> Option<&Str> {match self{Val::Sym(s)=>Some(s), _=>None}} + pub const fn key (&self) -> Option<&Str> {match self{Val::Key(k)=>Some(k), _=>None}} + pub const fn str (&self) -> Option<&Str> {match self{Val::Str(s)=>Some(s), _=>None}} + pub const fn exp (&self) -> Option<&Exp> {match self{Val::Exp(_, x)=>Some(x),_=>None}} + pub const fn exp_depth (&self) -> Option {match self{Val::Exp(d, _)=>Some(*d), _=>None}} +} +/// Parsed substring with range and value. +#[derive(Debug, Clone, Default, PartialEq)] +pub struct Token { + /// Meaning of token. + pub value: Val, + /// Reference to source text. + pub source: Str, + /// Index of 1st character of span. + pub start: usize, + /// Length of span. + pub length: usize, +} +impl Copy for Token {} +impl Token { + pub const fn end (&self) -> usize { + self.start.saturating_add(self.length) } + pub const fn value (&self) + -> &Val { &self.value } + pub const fn err (&self) + -> Option { if let Val::Error(e) = self.value { Some(e) } else { None } } + pub const fn new (source: Str, start: usize, length: usize, value: Val) + -> Self { Self { value, start, length, source } } + pub const fn copy (&self) -> Self where Val: Copy, Str: Copy, Exp: Copy { + Self { value: self.value, ..*self } + } +} +impl<'s> CstToken<'s> { + pub const fn slice (&self) -> &str { + str_range(self.source, self.start, self.end()) } + pub const fn slice_exp (&self) -> &str { + str_range(self.source, self.start.saturating_add(1), self.end()) } + pub const fn grow (&mut self) -> &mut Self { + let max_length = self.source.len().saturating_sub(self.start); + self.length = self.length + 1; + if self.length > max_length { self.length = max_length } + self + } + pub const fn grow_exp (&'s mut self, depth: isize, source: &'s str) -> &mut Self { + self.value = Val::Exp(depth, Cst(CstConstIter(source))); + self + } +} +/// To the [Dsl], a token is equivalent to its `value` field. +impl Dsl for Token { + type Str = Str; type Exp = Exp; + fn dsl (&self) -> Val { self.value.clone() } +} +/// Coerce to [Val] for predefined [Self::Str] and [Self::Exp]. pub trait Dsl: Clone + Debug { /// The string representation for a dizzle. type Str: DslStr; @@ -22,139 +125,48 @@ pub trait Dsl: Clone + Debug { type Exp: DslExp; /// Request the top-level DSL [Val]ue. /// May perform cloning or parsing. - fn dsl (&self) -> Val; - fn err (&self) -> Option {self.dsl().err()} - fn nil (&self) -> bool {self.dsl().nil()} - fn num (&self) -> Option {self.dsl().num()} - fn sym (&self) -> Option {self.dsl().sym()} - fn key (&self) -> Option {self.dsl().key()} - fn str (&self) -> Option {self.dsl().str()} - fn exp (&self) -> Option {self.dsl().exp()} + fn dsl (&self) -> Val; + fn err (&self) -> Option {self.dsl().err()} + fn nil (&self) -> bool {self.dsl().nil()} + fn num (&self) -> Option {self.dsl().num()} + fn sym (&self) -> Option {self.dsl().sym()} + fn key (&self) -> Option {self.dsl().key()} + fn str (&self) -> Option {self.dsl().str()} + fn exp (&self) -> Option {self.dsl().exp()} + fn exp_depth (&self) -> Option {self.dsl().exp_depth()} + fn exp_head (&self) -> Val {self.dsl().exp_head()} + fn exp_tail (&self) -> Val {self.dsl().exp_tail()} } -/// Enumeration of values that may figure in an expression. -/// Generic over [Dsl] implementation. -#[derive(Clone, Debug, PartialEq, Default)] -pub enum Val { - /// Empty expression - #[default] Nil, - /// Unsigned integer literal - Num(usize), - /// An identifier that starts with `.` - Sym(D::Str), - /// An identifier that doesn't start with `:` - Key(D::Str), - /// A quoted string literal - Str(D::Str), - /// A sub-expression. - Exp( - /// Expression depth checksum. Must be 0, otherwise you have an unclosed delimiter. - isize, - /// Expression content. - D::Exp - ), - /// An error. - Error(DslError), -} -impl Val { - pub fn err (&self) -> Option {match self{Val::Error(e)=>Some(*e), _=>None}} - pub fn nil (&self) -> bool {match self{Val::Nil=>true, _=>false}} - pub fn num (&self) -> Option {match self{Val::Num(n)=>Some(*n), _=>None}} - pub fn sym (&self) -> Option {match self{Val::Sym(s)=>Some(s.clone()), _=>None}} - pub fn key (&self) -> Option {match self{Val::Key(k)=>Some(k.clone()), _=>None}} - pub fn str (&self) -> Option {match self{Val::Str(s)=>Some(s.clone()), _=>None}} - pub fn exp (&self) -> Option {match self{Val::Exp(_, x)=>Some(x.clone()),_=>None}} +/// The most basic implementor of the [Dsl] trait. +impl Dsl for Val { + type Str = Str; type Exp = Exp; + fn dsl (&self) -> Val { self.clone() } } /// The abstract syntax tree (AST) can be produced from the CST /// by cloning source slices into owned ([Arc]) string slices. #[derive(Debug, Clone, Default, PartialEq)] -pub struct Ast(Arc>>>); +pub struct Ast(Arc, Ast>>>>); impl Dsl for Ast { - type Str = Arc; - type Exp = Arc>>>; - fn dsl (&self) -> Val { Val::Exp(0, self.0.clone()) } + type Str = Arc; type Exp = Ast; + fn dsl (&self) -> Val, Ast> { Val::Exp(0, Ast(self.0.clone())) } } /// The concrete syntax tree (CST) implements zero-copy /// parsing of the DSL from a string reference. CST items /// preserve info about their location in the source. /// CST stores strings as source references and expressions as [CstIter] instances. #[derive(Debug, Copy, Clone, Default, PartialEq)] -pub struct Cst<'s>(CstConstIter<'s>); +pub struct Cst<'s>(pub CstConstIter<'s>); +pub type CstVal<'s> = Val<&'s str, Cst<'s>>; +pub type CstToken<'s> = Token<&'s str, Cst<'s>>; impl<'s> Dsl for Cst<'s> { - type Str = &'s str; - type Exp = CstConstIter<'s>; - fn dsl (&self) -> Val { Val::Exp(0, self.0) } + type Str = &'s str; type Exp = Cst<'s>; + fn dsl (&self) -> Val { Val::Exp(0, Cst(self.0)) } } impl<'s> From<&'s str> for Cst<'s> { fn from (source: &'s str) -> Self { Self(CstConstIter(source)) } } -/// The string representation for a [Dsl] implementation. -/// [Cst] uses `&'s str`. [Ast] uses `Arc`. -pub trait DslStr: PartialEq + Clone + Default + Debug + AsRef + std::ops::Deref {} -impl + std::ops::Deref> DslStr for T {} -/// The expression representation for a [Dsl] implementation. -/// [Cst] uses [CstIter]. [Ast] uses [VecDeque]. -pub trait DslExp: PartialEq + Clone + Default + Debug { - fn head (&self) -> Val; - fn tail (&self) -> Self; -} -impl DslExp for Arc>>> { - fn head (&self) -> Val { - self.get(0).cloned().unwrap_or_else(||Arc::new(Default::default())).value.into() - } - fn tail (&self) -> Self { - Self::new(self.iter().skip(1).cloned().collect()) - } -} -impl<'s> DslExp for CstConstIter<'s> { - fn head (&self) -> Val { - peek(self.0).value.into() - } - fn tail (&self) -> Self { - let Token { span: Span { start, length, source }, .. } = peek(self.0); - Self(&source[(start+length)..]) - } -} -impl + Copy> Copy for Val {} -impl Val { - pub fn is_nil (&self) -> bool { matches!(self, Self::Nil) } - pub fn as_error (&self) -> Option<&DslError> { if let Self::Error(e) = self { Some(e) } else { None } } - pub fn as_num (&self) -> Option {match self{Self::Num(n)=>Some(*n),_=>None}} - pub fn as_sym (&self) -> Option<&str> {match self{Self::Sym(s )=>Some(s.as_ref()),_=>None}} - pub fn as_key (&self) -> Option<&str> {match self{Self::Key(k )=>Some(k.as_ref()),_=>None}} - pub fn as_str (&self) -> Option<&str> {match self{Self::Str(s )=>Some(s.as_ref()),_=>None}} - //pub fn as_exp (&self) -> Option<&D::Exp> {match self{Self::Exp(_, x)=>Some(x),_=>None}} - pub fn exp_depth (&self) -> Option { todo!() } - pub fn exp_head_tail (&self) -> (Option<&Self>, Option<&D::Exp>) { (self.exp_head(), self.exp_tail()) } - pub fn exp_head (&self) -> Option<&Self> { todo!() } // TODO - pub fn exp_tail (&self) -> Option<&D::Exp> { todo!() } - pub fn peek (&self) -> Option { todo!() } - pub fn next (&mut self) -> Option { todo!() } - pub fn rest (self) -> Vec { todo!() } - //pub fn convert (&self) -> Val where - //T::Str: for<'a> From<&'a D::Str>, - //T::Exp: for<'a> From<&'a D::Exp> - //{ - //match self { Val::Nil => Val::Nil, - //Val::Num(u) => Val::Num(*u), - //Val::Sym(s) => Val::Sym(s.into()), - //Val::Key(s) => Val::Key(s.into()), - //Val::Str(s) => Val::Str(s.into()), - //Val::Exp(d, x) => Val::Exp(*d, x.into()), - //Val::Error(e) => Val::Error(*e) } } - //pub fn exp_match (&self, namespace: &str, cb: F) -> DslPerhaps - //where F: Fn(&str, &Exp)-> DslPerhaps { - //if let Some(Self::Key(key)) = self.exp_head() - //&& key.as_ref().starts_with(namespace) - //&& let Some(tail) = self.exp_tail() { - //cb(key.as_ref().split_at(namespace.len()).1, tail) - //} else { - //Ok(None) - //} - //} -} - /// DSL-specific error codes. #[derive(Error, Debug, Copy, Clone, PartialEq, PanicFmt)] pub enum DslError { #[error("parse failed: not implemented")] @@ -175,15 +187,15 @@ impl Val { /// [Cst::next] returns just the [Cst] and mutates `self`, /// instead of returning an updated version of the struct as [CstConstIter::next] does. #[derive(Copy, Clone, Debug, Default, PartialEq)] -pub struct CstIter<'s>(CstConstIter<'s>); +pub struct CstIter<'s>(pub CstConstIter<'s>); impl<'s> CstIter<'s> { pub const fn new (source: &'s str) -> Self { Self(CstConstIter::new(source)) } } impl<'s> Iterator for CstIter<'s> { - type Item = Token>; + type Item = CstToken<'s>; fn next (&mut self) -> Option { match self.0.advance() { - Some((item, rest)) => { self.0 = rest; item.into() }, + Some((item, rest)) => { self.0 = rest; Some(item.into()) }, None => None, } } @@ -199,8 +211,8 @@ impl<'s> From <&'s str> for CstConstIter<'s> { fn from (src: &'s str) -> Self { Self(src) } } impl<'s> Iterator for CstConstIter<'s> { - type Item = Token>; - fn next (&mut self) -> Option>> { self.advance().map(|x|x.0) } + type Item = CstToken<'s>; + fn next (&mut self) -> Option> { self.advance().map(|x|x.0) } } impl<'s> ConstIntoIter for CstConstIter<'s> { type Kind = IsIteratorKind; @@ -210,17 +222,100 @@ impl<'s> ConstIntoIter for CstConstIter<'s> { impl<'s> CstConstIter<'s> { pub const fn new (source: &'s str) -> Self { Self(source) } pub const fn chomp (&self, index: usize) -> Self { Self(split_at(self.0, index).1) } - pub const fn advance (&mut self) -> Option<(Token>, Self)> { - match peek(self.0) { + pub const fn advance (&mut self) -> Option<(CstToken<'s>, Self)> { + match peek(Val::Nil, self.0) { Token { value: Val::Nil, .. } => None, token => { - let end = self.chomp(token.span.end()); + let end = self.chomp(token.end()); Some((token.copy(), end)) }, } } } +pub const fn peek <'s> (mut value: CstVal<'s>, source: &'s str) -> CstToken<'s> { + use Val::*; + let mut start = 0; + let mut length = 0; + let mut source = source; + loop { + if let Some(((i, c), next)) = char_indices(source).next() { + if matches!(value, Error(_)) { + break + } else if matches!(value, Nil) { + if is_whitespace(c) { + length += 1; + continue + } + start = i; + length = 1; + if is_exp_start(c) { + value = Exp(1, Cst(CstConstIter(str_range(source, i, i+1)))); + } else if is_str_start(c) { + value = Str(str_range(source, i, i+1)); + } else if is_sym_start(c) { + value = Sym(str_range(source, i, i+1)); + } else if is_key_start(c) { + value = Key(str_range(source, i, i+1)); + } else if is_digit(c) { + value = match to_digit(c) { Ok(c) => Num(c), Err(e) => Error(e) }; + } else { + value = Error(Unexpected(c)); + break + } + } else if matches!(value, Str(_)) { + if is_str_end(c) { + break + } else { + value = Str(str_range(source, start, start + length + 1)); + } + } else if matches!(value, Sym(_)) { + if is_sym_end(c) { + break + } else if is_sym_char(c) { + value = Sym(str_range(source, start, start + length + 1)); + } else { + value = Error(Unexpected(c)); + } + } else if matches!(value, Key(_)) { + if is_key_end(c) { + break + } + length += 1; + if is_key_char(c) { + value = Key(str_range(source, start, start + length + 1)); + } else { + value = Error(Unexpected(c)); + } + } else if let Exp(depth, exp) = value { + if depth == 0 { + value = Exp(0, Cst(CstConstIter(str_range(source, start, start + length)))); + break + } + length += 1; + if c == ')' { + value = Exp(depth-1, Cst(CstConstIter(str_range(source, start, start + length)))); + } else if c == '(' { + value = Exp(depth+1, Cst(CstConstIter(str_range(source, start, start + length)))); + } else { + value = Exp(depth, Cst(CstConstIter(str_range(source, start, start + length)))); + } + } else if let Num(m) = value { + if is_num_end(c) { + break + } + length += 1; + match to_digit(c) { + Ok(n) => { value = Num(n+10*m); }, + Err(e) => { value = Error(e); } + } + } + } else { + break + } + } + return Token { value, source, start, length } +} const fn is_whitespace (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t') } const fn is_digit (c: char) -> bool { matches!(c, '0'..='9') } const fn is_num_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') } @@ -233,48 +328,6 @@ const fn is_sym_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') } const fn is_str_start (c: char) -> bool { matches!(c, '"') } const fn is_str_end (c: char) -> bool { matches!(c, '"') } const fn is_exp_start (c: char) -> bool { matches!(c, '(') } - -pub const fn peek <'s> (src: &'s str) -> Token> { - use Val::*; - let mut t = Token { value: Val::Nil, span: Span { source: src, start: 0, length: 0 } }; - let mut iter = char_indices(src); - while let Some(((i, c), next)) = iter.next() { - t = match (t.value(), c) { - (Error(_), _) => return t, - - (Nil, _) if is_exp_start(c) => Token::new(src, i, 1, Exp(1, CstConstIter(str_range(src, i, i+1)))), - (Nil, _) if is_str_start(c) => Token::new(src, i, 1, Str(str_range(src, i, i+1))), - (Nil, _) if is_sym_start(c) => Token::new(src, i, 1, Sym(str_range(src, i, i+1))), - (Nil, _) if is_key_start(c) => Token::new(src, i, 1, Key(str_range(src, i, i+1))), - (Nil, _) if is_digit(c) => Token::new(src, i, 1, match to_digit(c) { Ok(c) => Num(c), Err(e) => Error(e) }), - (Nil, _) if is_whitespace(c) => t.grown(), - (Nil, _) => { t.value = Val::Error(Unexpected(c)); t }, - - (Str(_), _) if is_str_end(c) => return t, - (Str(_), _) => { t.value = Str(t.span.grow().slice()); t }, - - (Sym(_), _) if is_sym_end(c) => return t, - (Sym(_), _) if is_sym_char(c) => { t.value = Sym(t.span.grow().slice()); t }, - (Sym(_), _) => { t.value = Error(Unexpected(c)); t }, - - (Key(_), _) if is_key_end(c) => return t, - (Key(_), _) if is_key_char(c) => { t.value = Key(t.span.grow().slice()); t }, - (Key(_), _) => { t.value = Error(Unexpected(c)); t }, - - (Exp(0, _), _) => { t.value = Exp(0, CstConstIter(t.span.grow().slice_exp())); return t }, - (Exp(d, _), ')') => { t.value = Exp((*d)-1, CstConstIter(t.span.grow().slice_exp())); t }, - (Exp(d, _), '(') => { t.value = Exp((*d)+1, CstConstIter(t.span.grow().slice_exp())); t }, - (Exp(d, _), _ ) => { t.value = Exp(*d, CstConstIter(t.span.grow().slice_exp())); t }, - - (Num(m), _) if is_num_end(c) => return t, - (Num(m), _) => match to_digit(c) { - Ok(n) => { let m = *m; t.span.grow(); t.value = Num(n+10*m); t }, - Err(e) => { t.span.grow(); t.value = Error(e); t } }, - }; - iter = next; - } - t -} pub const fn to_number (digits: &str) -> Result { let mut iter = char_indices(digits); let mut value = 0; @@ -294,60 +347,6 @@ pub const fn to_digit (c: char) -> Result { _ => return Err(Unexpected(c)) }) } -/// Parsed substring with range and value. -#[derive(Debug, Clone, Default, PartialEq)] -pub struct Token { - /// Source span of token. - span: Span, - /// Meaning of token. - value: Val, -} -#[derive(Debug, Copy, Clone, Default, PartialEq)] -pub struct Span { - /// Reference to source text. - pub source: D::Str, - /// Index of 1st character of span. - pub start: usize, - /// Length of span. - pub length: usize, -} -impl<'s, D: Dsl> Span { - pub const fn end (&self) -> usize { self.start.saturating_add(self.length) } -} -impl<'s, D: Dsl> Span { - pub const fn slice (&self) -> &'s str { - str_range(self.source, self.start, self.end()) } - pub const fn slice_exp (&self) -> &'s str { - str_range(self.source, self.start.saturating_add(1), self.end()) } - pub const fn grow (&mut self) -> &mut Self { - let max_length = self.source.len().saturating_sub(self.start); - self.length = self.length + 1; - if self.length > max_length { self.length = max_length } - self } -} - -impl Token { - pub const fn value (&self) -> &Val { &self.value } - pub const fn span (&self) -> &Span { &self.span } - pub const fn err (&self) -> Option { - if let Val::Error(e) = self.value { Some(e) } else { None } } - pub const fn new (source: D::Str, start: usize, length: usize, value: Val) -> Self { - Self { value, span: Span { source, start, length } } } - pub const fn copy (&self) -> Self where D::Str: Copy, D::Exp: Copy, Val: Copy { - Self { span: Span { ..self.span }, value: self.value } } -} -impl<'s, D: Dsl>> Token { - pub const fn grown (mut self) -> Self { self.span.grow(); self } - pub const fn grow_exp (&mut self, d: isize) -> &mut Self where D::Exp: From<&'s str> { - if let Val::Exp(depth, _) = self.value() { - self.value = Val::Exp(*depth as isize + d, CstConstIter(self.span.slice_exp())); - self - } else { - unreachable!() - } - } -} - /// `State` + [Dsl] -> `Self`. pub trait FromDsl: Sized { fn try_from_dsl (state: &State, dsl: &impl Dsl) -> Perhaps; diff --git a/input/src/input_dsl.rs b/input/src/input_dsl.rs index 882612d..49fb270 100644 --- a/input/src/input_dsl.rs +++ b/input/src/input_dsl.rs @@ -30,37 +30,34 @@ impl<'s, I: Debug + Ord, D: Dsl + From>> InputMap { Self::from_source(read_and_leak(path)?) } /// Create input layer collection from string. - pub fn from_source (source: impl AsRef) -> Usually { - Self::from_dsl(D::from(Cst::from(source.as_ref()))) + pub fn from_source (source: &'s str) -> Usually { + Self::from_dsl(D::from(Cst(CstConstIter(source)))) } /// Create input layer collection from DSL. pub fn from_dsl (dsl: D) -> Usually { use Val::*; let mut input_map: BTreeMap>> = Default::default(); - match dsl.exp() { - Some(exp) => match exp.head() { - Some(Str(path)) => { - let path = PathBuf::from(path.as_ref()); - for (key, val) in InputMap::::from_path(&path)?.0.into_iter() { - todo!("import {path:?} {key:?} {val:?}"); - if !input_map.contains_key(&key) { - input_map.insert(key, vec![]); - } + match dsl.exp_head() { + Str(path) => { + let path = PathBuf::from(path.as_ref()); + for (key, val) in InputMap::::from_path(&path)?.0.into_iter() { + todo!("import {path:?} {key:?} {val:?}"); + if !input_map.contains_key(&key) { + input_map.insert(key, vec![]); } - }, - Some(Sym(sym)) => { - //let key: I = sym.into(); - //if !input_map.contains_key(&key) { - //input_map.insert(key, vec![]); - //} - todo!("binding {sym:?} {:?}", exp.tail()); - }, - Some(Key("if")) => { - todo!("conditional binding {:?}", exp.tail()); - }, - _ => return Err(format!("invalid form in keymap: {exp:?}").into()) + } }, - _ => return Err(format!("not an expression: {dsl:?}").into()) + Sym(sym) => { + //let key: I = sym.into(); + //if !input_map.contains_key(&key) { + //input_map.insert(key, vec![]); + //} + todo!("binding {sym:?} {:?}", dsl.exp_tail()); + }, + Key(s) if s.as_ref() == "if" => { + todo!("conditional binding {:?}", dsl.exp_tail()); + }, + _ => return Err(format!("invalid form in keymap: {dsl:?}").into()) } Ok(Self(input_map)) }