mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
dsl: extract dsl_error; ParseError -> DslError
This commit is contained in:
parent
f18e01c220
commit
ed772b9872
4 changed files with 34 additions and 21 deletions
16
dsl/src/dsl_error.rs
Normal file
16
dsl/src/dsl_error.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
use crate::*;
|
||||||
|
|
||||||
|
pub type DslResult<T> = Result<T, DslError>;
|
||||||
|
|
||||||
|
#[derive(Error, Debug, Copy, Clone, PartialEq)] 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),
|
||||||
|
#[error("parse failed: error #{0}")]
|
||||||
|
Code(u8),
|
||||||
|
}
|
||||||
|
|
@ -1,20 +1,5 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
pub type ParseResult<T> = Result<T, ParseError>;
|
|
||||||
|
|
||||||
#[derive(Error, Debug, Copy, Clone, PartialEq)] pub enum ParseError {
|
|
||||||
#[error("parse failed: not implemented")]
|
|
||||||
Unimplemented,
|
|
||||||
#[error("parse failed: empty")]
|
|
||||||
Empty,
|
|
||||||
#[error("parse failed: incomplete")]
|
|
||||||
Incomplete,
|
|
||||||
#[error("parse failed: unexpected character '{0}'")]
|
|
||||||
Unexpected(char),
|
|
||||||
#[error("parse failed: error #{0}")]
|
|
||||||
Code(u8),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Implement the const iterator pattern.
|
/// Implement the const iterator pattern.
|
||||||
#[macro_export] macro_rules! const_iter {
|
#[macro_export] macro_rules! const_iter {
|
||||||
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {
|
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {
|
||||||
|
|
@ -170,7 +155,7 @@ pub const fn peek_src <'a> (source: &'a str) -> Option<Token<'a>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn to_number (digits: &str) -> Result<usize, ParseError> {
|
pub const fn to_number (digits: &str) -> DslResult<usize> {
|
||||||
let mut value = 0;
|
let mut value = 0;
|
||||||
iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
|
iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
|
||||||
Ok(digit) => value = 10 * value + digit,
|
Ok(digit) => value = 10 * value + digit,
|
||||||
|
|
@ -179,7 +164,7 @@ pub const fn to_number (digits: &str) -> Result<usize, ParseError> {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn to_digit (c: char) -> Result<usize, ParseError> {
|
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,
|
||||||
|
|
@ -196,7 +181,7 @@ pub const fn to_digit (c: char) -> Result<usize, ParseError> {
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum Value<'source> {
|
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum Value<'source> {
|
||||||
#[default] Nil,
|
#[default] Nil,
|
||||||
Err(ParseError),
|
Err(DslError),
|
||||||
Num(usize),
|
Num(usize),
|
||||||
Sym(&'source str),
|
Sym(&'source str),
|
||||||
Key(&'source str),
|
Key(&'source str),
|
||||||
|
|
@ -228,7 +213,7 @@ impl<'source> Token<'source> {
|
||||||
pub const fn value (&self) -> Value {
|
pub const fn value (&self) -> Value {
|
||||||
self.value
|
self.value
|
||||||
}
|
}
|
||||||
pub const fn error (self, error: ParseError) -> Self {
|
pub const fn error (self, error: DslError) -> Self {
|
||||||
Self { value: Value::Err(error), ..self }
|
Self { value: Value::Err(error), ..self }
|
||||||
}
|
}
|
||||||
pub const fn grow (self) -> Self {
|
pub const fn grow (self) -> Self {
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,21 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
/// Map EDN tokens to parameters of a given type for a given context
|
/// Map EDN tokens to parameters of a given type for a given context
|
||||||
|
/// TODO: Replace both [Context] and [TryFromDsl] with this trait
|
||||||
|
/// which returns a [Result].
|
||||||
pub trait Dsl<U>: Sized {
|
pub trait Dsl<U>: Sized {
|
||||||
fn take <'state, 'source> (_: &'state Self, _: &mut TokenIter<'source>) -> Perhaps<U> {
|
fn take <'state, 'source> (_: &'state Self, _: &mut TokenIter<'source>) -> Perhaps<U> {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
fn take_or_fail <'state, 'source> (
|
||||||
|
state: &'state Self, iter: &mut TokenIter<'source>
|
||||||
|
) -> Usually<U> {
|
||||||
|
if let Some(value) = Self::take(state, iter)? {
|
||||||
|
Ok(value)
|
||||||
|
} else {
|
||||||
|
Result::Err("not found".into()) // TODO add info and error type
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Map EDN tokens to parameters of a given type for a given context
|
/// Map EDN tokens to parameters of a given type for a given context
|
||||||
|
|
|
||||||
|
|
@ -43,8 +43,9 @@ pub(crate) use konst::iter::{ConstIntoIter, IsIteratorKind};
|
||||||
pub(crate) use konst::string::{split_at, str_range, char_indices};
|
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::Value::*;
|
pub(crate) use self::Value::*;
|
||||||
pub(crate) use self::ParseError::*;
|
pub(crate) use self::DslError::*;
|
||||||
|
|
||||||
|
mod dsl_error; pub use self::dsl_error::*;
|
||||||
mod dsl_parse; pub use self::dsl_parse::*;
|
mod dsl_parse; pub use self::dsl_parse::*;
|
||||||
mod dsl_provide; pub use self::dsl_provide::*;
|
mod dsl_provide; pub use self::dsl_provide::*;
|
||||||
|
|
||||||
|
|
@ -148,7 +149,7 @@ mod dsl_provide; pub use self::dsl_provide::*;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
//#[cfg(test)] #[test] fn test_examples () -> Result<(), ParseError> {
|
//#[cfg(test)] #[test] fn test_examples () -> Result<(), DslError> {
|
||||||
//// Let's pretend to render some view.
|
//// Let's pretend to render some view.
|
||||||
//let source = include_str!("../../tek/src/view_arranger.edn");
|
//let source = include_str!("../../tek/src/view_arranger.edn");
|
||||||
//// The token iterator allows you to get the tokens represented by the source text.
|
//// The token iterator allows you to get the tokens represented by the source text.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue