Initial Commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
||||
316
Cargo.lock
generated
Normal file
316
Cargo.lock
generated
Normal file
@ -0,0 +1,316 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "ansi_term"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||
|
||||
[[package]]
|
||||
name = "bitvec"
|
||||
version = "0.19.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8942c8d352ae1838c9dda0b0ca2ab657696ef2232a20147cf1b30ae1a9cb4321"
|
||||
dependencies = [
|
||||
"funty",
|
||||
"radium",
|
||||
"tap",
|
||||
"wyz",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "blueprint"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"nom",
|
||||
"structopt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "2.33.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
|
||||
dependencies = [
|
||||
"ansi_term",
|
||||
"atty",
|
||||
"bitflags",
|
||||
"strsim",
|
||||
"textwrap",
|
||||
"unicode-width",
|
||||
"vec_map",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "funty"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac"
|
||||
dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "lexical-core"
|
||||
version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bitflags",
|
||||
"cfg-if",
|
||||
"ryu",
|
||||
"static_assertions",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.94"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "6.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7413f999671bd4745a7b624bd370a569fb6bc574b23c83a3c5ed2e453f3d5e2"
|
||||
dependencies = [
|
||||
"bitvec",
|
||||
"funty",
|
||||
"lexical-core",
|
||||
"memchr",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
||||
dependencies = [
|
||||
"proc-macro-error-attr",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error-attr"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "radium"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
|
||||
|
||||
[[package]]
|
||||
name = "static_assertions"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||
|
||||
[[package]]
|
||||
name = "structopt"
|
||||
version = "0.3.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5277acd7ee46e63e5168a80734c9f6ee81b1367a7d8772a2d765df2a3705d28c"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"lazy_static",
|
||||
"structopt-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "structopt-derive"
|
||||
version = "0.4.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ba9cdfda491b814720b6b06e0cac513d922fc407582032e8706e9f137976f90"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.72"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tap"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
|
||||
dependencies = [
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
||||
[[package]]
|
||||
name = "vec_map"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "wyz"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214"
|
||||
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "blueprint"
|
||||
version = "0.1.0"
|
||||
authors = ["Joakim Hulthe <joakim@hulthe.net>"]
|
||||
description = "A simple templating library"
|
||||
edition = "2018"
|
||||
|
||||
|
||||
[dependencies]
|
||||
nom = "6.1.2"
|
||||
structopt = "0.3.21"
|
||||
137
src/ast.rs
Normal file
137
src/ast.rs
Normal file
@ -0,0 +1,137 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Template<'a> {
|
||||
pub parts: Vec<Part<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct IfThen<'a> {
|
||||
pub cond: Expr<'a>,
|
||||
pub parts: Vec<Part<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Part<'a> {
|
||||
Text(Cow<'a, str>),
|
||||
If {
|
||||
if_then: IfThen<'a>,
|
||||
elifs: Vec<IfThen<'a>>,
|
||||
else_part: Option<Box<Part<'a>>>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum CmpOp {
|
||||
Eq,
|
||||
NEq,
|
||||
GrEq,
|
||||
LeEq,
|
||||
GrTh,
|
||||
LeTh,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Expr<'a> {
|
||||
Cmp {
|
||||
op: CmpOp,
|
||||
expr1: Box<Expr<'a>>,
|
||||
expr2: Box<Expr<'a>>,
|
||||
},
|
||||
|
||||
Variable(Cow<'a, str>),
|
||||
}
|
||||
|
||||
use crate::env::{Env, Value};
|
||||
use std::io::{self, Write};
|
||||
|
||||
impl Template<'_> {
|
||||
pub fn write(&self, env: &Env, w: &mut impl Write) -> io::Result<()> {
|
||||
for part in &self.parts {
|
||||
part.write(env, w)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Part<'_> {
|
||||
pub fn write(&self, env: &Env, w: &mut impl Write) -> io::Result<()> {
|
||||
match self {
|
||||
Part::Text(text) => w.write_all(text.as_bytes())?,
|
||||
Part::If {
|
||||
if_then,
|
||||
elifs,
|
||||
else_part,
|
||||
} => {
|
||||
if if_then.write(env, w)? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
for elif in elifs {
|
||||
if elif.write(env, w)? {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(else_part) = else_part {
|
||||
else_part.write(env, w)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl IfThen<'_> {
|
||||
pub fn write(&self, env: &Env, w: &mut impl Write) -> io::Result<bool> {
|
||||
let cond = self.cond.eval(env);
|
||||
let cond = match &*cond {
|
||||
&Value::Bool(cond) => cond,
|
||||
Value::Str(_) => true,
|
||||
Value::Null => false,
|
||||
};
|
||||
|
||||
if cond {
|
||||
for part in &self.parts {
|
||||
part.write(env, w)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(cond)
|
||||
}
|
||||
}
|
||||
|
||||
impl Expr<'_> {
|
||||
pub fn eval<'a>(&self, env: &'a Env) -> Cow<'a, Value> {
|
||||
match self {
|
||||
Expr::Variable(var) => Cow::Borrowed(env.get(&**var).unwrap_or(&Value::Null)),
|
||||
Expr::Cmp { op, expr1, expr2 } => {
|
||||
let val1 = expr1.eval(env);
|
||||
let val2 = expr2.eval(env);
|
||||
|
||||
let result = match (&*val1, &*val2) {
|
||||
(_, Value::Null) | (Value::Null, _) => false,
|
||||
(Value::Bool(b1), Value::Bool(b2)) => match op {
|
||||
CmpOp::Eq => b1 == b2,
|
||||
CmpOp::NEq => b1 != b2,
|
||||
_ => panic!("bools cannot be ordered"),
|
||||
},
|
||||
(Value::Str(s1), Value::Str(s2)) => match op {
|
||||
CmpOp::Eq => s1 == s2,
|
||||
CmpOp::NEq => s1 != s2,
|
||||
CmpOp::GrEq => s1 >= s2,
|
||||
CmpOp::LeEq => s1 <= s2,
|
||||
CmpOp::GrTh => s1 > s2,
|
||||
CmpOp::LeTh => s1 < s2,
|
||||
},
|
||||
_ => panic!(
|
||||
"invalid {:?}, {:?} and {:?} can't be compared",
|
||||
op, val1, val2
|
||||
),
|
||||
};
|
||||
|
||||
Cow::Owned(Value::Bool(result))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
10
src/env.rs
Normal file
10
src/env.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub type Env = HashMap<String, Value>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Value {
|
||||
Str(String),
|
||||
Bool(bool),
|
||||
Null,
|
||||
}
|
||||
146
src/lib.rs
Normal file
146
src/lib.rs
Normal file
@ -0,0 +1,146 @@
|
||||
mod ast;
|
||||
mod env;
|
||||
mod parser;
|
||||
|
||||
pub use ast::{CmpOp, Expr, IfThen, Part, Template};
|
||||
pub use env::{Env, Value};
|
||||
pub use parser::parse_file;
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse_empty() {
|
||||
let input = "";
|
||||
let ast = parse_file(&input).expect("failed to parse");
|
||||
eprintln!("{:#?}", ast);
|
||||
assert_eq!(ast, Template { parts: vec![] });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_text() {
|
||||
let input = "hello there GENERAL KENOBI";
|
||||
let ast = parse_file(&input).expect("failed to parse");
|
||||
eprintln!("{:#?}", ast);
|
||||
assert_eq!(
|
||||
ast,
|
||||
Template {
|
||||
parts: vec![Part::Text(input.into())]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_if() {
|
||||
let input = "one {% if foo %} two {% end %} three";
|
||||
let ast = parse_file(&input).expect("failed to parse");
|
||||
eprintln!("{:#?}", ast);
|
||||
assert_eq!(
|
||||
ast,
|
||||
Template {
|
||||
parts: vec![
|
||||
Part::Text("one ".into()),
|
||||
Part::If {
|
||||
if_then: IfThen {
|
||||
cond: Expr::Variable("foo".into()),
|
||||
parts: vec![Part::Text(" two ".into()),]
|
||||
},
|
||||
elifs: vec![],
|
||||
else_part: None,
|
||||
},
|
||||
Part::Text(" three".into()),
|
||||
],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_eq() {
|
||||
let input = "one {% if foo == bar %} two {% end %} three";
|
||||
let ast = parse_file(&input).expect("failed to parse");
|
||||
eprintln!("{:#?}", ast);
|
||||
assert_eq!(
|
||||
ast,
|
||||
Template {
|
||||
parts: vec![
|
||||
Part::Text("one ".into()),
|
||||
Part::If {
|
||||
if_then: IfThen {
|
||||
cond: Expr::Cmp {
|
||||
op: CmpOp::Eq,
|
||||
expr1: Box::new(Expr::Variable("foo".into())),
|
||||
expr2: Box::new(Expr::Variable("bar".into())),
|
||||
},
|
||||
parts: vec![Part::Text(" two ".into()),]
|
||||
},
|
||||
elifs: vec![],
|
||||
else_part: None,
|
||||
},
|
||||
Part::Text(" three".into()),
|
||||
],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_if_elif_else() {
|
||||
let input = "one {% if foo %} two {% elif bar %} three {% else %} four {% end %} five";
|
||||
let ast = parse_file(&input).expect("failed to parse");
|
||||
eprintln!("{:#?}", ast);
|
||||
assert_eq!(
|
||||
ast,
|
||||
Template {
|
||||
parts: vec![
|
||||
Part::Text("one ".into()),
|
||||
Part::If {
|
||||
if_then: IfThen {
|
||||
cond: Expr::Variable("foo".into()),
|
||||
parts: vec![Part::Text(" two ".into()),]
|
||||
},
|
||||
elifs: vec![IfThen {
|
||||
cond: Expr::Variable("bar".into()),
|
||||
parts: vec![Part::Text(" three ".into()),]
|
||||
}],
|
||||
else_part: Some(Box::new(Part::Text(" four ".into()))),
|
||||
},
|
||||
Part::Text(" five".into()),
|
||||
],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_nested() {
|
||||
let input = "one {% if foo %} two {% if bar %} three {% end %} four {% end %} five";
|
||||
let ast = parse_file(&input).expect("failed to parse");
|
||||
eprintln!("{:#?}", ast);
|
||||
let expected = Template {
|
||||
parts: vec![
|
||||
Part::Text("one ".into()),
|
||||
Part::If {
|
||||
if_then: IfThen {
|
||||
cond: Expr::Variable("foo".into()),
|
||||
parts: vec![
|
||||
Part::Text(" two ".into()),
|
||||
Part::If {
|
||||
if_then: IfThen {
|
||||
cond: Expr::Variable("bar".into()),
|
||||
parts: vec![Part::Text(" three ".into())],
|
||||
},
|
||||
elifs: vec![],
|
||||
else_part: None,
|
||||
},
|
||||
Part::Text(" four ".into()),
|
||||
],
|
||||
},
|
||||
elifs: vec![],
|
||||
else_part: None,
|
||||
},
|
||||
Part::Text(" five".into()),
|
||||
],
|
||||
};
|
||||
|
||||
assert_eq!(ast, expected);
|
||||
}
|
||||
}
|
||||
27
src/main.rs
Normal file
27
src/main.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use blueprint::Value;
|
||||
use std::fs::read_to_string;
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(StructOpt)]
|
||||
struct Opt {
|
||||
template: PathBuf,
|
||||
env: Vec<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let opt = Opt::from_args();
|
||||
let file = read_to_string(&opt.template).expect("failed to load file");
|
||||
let template = blueprint::parse_file(&file).expect("failed to parse");
|
||||
let env = opt
|
||||
.env
|
||||
.into_iter()
|
||||
.map(|key| (key, Value::Str("".into())))
|
||||
.collect();
|
||||
|
||||
let mut stdout = io::stdout();
|
||||
template
|
||||
.write(&env, &mut stdout)
|
||||
.expect("failed to write output");
|
||||
}
|
||||
172
src/parser.rs
Normal file
172
src/parser.rs
Normal file
@ -0,0 +1,172 @@
|
||||
use crate::ast::*;
|
||||
use nom::{
|
||||
branch::alt,
|
||||
bytes::complete::{tag, take_until},
|
||||
character::complete::{alpha1, multispace0, multispace1},
|
||||
combinator::{map, opt},
|
||||
multi::many0,
|
||||
sequence::tuple,
|
||||
IResult,
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// Try to parse an Expr::Variable
|
||||
fn expr_var(i: &str) -> IResult<&str, Expr> {
|
||||
//eprintln!("expr_var \"{}\"", i);
|
||||
// TODO: allow numbers?
|
||||
map(alpha1, |v| Expr::Variable(Cow::Borrowed(v)))(i)
|
||||
}
|
||||
|
||||
fn expr1(i: &str) -> IResult<&str, Expr> {
|
||||
expr_var(i)
|
||||
}
|
||||
|
||||
fn cmp_op(i: &str) -> IResult<&str, CmpOp> {
|
||||
//eprintln!("cmp_op \"{}\"", i);
|
||||
alt((
|
||||
map(tag("=="), |_| CmpOp::Eq),
|
||||
map(tag("!="), |_| CmpOp::NEq),
|
||||
map(tag(">="), |_| CmpOp::GrEq),
|
||||
map(tag("<="), |_| CmpOp::LeEq),
|
||||
map(tag("<"), |_| CmpOp::LeTh),
|
||||
map(tag(">"), |_| CmpOp::GrTh),
|
||||
))(i)
|
||||
}
|
||||
|
||||
fn expr_cmp(i: &str) -> IResult<&str, Expr> {
|
||||
//eprintln!("expr_cmp \"{}\"", i);
|
||||
let (i, e1) = expr1(i)?;
|
||||
let (i, _) = multispace0(i)?;
|
||||
let (i, op) = cmp_op(i)?;
|
||||
let (i, _) = multispace0(i)?;
|
||||
let (i, e2) = expr(i)?;
|
||||
|
||||
let expr = Expr::Cmp {
|
||||
op,
|
||||
expr1: Box::new(e1),
|
||||
expr2: Box::new(e2),
|
||||
};
|
||||
|
||||
Ok((i, expr))
|
||||
}
|
||||
|
||||
fn expr(i: &str) -> IResult<&str, Expr> {
|
||||
//eprintln!("expr \"{}\"", i);
|
||||
let (i, _) = multispace0(i)?;
|
||||
|
||||
let (i, expr) = alt((expr_cmp, expr1))(i)?;
|
||||
|
||||
let (i, _) = multispace0(i)?;
|
||||
|
||||
Ok((i, expr))
|
||||
}
|
||||
|
||||
fn part(i: &str) -> IResult<&str, Part> {
|
||||
//eprintln!("part \"{}\"", i);
|
||||
alt((if_part, text_part))(i)
|
||||
}
|
||||
|
||||
fn parts(i: &str) -> IResult<&str, Vec<Part>> {
|
||||
//eprintln!("partS \"{}\"", i);
|
||||
many0(part)(i)
|
||||
}
|
||||
|
||||
fn text_part(i: &str) -> IResult<&str, Part> {
|
||||
//eprintln!("text_part 1 \"{}\"", i);
|
||||
let r: IResult<_, _> = take_until("{%")(i);
|
||||
//eprintln!("text_part 2 \"{}\"", i);
|
||||
let (i, o) = r.unwrap_or(("", i));
|
||||
//eprintln!("text_part 3 \"{}\"", i);
|
||||
if o == "" {
|
||||
//eprintln!("text_part 4A \"{}\"", i);
|
||||
Err(nom::Err::Error(nom::error::Error::new(
|
||||
i,
|
||||
nom::error::ErrorKind::TakeUntil,
|
||||
)))
|
||||
} else {
|
||||
//eprintln!("text_part 4B \"{}\"", i);
|
||||
Ok((i, Part::Text(Cow::Borrowed(o))))
|
||||
}
|
||||
}
|
||||
|
||||
fn if_part(i: &str) -> IResult<&str, Part> {
|
||||
//eprintln!("if_part 1 \"{}\"", i);
|
||||
let (i, if_cond) = if_block(i)?;
|
||||
//eprintln!("if_part 2 \"{}\"", i);
|
||||
let (i, if_parts) = parts(i)?;
|
||||
//eprintln!("if_part 3 \"{}\"", i);
|
||||
let (i, elifs) = many0(map(tuple((elif_block, parts)), |(cond, parts)| IfThen {
|
||||
cond,
|
||||
parts,
|
||||
}))(i)?;
|
||||
//eprintln!("if_part 4 \"{}\"", i);
|
||||
let (i, el) = opt(tuple((block("else"), part)))(i)?;
|
||||
//eprintln!("if_part 5 \"{}\"", i);
|
||||
let (i, _) = block("end")(i)?;
|
||||
//eprintln!("if_part 6 \"{}\"", i);
|
||||
|
||||
let if_then = IfThen {
|
||||
cond: if_cond,
|
||||
parts: if_parts,
|
||||
};
|
||||
|
||||
let else_part = el.map(|(_, part)| Box::new(part));
|
||||
|
||||
Ok((
|
||||
i,
|
||||
Part::If {
|
||||
if_then,
|
||||
elifs,
|
||||
else_part,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
fn if_block(i: &str) -> IResult<&str, Expr> {
|
||||
//eprintln!("if_block \"{}\"", i);
|
||||
let (i, _) = tag("{%")(i)?;
|
||||
let (i, _) = multispace1(i)?;
|
||||
let (i, _) = tag("if")(i)?;
|
||||
let (i, expr) = expr(i)?;
|
||||
let (i, _) = tag("%}")(i)?;
|
||||
Ok((i, expr))
|
||||
}
|
||||
|
||||
fn elif_block(i: &str) -> IResult<&str, Expr> {
|
||||
//eprintln!("elif_block \"{}\"", i);
|
||||
let (i, _) = tag("{%")(i)?;
|
||||
let (i, _) = multispace1(i)?;
|
||||
let (i, _) = tag("elif")(i)?;
|
||||
let (i, expr) = expr(i)?;
|
||||
let (i, _) = tag("%}")(i)?;
|
||||
Ok((i, expr))
|
||||
}
|
||||
|
||||
fn block(word: &'static str) -> impl Fn(&str) -> IResult<&str, ()> {
|
||||
move |i: &str| {
|
||||
//eprintln!("{}_block \"{}\"", word, i);
|
||||
let (i, _) = tag("{%")(i)?;
|
||||
let (i, _) = multispace1(i)?;
|
||||
let (i, _) = tag(word)(i)?;
|
||||
let (i, _) = multispace1(i)?;
|
||||
let (i, _) = tag("%}")(i)?;
|
||||
Ok((i, ()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_file(file: &str) -> Result<Template, ()> {
|
||||
match parts(file) {
|
||||
Ok((rest, parts)) => {
|
||||
//eprintln!("{:#?}", parts);
|
||||
|
||||
if rest != "" {
|
||||
panic!("failed to parse input. remainder: \"{}\"", rest);
|
||||
}
|
||||
|
||||
Ok(Template { parts })
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("{:#?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user