Compare commits
No commits in common. "6e0a7375c0898ee23f53d0e76d42e0b9b09badc3" and "323a38db3a20c342bf740035a9f69b8881954cf6" have entirely different histories.
6e0a7375c0
...
323a38db3a
|
|
@ -2,46 +2,30 @@ use core::fmt::Write;
|
||||||
|
|
||||||
use crate::Value;
|
use crate::Value;
|
||||||
|
|
||||||
use super::parse::{Ast, AstArena, AstNodeKind, For, Has, If, Path, Print};
|
use super::parse::{Ast, AstKind, For, Has, If, Print, ValuePath};
|
||||||
|
|
||||||
pub fn execute(ast: &Ast, value: &dyn Value) -> String {
|
pub fn execute(ast: &Ast, value: &dyn Value) -> String {
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
let nodes = ast.arena.deref_many(&ast.root);
|
execute_(&mut out, ast, value);
|
||||||
execute_(&mut out, nodes, value, &ast.arena);
|
|
||||||
|
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execute_(
|
pub fn execute_(out: &mut String, ast: &Ast, value: &dyn Value) {
|
||||||
out: &mut String,
|
for node in ast {
|
||||||
nodes: &[AstNodeKind],
|
match node {
|
||||||
value: &dyn Value,
|
AstKind::Text(t) => out.push_str(&t),
|
||||||
arena: &AstArena,
|
AstKind::Print(p) => print(out, p, value),
|
||||||
) {
|
AstKind::For(f) => for_(out, f, value),
|
||||||
for node in nodes {
|
AstKind::Has(h) => has(out, h, value),
|
||||||
execute_one(out, node, value, arena)
|
AstKind::If(h) => if_(out, h, value),
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn execute_one(
|
|
||||||
out: &mut String,
|
|
||||||
node: &AstNodeKind,
|
|
||||||
value: &dyn Value,
|
|
||||||
arena: &AstArena,
|
|
||||||
) {
|
|
||||||
match node {
|
|
||||||
AstNodeKind::Text(t) => out.push_str(&t),
|
|
||||||
AstNodeKind::Print(p) => print(out, p, value),
|
|
||||||
AstNodeKind::For(f) => for_(out, f, value, arena),
|
|
||||||
AstNodeKind::Has(h) => has(out, h, value, arena),
|
|
||||||
AstNodeKind::With(_) => todo!(),
|
|
||||||
AstNodeKind::If(h) => if_(out, h, value, arena),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup<'a>(
|
pub fn lookup<'a>(
|
||||||
out: &mut String,
|
out: &mut String,
|
||||||
path: &Path,
|
path: &ValuePath,
|
||||||
value: &'a dyn Value,
|
value: &'a dyn Value,
|
||||||
) -> Option<&'a dyn Value> {
|
) -> Option<&'a dyn Value> {
|
||||||
let mut value = value;
|
let mut value = value;
|
||||||
|
|
@ -60,7 +44,7 @@ pub fn lookup<'a>(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print(out: &mut String, p: &Print, value: &dyn Value) {
|
pub fn print(out: &mut String, p: &Print, value: &dyn Value) {
|
||||||
let value = match lookup(out, &p.path, value) {
|
let value = match lookup(out, &p.0, value) {
|
||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
@ -68,43 +52,39 @@ pub fn print(out: &mut String, p: &Print, value: &dyn Value) {
|
||||||
value.print(out)
|
value.print(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn for_(out: &mut String, f: &For, value: &dyn Value, arena: &AstArena) {
|
pub fn for_(out: &mut String, f: &For, value: &dyn Value) {
|
||||||
let value = match lookup(out, &f.path, value) {
|
let value = match lookup(out, &f.expr, value) {
|
||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut idx = 0;
|
let mut idx = 0;
|
||||||
while let Some(value) = value.for_(idx) {
|
while let Some(value) = value.for_(idx) {
|
||||||
let nodes = arena.deref_many(&f.body);
|
execute_(out, &f.body, value);
|
||||||
execute_(out, nodes, value, arena);
|
|
||||||
idx += 1;
|
idx += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has(out: &mut String, f: &Has, value: &dyn Value, arena: &AstArena) {
|
pub fn has(out: &mut String, f: &Has, value: &dyn Value) {
|
||||||
let value = match lookup(out, &f.path, value) {
|
let value = match lookup(out, &f.expr, value) {
|
||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(value) = value.has() {
|
if let Some(value) = value.has() {
|
||||||
let nodes = arena.deref_many(&f.then);
|
execute_(out, &f.body, value);
|
||||||
execute_(out, nodes, value, arena);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn if_(out: &mut String, f: &If, value: &dyn Value, arena: &AstArena) {
|
pub fn if_(out: &mut String, f: &If, value: &dyn Value) {
|
||||||
let value = match lookup(out, &f.path, value) {
|
let value = match lookup(out, &f.expr, value) {
|
||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
if value.if_() {
|
if value.if_() {
|
||||||
let nodes = arena.deref_many(&f.then);
|
execute_(out, &f.then, value);
|
||||||
execute_(out, nodes, value, arena);
|
|
||||||
} else if let Some(else_) = f.else_.as_ref() {
|
} else if let Some(else_) = f.else_.as_ref() {
|
||||||
let nodes = arena.deref_many(&else_);
|
execute_(out, else_, value);
|
||||||
execute_(out, nodes, value, arena);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,4 +4,3 @@
|
||||||
|
|
||||||
pub mod execute;
|
pub mod execute;
|
||||||
pub mod parse;
|
pub mod parse;
|
||||||
mod parse_backup;
|
|
||||||
|
|
|
||||||
|
|
@ -1,546 +1,457 @@
|
||||||
use std::ops::ControlFlow;
|
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
|
pub struct ValuePath(pub Vec<String>);
|
||||||
|
|
||||||
pub use super::parse_backup::*;
|
pub type Expr = ValuePath;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
pub enum AstNodeKind {
|
pub struct If {
|
||||||
If(If),
|
pub expr: Expr,
|
||||||
For(For),
|
pub then: Ast,
|
||||||
Has(Has),
|
pub else_: Option<Ast>,
|
||||||
With(With),
|
|
||||||
Print(Print),
|
|
||||||
Text(String),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
pub struct Path(pub Vec<String>);
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
||||||
pub struct If {
|
|
||||||
pub path: Path,
|
|
||||||
pub then: Vec<AstNodeKind>,
|
|
||||||
pub else_: Option<Vec<AstNodeKind>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
||||||
pub struct For {
|
pub struct For {
|
||||||
pub path: Path,
|
pub expr: Expr,
|
||||||
pub body: Vec<AstNodeKind>,
|
pub body: Ast,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
pub struct Has {
|
pub struct Has {
|
||||||
pub path: Path,
|
pub expr: Expr,
|
||||||
pub then: Vec<AstNodeKind>,
|
pub body: Ast,
|
||||||
pub else_: Option<Vec<AstNodeKind>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
pub struct With {
|
pub struct Print(pub Expr);
|
||||||
pub path: Path,
|
|
||||||
pub body: Vec<AstNodeKind>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
pub struct Print {
|
pub enum AstKind {
|
||||||
pub path: Path,
|
Text(String),
|
||||||
|
Print(Print),
|
||||||
|
Has(Has),
|
||||||
|
If(If),
|
||||||
|
For(For),
|
||||||
}
|
}
|
||||||
|
pub type Ast = Vec<AstKind>;
|
||||||
|
|
||||||
pub fn parse(source: &str) -> Vec<AstNodeKind> {
|
pub struct ParseState<'a> {
|
||||||
parse_many_until_eof(source).1
|
template: &'a str,
|
||||||
|
last_byte_offset: usize,
|
||||||
|
speculative_text_starts_at: usize,
|
||||||
|
speculative_text_ends_at: usize,
|
||||||
|
current_byte_offset: usize,
|
||||||
|
ast: Ast,
|
||||||
}
|
}
|
||||||
|
impl<'a> ParseState<'a> {
|
||||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
pub fn out_of_input(&self) -> bool {
|
||||||
struct ParseResult<'a, T>(
|
self.current_byte_offset >= self.last_byte_offset
|
||||||
&'a str, // remaining
|
|
||||||
T, // output
|
|
||||||
);
|
|
||||||
impl<'a, T> ParseResult<'a, T> {
|
|
||||||
fn remaining(&self) -> &'a str {
|
|
||||||
self.0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn output(&self) -> &T {
|
pub fn has_input(&self) -> bool {
|
||||||
&self.1
|
!self.out_of_input()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map<U>(self, f: impl FnOnce(T) -> U) -> ParseResult<'a, U> {
|
pub fn remaining(&self) -> &str {
|
||||||
ParseResult(self.0, f(self.1))
|
&self.template[self.current_byte_offset..]
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_block(rest: &str) -> ParseResult<Option<AstNodeKind>> {
|
pub fn add_current_text_to_ast(&mut self) {
|
||||||
let rest_ = match rest.strip_prefix('{') {
|
let text = &self.template
|
||||||
Some(v) => v,
|
[self.speculative_text_starts_at..self.speculative_text_ends_at];
|
||||||
None => return ParseResult(rest, None),
|
if text.is_empty() {
|
||||||
};
|
return;
|
||||||
let rest_ = rest_.trim_start();
|
}
|
||||||
if rest_.starts_with("for ") {
|
|
||||||
parse_for(rest).map(|v| v.map(AstNodeKind::For))
|
self.ast.push(AstKind::Text(
|
||||||
} else if rest_.starts_with("if ") {
|
self.template[self.speculative_text_starts_at
|
||||||
parse_if(rest).map(|v| v.map(AstNodeKind::If))
|
..self.speculative_text_ends_at]
|
||||||
} else if rest_.starts_with("has ") {
|
.to_string(),
|
||||||
parse_has(rest).map(|v| v.map(AstNodeKind::Has))
|
));
|
||||||
} else if rest_.starts_with("with ") {
|
|
||||||
parse_with(rest).map(|v| v.map(AstNodeKind::With))
|
|
||||||
} else {
|
|
||||||
parse_print(rest).map(|v| v.map(AstNodeKind::Print))
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
pub fn text_might_end(&mut self) {
|
||||||
fn test_parse_block_print_simple() {
|
self.speculative_text_ends_at = self.current_byte_offset;
|
||||||
assert_eq!(
|
|
||||||
parse_block("{.}"),
|
|
||||||
ParseResult("", Some(AstNodeKind::Print(Print { path: Path(vec![]) })))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_block_print_simple_trailing() {
|
|
||||||
assert_eq!(
|
|
||||||
parse_block("{.} Text"),
|
|
||||||
ParseResult(
|
|
||||||
" Text",
|
|
||||||
Some(AstNodeKind::Print(Print { path: Path(vec![]) }))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_print(rest: &str) -> ParseResult<Option<Print>> {
|
|
||||||
let rest = match rest.strip_prefix('{') {
|
|
||||||
Some(r) => r,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
let rest = rest.trim_start();
|
|
||||||
|
|
||||||
let ParseResult(rest, path) = parse_path(rest);
|
|
||||||
let path = match path {
|
|
||||||
Some(p) => p,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
|
|
||||||
let rest = rest.trim_start();
|
|
||||||
if let Some(rest) = rest.strip_prefix('}') {
|
|
||||||
ParseResult(rest, Some(Print { path }))
|
|
||||||
} else {
|
|
||||||
ParseResult(rest, None)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
pub fn parse(&mut self) {
|
||||||
fn test_parse_print_trailing() {
|
while self.has_input() {
|
||||||
assert_eq!(
|
self.parse_one();
|
||||||
parse_print("{.} World"),
|
}
|
||||||
ParseResult(" World", Some(Print { path: Path(vec![]) }))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_for(rest: &str) -> ParseResult<Option<For>> {
|
self.text_might_end();
|
||||||
let rest = match rest.strip_prefix('{') {
|
self.add_current_text_to_ast();
|
||||||
Some(r) => r,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
let rest = rest.trim_start();
|
|
||||||
|
|
||||||
let rest = match rest.strip_prefix("for ") {
|
|
||||||
Some(r) => r,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
|
|
||||||
let ParseResult(rest, path) = parse_path(rest);
|
|
||||||
let path = match path {
|
|
||||||
Some(p) => p,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
|
|
||||||
let rest = rest.trim_start();
|
|
||||||
if let Some(rest) = rest.strip_prefix('}') {
|
|
||||||
let ParseResult(rest, body) =
|
|
||||||
parse_many_until(rest, |s| s.starts_with("{/for}"));
|
|
||||||
let body = match body {
|
|
||||||
Some(b) => b,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
let rest = rest.strip_prefix("{/for}").unwrap();
|
|
||||||
|
|
||||||
ParseResult(rest, Some(For { path, body }))
|
|
||||||
} else {
|
|
||||||
ParseResult(rest, None)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
pub fn parse_one(&mut self) {
|
||||||
fn test_parse_for_basic() {
|
assert!(self.has_input());
|
||||||
assert_eq!(
|
let started_at = self.current_byte_offset;
|
||||||
parse_for("{for .}{.}{/for}"),
|
if self.remaining().starts_with('{') {
|
||||||
ParseResult(
|
self.text_might_end();
|
||||||
"",
|
self.maybe_parse_block();
|
||||||
Some(For {
|
} else {
|
||||||
path: Path(vec![]),
|
self.current_byte_offset += 1;
|
||||||
body: vec![AstNodeKind::Print(Print { path: Path(vec![]) })]
|
}
|
||||||
})
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_with(rest: &str) -> ParseResult<Option<With>> {
|
if started_at == self.current_byte_offset {
|
||||||
let rest = match rest.strip_prefix('{') {
|
panic!("Parser made no progress");
|
||||||
Some(r) => r,
|
}
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
let rest = rest.trim_start();
|
|
||||||
|
|
||||||
let rest = match rest.strip_prefix("with ") {
|
|
||||||
Some(r) => r,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
|
|
||||||
let ParseResult(rest, path) = parse_path(rest);
|
|
||||||
let path = match path {
|
|
||||||
Some(p) => p,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
|
|
||||||
let rest = rest.trim_start();
|
|
||||||
if let Some(rest) = rest.strip_prefix('}') {
|
|
||||||
let ParseResult(rest, body) =
|
|
||||||
parse_many_until(rest, |s| s.starts_with("{/with}"));
|
|
||||||
let body = match body {
|
|
||||||
Some(b) => b,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
let rest = rest.strip_prefix("{/with}").unwrap();
|
|
||||||
|
|
||||||
ParseResult(rest, Some(With { path, body }))
|
|
||||||
} else {
|
|
||||||
ParseResult(rest, None)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
pub fn maybe_parse_block(&mut self) {
|
||||||
fn test_parse_with_basic() {
|
assert!(self.remaining().starts_with('{'));
|
||||||
assert_eq!(
|
self.current_byte_offset += 1;
|
||||||
parse_with("{with .}{.}{/with}"),
|
self.skip_whitespace();
|
||||||
ParseResult(
|
|
||||||
"",
|
|
||||||
Some(With {
|
|
||||||
path: Path(vec![]),
|
|
||||||
body: vec![AstNodeKind::Print(Print { path: Path(vec![]) })]
|
|
||||||
})
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_if(rest: &str) -> ParseResult<Option<If>> {
|
let remaining = self.remaining();
|
||||||
let rest = match rest.strip_prefix('{') {
|
if remaining.starts_with('.') {
|
||||||
Some(r) => r,
|
self.maybe_parse_print();
|
||||||
None => return ParseResult(rest, None),
|
} else if remaining.starts_with("for") {
|
||||||
};
|
self.maybe_parse_for();
|
||||||
let rest = rest.trim_start();
|
} else if remaining.starts_with("has") {
|
||||||
|
self.maybe_parse_has();
|
||||||
|
} else if remaining.starts_with("if") {
|
||||||
|
self.maybe_parse_if();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let rest = match rest.strip_prefix("if ") {
|
pub fn maybe_parse_print(&mut self) {
|
||||||
Some(r) => r,
|
assert!(self.remaining().starts_with('.'));
|
||||||
None => return ParseResult(rest, None),
|
let path = match self.parse_path() {
|
||||||
};
|
None => return,
|
||||||
|
Some(vp) => vp,
|
||||||
let ParseResult(rest, path) = parse_path(rest);
|
|
||||||
let path = match path {
|
|
||||||
Some(p) => p,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
|
|
||||||
let rest = rest.trim_start();
|
|
||||||
if let Some(rest) = rest.strip_prefix('}') {
|
|
||||||
let ParseResult(rest, body) = parse_many_until(rest, |s| {
|
|
||||||
s.starts_with("{/if}") || s.starts_with("{else}")
|
|
||||||
});
|
|
||||||
let then = match body {
|
|
||||||
Some(b) => b,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let ParseResult(rest, else_) =
|
self.skip_whitespace();
|
||||||
if let Some(rest) = rest.strip_prefix("{else}") {
|
if !self.remaining().starts_with('}') {
|
||||||
let ParseResult(rest, else_) =
|
return;
|
||||||
parse_many_until(rest, |s| s.starts_with("{/if}"));
|
}
|
||||||
let else_ = match else_ {
|
self.current_byte_offset += 1;
|
||||||
Some(b) => b,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
let rest = rest.strip_prefix("{/if}").unwrap();
|
|
||||||
|
|
||||||
ParseResult(rest, Some(else_))
|
self.push_node_and_text(AstKind::Print(Print(path)));
|
||||||
} else {
|
self.new_empty_text();
|
||||||
let rest = rest.strip_prefix("{/if}").unwrap();
|
}
|
||||||
|
|
||||||
ParseResult(rest, None)
|
pub fn maybe_parse_for(&mut self) {
|
||||||
|
assert!(self.remaining().starts_with("for"));
|
||||||
|
self.current_byte_offset += "for".len();
|
||||||
|
self.skip_whitespace();
|
||||||
|
let path = match self.parse_path() {
|
||||||
|
None => return,
|
||||||
|
Some(vp) => vp,
|
||||||
|
};
|
||||||
|
self.skip_whitespace();
|
||||||
|
if !self.remaining().starts_with('}') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.current_byte_offset += 1;
|
||||||
|
|
||||||
|
let mut inner = ParseState {
|
||||||
|
template: self.template,
|
||||||
|
last_byte_offset: self.last_byte_offset,
|
||||||
|
speculative_text_starts_at: self.current_byte_offset,
|
||||||
|
speculative_text_ends_at: self.current_byte_offset,
|
||||||
|
current_byte_offset: self.current_byte_offset,
|
||||||
|
ast: Ast::new(),
|
||||||
|
};
|
||||||
|
while inner.has_input() {
|
||||||
|
if inner.remaining().starts_with("{/for}") {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
inner.parse_one();
|
||||||
|
}
|
||||||
|
inner.text_might_end();
|
||||||
|
inner.add_current_text_to_ast();
|
||||||
|
self.current_byte_offset = inner.current_byte_offset;
|
||||||
|
if !self.remaining().starts_with("{/for}") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.current_byte_offset += "{/for}".len();
|
||||||
|
let body = inner.ast;
|
||||||
|
|
||||||
|
self.push_node_and_text(AstKind::For(For { expr: path, body }));
|
||||||
|
self.new_empty_text();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn maybe_parse_has(&mut self) {
|
||||||
|
assert!(self.remaining().starts_with("has"));
|
||||||
|
self.current_byte_offset += "has".len();
|
||||||
|
self.skip_whitespace();
|
||||||
|
let path = match self.parse_path() {
|
||||||
|
None => return,
|
||||||
|
Some(vp) => vp,
|
||||||
|
};
|
||||||
|
self.skip_whitespace();
|
||||||
|
if !self.remaining().starts_with('}') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.current_byte_offset += 1;
|
||||||
|
|
||||||
|
let mut inner = ParseState {
|
||||||
|
template: self.template,
|
||||||
|
last_byte_offset: self.last_byte_offset,
|
||||||
|
speculative_text_starts_at: self.current_byte_offset,
|
||||||
|
speculative_text_ends_at: self.current_byte_offset,
|
||||||
|
current_byte_offset: self.current_byte_offset,
|
||||||
|
ast: Ast::new(),
|
||||||
|
};
|
||||||
|
while inner.has_input() {
|
||||||
|
if inner.remaining().starts_with("{/has}") {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
inner.parse_one();
|
||||||
|
}
|
||||||
|
inner.text_might_end();
|
||||||
|
inner.add_current_text_to_ast();
|
||||||
|
self.current_byte_offset = inner.current_byte_offset;
|
||||||
|
if !self.remaining().starts_with("{/has}") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.current_byte_offset += "{/has}".len();
|
||||||
|
let body = inner.ast;
|
||||||
|
|
||||||
|
self.push_node_and_text(AstKind::Has(Has { expr: path, body }));
|
||||||
|
self.new_empty_text();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn maybe_parse_if(&mut self) {
|
||||||
|
assert!(self.remaining().starts_with("if"));
|
||||||
|
self.current_byte_offset += "if".len();
|
||||||
|
self.skip_whitespace();
|
||||||
|
let path = match self.parse_path() {
|
||||||
|
None => return,
|
||||||
|
Some(vp) => vp,
|
||||||
|
};
|
||||||
|
self.skip_whitespace();
|
||||||
|
if !self.remaining().starts_with('}') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.current_byte_offset += 1;
|
||||||
|
|
||||||
|
let mut inner = ParseState {
|
||||||
|
template: self.template,
|
||||||
|
last_byte_offset: self.last_byte_offset,
|
||||||
|
speculative_text_starts_at: self.current_byte_offset,
|
||||||
|
speculative_text_ends_at: self.current_byte_offset,
|
||||||
|
current_byte_offset: self.current_byte_offset,
|
||||||
|
ast: Ast::new(),
|
||||||
|
};
|
||||||
|
while inner.has_input() {
|
||||||
|
if inner.remaining().starts_with("{/if}") {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if inner.remaining().starts_with("{else}") {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
inner.parse_one();
|
||||||
|
}
|
||||||
|
inner.text_might_end();
|
||||||
|
inner.add_current_text_to_ast();
|
||||||
|
self.current_byte_offset = inner.current_byte_offset;
|
||||||
|
|
||||||
|
let else_ = if self.remaining().starts_with("{else}") {
|
||||||
|
self.current_byte_offset += "{else}".len();
|
||||||
|
let mut inner = ParseState {
|
||||||
|
template: self.template,
|
||||||
|
last_byte_offset: self.last_byte_offset,
|
||||||
|
speculative_text_starts_at: self.current_byte_offset,
|
||||||
|
speculative_text_ends_at: self.current_byte_offset,
|
||||||
|
current_byte_offset: self.current_byte_offset,
|
||||||
|
ast: Ast::new(),
|
||||||
};
|
};
|
||||||
|
while inner.has_input() {
|
||||||
ParseResult(rest, Some(If { path, then, else_ }))
|
if inner.remaining().starts_with("{/if}") {
|
||||||
} else {
|
break;
|
||||||
ParseResult(rest, None)
|
}
|
||||||
}
|
inner.parse_one();
|
||||||
}
|
}
|
||||||
|
inner.text_might_end();
|
||||||
#[test]
|
inner.add_current_text_to_ast();
|
||||||
fn test_parse_if_basic() {
|
self.current_byte_offset = inner.current_byte_offset;
|
||||||
assert_eq!(
|
Some(inner.ast)
|
||||||
parse_if("{if .}{.}{/if}"),
|
} else {
|
||||||
ParseResult(
|
None
|
||||||
"",
|
|
||||||
Some(If {
|
|
||||||
path: Path(vec![]),
|
|
||||||
then: vec![AstNodeKind::Print(Print { path: Path(vec![]) })],
|
|
||||||
else_: None,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_if_else_basic() {
|
|
||||||
assert_eq!(
|
|
||||||
parse_if("{if .}{.}{else}Else{/if}"),
|
|
||||||
ParseResult(
|
|
||||||
"",
|
|
||||||
Some(If {
|
|
||||||
path: Path(vec![]),
|
|
||||||
then: vec![AstNodeKind::Print(Print { path: Path(vec![]) })],
|
|
||||||
else_: Some(vec![AstNodeKind::Text("Else".to_string())]),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_has(rest: &str) -> ParseResult<Option<Has>> {
|
|
||||||
let rest = match rest.strip_prefix('{') {
|
|
||||||
Some(r) => r,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
let rest = rest.trim_start();
|
|
||||||
|
|
||||||
let rest = match rest.strip_prefix("has ") {
|
|
||||||
Some(r) => r,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
|
|
||||||
let ParseResult(rest, path) = parse_path(rest);
|
|
||||||
let path = match path {
|
|
||||||
Some(p) => p,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
|
|
||||||
let rest = rest.trim_start();
|
|
||||||
if let Some(rest) = rest.strip_prefix('}') {
|
|
||||||
let ParseResult(rest, body) = parse_many_until(rest, |s| {
|
|
||||||
s.starts_with("{/has}") || s.starts_with("{else}")
|
|
||||||
});
|
|
||||||
let then = match body {
|
|
||||||
Some(b) => b,
|
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
};
|
||||||
|
if !self.remaining().starts_with("{/if}") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.current_byte_offset += "{/if}".len();
|
||||||
|
let body = inner.ast;
|
||||||
|
|
||||||
let ParseResult(rest, else_) =
|
self.push_node_and_text(AstKind::If(If {
|
||||||
if let Some(rest) = rest.strip_prefix("{else}") {
|
expr: path,
|
||||||
let ParseResult(rest, else_) =
|
then: body,
|
||||||
parse_many_until(rest, |s| s.starts_with("{/has}"));
|
else_,
|
||||||
let else_ = match else_ {
|
}));
|
||||||
Some(b) => b,
|
self.new_empty_text();
|
||||||
None => return ParseResult(rest, None),
|
|
||||||
};
|
|
||||||
let rest = rest.strip_prefix("{/has}").unwrap();
|
|
||||||
|
|
||||||
ParseResult(rest, Some(else_))
|
|
||||||
} else {
|
|
||||||
let rest = rest.strip_prefix("{/has}").unwrap();
|
|
||||||
|
|
||||||
ParseResult(rest, None)
|
|
||||||
};
|
|
||||||
|
|
||||||
ParseResult(rest, Some(Has { path, then, else_ }))
|
|
||||||
} else {
|
|
||||||
ParseResult(rest, None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_path(rest: &str) -> ParseResult<Option<Path>> {
|
|
||||||
if !rest.starts_with('.') {
|
|
||||||
return ParseResult(rest, None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let rest = &rest[1..];
|
pub fn parse_path(&mut self) -> Option<ValuePath> {
|
||||||
let terminator = rest
|
assert!(self.remaining().starts_with('.'));
|
||||||
.find(|c: char| c.is_whitespace() || c == '}')
|
self.current_byte_offset += 1;
|
||||||
.unwrap_or(rest.len());
|
let end = self
|
||||||
|
.remaining()
|
||||||
|
.find(|ch: char| ch == '}' || ch.is_whitespace())
|
||||||
|
.unwrap_or(self.last_byte_offset - self.current_byte_offset);
|
||||||
|
let inner = &self.remaining()[..end];
|
||||||
|
let out = inner.split_terminator('.').map(|s| s.to_string()).collect();
|
||||||
|
let out = ValuePath(out);
|
||||||
|
|
||||||
let (path, rest) = rest.split_at(terminator);
|
self.current_byte_offset += end;
|
||||||
let path_parts = path
|
|
||||||
.split('.')
|
|
||||||
.filter(|s| !s.is_empty())
|
|
||||||
.map(|s| s.to_string())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
ParseResult(rest, Some(Path(path_parts)))
|
Some(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
pub fn push_node_and_text(&mut self, node: AstKind) {
|
||||||
fn test_parse_path_deep() {
|
self.add_current_text_to_ast();
|
||||||
assert_eq!(
|
self.ast.push(node);
|
||||||
parse_path(".test.best"),
|
}
|
||||||
ParseResult(
|
|
||||||
"",
|
|
||||||
Some(Path(vec!["test".to_string(), "best".to_string()]))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
pub fn new_empty_text(&mut self) {
|
||||||
fn test_parse_path_dot_only() {
|
self.speculative_text_starts_at = self.current_byte_offset;
|
||||||
assert_eq!(parse_path("."), ParseResult("", Some(Path(vec![]))))
|
self.speculative_text_ends_at = self.speculative_text_starts_at;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
pub fn skip_whitespace(&mut self) {
|
||||||
fn test_parse_path_deep_trailing() {
|
while self.has_input() {
|
||||||
assert_eq!(
|
if self.remaining().starts_with(char::is_whitespace) {
|
||||||
parse_path(".test.best Rest"),
|
self.current_byte_offset +=
|
||||||
ParseResult(
|
self.remaining().chars().next().unwrap().len_utf8();
|
||||||
" Rest",
|
continue;
|
||||||
Some(Path(vec!["test".to_string(), "best".to_string()]))
|
}
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_print_simple() {
|
|
||||||
assert_eq!(
|
|
||||||
parse_block("{.}"),
|
|
||||||
ParseResult("", Some(AstNodeKind::Print(Print { path: Path(vec![]) })))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_many_until_eof(rest: &str) -> ParseResult<Vec<AstNodeKind>> {
|
|
||||||
parse_many_until(rest, |s| s.is_empty()).map(|v| v.unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_many_until_eof_only_text() {
|
|
||||||
assert_eq!(
|
|
||||||
parse_many_until_eof("Hello "),
|
|
||||||
ParseResult("", vec![AstNodeKind::Text("Hello ".to_string()),]),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_many_until_eof_text_then_print() {
|
|
||||||
assert_eq!(
|
|
||||||
parse_many_until_eof("Hello {.}"),
|
|
||||||
ParseResult(
|
|
||||||
"",
|
|
||||||
vec![
|
|
||||||
AstNodeKind::Text("Hello ".to_string()),
|
|
||||||
AstNodeKind::Print(Print { path: Path(vec![]) })
|
|
||||||
]
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_many_until_eof_text_then_print_then_text() {
|
|
||||||
assert_eq!(
|
|
||||||
parse_many_until_eof("Hello {.} world"),
|
|
||||||
ParseResult(
|
|
||||||
"",
|
|
||||||
vec![
|
|
||||||
AstNodeKind::Text("Hello ".to_string()),
|
|
||||||
AstNodeKind::Print(Print { path: Path(vec![]) }),
|
|
||||||
AstNodeKind::Text(" world".to_string()),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_many_until(
|
|
||||||
rest: &str,
|
|
||||||
should_terminate: impl Fn(&str) -> bool,
|
|
||||||
) -> ParseResult<Option<Vec<AstNodeKind>>> {
|
|
||||||
let mut out = Vec::new();
|
|
||||||
let mut text_start = rest;
|
|
||||||
let mut remaining = rest;
|
|
||||||
loop {
|
|
||||||
if should_terminate(remaining) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if remaining == "" {
|
|
||||||
return ParseResult("", None);
|
|
||||||
}
|
|
||||||
|
|
||||||
if remaining.starts_with('{') {
|
|
||||||
let text_end = text_start.len() - remaining.len();
|
|
||||||
let ParseResult(rest, block) = parse_block(remaining);
|
|
||||||
if let Some(block) = block {
|
|
||||||
let text = &text_start[..text_end];
|
|
||||||
if !text.is_empty() {
|
|
||||||
out.push(AstNodeKind::Text(text.to_string()));
|
|
||||||
}
|
|
||||||
out.push(block);
|
|
||||||
text_start = rest;
|
|
||||||
}
|
|
||||||
remaining = rest;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
remaining = &remaining[1..];
|
|
||||||
}
|
}
|
||||||
let text_end = text_start.len() - remaining.len();
|
|
||||||
let text = &text_start[..text_end];
|
|
||||||
if !text.is_empty() {
|
|
||||||
out.push(AstNodeKind::Text(text.to_string()));
|
|
||||||
}
|
|
||||||
|
|
||||||
ParseResult(remaining, Some(out))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
pub fn parse(template: &str) -> Ast {
|
||||||
fn test_parse_many_until() {
|
let mut state = ParseState {
|
||||||
assert_eq!(
|
template,
|
||||||
parse_many_until("Hello {.} {/end}World", |rest| {
|
speculative_text_starts_at: 0,
|
||||||
rest.starts_with("{/end}")
|
speculative_text_ends_at: 0,
|
||||||
}),
|
current_byte_offset: 0,
|
||||||
ParseResult(
|
last_byte_offset: template.len(),
|
||||||
"{/end}World",
|
ast: Ast::new(),
|
||||||
Some(vec![
|
};
|
||||||
AstNodeKind::Text("Hello ".to_string()),
|
state.parse();
|
||||||
AstNodeKind::Print(Print { path: Path(vec![]) }),
|
|
||||||
AstNodeKind::Text(" ".to_string()),
|
state.ast
|
||||||
])
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_parse_complex() {
|
mod test {
|
||||||
assert_eq!(
|
use crate::internal::parse::{AstKind, Print, ValuePath};
|
||||||
parse(
|
|
||||||
"{for .}{if .}{with .}{.}{/with}{else}{has .}Yo{else}{/has}{/if}{/for}"
|
use super::{parse, Ast, For, Has, If};
|
||||||
),
|
|
||||||
vec![AstNodeKind::For(For {
|
fn go(tmpl: &str, rhs: Ast) {
|
||||||
path: Path(vec![]),
|
let ast = parse(tmpl);
|
||||||
body: vec![AstNodeKind::If(If {
|
assert_eq!(ast, rhs);
|
||||||
path: Path(vec![]),
|
}
|
||||||
then: vec![AstNodeKind::With(With {
|
|
||||||
path: Path(vec![]),
|
#[test]
|
||||||
body: vec![AstNodeKind::Print(Print {
|
fn parse_print_only() {
|
||||||
path: Path(vec![])
|
go("{.}", vec![AstKind::Print(Print(ValuePath(vec![])))]);
|
||||||
})]
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_print() {
|
||||||
|
go(
|
||||||
|
"hello, {.}!",
|
||||||
|
vec![
|
||||||
|
AstKind::Text("hello, ".to_string()),
|
||||||
|
AstKind::Print(Print(ValuePath(vec![]))),
|
||||||
|
AstKind::Text("!".to_string()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_print_lookup() {
|
||||||
|
go(
|
||||||
|
"hello, {.name}!",
|
||||||
|
vec![
|
||||||
|
AstKind::Text("hello, ".to_string()),
|
||||||
|
AstKind::Print(Print(ValuePath(vec!["name".to_string()]))),
|
||||||
|
AstKind::Text("!".to_string()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_for() {
|
||||||
|
go(
|
||||||
|
"{for .}{.}{/for}",
|
||||||
|
vec![AstKind::For(For {
|
||||||
|
expr: ValuePath(vec![]),
|
||||||
|
body: vec![AstKind::Print(Print(ValuePath(vec![])))],
|
||||||
|
})],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_has() {
|
||||||
|
go(
|
||||||
|
"{has .}{.}{/has}",
|
||||||
|
vec![AstKind::Has(Has {
|
||||||
|
expr: ValuePath(vec![]),
|
||||||
|
body: vec![AstKind::Print(Print(ValuePath(vec![])))],
|
||||||
|
})],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_if() {
|
||||||
|
go(
|
||||||
|
"{if .}{.}{/if}",
|
||||||
|
vec![AstKind::If(If {
|
||||||
|
expr: ValuePath(vec![]),
|
||||||
|
then: vec![AstKind::Print(Print(ValuePath(vec![])))],
|
||||||
|
else_: None,
|
||||||
|
})],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_if_else() {
|
||||||
|
go(
|
||||||
|
"{if .}{.}{else}Empty{/if}",
|
||||||
|
vec![AstKind::If(If {
|
||||||
|
expr: ValuePath(vec![]),
|
||||||
|
then: vec![AstKind::Print(Print(ValuePath(vec![])))],
|
||||||
|
else_: Some(vec![AstKind::Text("Empty".to_string())]),
|
||||||
|
})],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_for_2() {
|
||||||
|
go(
|
||||||
|
"{for .}{.}, {/for}",
|
||||||
|
vec![AstKind::For(For {
|
||||||
|
expr: ValuePath(vec![]),
|
||||||
|
body: vec![
|
||||||
|
AstKind::Print(Print(ValuePath(vec![]))),
|
||||||
|
AstKind::Text(", ".to_string()),
|
||||||
|
],
|
||||||
|
})],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_for_nested() {
|
||||||
|
go(
|
||||||
|
"{for .items}{for .names}{.},{/for}{/for}",
|
||||||
|
vec![AstKind::For(For {
|
||||||
|
expr: ValuePath(vec!["items".to_string()]),
|
||||||
|
body: vec![AstKind::For(For {
|
||||||
|
expr: ValuePath(vec!["names".to_string()]),
|
||||||
|
body: vec![
|
||||||
|
AstKind::Print(Print(ValuePath(vec![]))),
|
||||||
|
AstKind::Text(",".to_string()),
|
||||||
|
],
|
||||||
})],
|
})],
|
||||||
else_: Some(vec![AstNodeKind::Has(Has {
|
})],
|
||||||
path: Path(vec![]),
|
)
|
||||||
then: vec![AstNodeKind::Text("Yo".to_string())],
|
}
|
||||||
else_: Some(vec![]),
|
|
||||||
})])
|
|
||||||
})]
|
|
||||||
})]
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,188 +0,0 @@
|
||||||
/*
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::{
|
|
||||||
parse, AstMany, AstNodeKind, AstNodeRef, For, Has, If, Path, Print,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn go(input: &str, expected: Vec<AstNodeKind>) {
|
|
||||||
assert_eq!(parse(input).arena.0, expected)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_text() {
|
|
||||||
go(
|
|
||||||
"Hello, world!",
|
|
||||||
vec![AstNodeKind::Text("Hello, world!".to_string())],
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_print() {
|
|
||||||
go(
|
|
||||||
"{ .test }",
|
|
||||||
vec![AstNodeKind::Print(Print {
|
|
||||||
path: Path(vec!["test".to_string()]),
|
|
||||||
})],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_print_just_dot() {
|
|
||||||
go(
|
|
||||||
"{ . }",
|
|
||||||
vec![AstNodeKind::Print(Print { path: Path(vec![]) })],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_print_nested_path() {
|
|
||||||
go(
|
|
||||||
"{ .foo.bar }",
|
|
||||||
vec![AstNodeKind::Print(Print {
|
|
||||||
path: Path(vec!["foo".to_string(), "bar".to_string()]),
|
|
||||||
})],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_has() {
|
|
||||||
go(
|
|
||||||
"{ has .test }{.}{/has}",
|
|
||||||
vec![
|
|
||||||
AstNodeKind::Print(Print { path: Path(vec![]) }),
|
|
||||||
AstNodeKind::Has(Has {
|
|
||||||
path: Path(vec!["test".to_string()]),
|
|
||||||
body: AstMany {
|
|
||||||
start: AstNodeRef(0),
|
|
||||||
end: AstNodeRef(1),
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_for() {
|
|
||||||
go(
|
|
||||||
"{ for .test }{.}{/for}",
|
|
||||||
vec![
|
|
||||||
AstNodeKind::Print(Print { path: Path(vec![]) }),
|
|
||||||
AstNodeKind::For(For {
|
|
||||||
path: Path(vec!["test".to_string()]),
|
|
||||||
body: AstMany {
|
|
||||||
start: AstNodeRef(0),
|
|
||||||
end: AstNodeRef(1),
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_if() {
|
|
||||||
go(
|
|
||||||
"{ if .test }{.test}{/if}",
|
|
||||||
vec![
|
|
||||||
AstNodeKind::Print(Print {
|
|
||||||
path: Path(vec!["test".to_string()]),
|
|
||||||
}),
|
|
||||||
AstNodeKind::If(If {
|
|
||||||
path: Path(vec!["test".to_string()]),
|
|
||||||
then: AstMany {
|
|
||||||
start: AstNodeRef(0),
|
|
||||||
end: AstNodeRef(1),
|
|
||||||
},
|
|
||||||
else_: None,
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn parse_if_else() {
|
|
||||||
go(
|
|
||||||
"{ if .test }{.test}{else}Hello{/if}",
|
|
||||||
vec![
|
|
||||||
AstNodeKind::Print(Print {
|
|
||||||
path: Path(vec!["test".to_string()]),
|
|
||||||
}),
|
|
||||||
AstNodeKind::If(If {
|
|
||||||
path: Path(vec!["test".to_string()]),
|
|
||||||
then: AstMany {
|
|
||||||
start: AstNodeRef(0),
|
|
||||||
end: AstNodeRef(1),
|
|
||||||
},
|
|
||||||
else_: Some(AstMany {
|
|
||||||
start: AstNodeRef(0),
|
|
||||||
end: AstNodeRef(1),
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
use super::parse::AstNodeKind;
|
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
|
||||||
pub struct AstArena(Vec<AstNodeKind>);
|
|
||||||
impl AstArena {
|
|
||||||
pub const fn new() -> Self {
|
|
||||||
Self(Vec::new())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn next_id(&self) -> AstNodeRef {
|
|
||||||
AstNodeRef(
|
|
||||||
self.0
|
|
||||||
.len()
|
|
||||||
.try_into()
|
|
||||||
.expect("Tried to store too many AST nodes"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add(&mut self, n: AstNodeKind) -> AstNodeRef {
|
|
||||||
let id = self.next_id();
|
|
||||||
self.0.push(n);
|
|
||||||
|
|
||||||
id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_many(&mut self, mut many: Vec<AstNodeKind>) -> AstMany {
|
|
||||||
let start = self.next_id();
|
|
||||||
let end = AstNodeRef(
|
|
||||||
(start.0 as usize + many.len())
|
|
||||||
.try_into()
|
|
||||||
.expect("Tried to store too many AST nodes"),
|
|
||||||
);
|
|
||||||
self.0.append(&mut many);
|
|
||||||
|
|
||||||
AstMany { start, end }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deref(&self, rf: AstNodeRef) -> &AstNodeKind {
|
|
||||||
&self.0[rf.0 as usize]
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
pub fn deref_many(&self, rf: AstMany) -> &[AstNodeKind] {
|
|
||||||
&self.0[rf.start.0 as usize..rf.end.0 as usize]
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
pub fn deref_many(&self, rf: &Vec<AstNodeKind>) -> &[AstNodeKind] {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
|
|
||||||
pub struct AstNodeRef(u32);
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
|
|
||||||
pub struct AstMany {
|
|
||||||
start: AstNodeRef,
|
|
||||||
end: AstNodeRef,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct Ast {
|
|
||||||
pub(crate) arena: AstArena,
|
|
||||||
pub(crate) root: Vec<AstNodeKind>,
|
|
||||||
}
|
|
||||||
|
|
@ -333,7 +333,7 @@ pub struct Newt {
|
||||||
impl Newt {
|
impl Newt {
|
||||||
pub fn build(tmpl: &str) -> Self {
|
pub fn build(tmpl: &str) -> Self {
|
||||||
let ast = parse(tmpl);
|
let ast = parse(tmpl);
|
||||||
Self { ast: todo!() }
|
Self { ast }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execute(&self, value: &dyn Value) -> String {
|
pub fn execute(&self, value: &dyn Value) -> String {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue