Starting the rust-based parser of dust templates

This commit is contained in:
Tom Alexander 2020-04-04 20:16:44 -04:00
parent d3b58c9a0e
commit 9b3084856b
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 55 additions and 0 deletions

View File

@ -1,6 +1,7 @@
//! This module contains a rust implementation of LinkedIn Dust //! This module contains a rust implementation of LinkedIn Dust
mod node_invoker; mod node_invoker;
mod parser;
pub use node_invoker::run_node_dust; pub use node_invoker::run_node_dust;
pub use node_invoker::NodeError; pub use node_invoker::NodeError;

54
src/duster/parser.rs Normal file
View File

@ -0,0 +1,54 @@
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::take_until;
use nom::combinator::map;
use nom::combinator::value;
use nom::sequence::delimited;
use nom::IResult;
#[derive(Clone, Debug)]
enum DustTag<'a> {
DTSpecial(Special),
DTComment(Comment<'a>),
}
#[derive(Clone, Debug)]
enum Special {
Space,
NewLine,
CarriageReturn,
LeftCurlyBrace,
RightCurlyBrace,
}
#[derive(Clone, Debug)]
struct Comment<'a> {
value: &'a str,
}
fn dust_tag(i: &str) -> IResult<&str, DustTag> {
alt((
map(special, DustTag::DTSpecial),
map(comment, DustTag::DTComment),
))(i)
}
fn special(i: &str) -> IResult<&str, Special> {
delimited(
tag("{~"),
alt((
value(Special::Space, tag("s")),
value(Special::NewLine, tag("s")),
value(Special::CarriageReturn, tag("s")),
value(Special::LeftCurlyBrace, tag("s")),
value(Special::RightCurlyBrace, tag("s")),
)),
tag("}"),
)(i)
}
fn comment(i: &str) -> IResult<&str, Comment> {
map(delimited(tag("{!"), take_until("!}"), tag("!}")), |body| {
Comment { value: body }
})(i)
}