Compare commits

..

No commits in common. "a145e332dec73ebea1fa3576ae48be13e3ad104c" and "e72225f83c89a2bc9c6bee9b00373dfa530e1909" have entirely different histories.

3 changed files with 285 additions and 295 deletions

View file

@ -14,35 +14,50 @@ pub(crate) use konst::string::{split_at, str_range, char_indices};
pub(crate) use thiserror::Error; pub(crate) use thiserror::Error;
pub(crate) use self::DslError::*; pub(crate) use self::DslError::*;
#[cfg(test)] mod test; #[cfg(test)] mod test;
/// Enumeration of values that may figure in an expression.
pub type DslUsually<T> = Result<T, DslError>;
pub type DslPerhaps<T> = Result<Option<T>, DslError>;
/// Pronounced dizzle.
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<Self>>;
}
/// Enumeration of values representable by a DSL [Token]s.
/// Generic over string and expression storage. /// Generic over string and expression storage.
#[derive(Clone, Debug, PartialEq, Default)] #[derive(Clone, Debug, PartialEq, Default)]
pub enum Val<Str, Exp> { pub enum Val<D: Dsl> {
/// Empty expression #[default]
#[default] Nil, Nil,
/// Unsigned integer literal /// Unsigned integer literal
Num(usize), Num(usize),
/// An identifier that starts with `.` /// Tokens that start with `:`
Sym(Str), Sym(D::Str),
/// An identifier that doesn't start with `:` /// Tokens that don't start with `:`
Key(Str), Key(D::Str),
/// A quoted string literal /// Quoted string literals
Str(Str), Str(D::Str),
/// A DSL expression. /// Expressions.
Exp( Exp(
/// Number of unclosed parentheses. Must be 0 to be valid. /// Expression depth checksum. Must be 0, otherwise you have an unclosed delimiter.
isize, usize,
/// Expression content. /// Expression content.
Exp D::Exp
), ),
/// An error.
Error(DslError), Error(DslError),
} }
impl<Str: Copy, Exp: Copy> Copy for Val<Str, Exp> {}
impl<Str: DslStr, Exp: DslExp> Val<Str, Exp> { impl<Str: Copy, Exp: Copy, D: Dsl<Str=Str,Exp=Exp>> Copy for Val<D> {}
pub fn convert <T: Dsl> (&self) -> Val<T::Str, T::Exp> where
T::Str: for<'a> From<&'a Str>, impl<D: Dsl> Val<D> {
T::Exp: for<'a> From<&'a Exp> pub fn convert <T: Dsl> (&self) -> Val<T> where
B::Str: for<'a> From<&'a D::Str>,
B::Exp: for<'a> From<&'a D::Exp>
{ {
match self { Val::Nil => Val::Nil, match self { Val::Nil => Val::Nil,
Val::Num(u) => Val::Num(*u), Val::Num(u) => Val::Num(*u),
@ -51,122 +66,87 @@ impl<Str: DslStr, Exp: DslExp> Val<Str, Exp> {
Val::Str(s) => Val::Str(s.into()), Val::Str(s) => Val::Str(s.into()),
Val::Exp(d, x) => Val::Exp(*d, x.into()), Val::Exp(d, x) => Val::Exp(*d, x.into()),
Val::Error(e) => Val::Error(*e) } } 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<usize> {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<usize> { 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<Self> { todo!() }
pub fn next (&mut self) -> Option<Self> { todo!() }
pub fn rest (self) -> Vec<Self> { todo!() }
//pub fn exp_match <T, F> (&self, namespace: &str, cb: F) -> DslPerhaps<T>
//where F: Fn(&str, &Exp)-> DslPerhaps<T> {
//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)
//}
//}
} }
/// The expression representation for a [Dsl] implementation.
/// [Cst] uses [CstIter]. [Ast] uses [VecDeque].
pub trait DslExp: PartialEq + Clone + Default + Debug + Dsl {}
impl<T: PartialEq + Clone + Default + Debug + Dsl> DslExp for T {}
/// The string representation for a [Dsl] implementation. /// The string representation for a [Dsl] implementation.
/// [Cst] uses `&'s str`. [Ast] uses `Arc<str>`. /// [Cst] uses `&'s str`. [Ast] uses `Arc<str>`.
pub trait DslStr: PartialEq + Clone + Default + Debug + AsRef<str> + std::ops::Deref<Target = str> {} pub trait DslStr: PartialEq + Clone + Default + Debug + AsRef<str> + std::ops::Deref<Target = str> {}
impl<T: PartialEq + Clone + Default + Debug + AsRef<str> + std::ops::Deref<Target = str>> DslStr for T {} impl<T: PartialEq + Clone + Default + Debug + AsRef<str> + std::ops::Deref<Target = str>> DslStr for T {}
impl<Str: DslStr, Exp: DslExp + Dsl<Str=Str, Exp=Exp>> Val<Str, Exp> {
pub const fn err (&self) -> Option<DslError> {match self{Val::Error(e)=>Some(*e), _=>None}} /// The expression representation for a [Dsl] implementation.
pub const fn nil (&self) -> bool {match self{Val::Nil=>true, _=>false}} /// [Cst] uses [CstIter]. [Ast] uses [VecDeque].
pub const fn num (&self) -> Option<usize> {match self{Val::Num(n)=>Some(*n), _=>None}} pub trait DslExp: PartialEq + Clone + Default + Debug {}
pub const fn sym (&self) -> Option<&Str> {match self{Val::Sym(s)=>Some(s), _=>None}} impl <T: PartialEq + Clone + Default + Debug> DslExp for T {}
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<isize> {match self{Val::Exp(d, _)=>Some(*d), _=>None}}
}
/// Parsed substring with range and value.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Token<Str, Exp> {
/// Meaning of token.
pub value: Val<Str, Exp>,
/// Reference to source text.
pub source: Str,
/// Index of 1st character of span.
pub start: usize,
/// Length of span.
pub length: usize,
}
impl<Str: Copy, Exp: Copy> Copy for Token<Str, Exp> {}
impl<Str, Exp> Token<Str, Exp> {
pub const fn end (&self) -> usize {
self.start.saturating_add(self.length) }
pub const fn value (&self)
-> &Val<Str, Exp> { &self.value }
pub const fn err (&self)
-> Option<DslError> { if let Val::Error(e) = self.value { Some(e) } else { None } }
pub const fn new (source: Str, start: usize, length: usize, value: Val<Str, Exp>)
-> Self { Self { value, start, length, source } }
pub const fn copy (&self) -> Self where Val<Str, Exp>: 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<Str: DslStr, Exp: DslExp> Dsl for Token<Str, Exp> {
type Str = Str; type Exp = Exp;
fn dsl (&self) -> Val<Str, Exp> { 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;
/// The expression representation for a dizzle.
type Exp: DslExp;
/// Request the top-level DSL [Val]ue.
/// May perform cloning or parsing.
fn dsl (&self) -> Val<Self::Str, Self::Exp>;
fn err (&self) -> Option<DslError> {self.dsl().err()}
fn nil (&self) -> bool {self.dsl().nil()}
fn num (&self) -> Option<usize> {self.dsl().num()}
fn sym (&self) -> Option<Self::Str> {self.dsl().sym()}
fn key (&self) -> Option<Self::Str> {self.dsl().key()}
fn str (&self) -> Option<Self::Str> {self.dsl().str()}
fn exp (&self) -> Option<Self::Exp> {self.dsl().exp()}
fn exp_depth (&self) -> Option<isize> {self.dsl().exp_depth()}
fn exp_head (&self) -> Val<Self::Str, Self::Exp> {self.dsl().exp_head()}
fn exp_tail (&self) -> Val<Self::Str, Self::Exp> {self.dsl().exp_tail()}
}
/// The most basic implementor of the [Dsl] trait.
impl<Str: DslStr, Exp: DslExp> Dsl for Val<Str, Exp> {
type Str = Str; type Exp = Exp;
fn dsl (&self) -> Val<Str, Exp> { self.clone() }
}
/// The abstract syntax tree (AST) can be produced from the CST /// The abstract syntax tree (AST) can be produced from the CST
/// by cloning source slices into owned ([Arc]) string slices. /// by cloning source slices into owned ([Arc]) string slices.
#[derive(Debug, Clone, Default, PartialEq)] #[derive(Debug, Clone, Default, PartialEq)]
pub struct Ast(Arc<VecDeque<Arc<Token<Arc<str>, Ast>>>>); pub struct Ast(Token<Ast>);
impl Dsl for Ast { impl Dsl for Ast {
type Str = Arc<str>; type Exp = Ast; type Str = Arc<str>;
fn dsl (&self) -> Val<Arc<str>, Ast> { Val::Exp(0, Ast(self.0.clone())) } type Exp = VecDeque<Arc<Token<Ast>>>;
fn dsl (&self) -> DslUsually<&Val<Self>> {
Ok(self.0.value())
} }
}
/// The concrete syntax tree (CST) implements zero-copy /// The concrete syntax tree (CST) implements zero-copy
/// parsing of the DSL from a string reference. CST items /// parsing of the DSL from a string reference. CST items
/// preserve info about their location in the source. /// preserve info about their location in the source.
/// CST stores strings as source references and expressions as [CstIter] instances. /// CST stores strings as source references and expressions as [CstIter] instances.
#[derive(Debug, Copy, Clone, Default, PartialEq)] #[derive(Debug, Clone, Default, PartialEq)]
pub struct Cst<'s>(pub CstConstIter<'s>); pub struct Cst<'s>(Token<Cst<'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> { impl<'s> Dsl for Cst<'s> {
type Str = &'s str; type Exp = Cst<'s>; type Str = &'s str;
fn dsl (&self) -> Val<Self::Str, Self::Exp> { Val::Exp(0, Cst(self.0)) } type Exp = CstConstIter<'s>;
} fn dsl (&self) -> DslUsually<&Val<Self>> {
impl<'s> From<&'s str> for Cst<'s> { Ok(self.0.value())
fn from (source: &'s str) -> Self {
Self(CstConstIter(source))
} }
} }
/// `State` + [Dsl] -> `Self`.
pub trait FromDsl<State>: Sized {
fn try_from_dsl (state: &State, dsl: &impl Dsl) -> Perhaps<Self>;
fn from_dsl (state: &State, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<Self> {
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<Item> {
fn try_dsl_into (&self, dsl: &impl Dsl) -> Perhaps<Item>;
fn dsl_into (&self, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<Item> {
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<T> = Result<T, DslError>;
/// DSL-specific error codes. /// DSL-specific error codes.
#[derive(Error, Debug, Copy, Clone, PartialEq, PanicFmt)] pub enum DslError { #[derive(Error, Debug, Copy, Clone, PartialEq, PanicFmt)] pub enum DslError {
#[error("parse failed: not implemented")] #[error("parse failed: not implemented")]
@ -187,16 +167,17 @@ impl<'s> From<&'s str> for Cst<'s> {
/// [Cst::next] returns just the [Cst] and mutates `self`, /// [Cst::next] returns just the [Cst] and mutates `self`,
/// instead of returning an updated version of the struct as [CstConstIter::next] does. /// instead of returning an updated version of the struct as [CstConstIter::next] does.
#[derive(Copy, Clone, Debug, Default, PartialEq)] #[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct CstIter<'s>(pub CstConstIter<'s>); pub struct CstIter<'s>(CstConstIter<'s>);
impl<'s> CstIter<'s> { impl<'s> CstIter<'s> {
pub const fn new (source: &'s str) -> Self { Self(CstConstIter::new(source)) } pub const fn new (source: &'s str) -> Self { Self(CstConstIter::new(source)) }
} }
impl<'s> Iterator for CstIter<'s> { impl<'s> Iterator for CstIter<'s> {
type Item = CstToken<'s>; type Item = Token<Cst<'s>>;
fn next (&mut self) -> Option<Self::Item> { fn next (&mut self) -> Option<Self::Item> {
match self.0.advance() { match self.0.advance() {
Some((item, rest)) => { self.0 = rest; Some(item.into()) }, Ok(Some((item, rest))) => { self.0 = rest; item.into() },
None => None, Ok(None) => None,
Err(e) => panic!("{e:?}")
} }
} }
} }
@ -211,8 +192,8 @@ impl<'s> From <&'s str> for CstConstIter<'s> {
fn from (src: &'s str) -> Self { Self(src) } fn from (src: &'s str) -> Self { Self(src) }
} }
impl<'s> Iterator for CstConstIter<'s> { impl<'s> Iterator for CstConstIter<'s> {
type Item = CstToken<'s>; type Item = Token<Cst<'s>>;
fn next (&mut self) -> Option<CstToken<'s>> { self.advance().map(|x|x.0) } fn next (&mut self) -> Option<Token<Cst<'s>>> { self.advance().unwrap().map(|x|x.0) }
} }
impl<'s> ConstIntoIter for CstConstIter<'s> { impl<'s> ConstIntoIter for CstConstIter<'s> {
type Kind = IsIteratorKind; type Kind = IsIteratorKind;
@ -222,147 +203,150 @@ impl<'s> ConstIntoIter for CstConstIter<'s> {
impl<'s> CstConstIter<'s> { impl<'s> CstConstIter<'s> {
pub const fn new (source: &'s str) -> Self { Self(source) } 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 chomp (&self, index: usize) -> Self { Self(split_at(self.0, index).1) }
pub const fn advance (&mut self) -> Option<(CstToken<'s>, Self)> { pub const fn peek (&self) -> DslPerhaps<Token<Cst<'s>>> { Token::peek(self.0) }
match peek(Val::Nil, self.0) { //pub const fn next (mut self) -> Option<(Token<Cst<'s>>, Self)> {
Token { value: Val::Nil, .. } => None, //Self::advance(&mut self).unwrap() }
token => { pub const fn advance (&mut self) -> DslPerhaps<(Token<Cst<'s>>, Self)> {
let end = self.chomp(token.end()); match self.peek() {
Some((token.copy(), end)) Ok(Some(token)) => {
let end = self.chomp(token.span.end());
Ok(Some((token.copy(), end)))
}, },
Ok(None) => Ok(None),
Err(e) => Err(e)
} }
} }
} }
pub const fn peek <'s> (mut value: CstVal<'s>, source: &'s str) -> CstToken<'s> { #[derive(Debug, Copy, Clone, Default, PartialEq)]
pub struct Span<D: Dsl> {
/// 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<D> {
pub const fn end (&self) -> usize { self.start.saturating_add(self.length) }
}
impl<'s, D: Dsl<Str=&'s str>> Span<D> {
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) -> DslUsually<&mut Self> {
if self.length + self.start >= self.source.len() { return Err(End) }
self.length = self.length.saturating_add(1);
Ok(self) }
}
/// Parsed substring with range and value.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Token<D: Dsl> {
/// Source span of token.
span: Span<D>,
/// Meaning of token.
value: Val<D>,
}
impl<D: Dsl> Token<D> {
pub const fn value (&self) -> &Val<D> { &self.value }
pub const fn span (&self) -> &Span<D> { &self.span }
pub const fn err (&self) -> Option<DslError> {
if let Val::Error(e) = self.value { Some(e) } else { None } }
pub const fn new (source: D::Str, start: usize, length: usize, value: Val<D>) -> Self {
Self { value, span: Span { source, start, length } } }
pub const fn copy (&self) -> Self where D::Str: Copy, D::Exp: Copy {
Self { span: Span { ..self.span }, value: self.value } }
}
const fn or_panic <T> (result: DslUsually<T>) -> T {
match result { Ok(t) => t, Err(e) => const_panic::concat_panic!(e) }
}
impl<'s, D: Dsl<Str = &'s str>> Token<D> {
pub const fn peek (src: D::Str) -> DslPerhaps<Self> where D::Exp: From<&'s str> {
use Val::*; use Val::*;
let mut start = 0; let mut t = Self::new(src, 0, 0, Nil);
let mut length = 0; let mut iter = char_indices(src);
let mut source = source; while let Some(((i, c), next)) = iter.next() {
loop { t = match (t.value(), c) {
if let Some(((i, c), next)) = char_indices(source).next() { (Error(_), _) =>
if matches!(value, Error(_)) { return Ok(Some(t)),
break (Nil, ' '|'\n'|'\r'|'\t') =>
} else if matches!(value, Nil) { *or_panic(t.grow()),
if is_whitespace(c) { (Nil, '(') =>
length += 1; Self::new(src, i, 1, Exp(1, D::Exp::from(str_range(src, i, i + 1)))),
continue (Nil, '"') =>
} Self::new(src, i, 1, Str(str_range(src, i, i + 1))),
start = i; (Nil, ':'|'@') =>
length = 1; Self::new(src, i, 1, Sym(str_range(src, i, i + 1))),
if is_exp_start(c) { (Nil, '/'|'a'..='z') =>
value = Exp(1, Cst(CstConstIter(str_range(source, i, i+1)))); Self::new(src, i, 1, Key(str_range(src, i, i + 1))),
} else if is_str_start(c) { (Nil, '0'..='9') =>
value = Str(str_range(source, i, i+1)); Self::new(src, i, 1, match to_digit(c) { Ok(c) => Num(c), Err(e) => Error(e) }),
} else if is_sym_start(c) { (Nil, _) =>
value = Sym(str_range(source, i, i+1)); { t.value = Val::Error(Unexpected(c)); t },
} else if is_key_start(c) { (Str(_), '"') =>
value = Key(str_range(source, i, i+1)); return Ok(Some(t)),
} else if is_digit(c) { (Str(_), _) =>
value = match to_digit(c) { Ok(c) => Num(c), Err(e) => Error(e) }; { or_panic(t.grow()); t.value = Str(t.span.slice()); t },
} else { (Num(m), ' '|'\n'|'\r'|'\t'|')') =>
value = Error(Unexpected(c)); return Ok(Some(t)),
break (Num(m), _) => match to_digit(c) {
} Ok(n) => { t.grow()?; t.value = Num(10*m+n); t },
} else if matches!(value, Str(_)) { Err(e) => { t.grow()?; t.value = Error(e); t } },
if is_str_end(c) { (Sym(_), ' '|'\n'|'\r'|'\t'|')') =>
break return Ok(Some(t)),
} else { (Sym(_), 'a'..='z'|'A'..='Z'|'0'..='9'|'-') => {
value = Str(str_range(source, start, start + length + 1)); t.grow()?; t.value = Sym(t.span.slice()); t },
} (Sym(_), _) =>
} else if matches!(value, Sym(_)) { { t.value = Error(Unexpected(c)); t },
if is_sym_end(c) { (Key(_), ' '|'\n'|'\r'|'\t'|')') =>
break return Ok(Some(t)),
} else if is_sym_char(c) { (Key(_), 'a'..='z'|'0'..='9'|'-'|'/') =>
value = Sym(str_range(source, start, start + length + 1)); { t.grow()?; t.value = Key(t.span.slice()); t },
} else { (Key(_), _ ) =>
value = Error(Unexpected(c)); { t.value = Error(Unexpected(c)); t },
} (Exp(0, _), _) =>
} else if matches!(value, Key(_)) { { t.grow()?; t.value = Exp(0, D::Exp::from(t.span.slice_exp())); return Ok(Some(t)) },
if is_key_end(c) { (Exp(d, _), ')') =>
break { t.grow()?; t.value = Exp(d-1, D::Exp::from(t.span.slice_exp())); t },
} (Exp(d, _), '(') =>
length += 1; { t.grow()?; t.value = Exp(d+1, D::Exp::from(t.span.slice_exp())); t },
if is_key_char(c) { (Exp(d, _), _ ) =>
value = Key(str_range(source, start, start + length + 1)); { t.grow()?; t.value = Exp(*d, D::Exp::from(t.span.slice_exp())); t },
} 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'|')') }
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 to_number <D: Dsl> (digits: &str) -> Result<usize, DslError> {
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; iter = next;
} }
Ok(value) Ok(match t.value() {
Nil => None,
_ => Some(t)
})
} }
pub const fn to_digit (c: char) -> Result<usize, DslError> { pub const fn grow (&mut self) -> DslUsually<&mut Self> { self.span.grow()?; Ok(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
} else {
unreachable!()
}
}
}
pub const fn to_digit (c: char) -> DslResult<usize> {
Ok(match c { Ok(match c {
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
_ => return Err(Unexpected(c)) _ => return Result::Err(Unexpected(c)) }) }
})
} macro_rules! iterate(($expr:expr => $arg: pat => $body:expr) => {
/// `State` + [Dsl] -> `Self`. let mut iter = $expr; while let Some(($arg, next)) = iter.next() { $body; iter = next; } });
pub trait FromDsl<State>: Sized { pub const fn to_number (digits: &str) -> DslResult<usize> {
fn try_from_dsl (state: &State, dsl: &impl Dsl) -> Perhaps<Self>; let mut value = 0;
fn from_dsl (state: &State, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<Self> { iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
match Self::try_from_dsl(state, dsl)? { Some(dsl) => Ok(dsl), _ => Err(err()) } } Ok(digit) => value = 10 * value + digit,
} Result::Err(e) => return Result::Err(e) });
/// `self` + `Options` -> [Dsl] Ok(value) }
pub trait IntoDsl { /*TODO*/ }
/// `self` + [Dsl] -> `Item`
pub trait DslInto<Item> {
fn try_dsl_into (&self, dsl: &impl Dsl) -> Perhaps<Item>;
fn dsl_into (&self, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<Item> {
match Self::try_dsl_into(self, dsl)? { Some(dsl) => Ok(dsl), _ => Err(err()) } }
}
/// `self` + `Item` -> [Dsl]
pub trait DslFrom { /*TODO*/ }
/// Implement type conversions. /// Implement type conversions.
macro_rules! from(($($Struct:ty { $( macro_rules! from(($($Struct:ty { $(

View file

@ -4,24 +4,24 @@ use crate::*;
/// Each contained layer defines a mapping from input event to command invocation /// Each contained layer defines a mapping from input event to command invocation
/// over a given state. Furthermore, each layer may have an associated cond, /// 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. /// so that only certain layers are active at a given time depending on state.
#[derive(Debug)] pub struct InputMap<I, D: Dsl>( #[derive(Debug)] pub struct InputMap<I, T: Dsl>(
/// Map of input event (key combination) to /// Map of input event (key combination) to
/// all command expressions bound to it by /// all command expressions bound to it by
/// all loaded input layers. /// all loaded input layers.
pub BTreeMap<I, Vec<InputBinding<D>>> pub BTreeMap<I, Vec<InputBinding<T>>>
); );
impl<I, D: Dsl> Default for InputMap<I, D> { impl<I, T: Dsl> Default for InputMap<I, T> {
fn default () -> Self { fn default () -> Self {
Self(Default::default()) Self(Default::default())
} }
} }
#[derive(Debug, Default)] pub struct InputBinding<D: Dsl> { #[derive(Debug, Default)] pub struct InputBinding<T: Dsl> {
condition: Option<D>, condition: Option<T>,
command: D, command: T,
description: Option<Arc<str>>, description: Option<Arc<str>>,
source: Option<Arc<PathBuf>>, source: Option<Arc<PathBuf>>,
} }
impl<'s, I: Debug + Ord, D: Dsl + From<Cst<'s>>> InputMap<I, D> { impl<I: Debug + Ord, T: Dsl> InputMap<I, T> {
/// Create input layer collection from path to text file. /// Create input layer collection from path to text file.
pub fn from_path <P: Debug + AsRef<Path>> (path: P) -> Usually<Self> { pub fn from_path <P: Debug + AsRef<Path>> (path: P) -> Usually<Self> {
if !exists(path.as_ref())? { if !exists(path.as_ref())? {
@ -30,34 +30,40 @@ impl<'s, I: Debug + Ord, D: Dsl + From<Cst<'s>>> InputMap<I, D> {
Self::from_source(read_and_leak(path)?) Self::from_source(read_and_leak(path)?)
} }
/// Create input layer collection from string. /// Create input layer collection from string.
pub fn from_source (source: &'s str) -> Usually<Self> { pub fn from_source <S: AsRef<str>> (source: S) -> Usually<Self> {
Self::from_dsl(D::from(Cst(CstConstIter(source)))) Self::from_dsl(CstIter::from(source.as_ref()))
} }
/// Create input layer collection from DSL. /// Create input layer collection from DSL.
pub fn from_dsl (dsl: D) -> Usually<Self> { pub fn from_dsl <D: Dsl> (dsl: D) -> Usually<Self> {
use Val::*; use DslVal::*;
let mut input_map: BTreeMap<I, Vec<InputBinding<D>>> = Default::default(); let mut input_map: BTreeMap<I, Vec<InputBinding<T>>> = Default::default();
match dsl.exp_head() { let mut index = 0;
Str(path) => { while let Some(Exp(_, mut exp)) = dsl.nth(index) {
let val = exp.nth(0).map(|x|x.val());
match val {
Some(Str(path)) => {
let path = PathBuf::from(path.as_ref()); let path = PathBuf::from(path.as_ref());
for (key, val) in InputMap::<I, D>::from_path(&path)?.0.into_iter() { let module = InputMap::<I, T>::from_path(&path)?;
todo!("import {path:?} {key:?} {val:?}"); for (key, val) in module.0.into_iter() {
todo!("import {exp:?} {key:?} {val:?} {path:?}");
if !input_map.contains_key(&key) { if !input_map.contains_key(&key) {
input_map.insert(key, vec![]); input_map.insert(key, vec![]);
} }
} }
}, },
Sym(sym) => { Some(Sym(sym)) => {
//let key: I = sym.into(); //let key: I = sym.into();
//if !input_map.contains_key(&key) { //if !input_map.contains_key(&key) {
//input_map.insert(key, vec![]); //input_map.insert(key, vec![]);
//} //}
todo!("binding {sym:?} {:?}", dsl.exp_tail()); todo!("binding {exp:?} {sym:?}");
}, },
Key(s) if s.as_ref() == "if" => { Some(Key(key)) if key.as_ref() == "if" => {
todo!("conditional binding {:?}", dsl.exp_tail()); todo!("conditional binding {exp:?} {key:?}");
}, },
_ => return Err(format!("invalid form in keymap: {dsl:?}").into()) _ => return Result::Err(format!("invalid token in keymap: {val:?}").into()),
}
index += 1;
} }
Ok(Self(input_map)) Ok(Self(input_map))
} }
@ -86,23 +92,23 @@ impl<'s, I: Debug + Ord, D: Dsl + From<Cst<'s>>> InputMap<I, D> {
/* /*
/// Create an input map with a single non-condal layer. /// Create an input map with a single non-condal layer.
/// (Use [Default::default] to get an empty map.) /// (Use [Default::default] to get an empty map.)
pub fn new (layer: Val<T::Str, T::Exp>) -> Self { pub fn new (layer: DslVal<T::Str, T::Exp>) -> Self {
Self::default().layer(layer) Self::default().layer(layer)
} }
/// Add layer, return `Self`. /// Add layer, return `Self`.
pub fn layer (mut self, layer: Val<T::Str, T::Exp>) -> Self { pub fn layer (mut self, layer: DslVal<T::Str, T::Exp>) -> Self {
self.add_layer(layer); self self.add_layer(layer); self
} }
/// Add condal layer, return `Self`. /// Add condal layer, return `Self`.
pub fn layer_if (mut self, cond: Val<T::Str, T::Exp>, layer: Val<T::Str, T::Exp>) -> Self { pub fn layer_if (mut self, cond: DslVal<T::Str, T::Exp>, layer: DslVal<T::Str, T::Exp>) -> Self {
self.add_layer_if(Some(cond), layer); self self.add_layer_if(Some(cond), layer); self
} }
/// Add layer, return `&mut Self`. /// Add layer, return `&mut Self`.
pub fn add_layer (&mut self, layer: Val<T::Str, T::Exp>) -> &mut Self { pub fn add_layer (&mut self, layer: DslVal<T::Str, T::Exp>) -> &mut Self {
self.add_layer_if(None, layer.into()); self self.add_layer_if(None, layer.into()); self
} }
/// Add condal layer, return `&mut Self`. /// Add condal layer, return `&mut Self`.
pub fn add_layer_if (&mut self, cond: Option<Val<T::Str, T::Exp>>, bind: Val<T::Str, T::Exp>) -> &mut Self { pub fn add_layer_if (&mut self, cond: Option<DslVal<T::Str, T::Exp>>, bind: DslVal<T::Str, T::Exp>) -> &mut Self {
self.0.push(InputLayer { cond, bind }); self.0.push(InputLayer { cond, bind });
self self
} }
@ -176,7 +182,7 @@ impl<'s, I: Debug + Ord, D: Dsl + From<Cst<'s>>> InputMap<I, D> {
//} //}
//fn from (source: &'s str) -> Self { //fn from (source: &'s str) -> Self {
//// this should be for single layer: //// this should be for single layer:
//use Val::*; //use DslVal::*;
//let mut layers = vec![]; //let mut layers = vec![];
//let mut source = CstIter::from(source); //let mut source = CstIter::from(source);
//while let Some(Exp(_, mut iter)) = source.next().map(|x|x.value) { //while let Some(Exp(_, mut iter)) = source.next().map(|x|x.value) {

View file

@ -392,7 +392,7 @@ transform_xy_unit!("padding/x" "padding/y" "padding/xy"|self: Padding, area|{
//)*) => { //)*) => {
//$( //$(
//impl<S,$($($A),+)?> FromDsl<S> for $Struct$(<$($A),+>)? { //impl<S,$($($A),+)?> FromDsl<S> for $Struct$(<$($A),+>)? {
//fn try_from_dsl ( //fn try_dsl_from (
//state: &S, dsl: &impl Dsl //state: &S, dsl: &impl Dsl
//) -> Perhaps<Self> { //) -> Perhaps<Self> {
//todo!() //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 $op:literal $(/)? [$head: ident, $tail: ident] $expr:expr
) => { ) => {
impl<S,$($($A),+)?> FromDsl<S> for $Struct$(<$($A),+>)? { impl<S,$($($A),+)?> FromDsl<S> for $Struct$(<$($A),+>)? {
fn try_from_dsl ( fn try_dsl_from (
_state: &S, _dsl: &impl Dsl _state: &S, _dsl: &impl Dsl
) -> Perhaps<Self> { ) -> Perhaps<Self> {
todo!() 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 $op:literal $(/)? [$head: ident, $tail: ident] $expr:expr
) => { ) => {
impl<S,$($($A),+)?> FromDsl<S> for $Struct$(<$($A),+>)? { impl<S,$($($A),+)?> FromDsl<S> for $Struct$(<$($A),+>)? {
fn try_from_dsl ( fn try_dsl_from (
_state: &S, _dsl: &impl Dsl _state: &S, _dsl: &impl Dsl
) -> Perhaps<Self> { ) -> Perhaps<Self> {
todo!() todo!()