mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
wip: dsl refactor
This commit is contained in:
parent
a1190a24a1
commit
f626860924
5 changed files with 285 additions and 236 deletions
348
dsl/src/dsl.rs
348
dsl/src/dsl.rs
|
|
@ -6,12 +6,18 @@
|
||||||
extern crate const_panic;
|
extern crate const_panic;
|
||||||
use const_panic::PanicFmt;
|
use const_panic::PanicFmt;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
pub(crate) use std::{error::Error, sync::Arc};
|
pub(crate) use ::{
|
||||||
pub(crate) use konst::{iter::for_each, string::{str_from, str_range, char_indices}};
|
std::sync::Arc,
|
||||||
pub(crate) use thiserror::Error;
|
std::error::Error,
|
||||||
pub(crate) use ::tengri_core::*;
|
konst::iter::for_each,
|
||||||
|
konst::string::{str_from, str_range, char_indices},
|
||||||
|
thiserror::Error,
|
||||||
|
tengri_core::*
|
||||||
|
};
|
||||||
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_type;
|
||||||
#[cfg(test)] mod dsl_test;
|
#[cfg(test)] mod dsl_test;
|
||||||
/// DSL-specific result type.
|
/// DSL-specific result type.
|
||||||
pub type DslResult<T> = Result<T, DslError>;
|
pub type DslResult<T> = Result<T, DslError>;
|
||||||
|
|
@ -21,42 +27,41 @@ pub type DslPerhaps<T> = Result<Option<T>, DslError>;
|
||||||
impl Dsl for String { fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) } }
|
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 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<'s> Dsl for &'s str { fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) } }
|
||||||
|
|
||||||
// Designates a string as parsable DSL.
|
// Designates a string as parsable DSL.
|
||||||
flex_trait!(Dsl: Debug + Send + Sync + Sized {
|
flex_trait!(Dsl: Debug + Send + Sync + Sized { fn src (&self) -> DslPerhaps<&str> { unreachable!("Dsl::src default impl") } });
|
||||||
fn src (&self) -> DslPerhaps<&str> { unreachable!("Dsl::src default impl") }
|
|
||||||
});
|
|
||||||
impl<D: Dsl> Dsl for Option<D> {
|
impl<D: Dsl> Dsl for Option<D> {
|
||||||
fn src (&self) -> DslPerhaps<&str> {Ok(if let Some(dsl) = self { dsl.src()? } else { None })}
|
fn src (&self) -> DslPerhaps<&str> {Ok(if let Some(dsl) = self { dsl.src()? } else { None })}
|
||||||
}
|
}
|
||||||
impl<D: Dsl> Dsl for Result<D, DslError> {
|
impl<D: Dsl> Dsl for Result<D, DslError> {
|
||||||
fn src (&self) -> DslPerhaps<&str> {match self {Ok(dsl) => Ok(dsl.src()?), Err(e) => Err(*e)}}
|
fn src (&self) -> DslPerhaps<&str> {match self {Ok(dsl) => Ok(dsl.src()?), Err(e) => Err(*e)}}
|
||||||
}
|
}
|
||||||
impl<D: Dsl> DslText for D {} pub trait DslText: Dsl {
|
|
||||||
fn text (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(text_peek_only))}
|
/// DSL-specific error codes.
|
||||||
|
#[derive(Error, Debug, Copy, Clone, PartialEq, PanicFmt)]
|
||||||
|
pub enum DslError {
|
||||||
|
#[error("parse failed: not implemented")] Unimplemented,
|
||||||
|
#[error("parse failed: empty")] Empty,
|
||||||
|
#[error("parse failed: incomplete")] Incomplete,
|
||||||
|
#[error("parse failed: unexpected character '{0}'")] Unexpected(char, Option<usize>, Option<&'static str>),
|
||||||
|
#[error("parse failed: error #{0}")] Code(u8),
|
||||||
|
#[error("end reached")] End
|
||||||
}
|
}
|
||||||
impl<D: Dsl> DslSym for D {} pub trait DslSym: Dsl {
|
|
||||||
fn sym (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(sym_peek_only))}
|
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')
|
||||||
}
|
}
|
||||||
impl<D: Dsl> DslKey for D {} pub trait DslKey: Dsl {
|
pub const fn no_trailing_non_space (src: &str, offset: usize, context: Option<&'static str>) -> DslResult<()> {
|
||||||
fn key (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(key_peek_only))}
|
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 {
|
||||||
impl<D: Dsl> DslNum for D {} pub trait DslNum: Dsl {
|
Some(context)
|
||||||
fn num (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(num_peek_only))}
|
} else {
|
||||||
}
|
Some("trailing non-space")
|
||||||
impl<D: Dsl> DslExp for D {} pub trait DslExp: Dsl {
|
}))
|
||||||
fn exp (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(exp_peek_inner_only))}
|
}))
|
||||||
fn head (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek))}
|
|
||||||
fn tail (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek_tail))}
|
|
||||||
/// my other car is a cdr :<
|
|
||||||
fn each (&self, mut cb: impl FnMut(&str)->Usually<()>) -> Usually<()> {
|
|
||||||
Ok(if let Some(head) = self.head()? {
|
|
||||||
cb(head)?;
|
|
||||||
if let Some(tail) = self.tail()? {
|
|
||||||
tail.each(cb)?;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn peek (src: &str) -> DslPerhaps<&str> {
|
pub const fn peek (src: &str) -> DslPerhaps<&str> {
|
||||||
Ok(Some(if let Ok(Some(exp)) = exp_peek(src) { exp } else
|
Ok(Some(if let Ok(Some(exp)) = exp_peek(src) { exp } else
|
||||||
if let Ok(Some(sym)) = sym_peek(src) { sym } else
|
if let Ok(Some(sym)) = sym_peek(src) { sym } else
|
||||||
|
|
@ -86,185 +91,160 @@ pub const fn peek_tail (src: &str) -> DslPerhaps<&str> {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// DSL-specific error codes.
|
|
||||||
#[derive(Error, Debug, Copy, Clone, PartialEq, PanicFmt)]
|
pub const fn is_exp_start (c: char) -> bool { c == '(' }
|
||||||
pub enum DslError {
|
pub const fn is_exp_end (c: char) -> bool { c == ')' }
|
||||||
#[error("parse failed: not implemented")] Unimplemented,
|
dsl_type!(DslExp {
|
||||||
#[error("parse failed: empty")] Empty,
|
fn exp (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(exp_peek_inner_only))}
|
||||||
#[error("parse failed: incomplete")] Incomplete,
|
fn head (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek))}
|
||||||
#[error("parse failed: unexpected character '{0}'")] Unexpected(char, Option<usize>, Option<&'static str>),
|
fn tail (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek_tail))}
|
||||||
#[error("parse failed: error #{0}")] Code(u8),
|
/// my other car is a cdr :<
|
||||||
#[error("end reached")] End
|
fn each (&self, mut cb: impl FnMut(&str)->Usually<()>) -> Usually<()> {
|
||||||
}
|
Ok(if let Some(head) = self.head()? {
|
||||||
fn ok_flat <T> (x: Option<DslPerhaps<T>>) -> DslPerhaps<T> { Ok(x.transpose()?.flatten()) }
|
cb(head)?;
|
||||||
pub const fn is_space (c: char) -> bool {
|
if let Some(tail) = self.tail()? {
|
||||||
matches!(c, ' '|'\n'|'\r'|'\t')
|
tail.each(cb)?;
|
||||||
}
|
|
||||||
pub const fn no_trailing_non_space (source: &str, offset: usize, context: Option<&'static str>) -> DslResult<()> {
|
|
||||||
Ok(for_each!((i, c) in char_indices(str_range(source, offset, source.len())) => if !is_space(c) {
|
|
||||||
return Err(Unexpected(c, Some(offset + i), if let Some(context) = context {
|
|
||||||
Some(context)
|
|
||||||
} else {
|
|
||||||
Some("trailing non-space")
|
|
||||||
}))
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
macro_rules! def_peek_seek(($peek:ident, $peek_only:ident, $seek:ident, $seek_start:ident, $seek_length:ident)=>{
|
|
||||||
/// Find a slice corrensponding to a syntax token.
|
|
||||||
pub const fn $peek (source: &str) -> DslPerhaps<&str> {
|
|
||||||
match $seek(source) {
|
|
||||||
Err(e) => Err(e),
|
|
||||||
Ok(None) => Ok(None),
|
|
||||||
Ok(Some((start, length))) => Ok(Some(str_range(source, start, start + length))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// Find a slice corrensponding to a syntax token
|
|
||||||
/// but return an error if it isn't the only thing
|
|
||||||
/// in the source.
|
|
||||||
pub const fn $peek_only (source: &str) -> DslPerhaps<&str> {
|
|
||||||
match $seek(source) {
|
|
||||||
Err(e) => Err(e),
|
|
||||||
Ok(None) => Ok(None),
|
|
||||||
Ok(Some((start, length))) => {
|
|
||||||
if let Err(e) = no_trailing_non_space(source, start + length, Some("peek_only")) { return Err(e) }
|
|
||||||
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)> {
|
pub const fn exp_peek [generated];
|
||||||
match $seek_start(source) {
|
pub const fn exp_peek_only [generated];
|
||||||
Err(e) => Err(e),
|
pub const fn exp_seek [generated];
|
||||||
Ok(None) => Ok(None),
|
pub const fn exp_seek_start (src) {
|
||||||
Ok(Some(start)) => match $seek_length(str_from(source, start)) {
|
for_each!((i, c) in char_indices(src) =>
|
||||||
Ok(Some(length)) => Ok(Some((start, length))),
|
if is_exp_start(c) { return Ok(Some(i)) } else
|
||||||
Ok(None) => Ok(None),
|
if !is_space(c) { return Err(Unexpected(c, Some(i), Some("expected expression start"))) });
|
||||||
Err(e) => Err(e),
|
Ok(None)
|
||||||
},
|
}
|
||||||
}
|
pub const fn exp_seek_length (src) {
|
||||||
|
let mut depth = 0;
|
||||||
|
for_each!((i, c) in char_indices(src) =>
|
||||||
|
if is_exp_start(c) { depth += 1; } else
|
||||||
|
if is_exp_end(c) {
|
||||||
|
if depth == 0 {
|
||||||
|
return Err(Unexpected(c, Some(i), Some("expected expression end")))
|
||||||
|
} else if depth == 1 {
|
||||||
|
return Ok(Some(i + 1))
|
||||||
|
} else {
|
||||||
|
depth -= 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Err(Incomplete)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
def_peek_seek!(exp_peek, exp_peek_only, exp_seek, exp_seek_start, exp_seek_length);
|
|
||||||
pub const fn exp_peek_inner (source: &str) -> DslPerhaps<&str> {
|
pub const fn exp_peek_inner (src: &str) -> DslPerhaps<&str> {
|
||||||
match exp_peek(source) {
|
match exp_peek(src) {
|
||||||
Ok(Some(peeked)) => {
|
Ok(Some(peeked)) => {
|
||||||
let len = peeked.len();
|
let len = peeked.len();
|
||||||
let start = if len > 0 { 1 } else { 0 };
|
let start = if len > 0 { 1 } else { 0 };
|
||||||
Ok(Some(str_range(source, start, start + len.saturating_sub(2))))
|
Ok(Some(str_range(src, start, start + len.saturating_sub(2))))
|
||||||
},
|
},
|
||||||
e => e
|
e => e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub const fn exp_peek_inner_only (source: &str) -> DslPerhaps<&str> {
|
|
||||||
match exp_seek(source) {
|
pub const fn exp_peek_inner_only (src: &str) -> DslPerhaps<&str> {
|
||||||
|
match exp_seek(src) {
|
||||||
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("exp_peek_inner_only")) { return Err(e) }
|
if let Err(e) = no_trailing_non_space(src, start + length, Some("exp_peek_inner_only")) { return Err(e) }
|
||||||
let peeked = str_range(source, start, start + length);
|
let peeked = str_range(src, start, start + length);
|
||||||
let len = peeked.len();
|
let len = peeked.len();
|
||||||
let start = if len > 0 { 1 } else { 0 };
|
let start = if len > 0 { 1 } else { 0 };
|
||||||
Ok(Some(str_range(peeked, start, start + len.saturating_sub(2))))
|
Ok(Some(str_range(peeked, start, start + len.saturating_sub(2))))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub const fn is_exp_start (c: char) -> bool { c == '(' }
|
|
||||||
pub const fn is_exp_end (c: char) -> bool { c == ')' }
|
|
||||||
pub const fn exp_seek_start (source: &str) -> DslPerhaps<usize> {
|
|
||||||
for_each!((i, c) in char_indices(source) => if is_exp_start(c) {
|
|
||||||
return Ok(Some(i))
|
|
||||||
} else if !is_space(c) {
|
|
||||||
return Err(Unexpected(c, Some(i), Some("expected expression start")))
|
|
||||||
});
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
pub const fn exp_seek_length (source: &str) -> DslPerhaps<usize> {
|
|
||||||
let mut depth = 0;
|
|
||||||
for_each!((i, c) in char_indices(source) => if is_exp_start(c) {
|
|
||||||
depth += 1;
|
|
||||||
} else if is_exp_end(c) {
|
|
||||||
if depth == 0 {
|
|
||||||
return Err(Unexpected(c, Some(i), Some("expected expression end")))
|
|
||||||
} else if depth == 1 {
|
|
||||||
return Ok(Some(i + 1))
|
|
||||||
} else {
|
|
||||||
depth -= 1;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Err(Incomplete)
|
|
||||||
}
|
|
||||||
def_peek_seek!(sym_peek, sym_peek_only, sym_seek, sym_seek_start, sym_seek_length);
|
|
||||||
pub const fn is_sym_start (c: char) -> bool { matches!(c, ':'|'@') }
|
pub const fn is_sym_start (c: char) -> bool { matches!(c, ':'|'@') }
|
||||||
pub const fn is_sym_char (c: char) -> bool { is_sym_start(c) || matches!(c, 'a'..='z'|'A'..='Z'|'0'..='9'|'-'|'/') }
|
pub const fn is_sym_char (c: char) -> bool { is_sym_start(c) || matches!(c, 'a'..='z'|'A'..='Z'|'0'..='9'|'-'|'/') }
|
||||||
pub const fn is_sym_end (c: char) -> bool { is_space(c) || matches!(c, ')') }
|
pub const fn is_sym_end (c: char) -> bool { is_space(c) || matches!(c, ')') }
|
||||||
pub const fn sym_seek_start (source: &str) -> DslPerhaps<usize> {
|
dsl_type!(DslSym {
|
||||||
for_each!((i, c) in char_indices(source) => if is_sym_start(c) {
|
fn sym (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(sym_peek_only))}
|
||||||
return Ok(Some(i))
|
} {
|
||||||
} else if !is_space(c) {
|
pub const fn sym_peek [generated];
|
||||||
return Err(Unexpected(c, Some(i), Some("sym_seek_start")))
|
pub const fn sym_peek_only [generated];
|
||||||
});
|
pub const fn sym_seek [generated];
|
||||||
Ok(None)
|
pub const fn sym_seek_start (src) {
|
||||||
}
|
for_each!((i, c) in char_indices(src) => if
|
||||||
pub const fn sym_seek_length (source: &str) -> DslPerhaps<usize> {
|
is_sym_start(c) { return Ok(Some(i)) } else
|
||||||
for_each!((i, c) in char_indices(source) => if is_sym_end(c) {
|
if !is_space(c) { return Err(Unexpected(c, Some(i), Some("sym_seek_start"))) });
|
||||||
return Ok(Some(i))
|
Ok(None)
|
||||||
} else if !is_sym_char(c) {
|
}
|
||||||
return Err(Unexpected(c, Some(i), Some("sym_seek_length")))
|
pub const fn sym_seek_length (src) {
|
||||||
});
|
for_each!((i, c) in char_indices(src) =>
|
||||||
Ok(Some(source.len()))
|
if is_sym_end(c) { return Ok(Some(i)) } else
|
||||||
}
|
if !is_sym_char(c) { return Err(Unexpected(c, Some(i), Some("sym_seek_length"))) });
|
||||||
def_peek_seek!(key_peek, key_peek_only, key_seek, key_seek_start, key_seek_length);
|
Ok(Some(src.len()))
|
||||||
pub const fn is_key_start (c: char) -> bool { matches!(c, '/'|('a'..='z')) }
|
}
|
||||||
pub const fn is_key_char (c: char) -> bool { matches!(c, 'a'..='z'|'0'..='9'|'-'|'/') }
|
});
|
||||||
pub const fn is_key_end (c: char) -> bool { is_space(c) || matches!(c, ')') }
|
|
||||||
pub const fn key_seek_start (source: &str) -> DslPerhaps<usize> {
|
|
||||||
for_each!((i, c) in char_indices(source) => 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 (source: &str) -> DslPerhaps<usize> {
|
|
||||||
for_each!((i, c) in char_indices(source) => if is_key_end(c) {
|
|
||||||
return Ok(Some(i))
|
|
||||||
} else if !is_key_char(c) {
|
|
||||||
return Err(Unexpected(c, Some(i), None))
|
|
||||||
});
|
|
||||||
Ok(Some(source.len()))
|
|
||||||
}
|
|
||||||
def_peek_seek!(text_peek, text_peek_only, text_seek, text_seek_start, text_seek_length);
|
|
||||||
pub const fn is_text_start (c: char) -> bool { matches!(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_text_end (c: char) -> bool { matches!(c, '"') }
|
||||||
pub const fn text_seek_start (source: &str) -> DslPerhaps<usize> {
|
dsl_type!(DslText {
|
||||||
for_each!((i, c) in char_indices(source) => if is_text_start(c) {
|
fn text (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(text_peek_only)) }
|
||||||
return Ok(Some(i))
|
} {
|
||||||
} else if !is_space(c) {
|
pub const fn text_peek [generated];
|
||||||
return Err(Unexpected(c, Some(i), None))
|
pub const fn text_peek_only [generated];
|
||||||
});
|
pub const fn text_seek [generated];
|
||||||
Ok(None)
|
pub const fn text_seek_start (src) {
|
||||||
}
|
for_each!((i, c) in char_indices(src) =>
|
||||||
pub const fn text_seek_length (source: &str) -> DslPerhaps<usize> {
|
if is_text_start(c) { return Ok(Some(i)) } else
|
||||||
for_each!((i, c) in char_indices(source) => if is_text_end(c) { return Ok(Some(i)) });
|
if !is_space(c) { return Err(Unexpected(c, Some(i), None)) });
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
def_peek_seek!(num_peek, num_peek_only, num_seek, num_seek_start, num_seek_length);
|
pub const fn text_seek_length (src) {
|
||||||
pub const fn num_seek_start (source: &str) -> DslPerhaps<usize> {
|
for_each!((i, c) in char_indices(src) =>
|
||||||
for_each!((i, c) in char_indices(source) => if is_digit(c) {
|
if is_text_end(c) { return Ok(Some(i)) });
|
||||||
return Ok(Some(i));
|
Ok(None)
|
||||||
} else if !is_space(c) {
|
}
|
||||||
return Err(Unexpected(c, Some(i), None))
|
});
|
||||||
});
|
|
||||||
Ok(None)
|
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 num_seek_length (source: &str) -> DslPerhaps<usize> {
|
pub const fn is_key_end (c: char) -> bool { !is_key_char(c) }
|
||||||
for_each!((i, c) in char_indices(source) => if is_num_end(c) {
|
dsl_type!(DslKey {
|
||||||
return Ok(Some(i))
|
fn key (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(key_peek_only)) }
|
||||||
} else if !is_digit(c) {
|
} {
|
||||||
return Err(Unexpected(c, Some(i), None))
|
pub const fn key_peek [generated];
|
||||||
});
|
pub const fn key_peek_only [generated];
|
||||||
Ok(None)
|
pub const fn key_seek [generated];
|
||||||
}
|
pub const fn key_seek_start (src) {
|
||||||
|
for_each!((i, c) in char_indices(src) =>
|
||||||
|
if is_key_start(c) { return Ok(Some(i)) } else
|
||||||
|
if !is_space(c) { return Err(Unexpected(c, Some(i), None)) });
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
pub const fn key_seek_length (src) {
|
||||||
|
for_each!((i, c) in char_indices(src) =>
|
||||||
|
if is_key_end(c) { return Ok(Some(i)) } else
|
||||||
|
if !is_key_char(c) { return Err(Unexpected(c, Some(i), None)) });
|
||||||
|
Ok(Some(src.len()))
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
dsl_type!(DslNum {
|
||||||
|
fn num (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(num_peek_only))}
|
||||||
|
} {
|
||||||
|
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_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 is_num_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') }
|
||||||
pub const fn to_number <D: Dsl> (digits: &str) -> Result<usize, DslError> {
|
pub const fn to_number <D: Dsl> (digits: &str) -> Result<usize, DslError> {
|
||||||
|
|
|
||||||
|
|
@ -1,51 +1,5 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
/// Namespace. Currently linearly searched.
|
|
||||||
pub struct DslNs<'t, T: 't>(pub &'t [(&'t str, T)]);
|
|
||||||
|
|
||||||
/// Namespace where keys are symbols.
|
|
||||||
pub trait DslSymNs<'t, T: 't>: 't {
|
|
||||||
const SYMS: DslNs<'t, fn (&'t Self)->T>;
|
|
||||||
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
|
||||||
if let Some(dsl) = dsl.sym()? {
|
|
||||||
for (sym, get) in Self::SYMS.0 {
|
|
||||||
if dsl == *sym {
|
|
||||||
return Ok(Some(get(self)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export] macro_rules! dsl_sym (
|
|
||||||
(|$state:ident:$State:ty| -> $type:ty {$($lit:literal => $exp:expr),* $(,)?})=>{
|
|
||||||
impl<'t> DslSymNs<'t, $type> for $State {
|
|
||||||
const SYMS: DslNs<'t, fn (&'t $State)->$type> =
|
|
||||||
DslNs(&[$(($lit, |$state: &$State|$exp)),*]); } });
|
|
||||||
|
|
||||||
pub trait DslExpNs<'t, T: 't>: 't {
|
|
||||||
const EXPS: DslNs<'t, fn (&'t Self, &str)->T>;
|
|
||||||
fn from_exp <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
|
||||||
if let Some(exp) = dsl.exp()? {
|
|
||||||
for (key, value) in Self::EXPS.0.iter() {
|
|
||||||
if exp.head() == Ok(Some(key)) {
|
|
||||||
return Ok(Some(value(self, exp.tail()?.unwrap_or(""))))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export] macro_rules! dsl_exp (
|
|
||||||
(|$state:ident:$State:ty$(, $tail:ident)?|->$type:ty { $(
|
|
||||||
($key:literal $(/ $sub:ident: $Sub:ty)? $(, $arg:ident $(?)? :$argtype:ty)*) => $body:expr
|
|
||||||
),* $(,)? }) => {
|
|
||||||
impl<'t> DslExpNs<'t, $type> for $State {
|
|
||||||
const EXPS: DslNs<'t, fn (&'t $State, &str)->$type> =
|
|
||||||
DslNs(&[]); } });
|
|
||||||
|
|
||||||
// TODO DEPRECATE:
|
// TODO DEPRECATE:
|
||||||
|
|
||||||
/// `T` + [Dsl] -> `Self`.
|
/// `T` + [Dsl] -> `Self`.
|
||||||
|
|
|
||||||
70
dsl/src/dsl_ns.rs
Normal file
70
dsl/src/dsl_ns.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
use crate::*;
|
||||||
|
|
||||||
|
/// Define a symbol namespace.
|
||||||
|
#[macro_export] macro_rules! dsl_sym_ns (
|
||||||
|
(|$state:ident:$State:ty| -> $type:ty {$($lit:literal => $exp:expr),* $(,)?})=>{
|
||||||
|
impl<'t> DslSymNs<'t, $type> for $State {
|
||||||
|
const SYMS: DslNs<'t, fn (&'t $State)->$type> = DslNs(&[
|
||||||
|
$(($lit, |$state: &$State|$exp)),*
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Define an expression namespace.
|
||||||
|
#[macro_export] macro_rules! dsl_exp_ns (
|
||||||
|
(|$state:ident:$State:ty|->$type:ty { $( (
|
||||||
|
$key:literal
|
||||||
|
$(/ $sub:ident : $Sub:ty)?
|
||||||
|
$(, $arg:ident $(?)? : $argtype:ty)*
|
||||||
|
$(, ..$tail:ident)?
|
||||||
|
) => $body:expr),* $(,)? }) => {
|
||||||
|
impl<'t> DslExpNs<'t, fn (&'t $State, &str)->$type> for $State {
|
||||||
|
const EXPS: DslNs<'t, fn (&'t $State, &str)->$type> = DslNs(&[ $(
|
||||||
|
($key, |$state: &$State, mut tail: &str|{
|
||||||
|
$(let $arg = dsl_next(&mut tail)?;)*
|
||||||
|
$(let $tail = tail;)?
|
||||||
|
$body
|
||||||
|
})
|
||||||
|
),* ]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Namespace where keys are symbols.
|
||||||
|
pub trait DslSymNs<'t, T: 't>: 't {
|
||||||
|
const SYMS: DslNs<'t, fn (&'t Self)->T> = DslNs(&[]);
|
||||||
|
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||||
|
if let Some(dsl) = dsl.sym()? {
|
||||||
|
for (sym, get) in Self::SYMS.0 {
|
||||||
|
if dsl == *sym {
|
||||||
|
return Ok(Some(get(self)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Namespace where the keys are heads of expressions.
|
||||||
|
pub trait DslExpNs<'t, T: 't>: 't {
|
||||||
|
const EXPS: DslNs<'t, fn (&'t Self, &str)->T> = DslNs(&[]);
|
||||||
|
fn from_exp <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||||
|
if let Some(exp) = dsl.exp()? {
|
||||||
|
panic!("exp\n{:?}\n{:?}\n{:?}\n{:?}", exp.src(), exp.head(), exp.head().src(), Self::EXPS.0);
|
||||||
|
for (key, value) in Self::EXPS.0.iter() {
|
||||||
|
println!("{key} {:?}", exp.head());
|
||||||
|
if exp.head() == Ok(Some(key)) {
|
||||||
|
return Ok(Some(value(self, exp.tail()?.unwrap_or(""))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Namespace.
|
||||||
|
///
|
||||||
|
/// Currently linearly searched.
|
||||||
|
/// Maybe it's better to use a trie.
|
||||||
|
/// I don't think there's a `const` trie in Rust?
|
||||||
|
/// Oughtta be.
|
||||||
|
pub struct DslNs<'t, T: 't>(pub &'t [(&'t str, T)]);
|
||||||
51
dsl/src/dsl_type.rs
Normal file
51
dsl/src/dsl_type.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
use crate::*;
|
||||||
|
|
||||||
|
#[macro_export] macro_rules! dsl_type (($T:ident { $($trait:tt)* } {
|
||||||
|
pub const fn $peek:ident $($_1:tt)?;
|
||||||
|
pub const fn $peek_only:ident $($_2:tt)?;
|
||||||
|
pub const fn $seek:ident $($_3:tt)?;
|
||||||
|
pub const fn $seek_start:ident ($source1:ident) $body1:block
|
||||||
|
pub const fn $seek_length:ident ($source2:ident) $body2:block
|
||||||
|
})=>{
|
||||||
|
pub trait $T: Dsl { $($trait)* }
|
||||||
|
impl<D: Dsl> $T for D {}
|
||||||
|
dsl_peek_seek!($peek, $peek_only, $seek, $seek_start, $seek_length);
|
||||||
|
pub const fn $seek_start ($source1: &str) -> DslPerhaps<usize> $body1
|
||||||
|
pub const fn $seek_length ($source2: &str) -> DslPerhaps<usize> $body2
|
||||||
|
});
|
||||||
|
|
||||||
|
#[macro_export] macro_rules! dsl_peek_seek(($peek:ident, $peek_only:ident, $seek:ident, $seek_start:ident, $seek_length:ident)=>{
|
||||||
|
/// Find a slice corrensponding to a syntax token.
|
||||||
|
pub const fn $peek (source: &str) -> DslPerhaps<&str> {
|
||||||
|
match $seek(source) {
|
||||||
|
Err(e) => Err(e),
|
||||||
|
Ok(None) => Ok(None),
|
||||||
|
Ok(Some((start, length))) => Ok(Some(str_range(source, start, start + length))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Find a slice corrensponding to a syntax token
|
||||||
|
/// but return an error if it isn't the only thing
|
||||||
|
/// in the source.
|
||||||
|
pub const fn $peek_only (source: &str) -> DslPerhaps<&str> {
|
||||||
|
match $seek(source) {
|
||||||
|
Err(e) => Err(e),
|
||||||
|
Ok(None) => Ok(None),
|
||||||
|
Ok(Some((start, length))) => {
|
||||||
|
if let Err(e) = no_trailing_non_space(source, start + length, Some("peek_only")) { return Err(e) }
|
||||||
|
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),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
@ -128,9 +128,3 @@ impl<C> Binding<C> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn unquote (x: &str) -> &str {
|
|
||||||
let mut chars = x.chars();
|
|
||||||
chars.next();
|
|
||||||
//chars.next_back();
|
|
||||||
chars.as_str()
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue