mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
Compare commits
2 commits
ca4138e365
...
1ef898ac32
| Author | SHA1 | Date | |
|---|---|---|---|
| 1ef898ac32 | |||
| ddd162f225 |
3 changed files with 23 additions and 46 deletions
|
|
@ -57,16 +57,16 @@ pub const fn no_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
|
||||||
if let Ok(Some(num)) = num_peek(src) { num } else
|
if let Ok(Some(num)) = num_peek(src) { num } else
|
||||||
if let Ok(Some(text)) = text_peek(src) { text } else
|
if let Ok(Some(text)) = text_peek(src) { text } else
|
||||||
if let Err(e) = no_trailing_non_space(src, 0, Some("peek")) { return Err(e) }
|
if let Err(e) = no_trailing_non_space(src, 0, Some("peek")) { return Err(e) }
|
||||||
else { return Ok(None) }))
|
else { return Ok(None) }))
|
||||||
}
|
}
|
||||||
pub const fn seek (src: &str) -> DslPerhaps<(usize, usize)> {
|
pub const fn seek (src: &str) -> DslPerhaps<(usize, usize)> {
|
||||||
Ok(Some(if let Ok(Some(expr)) = expr_seek(src) { expr } else
|
Ok(Some(if let Ok(Some(expr)) = expr_seek(src) { expr } else
|
||||||
if let Ok(Some(word)) = word_seek(src) { word } else
|
if let Ok(Some(word)) = word_seek(src) { word } else
|
||||||
if let Ok(Some(num)) = num_seek(src) { num } else
|
if let Ok(Some(num)) = num_seek(src) { num } else
|
||||||
if let Ok(Some(text)) = text_seek(src) { text } else
|
if let Ok(Some(text)) = text_seek(src) { text } else
|
||||||
if let Err(e) = no_trailing_non_space(src, 0, Some("seek")) { return Err(e) }
|
if let Err(e) = no_trailing_non_space(src, 0, Some("seek")) { return Err(e) }
|
||||||
|
|
@ -242,29 +242,6 @@ dsl_type!(DslText {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
pub const fn is_key_start (c: char) -> bool { matches!(c, '/'|('a'..='z')) }
|
|
||||||
pub const fn is_key_char (c: char) -> bool { is_key_start(c) || matches!(c, '0'..='9'|'-') }
|
|
||||||
pub const fn is_key_end (c: char) -> bool { !is_key_char(c) }
|
|
||||||
dsl_type!(DslKey {
|
|
||||||
fn key (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(key_peek_only)) }
|
|
||||||
} {
|
|
||||||
pub const fn key_peek [generated];
|
|
||||||
pub const fn key_peek_only [generated];
|
|
||||||
pub const fn key_seek [generated];
|
|
||||||
pub const fn key_seek_start (src) {
|
|
||||||
for_each!((i, c) in char_indices(src) =>
|
|
||||||
if is_key_start(c) { return Ok(Some(i)) } else
|
|
||||||
if !is_space(c) { return Err(Unexpected(c, Some(i), None)) });
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
pub const fn key_seek_length (src) {
|
|
||||||
for_each!((i, c) in char_indices(src) =>
|
|
||||||
if is_key_end(c) { return Ok(Some(i)) } else
|
|
||||||
if !is_key_char(c) { return Err(Unexpected(c, Some(i), None)) });
|
|
||||||
Ok(Some(src.len()))
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
dsl_type!(DslNum {
|
dsl_type!(DslNum {
|
||||||
fn num (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(num_peek_only))}
|
fn num (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(num_peek_only))}
|
||||||
} {
|
} {
|
||||||
|
|
|
||||||
|
|
@ -10,18 +10,18 @@ use crate::*;
|
||||||
// Special form for numeric types
|
// Special form for numeric types
|
||||||
(num |$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => {
|
(num |$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => {
|
||||||
impl<'t> DslNs<'t, $Type> for $State {
|
impl<'t> DslNs<'t, $Type> for $State {
|
||||||
const SYMS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> =
|
const WORDS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> =
|
||||||
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
|
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||||
const EXPS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
|
const EXPRS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
|
||||||
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
|
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||||
fn from <D: Dsl> (&'t self, dsl: D) -> Perhaps<$Type> {
|
fn from <D: Dsl> (&'t self, dsl: D) -> Perhaps<$Type> {
|
||||||
if let Ok(Some(src)) = dsl.src() {
|
if let Ok(Some(src)) = dsl.src() {
|
||||||
if let Ok(Some(num)) = src.num() {
|
if let Ok(num) = to_number(src) {
|
||||||
Ok(Some(to_number(num)? as $Type))
|
Ok(Some(num as $Type))
|
||||||
} else if let Ok(Some(src)) = src.word() {
|
} else if let Ok(Some(src)) = src.word() {
|
||||||
self.from_word(src)
|
self.from_word(src)
|
||||||
} else {
|
} else {
|
||||||
self.from_exp(src)
|
self.from_expr(src)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
|
@ -36,9 +36,9 @@ use crate::*;
|
||||||
// Regular form for single type
|
// Regular form for single type
|
||||||
(|$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => {
|
(|$state:ident : $State: ty| -> $Type:ty { $( $pat:tt => $body:expr ),* $(,)? }) => {
|
||||||
impl<'t> DslNs<'t, $Type> for $State {
|
impl<'t> DslNs<'t, $Type> for $State {
|
||||||
const SYMS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> =
|
const WORDS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> =
|
||||||
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
|
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||||
const EXPS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
|
const EXPRS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
|
||||||
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
|
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -54,15 +54,16 @@ use crate::*;
|
||||||
}) => { ($head, |$state, tail: &str|{
|
}) => { ($head, |$state, tail: &str|{
|
||||||
$(
|
$(
|
||||||
let head = tail.head()?.unwrap_or_default();
|
let head = tail.head()?.unwrap_or_default();
|
||||||
|
let tail = tail.tail()?.unwrap_or_default();
|
||||||
let $arg: $ty = if let Some(arg) = $state.from(&head)? {
|
let $arg: $ty = if let Some(arg) = $state.from(&head)? {
|
||||||
arg
|
arg
|
||||||
} else {
|
} else {
|
||||||
return Err(format!("{}: missing argument: {}",
|
return Err(format!("{}: missing argument: {} ({}); got: {tail}",
|
||||||
stringify!($head),
|
$head,
|
||||||
stringify!($arg)
|
stringify!($arg),
|
||||||
|
stringify!($Type),
|
||||||
).into())
|
).into())
|
||||||
};
|
};
|
||||||
let tail = tail.tail()?.unwrap_or_default();
|
|
||||||
)*
|
)*
|
||||||
Ok(Some($body))
|
Ok(Some($body))
|
||||||
}) };
|
}) };
|
||||||
|
|
@ -84,7 +85,7 @@ pub trait DslNs<'t, T: 't>: 't {
|
||||||
if let Ok(Some(src)) = src.word() {
|
if let Ok(Some(src)) = src.word() {
|
||||||
self.from_word(src)
|
self.from_word(src)
|
||||||
} else {
|
} else {
|
||||||
self.from_exp(src)
|
self.from_expr(src)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
|
@ -93,26 +94,25 @@ pub trait DslNs<'t, T: 't>: 't {
|
||||||
/// Resolve a symbol if known.
|
/// Resolve a symbol if known.
|
||||||
fn from_word <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
fn from_word <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||||
if let Some(dsl) = dsl.word()? {
|
if let Some(dsl) = dsl.word()? {
|
||||||
for (word, get) in Self::SYMS.0 { if dsl == *word { return get(self) } }
|
for (word, get) in Self::WORDS.0 { if dsl == *word { return get(self) } }
|
||||||
}
|
}
|
||||||
return Ok(None)
|
return Ok(None)
|
||||||
}
|
}
|
||||||
/// Resolve an expression if known.
|
/// Resolve an expression if known.
|
||||||
fn from_exp <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
fn from_expr <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||||
if let Some(head) = dsl.expr().head()? {
|
if let Some(head) = dsl.expr().head()? {
|
||||||
for (key, value) in Self::EXPS.0.iter() {
|
for (key, value) in Self::EXPRS.0.iter() {
|
||||||
if head == *key {
|
if head == *key {
|
||||||
return value(self, dsl.expr().tail()?.unwrap_or(""))
|
return value(self, dsl.expr().tail()?.unwrap_or(""))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic!("{dsl:?}");
|
|
||||||
return Ok(None)
|
return Ok(None)
|
||||||
}
|
}
|
||||||
/// Known symbols.
|
/// Known symbols.
|
||||||
const SYMS: DslNsMap<'t, fn (&'t Self)->Perhaps<T>> = DslNsMap::new(&[]);
|
const WORDS: DslNsMap<'t, fn (&'t Self)->Perhaps<T>> = DslNsMap::new(&[]);
|
||||||
/// Known expressions.
|
/// Known expressions.
|
||||||
const EXPS: DslNsMap<'t, fn (&'t Self, &str)->Perhaps<T>> = DslNsMap::new(&[]);
|
const EXPRS: DslNsMap<'t, fn (&'t Self, &str)->Perhaps<T>> = DslNsMap::new(&[]);
|
||||||
}
|
}
|
||||||
/// Namespace mapping.
|
/// Namespace mapping.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,7 @@ impl ExposeImpl {
|
||||||
/// Generated by [tengri_proc::expose].
|
/// Generated by [tengri_proc::expose].
|
||||||
impl ::tengri::dsl::FromDsl<#state> for #t {
|
impl ::tengri::dsl::FromDsl<#state> for #t {
|
||||||
fn from_dsl (state: &#state, dsl: &impl Dsl) -> Perhaps<Self> {
|
fn from_dsl (state: &#state, dsl: &impl Dsl) -> Perhaps<Self> {
|
||||||
match dsl.key()? {
|
match dsl.word()? {
|
||||||
Some("true") => Ok(Some(true)),
|
Some("true") => Ok(Some(true)),
|
||||||
Some("false") => Ok(Some(false)),
|
Some("false") => Ok(Some(false)),
|
||||||
_ => match dsl.word()? { #(#variants)* _ => Ok(None) }
|
_ => match dsl.word()? { #(#variants)* _ => Ok(None) }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue