mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
This commit is contained in:
parent
1ef898ac32
commit
56737c0f66
3 changed files with 254 additions and 288 deletions
396
dsl/src/dsl.rs
396
dsl/src/dsl.rs
|
|
@ -16,9 +16,17 @@ pub(crate) use ::{
|
||||||
};
|
};
|
||||||
pub(crate) use self::DslError::*;
|
pub(crate) use self::DslError::*;
|
||||||
mod dsl_conv; pub use self::dsl_conv::*;
|
mod dsl_conv; pub use self::dsl_conv::*;
|
||||||
mod dsl_ns; pub use self::dsl_ns::*;
|
|
||||||
mod dsl_src; pub use self::dsl_src::*;
|
|
||||||
#[cfg(test)] mod dsl_test;
|
#[cfg(test)] mod dsl_test;
|
||||||
|
// Trait that designates any string-like as potentially parsable DSL.
|
||||||
|
flex_trait!(Dsl: AsRef<str> + Debug + Send + Sync + Sized {
|
||||||
|
fn src (&self) -> DslPerhaps<&str> {Ok(Some(self.as_ref()))}
|
||||||
|
});
|
||||||
|
impl<D: Dsl> Dsl for Option<D> {
|
||||||
|
fn src (&self) -> DslPerhaps<&str> {Ok(if let Some(dsl) = self { dsl.src()? } else { None })}
|
||||||
|
}
|
||||||
|
impl<D: Dsl> Dsl for Result<D, DslError> {
|
||||||
|
fn src (&self) -> DslPerhaps<&str> {match self {Ok(dsl) => Ok(dsl.src()?), Err(e) => Err(*e)}}
|
||||||
|
}
|
||||||
/// DSL-specific result type.
|
/// DSL-specific result type.
|
||||||
pub type DslResult<T> = Result<T, DslError>;
|
pub type DslResult<T> = Result<T, DslError>;
|
||||||
/// DSL-specific optional result type.
|
/// DSL-specific optional result type.
|
||||||
|
|
@ -39,23 +47,6 @@ pub enum DslError {
|
||||||
#[error("end reached")]
|
#[error("end reached")]
|
||||||
End
|
End
|
||||||
}
|
}
|
||||||
fn ok_flat <T> (x: Option<DslPerhaps<T>>) -> DslPerhaps<T> {
|
|
||||||
Ok(x.transpose()?.flatten())
|
|
||||||
}
|
|
||||||
pub const fn is_space (c: char) -> bool {
|
|
||||||
matches!(c, ' '|'\n'|'\r'|'\t')
|
|
||||||
}
|
|
||||||
pub const fn no_trailing_non_space (
|
|
||||||
src: &str, offset: usize, context: Option<&'static str>
|
|
||||||
) -> DslResult<()> {
|
|
||||||
Ok(for_each!((i, c) in char_indices(str_range(src, offset, src.len())) => if !is_space(c) {
|
|
||||||
return Err(Unexpected(c, Some(offset + i), if let Some(context) = context {
|
|
||||||
Some(context)
|
|
||||||
} else {
|
|
||||||
Some("trailing non-space")
|
|
||||||
}))
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
pub const fn peek (src: &str) -> DslPerhaps<&str> {
|
pub const fn peek (src: &str) -> DslPerhaps<&str> {
|
||||||
Ok(Some(if let Ok(Some(expr)) = expr_peek(src) { expr } 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(word)) = word_peek(src) { word } else
|
||||||
|
|
@ -83,7 +74,74 @@ pub const fn peek_tail (src: &str) -> DslPerhaps<&str> {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub const fn expr_peek_inner (src: &str) -> DslPerhaps<&str> {
|
||||||
|
match expr_peek(src) {
|
||||||
|
Ok(Some(peeked)) => {
|
||||||
|
let len = peeked.len();
|
||||||
|
let start = if len > 0 { 1 } else { 0 };
|
||||||
|
Ok(Some(str_range(src, start, start + len.saturating_sub(2))))
|
||||||
|
},
|
||||||
|
e => e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub const fn expr_peek_inner_only (src: &str) -> DslPerhaps<&str> {
|
||||||
|
match expr_seek(src) {
|
||||||
|
Err(e) => Err(e),
|
||||||
|
Ok(None) => Ok(None),
|
||||||
|
Ok(Some((start, length))) => {
|
||||||
|
if let Err(e) = no_trailing_non_space(src, start + length, Some("expr_peek_inner_only")) {
|
||||||
|
Err(e)
|
||||||
|
} else {
|
||||||
|
let peeked = str_range(src, start, start + length);
|
||||||
|
let len = peeked.len();
|
||||||
|
let start = if len > 0 { 1 } else { 0 };
|
||||||
|
Ok(Some(str_range(peeked, start, start + len.saturating_sub(2))))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub const fn is_space (c: char) -> bool {
|
||||||
|
matches!(c, ' '|'\n'|'\r'|'\t')
|
||||||
|
}
|
||||||
|
pub const fn no_trailing_non_space (
|
||||||
|
src: &str, offset: usize, context: Option<&'static str>
|
||||||
|
) -> DslResult<()> {
|
||||||
|
Ok(for_each!((i, c) in char_indices(str_range(src, offset, src.len())) => if !is_space(c) {
|
||||||
|
return Err(Unexpected(c, Some(offset + i), if let Some(context) = context {
|
||||||
|
Some(context)
|
||||||
|
} else {
|
||||||
|
Some("trailing non-space")
|
||||||
|
}))
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
pub const fn is_word_char (c: char) -> bool {
|
||||||
|
matches!(c, 'a'..='z'|'A'..='Z'|'0'..='9'|'-'|'/'|'@'|':')
|
||||||
|
}
|
||||||
|
pub const fn is_word_end (c: char) -> bool { is_space(c) || is_expr_end(c) }
|
||||||
|
pub const fn is_text_start (c: char) -> bool { matches!(c, '"') }
|
||||||
|
pub const fn is_text_end (c: char) -> bool { matches!(c, '"') }
|
||||||
|
pub const fn is_expr_start (c: char) -> bool { c == '(' }
|
||||||
|
pub const fn is_expr_end (c: char) -> bool { c == ')' }
|
||||||
|
pub const fn is_digit (c: char) -> bool { matches!(c, '0'..='9') }
|
||||||
|
pub const fn to_number (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;
|
||||||
|
}
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
pub const fn to_digit (c: char) -> Result<usize, DslError> {
|
||||||
|
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, None, Some("parse digit")))
|
||||||
|
})
|
||||||
|
}
|
||||||
#[macro_export] macro_rules! dsl_type (($T:ident { $($trait:tt)* } {
|
#[macro_export] macro_rules! dsl_type (($T:ident { $($trait:tt)* } {
|
||||||
pub const fn $peek:ident $($_1:tt)?;
|
pub const fn $peek:ident $($_1:tt)?;
|
||||||
pub const fn $peek_only:ident $($_2:tt)?;
|
pub const fn $peek_only:ident $($_2:tt)?;
|
||||||
|
|
@ -93,6 +151,20 @@ pub const fn peek_tail (src: &str) -> DslPerhaps<&str> {
|
||||||
})=>{
|
})=>{
|
||||||
pub trait $T: Dsl { $($trait)* }
|
pub trait $T: Dsl { $($trait)* }
|
||||||
impl<D: Dsl> $T for D {}
|
impl<D: Dsl> $T for D {}
|
||||||
|
pub const fn $seek_start ($source1: &str) -> DslPerhaps<usize> $body1
|
||||||
|
pub const fn $seek_length ($source2: &str) -> DslPerhaps<usize> $body2
|
||||||
|
/// Find a start and length corresponding to a syntax token.
|
||||||
|
pub const fn $seek (source: &str) -> DslPerhaps<(usize, usize)> {
|
||||||
|
match $seek_start(source) {
|
||||||
|
Err(e) => Err(e),
|
||||||
|
Ok(None) => Ok(None),
|
||||||
|
Ok(Some(start)) => match $seek_length(str_from(source, start)) {
|
||||||
|
Ok(Some(length)) => Ok(Some((start, length))),
|
||||||
|
Ok(None) => Ok(None),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
/// Find a slice corrensponding to a syntax token.
|
/// Find a slice corrensponding to a syntax token.
|
||||||
pub const fn $peek (source: &str) -> DslPerhaps<&str> {
|
pub const fn $peek (source: &str) -> DslPerhaps<&str> {
|
||||||
match $seek(source) {
|
match $seek(source) {
|
||||||
|
|
@ -109,29 +181,53 @@ pub const fn peek_tail (src: &str) -> DslPerhaps<&str> {
|
||||||
Err(e) => Err(e),
|
Err(e) => Err(e),
|
||||||
Ok(None) => Ok(None),
|
Ok(None) => Ok(None),
|
||||||
Ok(Some((start, length))) => {
|
Ok(Some((start, length))) => {
|
||||||
if let Err(e) = no_trailing_non_space(source, start + length, Some("peek_only")) { return Err(e) }
|
if let Err(e) = no_trailing_non_space(source, start + length, Some("peek_only")) {
|
||||||
|
Err(e)
|
||||||
|
} else {
|
||||||
Ok(Some(str_range(source, start, start + length)))
|
Ok(Some(str_range(source, start, start + length)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Find a start and length corresponding to a syntax token.
|
|
||||||
pub const fn $seek (source: &str) -> DslPerhaps<(usize, usize)> {
|
|
||||||
match $seek_start(source) {
|
|
||||||
Err(e) => Err(e),
|
|
||||||
Ok(None) => Ok(None),
|
|
||||||
Ok(Some(start)) => match $seek_length(str_from(source, start)) {
|
|
||||||
Ok(Some(length)) => Ok(Some((start, length))),
|
|
||||||
Ok(None) => Ok(None),
|
|
||||||
Err(e) => Err(e),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
pub const fn $seek_start ($source1: &str) -> DslPerhaps<usize> $body1
|
|
||||||
pub const fn $seek_length ($source2: &str) -> DslPerhaps<usize> $body2
|
|
||||||
});
|
});
|
||||||
|
|
||||||
pub const fn is_expr_start (c: char) -> bool { c == '(' }
|
dsl_type!(DslWord {
|
||||||
pub const fn is_expr_end (c: char) -> bool { c == ')' }
|
fn word (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(word_peek_only))}
|
||||||
|
} {
|
||||||
|
pub const fn word_peek [generated];
|
||||||
|
pub const fn word_peek_only [generated];
|
||||||
|
pub const fn word_seek [generated];
|
||||||
|
pub const fn word_seek_start (src) {
|
||||||
|
for_each!((i, c) in char_indices(src) => if
|
||||||
|
is_word_char(c) { return Ok(Some(i)) } else
|
||||||
|
if !is_space(c) { return Err(Unexpected(c, Some(i), Some("word_seek_start"))) });
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
pub const fn word_seek_length (src) {
|
||||||
|
for_each!((i, c) in char_indices(src) => if !is_word_char(c) { return Ok(Some(i)) });
|
||||||
|
Ok(Some(src.len()))
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
dsl_type!(DslText {
|
||||||
|
fn text (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(text_peek_only)) }
|
||||||
|
} {
|
||||||
|
pub const fn text_peek [generated];
|
||||||
|
pub const fn text_peek_only [generated];
|
||||||
|
pub const fn text_seek [generated];
|
||||||
|
pub const fn text_seek_start (src) {
|
||||||
|
for_each!((i, c) in char_indices(src) =>
|
||||||
|
if is_text_start(c) { return Ok(Some(i)) } else
|
||||||
|
if !is_space(c) { return Err(Unexpected(c, Some(i), None)) });
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
pub const fn text_seek_length (src) {
|
||||||
|
for_each!((i, c) in char_indices(src) =>
|
||||||
|
if is_text_end(c) { return Ok(Some(i)) });
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
dsl_type!(DslExpr {
|
dsl_type!(DslExpr {
|
||||||
fn expr (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(expr_peek_inner_only))}
|
fn expr (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(expr_peek_inner_only))}
|
||||||
fn head (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek))}
|
fn head (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek))}
|
||||||
|
|
@ -172,113 +268,129 @@ dsl_type!(DslExpr {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
pub const fn expr_peek_inner (src: &str) -> DslPerhaps<&str> {
|
fn ok_flat <T> (x: Option<DslPerhaps<T>>) -> DslPerhaps<T> {
|
||||||
match expr_peek(src) {
|
Ok(x.transpose()?.flatten())
|
||||||
Ok(Some(peeked)) => {
|
}
|
||||||
let len = peeked.len();
|
|
||||||
let start = if len > 0 { 1 } else { 0 };
|
/// Define a DSL namespace that provides values to words and expressions.
|
||||||
Ok(Some(str_range(src, start, start + len.saturating_sub(2))))
|
#[macro_export] macro_rules! dsl_ns (
|
||||||
},
|
// Special form for numeric types
|
||||||
e => e
|
(num |$state:ident : $State: ty| $($($num:lifetime)? $Type:ty $(=> { $(
|
||||||
|
$pat:tt => $body:expr
|
||||||
|
),* $(,)? })?;)+) => {
|
||||||
|
$(dsl_ns!(num |$state: $State| -> $($num)? $Type { $( $($pat => $body),* )? });)+
|
||||||
|
};
|
||||||
|
// 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>> =
|
||||||
|
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||||
|
const EXPRS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
|
||||||
|
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||||
|
fn from <D: Dsl> (&'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))
|
||||||
|
} else if let Ok(Some(src)) = src.word() {
|
||||||
|
self.from_word(src)
|
||||||
|
} else {
|
||||||
|
self.from_expr(src)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// A namespace may resolve one or more types.
|
||||||
|
(|$state:ident : $State: ty| $($Type:ty $(=> { $( $pat:tt => $body:expr ),* $(,)? })? ;)+) => {
|
||||||
|
$(dsl_ns!(|$state: $State| -> $Type { $( $($pat => $body),* )? });)+
|
||||||
|
};
|
||||||
|
// 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>> =
|
||||||
|
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||||
|
const EXPRS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
|
||||||
|
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Symbols only.
|
||||||
|
(@word ($state:ident: $State:ty) -> $Type:ty {
|
||||||
|
$word:literal => $body:expr
|
||||||
|
}) => {
|
||||||
|
($word, |$state|Ok(Some($body)))
|
||||||
|
};
|
||||||
|
// Expression handlers only.
|
||||||
|
(@exp ($state:ident: $State:ty) -> $Type:ty {
|
||||||
|
($head:literal $(,$arg:ident:$ty:ty)* $(,)*) => $body:expr
|
||||||
|
}) => { ($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),
|
||||||
|
).into())
|
||||||
|
};
|
||||||
|
)*
|
||||||
|
Ok(Some($body))
|
||||||
|
}) };
|
||||||
|
// Nothing else in symbols.
|
||||||
|
(@word ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $body:expr }) => {
|
||||||
|
("", |_|Ok(None)) // FIXME don't emit at all
|
||||||
|
};
|
||||||
|
// Nothing else in expression handlers.
|
||||||
|
(@exp ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $body:expr }) => {
|
||||||
|
("", |_, _|Ok(None)) // FIXME don't emit at all
|
||||||
|
};
|
||||||
|
);
|
||||||
|
|
||||||
|
pub trait DslNs<'t, T: 't>: 't {
|
||||||
|
/// Known symbols.
|
||||||
|
const WORDS: DslNsMap<'t, fn (&'t Self)->Perhaps<T>> = DslNsMap::new(&[]);
|
||||||
|
/// Known expressions.
|
||||||
|
const EXPRS: DslNsMap<'t, fn (&'t Self, &str)->Perhaps<T>> = DslNsMap::new(&[]);
|
||||||
|
/// Resolve an expression or symbol.
|
||||||
|
fn from <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||||
|
if let Ok(Some(src)) = dsl.src() {
|
||||||
|
if let Ok(Some(src)) = src.word() {
|
||||||
|
self.from_word(src)
|
||||||
|
} else {
|
||||||
|
self.from_expr(src)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Resolve a symbol if known.
|
||||||
|
fn from_word <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||||
|
if let Some(dsl) = dsl.word()? {
|
||||||
|
for (word, get) in Self::WORDS.0 { if dsl == *word { return get(self) } }
|
||||||
|
}
|
||||||
|
return Ok(None)
|
||||||
|
}
|
||||||
|
/// Resolve an expression if known.
|
||||||
|
fn from_expr <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||||
|
if let Some(head) = dsl.expr().head()? {
|
||||||
|
for (key, value) in Self::EXPRS.0.iter() {
|
||||||
|
if head == *key {
|
||||||
|
return value(self, dsl.expr().tail()?.unwrap_or(""))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn expr_peek_inner_only (src: &str) -> DslPerhaps<&str> {
|
/// Namespace mapping.
|
||||||
match expr_seek(src) {
|
#[derive(Debug)]
|
||||||
Err(e) => Err(e),
|
pub struct DslNsMap<'t, T: Debug + 't>(pub &'t [(&'t str, T)]);
|
||||||
Ok(None) => Ok(None),
|
impl<'t, T: Debug + 't> DslNsMap<'t, T> {
|
||||||
Ok(Some((start, length))) => {
|
/// Populate a namespace with pre-existing values.
|
||||||
if let Err(e) = no_trailing_non_space(src, start + length, Some("expr_peek_inner_only")) { return Err(e) }
|
pub const fn new (data: &'t [(&'t str, T)]) -> Self { Self(data) /* TODO a search trie */ }
|
||||||
let peeked = str_range(src, start, start + length);
|
|
||||||
let len = peeked.len();
|
|
||||||
let start = if len > 0 { 1 } else { 0 };
|
|
||||||
Ok(Some(str_range(peeked, start, start + len.saturating_sub(2))))
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const fn is_word_char (c: char) -> bool {
|
|
||||||
matches!(c, 'a'..='z'|'A'..='Z'|'0'..='9'|'-'|'/'|'@'|':')
|
|
||||||
}
|
|
||||||
pub const fn is_word_end (c: char) -> bool {
|
|
||||||
is_space(c) || is_expr_end(c)
|
|
||||||
}
|
|
||||||
dsl_type!(DslWord {
|
|
||||||
fn word (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(word_peek_only))}
|
|
||||||
} {
|
|
||||||
pub const fn word_peek [generated];
|
|
||||||
pub const fn word_peek_only [generated];
|
|
||||||
pub const fn word_seek [generated];
|
|
||||||
pub const fn word_seek_start (src) {
|
|
||||||
for_each!((i, c) in char_indices(src) => if
|
|
||||||
is_word_char(c) { return Ok(Some(i)) } else
|
|
||||||
if !is_space(c) { return Err(Unexpected(c, Some(i), Some("word_seek_start"))) });
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
pub const fn word_seek_length (src) {
|
|
||||||
for_each!((i, c) in char_indices(src) => if !is_word_char(c) { return Ok(Some(i)) });
|
|
||||||
Ok(Some(src.len()))
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
pub const fn is_text_start (c: char) -> bool { matches!(c, '"') }
|
|
||||||
pub const fn is_text_end (c: char) -> bool { matches!(c, '"') }
|
|
||||||
dsl_type!(DslText {
|
|
||||||
fn text (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(text_peek_only)) }
|
|
||||||
} {
|
|
||||||
pub const fn text_peek [generated];
|
|
||||||
pub const fn text_peek_only [generated];
|
|
||||||
pub const fn text_seek [generated];
|
|
||||||
pub const fn text_seek_start (src) {
|
|
||||||
for_each!((i, c) in char_indices(src) =>
|
|
||||||
if is_text_start(c) { return Ok(Some(i)) } else
|
|
||||||
if !is_space(c) { return Err(Unexpected(c, Some(i), None)) });
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
pub const fn text_seek_length (src) {
|
|
||||||
for_each!((i, c) in char_indices(src) =>
|
|
||||||
if is_text_end(c) { return Ok(Some(i)) });
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
dsl_type!(DslNum {
|
|
||||||
fn num (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(num_peek_only))}
|
|
||||||
} {
|
|
||||||
pub const fn num_peek [generated];
|
|
||||||
pub const fn num_peek_only [generated];
|
|
||||||
pub const fn num_seek [generated];
|
|
||||||
pub const fn num_seek_start (src) {
|
|
||||||
for_each!((i, c) in char_indices(src) =>
|
|
||||||
if is_digit(c) { return Ok(Some(i)); } else
|
|
||||||
if !is_space(c) { return Err(Unexpected(c, Some(i), None)) });
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
pub const fn num_seek_length (src) {
|
|
||||||
for_each!((i, c) in char_indices(src) =>
|
|
||||||
if is_num_end(c) { return Ok(Some(i)) } else
|
|
||||||
if !is_digit(c) { return Err(Unexpected(c, Some(i), None)) });
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
pub const fn is_digit (c: char) -> bool { matches!(c, '0'..='9') }
|
|
||||||
pub const fn is_num_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') }
|
|
||||||
pub const fn to_number (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;
|
|
||||||
}
|
|
||||||
Ok(value)
|
|
||||||
}
|
|
||||||
pub const fn to_digit (c: char) -> Result<usize, DslError> {
|
|
||||||
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, None, Some("parse digit")))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,125 +0,0 @@
|
||||||
use crate::*;
|
|
||||||
/// Define a namespace:
|
|
||||||
#[macro_export] macro_rules! dsl_ns (
|
|
||||||
// Special form for numeric types
|
|
||||||
(num |$state:ident : $State: ty| $($($num:lifetime)? $Type:ty $(=> { $(
|
|
||||||
$pat:tt => $body:expr
|
|
||||||
),* $(,)? })?;)+) => {
|
|
||||||
$(dsl_ns!(num |$state: $State| -> $($num)? $Type { $( $($pat => $body),* )? });)+
|
|
||||||
};
|
|
||||||
// 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>> =
|
|
||||||
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
|
|
||||||
const EXPRS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
|
|
||||||
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
|
|
||||||
fn from <D: Dsl> (&'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))
|
|
||||||
} else if let Ok(Some(src)) = src.word() {
|
|
||||||
self.from_word(src)
|
|
||||||
} else {
|
|
||||||
self.from_expr(src)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// A namespace may resolve one or more types.
|
|
||||||
(|$state:ident : $State: ty| $($Type:ty $(=> { $( $pat:tt => $body:expr ),* $(,)? })? ;)+) => {
|
|
||||||
$(dsl_ns!(|$state: $State| -> $Type { $( $($pat => $body),* )? });)+
|
|
||||||
};
|
|
||||||
// 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>> =
|
|
||||||
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
|
|
||||||
const EXPRS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
|
|
||||||
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// Symbols only.
|
|
||||||
(@word ($state:ident: $State:ty) -> $Type:ty {
|
|
||||||
$word:literal => $body:expr
|
|
||||||
}) => {
|
|
||||||
($word, |$state|Ok(Some($body)))
|
|
||||||
};
|
|
||||||
// Expression handlers only.
|
|
||||||
(@exp ($state:ident: $State:ty) -> $Type:ty {
|
|
||||||
($head:literal $(,$arg:ident:$ty:ty)* $(,)*) => $body:expr
|
|
||||||
}) => { ($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),
|
|
||||||
).into())
|
|
||||||
};
|
|
||||||
)*
|
|
||||||
Ok(Some($body))
|
|
||||||
}) };
|
|
||||||
// Nothing else in symbols.
|
|
||||||
(@word ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $body:expr }) => {
|
|
||||||
("", |_|Ok(None)) // FIXME don't emit at all
|
|
||||||
};
|
|
||||||
// Nothing else in expression handlers.
|
|
||||||
(@exp ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $body:expr }) => {
|
|
||||||
("", |_, _|Ok(None)) // FIXME don't emit at all
|
|
||||||
};
|
|
||||||
|
|
||||||
);
|
|
||||||
|
|
||||||
pub trait DslNs<'t, T: 't>: 't {
|
|
||||||
/// Resolve an expression or symbol.
|
|
||||||
fn from <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
|
||||||
if let Ok(Some(src)) = dsl.src() {
|
|
||||||
if let Ok(Some(src)) = src.word() {
|
|
||||||
self.from_word(src)
|
|
||||||
} else {
|
|
||||||
self.from_expr(src)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Resolve a symbol if known.
|
|
||||||
fn from_word <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
|
||||||
if let Some(dsl) = dsl.word()? {
|
|
||||||
for (word, get) in Self::WORDS.0 { if dsl == *word { return get(self) } }
|
|
||||||
}
|
|
||||||
return Ok(None)
|
|
||||||
}
|
|
||||||
/// Resolve an expression if known.
|
|
||||||
fn from_expr <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
|
||||||
if let Some(head) = dsl.expr().head()? {
|
|
||||||
for (key, value) in Self::EXPRS.0.iter() {
|
|
||||||
if head == *key {
|
|
||||||
return value(self, dsl.expr().tail()?.unwrap_or(""))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Ok(None)
|
|
||||||
}
|
|
||||||
/// Known symbols.
|
|
||||||
const WORDS: DslNsMap<'t, fn (&'t Self)->Perhaps<T>> = DslNsMap::new(&[]);
|
|
||||||
/// Known expressions.
|
|
||||||
const EXPRS: DslNsMap<'t, fn (&'t Self, &str)->Perhaps<T>> = DslNsMap::new(&[]);
|
|
||||||
}
|
|
||||||
/// Namespace mapping.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct DslNsMap<'t, T: Debug + 't>(pub &'t [(&'t str, T)]);
|
|
||||||
impl<'t, T: Debug + 't> DslNsMap<'t, T> {
|
|
||||||
/// Populate a namespace.
|
|
||||||
pub const fn new (data: &'t [(&'t str, T)]) -> Self {
|
|
||||||
Self(data) // TODO build search index
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
use crate::*;
|
|
||||||
// Designates a string as parsable DSL.
|
|
||||||
flex_trait!(Dsl: Debug + Send + Sync + Sized {
|
|
||||||
fn src (&self) -> DslPerhaps<&str> { unreachable!("Dsl::src default impl") }
|
|
||||||
});
|
|
||||||
// Some things that can be DSL source:
|
|
||||||
impl Dsl for String {
|
|
||||||
fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) }
|
|
||||||
}
|
|
||||||
impl Dsl for Arc<str> {
|
|
||||||
fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) }
|
|
||||||
}
|
|
||||||
impl<'s> Dsl for &'s str {
|
|
||||||
fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) }
|
|
||||||
}
|
|
||||||
impl<D: Dsl> Dsl for Option<D> {
|
|
||||||
fn src (&self) -> DslPerhaps<&str> {Ok(if let Some(dsl) = self { dsl.src()? } else { None })}
|
|
||||||
}
|
|
||||||
impl<D: Dsl> Dsl for Result<D, DslError> {
|
|
||||||
fn src (&self) -> DslPerhaps<&str> {match self {Ok(dsl) => Ok(dsl.src()?), Err(e) => Err(*e)}}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue