From 105cda0e698fe86266d706f4e3bacc081e65c2aa Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 10 Jan 2021 21:38:58 +0100 Subject: =?UTF-8?q?Braced=20content=20->=20Bracketed=20templates=20?= =?UTF-8?q?=E2=9C=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/syntax/expr.rs | 44 +++++++++++++++++--------------------------- src/syntax/node.rs | 31 +++++++++++++++---------------- 2 files changed, 32 insertions(+), 43 deletions(-) (limited to 'src/syntax') diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index 834c4393..cb09041c 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -38,8 +38,8 @@ pub enum Expr { Array(ExprArray), /// A dictionary expression: `(color: #f79143, pattern: dashed)`. Dict(ExprDict), - /// A content expression: `{*Hello* there!}`. - Content(ExprContent), + /// A template expression: `[*Hi* there!]`. + Template(ExprTemplate), } impl Pretty for Expr { @@ -60,24 +60,16 @@ impl Pretty for Expr { Self::Binary(binary) => binary.pretty(p), Self::Array(array) => array.pretty(p), Self::Dict(dict) => dict.pretty(p), - Self::Content(content) => pretty_content_expr(content, p), + Self::Template(template) => pretty_template_expr(template, p), } } } -/// Pretty print content in an expression context. -pub fn pretty_content_expr(tree: &Tree, p: &mut Printer) { - if let [Spanned { v: Node::Expr(Expr::Call(call)), .. }] = tree.as_slice() { - // Remove unncessary braces from content expression containing just a - // single function call. - // - // Example: Transforms "{(call: {[f]})}" => "{(call: [f])}" - pretty_bracket_call(call, p, false); - } else { - p.push_str("{"); - tree.pretty(p); - p.push_str("}"); - } +/// Pretty print a template in an expression context. +pub fn pretty_template_expr(tree: &Tree, p: &mut Printer) { + p.push_str("["); + tree.pretty(p); + p.push_str("]"); } /// An invocation of a function: `[foo ...]`, `foo(...)`. @@ -111,8 +103,8 @@ pub fn pretty_bracket_call(call: &ExprCall, p: &mut Printer, chained: bool) { // Find out whether this can be written with a body or as a chain. // - // Example: Transforms "[v {Hi}]" => "[v][Hi]". - if let [head @ .., Argument::Pos(Spanned { v: Expr::Content(content), .. })] = + // Example: Transforms "[v [Hi]]" => "[v][Hi]". + if let [head @ .., Argument::Pos(Spanned { v: Expr::Template(template), .. })] = call.args.v.as_slice() { // Previous arguments. @@ -124,11 +116,11 @@ pub fn pretty_bracket_call(call: &ExprCall, p: &mut Printer, chained: bool) { // Find out whether this can written as a chain. // // Example: Transforms "[v][[f]]" => "[v | f]". - if let [Spanned { v: Node::Expr(Expr::Call(call)), .. }] = content.as_slice() { + if let [Spanned { v: Node::Expr(Expr::Call(call)), .. }] = template.as_slice() { return pretty_bracket_call(call, p, true); } else { p.push_str("]["); - content.pretty(p); + template.pretty(p); } } else if !call.args.v.is_empty() { p.push_str(" "); @@ -291,8 +283,8 @@ impl Pretty for ExprDict { } } -/// A content expression: `{*Hello* there!}`. -pub type ExprContent = Tree; +/// A template expression: `[*Hi* there!]`. +pub type ExprTemplate = Tree; #[cfg(test)] mod tests { @@ -301,8 +293,7 @@ mod tests { #[test] fn test_pretty_print_chaining() { // All equivalent. - test_pretty("[v [f]]", "[v | f]"); - test_pretty("[v {[f]}]", "[v | f]"); + test_pretty("[v [[f]]]", "[v | f]"); test_pretty("[v][[f]]", "[v | f]"); test_pretty("[v | f]", "[v | f]"); } @@ -321,9 +312,8 @@ mod tests { test_pretty("{(:)}", "{(:)}"); test_pretty("{(percent: 5%)}", "{(percent: 5%)}"); - // Content expression without unncessary braces. - test_pretty("[v [f], 1]", "[v [f], 1]"); - test_pretty("(func: {[f]})", "(func: [f])"); + // Content expression. + test_pretty("[v [[f]], 1]", "[v [[f]], 1]"); } #[test] diff --git a/src/syntax/node.rs b/src/syntax/node.rs index 01b4ee42..e7091a40 100644 --- a/src/syntax/node.rs +++ b/src/syntax/node.rs @@ -3,18 +3,18 @@ use super::*; /// A syntax node, encompassing a single logical entity of parsed source code. #[derive(Debug, Clone, PartialEq)] pub enum Node { - /// Plain text. - Text(String), + /// Strong text was enabled / disabled. + Strong, + /// Emphasized text was enabled / disabled. + Emph, /// Whitespace containing less than two newlines. Space, /// A forced line break. Linebreak, /// A paragraph break. Parbreak, - /// Strong text was enabled / disabled. - Strong, - /// Emphasized text was enabled / disabled. - Emph, + /// Plain text. + Text(String), /// A section heading. Heading(NodeHeading), /// An optionally syntax-highlighted raw block. @@ -26,12 +26,12 @@ pub enum Node { impl Pretty for Node { fn pretty(&self, p: &mut Printer) { match self { - Self::Text(text) => p.push_str(&text), + Self::Strong => p.push_str("*"), + Self::Emph => p.push_str("_"), Self::Space => p.push_str(" "), Self::Linebreak => p.push_str(r"\"), Self::Parbreak => p.push_str("\n\n"), - Self::Strong => p.push_str("*"), - Self::Emph => p.push_str("_"), + Self::Text(text) => p.push_str(&text), Self::Heading(heading) => heading.pretty(p), Self::Raw(raw) => raw.pretty(p), Self::Expr(expr) => pretty_expr_node(expr, p), @@ -50,8 +50,8 @@ pub fn pretty_expr_node(expr: &Expr, p: &mut Printer) { // Remove unncessary nesting of content and expression blocks. // - // Example: Transforms "{{Hi}}" => "Hi". - Expr::Content(content) => content.pretty(p), + // Example: Transforms "{[Hi]}" => "Hi". + Expr::Template(template) => template.pretty(p), _ => { p.push_str("{"); @@ -179,9 +179,9 @@ mod tests { #[test] fn test_pretty_print_removes_nesting() { - // Even levels of nesting do not matter. - test_pretty("{{Hi}}", "Hi"); - test_pretty("{{{{Hi}}}}", "Hi"); + // Nesting does not matter. + test_pretty("{{x}}", "{x}"); + test_pretty("{{{x}}}", "{x}"); } #[test] @@ -189,8 +189,7 @@ mod tests { // All reduces to a simple bracket call. test_pretty("{v()}", "[v]"); test_pretty("[v]", "[v]"); - test_pretty("{[v]}", "[v]"); - test_pretty("{{[v]}}", "[v]"); + test_pretty("{[[v]]}", "[v]"); } #[test] -- cgit v1.2.3