Add empty() function

This commit is contained in:
NGnius (Graham) 2022-01-27 16:06:32 -05:00
parent d3bb52d354
commit bd3d1465df
7 changed files with 113 additions and 2 deletions

View file

@ -126,6 +126,10 @@ Repeat the iterable count times, or infinite times if count is omitted.
Retrieve all files from a folder, matching a regex pattern.
#### empty();
Empty iterator. Useful for deleting items using replacement filters.
### Sorters
Operations to sort the items in an iterable: iterable~(sorter) OR iterable.sort(sorter)

View file

@ -174,5 +174,6 @@ pub(crate) fn standard_vocab(vocabulary: &mut MpsLanguageDictionary) {
.add(crate::lang::vocabulary::repeat_function_factory())
.add(crate::lang::vocabulary::AssignStatementFactory)
.add(crate::lang::vocabulary::sql_init_function_factory())
.add(crate::lang::vocabulary::files_function_factory());
.add(crate::lang::vocabulary::files_function_factory())
.add(crate::lang::vocabulary::empty_function_factory());
}

View file

@ -0,0 +1,88 @@
use std::collections::VecDeque;
use std::fmt::{Debug, Display, Error, Formatter};
use std::iter::Iterator;
use crate::tokens::MpsToken;
use crate::MpsContext;
#[cfg(debug_assertions)]
use crate::lang::utility::assert_empty;
use crate::lang::MpsLanguageDictionary;
use crate::lang::{MpsFunctionFactory, MpsFunctionStatementFactory, MpsIteratorItem, MpsOp};
use crate::lang::{RuntimeError, SyntaxError};
#[derive(Debug)]
pub struct EmptyStatement {
context: Option<MpsContext>,
}
impl Display for EmptyStatement {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "empty()")
}
}
impl std::clone::Clone for EmptyStatement {
fn clone(&self) -> Self {
Self {
context: None,
}
}
}
impl Iterator for EmptyStatement {
type Item = MpsIteratorItem;
fn next(&mut self) -> Option<Self::Item> {
None
}
}
impl MpsOp for EmptyStatement {
fn enter(&mut self, ctx: MpsContext) {
self.context = Some(ctx)
}
fn escape(&mut self) -> MpsContext {
self.context.take().unwrap()
}
fn is_resetable(&self) -> bool {
true
}
fn reset(&mut self) -> Result<(), RuntimeError> {
Ok(())
}
}
pub struct EmptyFunctionFactory;
impl MpsFunctionFactory<EmptyStatement> for EmptyFunctionFactory {
fn is_function(&self, name: &str) -> bool {
name == "empty" || name == "_"
}
fn build_function_params(
&self,
_name: String,
#[allow(unused_variables)]
tokens: &mut VecDeque<MpsToken>,
_dict: &MpsLanguageDictionary,
) -> Result<EmptyStatement, SyntaxError> {
// empty()
#[cfg(debug_assertions)]
assert_empty(tokens)?;
Ok(EmptyStatement {
context: None,
})
}
}
pub type EmptyStatementFactory = MpsFunctionStatementFactory<EmptyStatement, EmptyFunctionFactory>;
#[inline(always)]
pub fn empty_function_factory() -> EmptyStatementFactory {
EmptyStatementFactory::new(EmptyFunctionFactory)
}

View file

@ -1,4 +1,5 @@
mod comment;
mod empty;
mod files;
mod repeat;
mod sql_init;
@ -7,6 +8,7 @@ mod sql_simple_query;
mod variable_assign;
pub use comment::{CommentStatement, CommentStatementFactory};
pub use empty::{empty_function_factory, EmptyStatementFactory};
pub use files::{files_function_factory, FilesStatementFactory};
pub use repeat::{repeat_function_factory, RepeatStatementFactory};
pub use sql_init::{sql_init_function_factory, SqlInitStatementFactory};

View file

@ -124,6 +124,10 @@
//!
//! Retrieve all files from a folder, matching a regex pattern.
//!
//! ### empty();
//!
//! Empty iterator. Useful for deleting items using replacement filters.
//!
//! ## Sorters
//! Operations to sort the items in an iterable: iterable~(sorter) OR iterable.sort(sorter)
//!

View file

@ -367,3 +367,12 @@ fn execute_blisssort_line() -> Result<(), Box<dyn MpsLanguageError>> {
true,
)
}
#[test]
fn execute_emptyfn_line() -> Result<(), Box<dyn MpsLanguageError>> {
execute_single_line(
"empty()",
true,
true,
)
}

View file

@ -31,7 +31,10 @@ These always return an iterable which can be manipulated.
Repeat the iterable count times, or infinite times if count is omitted.
files(folder = `path/to/music`, recursive = true|false, regex = `pattern`)
Retrieve all files from a folder, matching a regex pattern.";
Retrieve all files from a folder, matching a regex pattern.
empty()
Empty iterator. Useful for deleting items using replacement filters.";
pub const FILTERS: &str =
"FILTERS (?filters)