wip: ast/cst
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-25 22:48:29 +03:00
parent 31e84bf5b3
commit f1b24d436a
20 changed files with 1081 additions and 1103 deletions

View file

@ -1,139 +1,67 @@
use crate::*;
use std::marker::PhantomData;
/// [Input] state that can be matched against a [Value].
pub trait DslInput: Input { fn matches_dsl (&self, token: &str) -> bool; }
/// A pre-configured mapping of input events to commands.
pub trait KeyMap<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> {
/// Try to find a command that matches the current input event.
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C>;
/// List of input layers with optional conditional filters.
#[derive(Default, Debug)] pub struct InputLayers<S>(Vec<InputLayer<S>>);
#[derive(Default, Debug)] pub struct InputLayer<S>{
__: PhantomData<S>,
condition: Option<Box<dyn Ast>>,
bindings: Box<dyn Ast>,
}
/// A [SourceIter] can be a [KeyMap].
impl<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for SourceIter<'k> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
let mut iter = self.clone();
while let Some((token, rest)) = iter.next() {
iter = rest;
match token {
Token { value: Value::Exp(0, exp_iter), .. } => {
let mut exp_iter = exp_iter.clone();
match exp_iter.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = Take::take(state, exp_iter)? {
return Ok(Some(command))
}
}
},
_ => panic!("invalid config (expected symbol)")
}
},
_ => panic!("invalid config (expected expression)")
}
}
Ok(None)
}
}
/// A [TokenIter] can be a [KeyMap].
impl<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for TokenIter<'k> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
let mut iter = self.clone();
while let Some(next) = iter.next() {
match next {
Token { value: Value::Exp(0, exp_iter), .. } => {
let mut e = exp_iter.clone();
match e.next() {
Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) {
if let Some(command) = Take::take(state, e)? {
return Ok(Some(command))
}
}
},
_ => panic!("invalid config (expected symbol, got: {exp_iter:?})")
}
},
_ => panic!("invalid config (expected expression, got: {next:?})")
}
}
Ok(None)
}
}
pub type InputCondition<'k, S> = Box<dyn Fn(&S)->Usually<bool> + Send + Sync + 'k>;
/// A collection of pre-configured mappings of input events to commands,
/// which may be made available subject to given conditions.
pub struct InputMap<'k,
S,
C: Command<S> + Take<'k, S>,
I: DslInput,
M: KeyMap<'k, S, C, I>
> {
__: PhantomData<&'k (S, C, I)>,
pub layers: Vec<(InputCondition<'k, S>, M)>,
}
impl<'k,
S,
C: Command<S> + Take<'k, S>,
I: DslInput,
M: KeyMap<'k, S, C, I>
> Default for InputMap<'k, S, C, I, M>{
fn default () -> Self {
Self { __: PhantomData, layers: vec![] }
}
}
impl<'k, S, C: Command<S> + Take<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
InputMap<'k, S, C, I, M> {
pub fn new (keymap: M) -> Self {
Self::default().layer(keymap)
}
pub fn layer (mut self, keymap: M) -> Self {
self.add_layer(keymap);
self
}
pub fn add_layer (&mut self, keymap: M) -> &mut Self {
self.add_layer_if(Box::new(|_|Ok(true)), keymap);
self
}
pub fn layer_if (mut self, condition: InputCondition<'k, S>, keymap: M) -> Self {
self.add_layer_if(condition, keymap);
self
}
pub fn add_layer_if (&mut self, condition: InputCondition<'k, S>, keymap: M) -> &mut Self {
self.layers.push((Box::new(condition), keymap));
impl<S> InputLayers<S> {
pub fn new (layer: Box<dyn Ast>) -> Self
{ Self(vec![]).layer(layer) }
pub fn layer (mut self, layer: Box<dyn Ast>) -> Self
{ self.add_layer(layer); self }
pub fn layer_if (mut self, condition: Box<dyn Ast>, layer: Box<dyn Ast>) -> Self
{ self.add_layer_if(Some(condition), layer); self }
pub fn add_layer (&mut self, layer: Box<dyn Ast>) -> &mut Self
{ self.add_layer_if(None, layer.into()); self }
pub fn add_layer_if (
&mut self,
condition: Option<Box<dyn Ast>>,
bindings: Box<dyn Ast>
) -> &mut Self {
self.0.push(InputLayer { condition, bindings, __: Default::default() });
self
}
}
impl<'k, S, C: Command<S> + Take<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>>
std::fmt::Debug for InputMap<'k, S, C, I, M> {
fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "[InputMap: {} layer(s)]", self.layers.len())
}
}
/// An [InputMap] can be a [KeyMap].
impl<'k,
S,
C: Command<S> + Take<'k, S>,
I: DslInput,
M: KeyMap<'k, S, C, I>
> KeyMap<'k, S, C, I> for InputMap<'k, S, C, I, M> {
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
for (condition, keymap) in self.layers.iter() {
if !condition(state)? {
continue
}
if let Some(command) = keymap.keybind_resolve(state, input)? {
impl<S: Eval<I, O>, I: Input, O: Command<S>> Eval<I, O> for InputLayers<S> {
fn eval (&self, input: I) -> Perhaps<O> {
for layer in self.0.iter() {
if let Some(command) = Ok(if let Some(condition) = layer.condition.as_ref() {
if self.matches(condition)? {
self.state().eval(*self)?
} else {
None
}
} else {
self.state().eval(*self)?
})? {
return Ok(Some(command))
}
}
}
}
/// [Input] state that can be matched against a [CstValue].
pub trait InputState<S>: Input {
fn state (&self) -> &S;
fn matches (&self, condition: &Box<dyn Ast>) -> Usually<bool>;
}
impl<S: Eval<I, O>, I: InputState<S>, O: Command<S>> Eval<I, O>
for InputLayer<S> {
fn eval (&self, input: I) -> Perhaps<O> {
if let Some(AstToken::Exp(exp)) = iter.peek() {
let mut e = exp.clone();
if let Some(AstToken::Sym(binding)) = e.next()
&& input.matches_dsl(binding)
&& let Some(command) = Dsl::from_dsl(input.state(), e)? {
return Ok(Some(command))
} else {
unreachable!("InputLayer: expected symbol, got: {e:?}")
}
} else {
panic!("InputLayer: expected expression, got: {self:?}")
}
Ok(None)
}
}