tengri/output/src/ops_dsl.rs

124 lines
6.1 KiB
Rust

use crate::*;
use Value::*;
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> {
if let Exp(_, mut exp) = source.0 && let Some(Ast(Key(id))) = exp.peek() && *id == *"when" {
let _ = exp.next();
return Ok(Some(Self(
state.eval(exp.next().unwrap(), ||"when: expected condition")?,
state.eval(exp.next().unwrap(), ||"when: expected content")?,
)))
}
Ok(None)
}
}
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> {
if let Exp(_, mut exp) = source.0 && let Some(Ast(Key(id))) = exp.peek() && *id == *"either" {
let _ = exp.next();
return Ok(Some(Self(
state.eval(exp.next().unwrap(), ||"either: expected condition")?,
state.eval(exp.next().unwrap(), ||"either: expected content 1")?,
state.eval(exp.next().unwrap(), ||"either: expected content 2")?,
)))
}
Ok(None)
}
}
impl<S, A> Dsl<S> for Align<A> where S: Eval<Option<Ast>, A> {
fn try_provide (state: &S, source: Ast) -> Perhaps<Self> {
if let Exp(_, source) = source.0 {
let mut rest = source.clone();
return Ok(Some(match rest.next().as_ref().and_then(|x|x.key()) {
Some("align/c") => Self::c(state.eval(rest.next(), ||"align/c: expected content")?),
Some("align/x") => Self::x(state.eval(rest.next(), ||"align/x: expected content")?),
Some("align/y") => Self::y(state.eval(rest.next(), ||"align/y: expected content")?),
Some("align/n") => Self::n(state.eval(rest.next(), ||"align/n: expected content")?),
Some("align/s") => Self::s(state.eval(rest.next(), ||"align/s: expected content")?),
Some("align/e") => Self::e(state.eval(rest.next(), ||"align/e: expected content")?),
Some("align/w") => Self::w(state.eval(rest.next(), ||"align/w: expected content")?),
Some("align/nw") => Self::nw(state.eval(rest.next(), ||"align/nw: expected content")?),
Some("align/ne") => Self::ne(state.eval(rest.next(), ||"align/ne: expected content")?),
Some("align/sw") => Self::sw(state.eval(rest.next(), ||"align/sw: expected content")?),
Some("align/se") => Self::se(state.eval(rest.next(), ||"align/se: expected content")?),
_ => return Ok(None),
}))
}
Ok(None)
}
}
impl<S, A, B> Dsl<S> for Bsp<A, B> where S: Eval<Option<Ast>, A> + Eval<Option<Ast>, B> {
fn try_provide (state: &S, source: Ast) -> Perhaps<Self> {
if let Exp(_, exp) = source.0 {
let mut rest = exp.clone();
return Ok(Some(match rest.next().as_ref().and_then(|x|x.key()) {
Some("bsp/n") => Self::n(
state.eval(rest.next(), ||"bsp/n: expected content 1")?,
state.eval(rest.next(), ||"bsp/n: expected content 2")?,
),
Some("bsp/s") => Self::s(
state.eval(rest.next(), ||"bsp/s: expected content 1")?,
state.eval(rest.next(), ||"bsp/s: expected content 2")?,
),
Some("bsp/e") => Self::e(
state.eval(rest.next(), ||"bsp/e: expected content 1")?,
state.eval(rest.next(), ||"bsp/e: expected content 2")?,
),
Some("bsp/w") => Self::w(
state.eval(rest.next(), ||"bsp/w: expected content 1")?,
state.eval(rest.next(), ||"bsp/w: expected content 2")?,
),
Some("bsp/a") => Self::a(
state.eval(rest.next(), ||"bsp/a: expected content 1")?,
state.eval(rest.next(), ||"bsp/a: expected content 2")?,
),
Some("bsp/b") => Self::b(
state.eval(rest.next(), ||"bsp/b: expected content 1")?,
state.eval(rest.next(), ||"bsp/b: expected content 2")?,
),
_ => return Ok(None),
}))
}
Ok(None)
}
}
//#[cfg(feature = "dsl")] take!($Enum<A>, A|state, words|Ok(
//if let Some(Token { value: Key(k), .. }) = words.peek() {
//let mut base = words.clone();
//let content = state.give_or_fail(words, ||format!("{k}: no content"))?;
//return Ok(Some(match words.next() {
//Some(Token{value: Key($x),..}) => Self::x(content),
//Some(Token{value: Key($y),..}) => Self::y(content),
//Some(Token{value: Key($xy),..}) => Self::xy(content),
//_ => unreachable!()
//}))
//} else {
//None
//}));
//#[cfg(feature = "dsl")] take!($Enum<U, A>, U, A|state, words|Ok(
//if let Some(Token { value: Key($x|$y|$xy), .. }) = words.peek() {
//let mut base = words.clone();
//Some(match words.next() {
//Some(Token { value: Key($x), .. }) => Self::x(
//state.give_or_fail(words, ||"x: no unit")?,
//state.give_or_fail(words, ||"x: no content")?,
//),
//Some(Token { value: Key($y), .. }) => Self::y(
//state.give_or_fail(words, ||"y: no unit")?,
//state.give_or_fail(words, ||"y: no content")?,
//),
//Some(Token { value: Key($x), .. }) => Self::xy(
//state.give_or_fail(words, ||"xy: no unit x")?,
//state.give_or_fail(words, ||"xy: no unit y")?,
//state.give_or_fail(words, ||"xy: no content")?
//),
//_ => unreachable!(),
//})
//} else {
//None
//}));