mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
wip: dsl: more rework
This commit is contained in:
parent
f1b24d436a
commit
7097333993
4 changed files with 145 additions and 159 deletions
|
|
@ -1,143 +1,131 @@
|
|||
use crate::*;
|
||||
|
||||
impl<I: Ast, S, A> Eval<I, When<A>> for S where
|
||||
S: Eval<AstValue, bool> + Eval<AstValue, A>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
impl<S, A> Dsl<S> for When<A> where S: Eval<Ast, bool> + Eval<Ast, A> {
|
||||
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
||||
Ok(match source.peek() {
|
||||
Some(Value::Key("when")) => Some(Self(
|
||||
self.provide(source, ||"when: expected condition")?,
|
||||
self.provide(source, ||"when: expected content")?,
|
||||
state.eval(source, ||"when: expected condition")?,
|
||||
state.eval(source, ||"when: expected content")?,
|
||||
)),
|
||||
_ => None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Ast, S, A, B> Eval<I, Either<A, B>> for S where
|
||||
S: Eval<AstValue, bool> + Eval<AstValue, A> + Eval<AstValue, B>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
impl<S, A, B> Dsl<S> for Either<A, B> where S: Eval<Ast, bool> + Eval<Ast, A> + Eval<Ast, B> {
|
||||
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
||||
Ok(match source.peek() {
|
||||
Some(Value::Key("either")) => Some(Self(
|
||||
self.provide(source, ||"either: expected condition")?,
|
||||
self.provide(source, ||"either: expected content 1")?,
|
||||
self.provide(source, ||"either: expected content 2")?
|
||||
state.eval(source, ||"either: expected condition")?,
|
||||
state.eval(source, ||"either: expected content 1")?,
|
||||
state.eval(source, ||"either: expected content 2")?
|
||||
)),
|
||||
_ => None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Ast, S, A, B> Eval<I, Bsp<A, B>> for S where
|
||||
S: Eval<AstValue, A> + Eval<AstValue, B>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
Ok(if let Some(Value::Key(key)) = source.peek() {
|
||||
Some(match key {
|
||||
"bsp/n" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/n: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/n: expected content 2")?;
|
||||
Self::n(a, b)
|
||||
},
|
||||
"bsp/s" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/s: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/s: expected content 2")?;
|
||||
Self::s(a, b)
|
||||
},
|
||||
"bsp/e" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/e: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/e: expected content 2")?;
|
||||
Self::e(a, b)
|
||||
},
|
||||
"bsp/w" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/w: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/w: expected content 2")?;
|
||||
Self::w(a, b)
|
||||
},
|
||||
"bsp/a" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/a: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/a: expected content 2")?;
|
||||
Self::a(a, b)
|
||||
},
|
||||
"bsp/b" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/b: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/b: expected content 2")?;
|
||||
Self::b(a, b)
|
||||
},
|
||||
_ => return Ok(None),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
})
|
||||
impl<S, A, B> Dsl<S> for Bsp<A, B> where S: Eval<Ast, A> + Eval<Ast, B> {
|
||||
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
||||
Ok(Some(match source.peek() {
|
||||
Some(Value::Key("bsp/n")) => {
|
||||
let _ = source.next();
|
||||
let a: A = state.eval(source, ||"bsp/n: expected content 1")?;
|
||||
let b: B = state.eval(source, ||"bsp/n: expected content 2")?;
|
||||
Self::n(a, b)
|
||||
},
|
||||
Some(Value::Key("bsp/s")) => {
|
||||
let _ = source.next();
|
||||
let a: A = state.eval(source, ||"bsp/s: expected content 1")?;
|
||||
let b: B = state.eval(source, ||"bsp/s: expected content 2")?;
|
||||
Self::s(a, b)
|
||||
},
|
||||
Some(Value::Key("bsp/e")) => {
|
||||
let _ = source.next();
|
||||
let a: A = state.eval(source, ||"bsp/e: expected content 1")?;
|
||||
let b: B = state.eval(source, ||"bsp/e: expected content 2")?;
|
||||
Self::e(a, b)
|
||||
},
|
||||
Some(Value::Key("bsp/w")) => {
|
||||
let _ = source.next();
|
||||
let a: A = state.eval(source, ||"bsp/w: expected content 1")?;
|
||||
let b: B = state.eval(source, ||"bsp/w: expected content 2")?;
|
||||
Self::w(a, b)
|
||||
},
|
||||
Some(Value::Key("bsp/a")) => {
|
||||
let _ = source.next();
|
||||
let a: A = state.eval(source, ||"bsp/a: expected content 1")?;
|
||||
let b: B = state.eval(source, ||"bsp/a: expected content 2")?;
|
||||
Self::a(a, b)
|
||||
},
|
||||
Some(Value::Key("bsp/b")) => {
|
||||
let _ = source.next();
|
||||
let a: A = state.eval(source, ||"bsp/b: expected content 1")?;
|
||||
let b: B = state.eval(source, ||"bsp/b: expected content 2")?;
|
||||
Self::b(a, b)
|
||||
},
|
||||
_ => return Ok(None),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Ast, S, A> Eval<I, Align<A>> for S where
|
||||
S: Eval<AstValue, A>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
impl<S, A> Dsl<S> for Align<A> where S: Eval<Ast, A> {
|
||||
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
||||
Ok(if let Some(Value::Key(key)) = source.peek() {
|
||||
Some(match key {
|
||||
"align/c" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/c: expected content")?;
|
||||
let content: A = state.eval(source, ||"align/c: expected content")?;
|
||||
Self::c(content)
|
||||
},
|
||||
"align/x" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/x: expected content")?;
|
||||
let content: A = state.eval(source, ||"align/x: expected content")?;
|
||||
Self::x(content)
|
||||
},
|
||||
"align/y" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/y: expected content")?;
|
||||
let content: A = state.eval(source, ||"align/y: expected content")?;
|
||||
Self::y(content)
|
||||
},
|
||||
"align/n" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/n: expected content")?;
|
||||
let content: A = state.eval(source, ||"align/n: expected content")?;
|
||||
Self::n(content)
|
||||
},
|
||||
"align/s" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/s: expected content")?;
|
||||
let content: A = state.eval(source, ||"align/s: expected content")?;
|
||||
Self::s(content)
|
||||
},
|
||||
"align/e" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/e: expected content")?;
|
||||
let content: A = state.eval(source, ||"align/e: expected content")?;
|
||||
Self::e(content)
|
||||
},
|
||||
"align/w" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/w: expected content")?;
|
||||
let content: A = state.eval(source, ||"align/w: expected content")?;
|
||||
Self::w(content)
|
||||
},
|
||||
"align/nw" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/nw: expected content")?;
|
||||
let content: A = state.eval(source, ||"align/nw: expected content")?;
|
||||
Self::nw(content)
|
||||
},
|
||||
"align/ne" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/ne: expected content")?;
|
||||
let content: A = state.eval(source, ||"align/ne: expected content")?;
|
||||
Self::ne(content)
|
||||
},
|
||||
"align/sw" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/sw: expected content")?;
|
||||
let content: A = state.eval(source, ||"align/sw: expected content")?;
|
||||
Self::sw(content)
|
||||
},
|
||||
"align/se" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/se: expected content")?;
|
||||
let content: A = state.eval(source, ||"align/se: expected content")?;
|
||||
Self::se(content)
|
||||
},
|
||||
_ => return Ok(None),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue