dsl goes fat
Some checks failed
/ build (push) Has been cancelled

This commit is contained in:
🪞👃🪞 2025-05-27 00:53:06 +03:00
parent 08a8dff93d
commit cb47c4d0ff
11 changed files with 513 additions and 492 deletions

View file

@ -1,134 +1,118 @@
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 Ast::Exp(exp) = source {
let mut iter = source.clone();
if let Some(Ast::Key(id)) = iter.next() && *id == *"when" {
return Ok(Some(Self(
state.eval(iter.next().unwrap(), ||"when: expected condition")?,
state.eval(iter.next().unwrap(), ||"when: expected content")?,
)))
}
fn try_provide (state: &S, source: Ast) -> Perhaps<Self> {
if let Exp(_, 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 Ast::Exp(source) = source {
let mut iter = source.clone();
if let Some(Ast::Key(id)) = iter.next() && *id == *"either" {
return Ok(Some(Self(
state.eval(iter.next().unwrap(), ||"either: expected condition")?,
state.eval(iter.next().unwrap(), ||"either: expected content 1")?,
state.eval(iter.next().unwrap(), ||"either: expected content 2")?,
)))
}
fn try_provide (state: &S, source: Ast) -> Perhaps<Self> {
if let Exp(_, 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().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> {
Ok(Some(if let Ast::Exp(source) = source {
let mut iter = source.clone();
match iter.next() {
Some(Value::Key("bsp/n")) => Self::n(
state.eval(iter.next(), ||"bsp/n: expected content 1")?,
state.eval(iter.next(), ||"bsp/n: expected content 2")?,
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().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(Value::Key("bsp/s")) => Self::s(
state.eval(iter.next(), ||"bsp/s: expected content 1")?,
state.eval(iter.next(), ||"bsp/s: 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(Value::Key("bsp/e")) => Self::e(
state.eval(iter.next(), ||"bsp/e: expected content 1")?,
state.eval(iter.next(), ||"bsp/e: 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(Value::Key("bsp/w")) => Self::w(
state.eval(iter.next(), ||"bsp/w: expected content 1")?,
state.eval(iter.next(), ||"bsp/w: 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(Value::Key("bsp/a")) => Self::a(
state.eval(iter.next(), ||"bsp/a: expected content 1")?,
state.eval(iter.next(), ||"bsp/a: 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(Value::Key("bsp/b")) => Self::b(
state.eval(iter.next(), ||"bsp/b: expected content 1")?,
state.eval(iter.next(), ||"bsp/b: 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),
}
} else {
return 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> {
Ok(Some(if let Ast::Exp(source) = source {
let mut iter = source.clone();
match iter.next() {
Some(Value::Key("align/c")) =>
Self::c(state.eval(iter.next(), ||"align/c: expected content")?),
Some(Value::Key("align/x")) =>
Self::x(state.eval(iter.next(), ||"align/x: expected content")?),
Some(Value::Key("align/y")) =>
Self::y(state.eval(iter.next(), ||"align/y: expected content")?),
Some(Value::Key("align/n")) =>
Self::n(state.eval(iter.next(), ||"align/n: expected content")?),
Some(Value::Key("align/s")) =>
Self::s(state.eval(iter.next(), ||"align/s: expected content")?),
Some(Value::Key("align/e")) =>
Self::e(state.eval(iter.next(), ||"align/e: expected content")?),
Some(Value::Key("align/w")) =>
Self::w(state.eval(iter.next(), ||"align/w: expected content")?),
Some(Value::Key("align/nw")) =>
Self::nw(state.eval(iter.next(), ||"align/nw: expected content")?),
Some(Value::Key("align/ne")) =>
Self::ne(state.eval(iter.next(), ||"align/ne: expected content")?),
Some(Value::Key("align/sw")) =>
Self::sw(state.eval(iter.next(), ||"align/sw: expected content")?),
Some(Value::Key("align/se")) =>
Self::se(state.eval(iter.next(), ||"align/se: expected content")?),
_ => return Ok(None),
}
} else {
return Ok(None)
}))
}))
}
Ok(None)
}
}
//#[cfg(feature = "dsl")] take!($Enum<A>, A|state, words|Ok(
//if let Some(Token { value: Value::Key(k), .. }) = words.peek() {
//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: Value::Key($x),..}) => Self::x(content),
//Some(Token{value: Value::Key($y),..}) => Self::y(content),
//Some(Token{value: Value::Key($xy),..}) => Self::xy(content),
//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: Value::Key($x|$y|$xy), .. }) = words.peek() {
//if let Some(Token { value: Key($x|$y|$xy), .. }) = words.peek() {
//let mut base = words.clone();
//Some(match words.next() {
//Some(Token { value: Value::Key($x), .. }) => Self::x(
//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: Value::Key($y), .. }) => Self::y(
//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: Value::Key($x), .. }) => Self::xy(
//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")?