Kul core no_std
crate
This is the no_std
crate that provides the core of a parser for:
A unique textual notation that can be used as both a data format and a markup
language and that has powerful extensibility of both lexical syntax and
semantics, and a Rust library for parsing it. It is inspired by the
little-known Curl language, but is only a notation like Curl's, not a
programming language like Curl.
See the README
of the main kul
crate for the fuller description of the
library.
Usage of kul_core
A primary purpose of this crate is to enable using the parser in applications
that do not have heap allocation and where all values used by the parsing are
allocated on the stack. This is done like:
use core::str::FromStr;
use kul_core::{
premade::inmem::{
parser, parser_no_bind, parse_text_with,
DatumType, DatumSlice, Text, BindingsSlice, DatumAllocator,
},
datum::premade::DatumMutRef,
Combiner, Datum, Error, Text as _,
};
fn no_extension() {
let input = r"Surrounding {▷ e\\sc} text.";
let e = || Datum::EmptyNest;
let alloc_from: DatumSlice<'_, '_> = &mut [e(), e(), e(), e(), e()];
let mut parser = parser_no_bind(alloc_from);
let input = Text::from_str(input);
let parse_iter = parse_text_with(&input, &mut parser);
for result in parse_iter {
dbg!(&result);
match result {
Ok(datum) =>
match datum { _ => () },
Err(error) =>
match error { _ => () }
}
}
}
fn with_extensions()
{
#[derive(Eq, PartialEq, Debug)]
enum MyDatumVariants {
Thing,
Integer(i128),
}
#[derive(Debug)]
enum MyCombinerError<'input, 'alloc> {
Oops,
Darnit(MyDatum<'input, 'alloc>),
TooLong,
}
type MyText<'input, 'alloc> = Text<'input, 'alloc, MyDatumVariants>;
type MyBindingsSlice<'input, 'alloc, 'funs, 'bind> =
BindingsSlice<'input, 'alloc, 'funs, 'bind,
MyDatumVariants, MyCombinerError<'input, 'alloc>>;
type MyDatum<'input, 'alloc> = DatumType<'input, 'alloc, MyDatumVariants>;
type MyDatumAllocator<'input, 'alloc> =
DatumAllocator<'input, 'alloc, MyDatumVariants>;
type MyDatumSlice<'input, 'alloc> =
DatumSlice<'input, 'alloc, MyDatumVariants>;
type AllocArg<'a, 'alloc> = &'a mut MyDatumAllocator<'static, 'alloc>;
let comment = |_operator, _operands, _: AllocArg<'_, '_>| {
Ok(None)
};
let pass_thru = |_operator, operands, _: AllocArg<'_, '_>| {
Ok(Some(operands))
};
let thing = |_operator, operands, _: AllocArg<'_, '_>| {
if let Datum::EmptyList = operands {
Ok(Some(Datum::Extra(MyDatumVariants::Thing)))
} else {
Err(Error::FailedCombiner(MyCombinerError::Darnit(operands)))
}
};
let int = |_operator, operands: MyText<'_, '_>, _: AllocArg<'_, '_>| {
let mut buf = [0; 39];
let s = operands.encode_utf8(&mut buf).map_err(
|_| Error::FailedCombiner(MyCombinerError::TooLong))?;
let i = i128::from_str(s).map_err(
|_| Error::FailedCombiner(MyCombinerError::Oops))?;
Ok(Some(Datum::Extra(MyDatumVariants::Integer(i))))
};
let mut cof_rator = Datum::Text(Text::from_str("compound"));
let mut cof_rands = Datum::EmptyList;
let compound_operator_form =
Datum::Combination {
operator: DatumMutRef(&mut cof_rator),
operands: DatumMutRef(&mut cof_rands),
};
let bindings: MyBindingsSlice<'_, '_, '_, '_> = &[
(Datum::Text(Text::from_str("#")),
Combiner::Operative(&comment)),
(Datum::Text(Text::from_str("thing")),
Combiner::Applicative(&thing)),
(Datum::Text(Text::from_str("int")),
Combiner::Operative(&int)),
(compound_operator_form,
Combiner::Applicative(&pass_thru)),
];
let e = || Datum::EmptyNest;
let alloc_from: MyDatumSlice<'_, '_> =
&mut [e(), e(), e(), e(), e(), e(), e(), e(), e(), e(), e(), e(),
e(), e(), e(), e(), e(), e()];
let mut parser = parser(alloc_from, bindings);
let input = "{{compound} {thing} {# removed} {unbound form} {int -42}}";
let input = Text::from_str(input);
let parse_iter = parse_text_with(&input, &mut parser);
for result in parse_iter {
dbg!(&result);
}
}
fn main() {
no_extension();
with_extensions();
}
The above example can be run by doing:
cargo run -p kul_core --example stack_only
Documentation
The source-code has many doc comments, which are rendered as the API
documentation.
View online at: http://docs.rs/kul_core
Or, you can generate them yourself and view locally by doing:
cargo doc -p kul_core --open