Skip to content

Commit

Permalink
Parser nearly works
Browse files Browse the repository at this point in the history
  • Loading branch information
elkowar committed Nov 28, 2024
1 parent f5214e2 commit 7b88b88
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 50 deletions.
30 changes: 15 additions & 15 deletions src/templating/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ impl<'a> Document<'a> {
comment_prefix: self.comment_prefix.clone(),
};
for element in &self.elements {

Check warning on line 34 in src/templating/document.rs

View workflow job for this annotation

GitHub Actions / build

unused variable: `element`
output.push_str(&element.render(&ctx, eval_ctx)?);
// output.push_str(&element.render(&ctx, eval_ctx)?);
}
Ok(output)
}

pub fn parse_string(s: &'a str) -> Result<Self> {
let mut document = Document::default();
let mut result = YolkParser::parse(Rule::YolkFile, s)?;
let yolk_file = result.next().unwrap();
// let mut result = YolkParser::parse(Rule::YolkFile, s)?;
// let yolk_file = result.next().unwrap();

for rule in yolk_file.into_inner() {
let element = element::Element::try_from_pair(rule)?;
if let element::Element::Directive {
name: "CommentPrefix",
content,
..
} = element
{
document.comment_prefix = content.trim().to_string();
}
document.elements.push(element);
}
// for rule in yolk_file.into_inner() {
// let element = element::Element::try_from_pair(rule)?;
// if let element::Element::Directive {
// name: "CommentPrefix",
// content,
// ..
// } = element
// {
// document.comment_prefix = content.trim().to_string();
// }
// document.elements.push(element);
// }
Ok(document)
}
}
Expand Down
87 changes: 52 additions & 35 deletions src/templating/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ impl<'a> ElementParser<'a> {
}
}

impl<'a> Element<'a> {
#[allow(unused)]
pub fn parse(s: &'a str) -> Result<Vec<Self>> {
let mut pairs = YolkParser::parse(Rule::Document, s)?;
let lines = pairs
.into_iter()
.map(|pair| linewise::ParsedLine::try_from_pair(pair))
.collect::<Result<_>>()?;
println!("{:#?}", &lines);
ElementParser::new(lines).parse()
}
}

/*
impl<'a> Element<'a> {
pub fn render(&self, render_ctx: &Context, eval_ctx: &mut EvalCtx<'_>) -> Result<String> {
match self {
Expand Down Expand Up @@ -415,12 +429,8 @@ impl<'a> Element<'a> {
}
}
#[allow(unused)]
pub fn parse(s: &'a str) -> Result<Self> {
let mut pairs = YolkParser::parse(Rule::Element, s)?;
Self::try_from_pair(pairs.next().context("No content")?)
}
}
*/

#[cfg(test)]
mod test {
Expand All @@ -430,38 +440,45 @@ mod test {
use crate::templating::document::Context;

Check warning on line 440 in src/templating/element.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `crate::templating::document::Context`
use crate::templating::element::Element;
#[test]
pub fn test_render_replace() -> TestResult {
let element = Element::parse("{% replace /'.*'/ `'new'` %}\nfoo: 'original'")?;
let render_ctx = Context::default();
let mut eval_ctx = eval_ctx::EvalCtx::new();
assert_eq!(
"{% replace /'.*'/ `'new'` %}\nfoo: 'new'",
element.render(&render_ctx, &mut eval_ctx)?
);
pub fn test_parse_if() -> TestResult {
let element = Element::parse("{%if foo %}\nfoo: 'original'\n{% end %}\n")?;
println!("{:#?}", element);
panic!();
Ok(())
}
// #[test]
// pub fn test_render_replace() -> TestResult {
// let element = Element::parse("{% replace /'.*'/ `'new'` %}\nfoo: 'original'")?;
// let render_ctx = Context::default();
// let mut eval_ctx = eval_ctx::EvalCtx::new();
// assert_eq!(
// "{% replace /'.*'/ `'new'` %}\nfoo: 'new'",
// element.render(&render_ctx, &mut eval_ctx)?
// );
// Ok(())
// }

#[test]
pub fn test_render_replace_inline() -> TestResult {
let element = Element::parse("foo: 'original' # {<< replace /'.*'/ `'new'` >>}")?;
let render_ctx = Context::default();
let mut eval_ctx = eval_ctx::EvalCtx::new();
assert_eq!(
"foo: 'new' # {<< replace /'.*'/ `'new'` >>}",
element.render(&render_ctx, &mut eval_ctx)?
);
Ok(())
}
// #[test]
// pub fn test_render_replace_inline() -> TestResult {
// let element = Element::parse("foo: 'original' # {<< replace /'.*'/ `'new'` >>}")?;
// let render_ctx = Context::default();
// let mut eval_ctx = eval_ctx::EvalCtx::new();
// assert_eq!(
// "foo: 'new' # {<< replace /'.*'/ `'new'` >>}",
// element.render(&render_ctx, &mut eval_ctx)?
// );
// Ok(())
// }

#[test]
pub fn test_render_replace_refuse_nonreversible() -> TestResult {
let element = Element::parse("{% replace /'.*'/ `no quotes` %}\nfoo: 'original'")?;
let render_ctx = Context::default();
let mut eval_ctx = eval_ctx::EvalCtx::new();
assert_eq!(
"{% replace /'.*'/ `no quotes` %}\nfoo: 'original'",
element.render(&render_ctx, &mut eval_ctx)?
);
Ok(())
}
// #[test]
// pub fn test_render_replace_refuse_nonreversible() -> TestResult {
// let element = Element::parse("{% replace /'.*'/ `no quotes` %}\nfoo: 'original'")?;
// let render_ctx = Context::default();
// let mut eval_ctx = eval_ctx::EvalCtx::new();
// assert_eq!(
// "{% replace /'.*'/ `no quotes` %}\nfoo: 'original'",
// element.render(&render_ctx, &mut eval_ctx)?
// );
// Ok(())
// }
}
2 changes: 2 additions & 0 deletions src/templating/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod test {
Rule, YolkParser,
},
};
/*
#[test]
pub fn test_parse_directive() {
Expand Down Expand Up @@ -86,4 +87,5 @@ mod test {
result
)
}
*/
}

0 comments on commit 7b88b88

Please sign in to comment.