Braced content -> Bracketed templates ✏

This commit is contained in:
Laurenz 2021-01-10 21:38:58 +01:00
parent 3c7d249ae4
commit 105cda0e69
10 changed files with 175 additions and 187 deletions

View File

@ -170,7 +170,7 @@ impl Eval for Spanned<&Expr> {
Expr::Binary(v) => v.with_span(self.span).eval(ctx), Expr::Binary(v) => v.with_span(self.span).eval(ctx),
Expr::Array(v) => Value::Array(v.with_span(self.span).eval(ctx)), Expr::Array(v) => Value::Array(v.with_span(self.span).eval(ctx)),
Expr::Dict(v) => Value::Dict(v.with_span(self.span).eval(ctx)), Expr::Dict(v) => Value::Dict(v.with_span(self.span).eval(ctx)),
Expr::Content(v) => Value::Content(v.clone()), Expr::Template(v) => Value::Template(v.clone()),
} }
} }
} }
@ -275,7 +275,7 @@ fn add(ctx: &mut EvalContext, span: Span, lhs: Value, rhs: Value) -> Value {
(Str(a), Str(b)) => Str(a + &b), (Str(a), Str(b)) => Str(a + &b),
(Array(a), Array(b)) => Array(concat(a, b)), (Array(a), Array(b)) => Array(concat(a, b)),
(Dict(a), Dict(b)) => Dict(concat(a, b)), (Dict(a), Dict(b)) => Dict(concat(a, b)),
(Content(a), Content(b)) => Content(concat(a, b)), (Template(a), Template(b)) => Template(concat(a, b)),
(a, b) => { (a, b) => {
ctx.diag(error!( ctx.diag(error!(

View File

@ -8,7 +8,7 @@ use super::{Args, Eval, EvalContext};
use crate::color::Color; use crate::color::Color;
use crate::geom::{Angle, Length, Linear, Relative}; use crate::geom::{Angle, Length, Linear, Relative};
use crate::pretty::{pretty, Pretty, Printer}; use crate::pretty::{pretty, Pretty, Printer};
use crate::syntax::{pretty_content_expr, Spanned, Tree, WithSpan}; use crate::syntax::{pretty_template_expr, Spanned, Tree, WithSpan};
/// A computational value. /// A computational value.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -37,8 +37,8 @@ pub enum Value {
Array(ValueArray), Array(ValueArray),
/// A dictionary value: `(color: #f79143, pattern: dashed)`. /// A dictionary value: `(color: #f79143, pattern: dashed)`.
Dict(ValueDict), Dict(ValueDict),
/// A content value: `{*Hi* there}`. /// A template value: `[*Hi* there]`.
Content(ValueContent), Template(ValueTemplate),
/// An executable function. /// An executable function.
Func(ValueFunc), Func(ValueFunc),
/// Any object. /// Any object.
@ -71,7 +71,7 @@ impl Value {
Self::Str(_) => String::TYPE_NAME, Self::Str(_) => String::TYPE_NAME,
Self::Array(_) => ValueArray::TYPE_NAME, Self::Array(_) => ValueArray::TYPE_NAME,
Self::Dict(_) => ValueDict::TYPE_NAME, Self::Dict(_) => ValueDict::TYPE_NAME,
Self::Content(_) => ValueContent::TYPE_NAME, Self::Template(_) => ValueTemplate::TYPE_NAME,
Self::Func(_) => ValueFunc::TYPE_NAME, Self::Func(_) => ValueFunc::TYPE_NAME,
Self::Any(v) => v.type_name(), Self::Any(v) => v.type_name(),
Self::Error => "error", Self::Error => "error",
@ -87,7 +87,7 @@ impl Eval for &Value {
ctx.push(ctx.make_text_node(match self { ctx.push(ctx.make_text_node(match self {
Value::None => return, Value::None => return,
Value::Str(s) => s.clone(), Value::Str(s) => s.clone(),
Value::Content(tree) => return tree.eval(ctx), Value::Template(tree) => return tree.eval(ctx),
other => pretty(other), other => pretty(other),
})); }));
} }
@ -114,7 +114,7 @@ impl Pretty for Value {
Value::Str(v) => write!(p, "{:?}", v).unwrap(), Value::Str(v) => write!(p, "{:?}", v).unwrap(),
Value::Array(array) => array.pretty(p), Value::Array(array) => array.pretty(p),
Value::Dict(dict) => dict.pretty(p), Value::Dict(dict) => dict.pretty(p),
Value::Content(content) => pretty_content_expr(content, p), Value::Template(template) => pretty_template_expr(template, p),
Value::Func(v) => v.pretty(p), Value::Func(v) => v.pretty(p),
Value::Any(v) => v.pretty(p), Value::Any(v) => v.pretty(p),
Value::Error => p.push_str("(error)"), Value::Error => p.push_str("(error)"),
@ -155,8 +155,8 @@ impl Pretty for ValueDict {
} }
} }
/// A content value: `{*Hi* there}`. /// A template value: `[*Hi* there]`.
pub type ValueContent = Tree; pub type ValueTemplate = Tree;
/// A wrapper around a reference-counted executable function. /// A wrapper around a reference-counted executable function.
#[derive(Clone)] #[derive(Clone)]
@ -407,28 +407,26 @@ macro_rules! impl_primitive {
impl_primitive! { bool: "boolean", Value::Bool } impl_primitive! { bool: "boolean", Value::Bool }
impl_primitive! { i64: "integer", Value::Int } impl_primitive! { i64: "integer", Value::Int }
impl_primitive! { Length: "length", Value::Length }
impl_primitive! { Angle: "angle", Value::Angle }
impl_primitive! { Relative: "relative", Value::Relative }
impl_primitive! { Color: "color", Value::Color }
impl_primitive! { String: "string", Value::Str }
impl_primitive! { ValueArray: "array", Value::Array }
impl_primitive! { ValueDict: "dictionary", Value::Dict }
impl_primitive! { ValueContent: "content", Value::Content }
impl_primitive! { ValueFunc: "function", Value::Func }
impl_primitive! { impl_primitive! {
f64: "float", f64: "float",
Value::Float, Value::Float,
Value::Int(v) => v as f64, Value::Int(v) => v as f64,
} }
impl_primitive! { Length: "length", Value::Length }
impl_primitive! { Angle: "angle", Value::Angle }
impl_primitive! { Relative: "relative", Value::Relative }
impl_primitive! { impl_primitive! {
Linear: "linear", Linear: "linear",
Value::Linear, Value::Linear,
Value::Length(v) => v.into(), Value::Length(v) => v.into(),
Value::Relative(v) => v.into(), Value::Relative(v) => v.into(),
} }
impl_primitive! { Color: "color", Value::Color }
impl_primitive! { String: "string", Value::Str }
impl_primitive! { ValueArray: "array", Value::Array }
impl_primitive! { ValueDict: "dictionary", Value::Dict }
impl_primitive! { ValueTemplate: "template", Value::Template }
impl_primitive! { ValueFunc: "function", Value::Func }
impl From<&str> for Value { impl From<&str> for Value {
fn from(v: &str) -> Self { fn from(v: &str) -> Self {
@ -512,7 +510,7 @@ mod tests {
test_pretty(Relative::new(0.3) + Length::cm(2.0), "30% + 2cm"); test_pretty(Relative::new(0.3) + Length::cm(2.0), "30% + 2cm");
test_pretty(Color::Rgba(RgbaColor::new(1, 1, 1, 0xff)), "#010101"); test_pretty(Color::Rgba(RgbaColor::new(1, 1, 1, 0xff)), "#010101");
test_pretty("hello", r#""hello""#); test_pretty("hello", r#""hello""#);
test_pretty(vec![Spanned::zero(Node::Strong)], "{*}"); test_pretty(vec![Spanned::zero(Node::Strong)], "[*]");
test_pretty(ValueFunc::new("nil", |_, _| Value::None), "(function nil)"); test_pretty(ValueFunc::new("nil", |_, _| Value::None), "(function nil)");
test_pretty(ValueAny::new(1), "1"); test_pretty(ValueAny::new(1), "1");
test_pretty(Value::Error, "(error)"); test_pretty(Value::Error, "(error)");
@ -528,8 +526,8 @@ mod tests {
// Dictionary. // Dictionary.
let mut dict = BTreeMap::new(); let mut dict = BTreeMap::new();
dict.insert("one".into(), Value::Int(1)); dict.insert("one".into(), Value::Int(1));
dict.insert("two".into(), Value::Content(parse("[f]").output)); dict.insert("two".into(), Value::Template(parse("[f]").output));
test_pretty(BTreeMap::new(), "(:)"); test_pretty(BTreeMap::new(), "(:)");
test_pretty(dict, "(one: 1, two: [f])"); test_pretty(dict, "(one: 1, two: [[f]])");
} }
} }

View File

@ -101,7 +101,7 @@ pub fn align(ctx: &mut EvalContext, args: &mut Args) -> Value {
ctx.start_par_group(); ctx.start_par_group();
} }
if let Some(body) = args.find::<ValueContent>(ctx) { if let Some(body) = args.find::<ValueTemplate>(ctx) {
body.eval(ctx); body.eval(ctx);
ctx.state = snapshot; ctx.state = snapshot;
} }
@ -190,7 +190,7 @@ pub fn box_(ctx: &mut EvalContext, args: &mut Args) -> Value {
ctx.start_content_group(); ctx.start_content_group();
if let Some(body) = args.find::<ValueContent>(ctx) { if let Some(body) = args.find::<ValueTemplate>(ctx) {
body.eval(ctx); body.eval(ctx);
} }
@ -317,7 +317,7 @@ pub fn page(ctx: &mut EvalContext, args: &mut Args) -> Value {
ctx.set_dirs(Gen::new(main, cross)); ctx.set_dirs(Gen::new(main, cross));
let mut softness = ctx.end_page_group(|_| false); let mut softness = ctx.end_page_group(|_| false);
if let Some(body) = args.find::<ValueContent>(ctx) { if let Some(body) = args.find::<ValueTemplate>(ctx) {
// TODO: Restrict body to a single page? // TODO: Restrict body to a single page?
ctx.start_page_group(Softness::Hard); ctx.start_page_group(Softness::Hard);
body.eval(ctx); body.eval(ctx);

View File

@ -95,7 +95,7 @@ pub fn font(ctx: &mut EvalContext, args: &mut Args) -> Value {
} }
} }
if let Some(body) = args.find::<ValueContent>(ctx) { if let Some(body) = args.find::<ValueTemplate>(ctx) {
body.eval(ctx); body.eval(ctx);
ctx.state = snapshot; ctx.state = snapshot;
} }

View File

@ -49,32 +49,20 @@ fn tree(p: &mut Parser) -> Tree {
/// Parse a syntax node. /// Parse a syntax node.
fn node(p: &mut Parser, at_start: bool) -> Option<Node> { fn node(p: &mut Parser, at_start: bool) -> Option<Node> {
let node = match p.peek()? { let node = match p.peek()? {
Token::Text(text) => Node::Text(text.into()), // Bracket call.
Token::Space(newlines) => {
if newlines < 2 {
Node::Space
} else {
Node::Parbreak
}
}
Token::LineComment(_) | Token::BlockComment(_) => {
p.eat();
return None;
}
Token::LeftBracket => { Token::LeftBracket => {
return Some(Node::Expr(Expr::Call(bracket_call(p)))); return Some(Node::Expr(Expr::Call(bracket_call(p))));
} }
// Code block.
Token::LeftBrace => { Token::LeftBrace => {
return Some(Node::Expr(block_expr(p)?)); return Some(Node::Expr(code_block(p)?));
} }
// Markup.
Token::Star => Node::Strong, Token::Star => Node::Strong,
Token::Underscore => Node::Emph, Token::Underscore => Node::Emph,
Token::Tilde => Node::Text("\u{00A0}".into()), Token::Tilde => Node::Text("\u{00A0}".into()),
Token::Backslash => Node::Linebreak,
Token::Hash => { Token::Hash => {
if at_start { if at_start {
return Some(Node::Heading(heading(p))); return Some(Node::Heading(heading(p)));
@ -82,9 +70,24 @@ fn node(p: &mut Parser, at_start: bool) -> Option<Node> {
Node::Text(p.get(p.peek_span()).into()) Node::Text(p.get(p.peek_span()).into())
} }
} }
Token::Backslash => Node::Linebreak,
Token::Space(newlines) => {
if newlines < 2 {
Node::Space
} else {
Node::Parbreak
}
}
Token::Text(text) => Node::Text(text.into()),
Token::Raw(t) => Node::Raw(raw(p, t)), Token::Raw(t) => Node::Raw(raw(p, t)),
Token::UnicodeEscape(t) => Node::Text(unicode_escape(p, t)), Token::UnicodeEscape(t) => Node::Text(unicode_escape(p, t)),
// Comments.
Token::LineComment(_) | Token::BlockComment(_) => {
p.eat();
return None;
}
_ => { _ => {
p.diag_unexpected(); p.diag_unexpected();
return None; return None;
@ -150,27 +153,6 @@ fn unicode_escape(p: &mut Parser, token: TokenUnicodeEscape) -> String {
text text
} }
/// Parse a block expression.
fn block_expr(p: &mut Parser) -> Option<Expr> {
p.push_mode(TokenMode::Code);
p.start_group(Group::Brace);
let expr = expr(p);
while !p.eof() {
p.diag_unexpected();
}
p.pop_mode();
p.end_group();
expr
}
/// Parse a parenthesized function call.
fn paren_call(p: &mut Parser, name: Spanned<Ident>) -> ExprCall {
p.start_group(Group::Paren);
let args = p.span(arguments);
p.end_group();
ExprCall { name, args }
}
/// Parse a bracketed function call. /// Parse a bracketed function call.
fn bracket_call(p: &mut Parser) -> ExprCall { fn bracket_call(p: &mut Parser) -> ExprCall {
p.push_mode(TokenMode::Code); p.push_mode(TokenMode::Code);
@ -189,7 +171,7 @@ fn bracket_call(p: &mut Parser) -> ExprCall {
p.end_group(); p.end_group();
if p.peek() == Some(Token::LeftBracket) { if p.peek() == Some(Token::LeftBracket) {
let body = p.span(|p| Expr::Content(bracket_body(p))); let body = p.span(|p| Expr::Template(bracket_body(p)));
inner.span.expand(body.span); inner.span.expand(body.span);
inner.v.args.v.push(Argument::Pos(body)); inner.v.args.v.push(Argument::Pos(body));
} }
@ -197,7 +179,7 @@ fn bracket_call(p: &mut Parser) -> ExprCall {
while let Some(mut top) = outer.pop() { while let Some(mut top) = outer.pop() {
let span = inner.span; let span = inner.span;
let node = inner.map(|c| Node::Expr(Expr::Call(c))); let node = inner.map(|c| Node::Expr(Expr::Call(c)));
let expr = Expr::Content(vec![node]).with_span(span); let expr = Expr::Template(vec![node]).with_span(span);
top.v.args.v.push(Argument::Pos(expr)); top.v.args.v.push(Argument::Pos(expr));
inner = top; inner = top;
} }
@ -236,6 +218,19 @@ fn bracket_body(p: &mut Parser) -> Tree {
tree tree
} }
/// Parse a code block: `{...}`.
fn code_block(p: &mut Parser) -> Option<Expr> {
p.push_mode(TokenMode::Code);
p.start_group(Group::Brace);
let expr = expr(p);
while !p.eof() {
p.diag_unexpected();
}
p.pop_mode();
p.end_group();
expr
}
/// Parse an expression: `term (+ term)*`. /// Parse an expression: `term (+ term)*`.
fn expr(p: &mut Parser) -> Option<Expr> { fn expr(p: &mut Parser) -> Option<Expr> {
binops(p, term, |token| match token { binops(p, term, |token| match token {
@ -297,15 +292,14 @@ fn factor(p: &mut Parser) -> Option<Expr> {
/// Parse a value. /// Parse a value.
fn value(p: &mut Parser) -> Option<Expr> { fn value(p: &mut Parser) -> Option<Expr> {
let expr = match p.peek() { let expr = match p.peek() {
// Bracketed function call. // Template.
Some(Token::LeftBracket) => { Some(Token::LeftBracket) => {
let node = p.span(|p| Node::Expr(Expr::Call(bracket_call(p)))); return Some(Expr::Template(template(p)));
return Some(Expr::Content(vec![node]));
} }
// Content expression. // Nested block.
Some(Token::LeftBrace) => { Some(Token::LeftBrace) => {
return Some(Expr::Content(content(p))); return code_block(p);
} }
// Dictionary or just a parenthesized expression. // Dictionary or just a parenthesized expression.
@ -346,16 +340,24 @@ fn value(p: &mut Parser) -> Option<Expr> {
Some(expr) Some(expr)
} }
// Parse a content value: `{...}`. // Parse a template value: `[...]`.
fn content(p: &mut Parser) -> Tree { fn template(p: &mut Parser) -> Tree {
p.push_mode(TokenMode::Markup); p.push_mode(TokenMode::Markup);
p.start_group(Group::Brace); p.start_group(Group::Bracket);
let tree = tree(p); let tree = tree(p);
p.pop_mode(); p.pop_mode();
p.end_group(); p.end_group();
tree tree
} }
/// Parse a parenthesized function call.
fn paren_call(p: &mut Parser, name: Spanned<Ident>) -> ExprCall {
p.start_group(Group::Paren);
let args = p.span(arguments);
p.end_group();
ExprCall { name, args }
}
/// Parse an identifier. /// Parse an identifier.
fn ident(p: &mut Parser) -> Option<Ident> { fn ident(p: &mut Parser) -> Option<Ident> {
p.eat_map(|token| match token { p.eat_map(|token| match token {

View File

@ -26,7 +26,7 @@ macro_rules! t {
$(spans = $spans;)? $(spans = $spans;)?
let Pass { output, feedback } = parse($src); let Pass { output, feedback } = parse($src);
check($src, Content![@$($node),*], output, spans); check($src, Template![@$($node),*], output, spans);
check( check(
$src, $src,
vec![ vec![
@ -158,9 +158,9 @@ macro_rules! Args {
}; };
} }
macro_rules! Content { macro_rules! Template {
(@$($node:expr),* $(,)?) => (vec![$(into!($node)),*]); (@$($node:expr),* $(,)?) => (vec![$(into!($node)),*]);
($($tts:tt)*) => (Expr::Content(Content![@$($tts)*])); ($($tts:tt)*) => (Expr::Template(Template![@$($tts)*]));
} }
macro_rules! Call { macro_rules! Call {
@ -227,30 +227,30 @@ fn test_parse_simple_nodes() {
fn test_parse_headings() { fn test_parse_headings() {
// Basics with spans. // Basics with spans.
t!("# a" t!("# a"
nodes: [S(0..3, Heading(S(0..1, 0), Content![ nodes: [S(0..3, Heading(S(0..1, 0), Template![
@S(1..2, Space), S(2..3, Text("a")) @S(1..2, Space), S(2..3, Text("a"))
]))], ]))],
spans: true); spans: true);
// Multiple hashtags. // Multiple hashtags.
t!("### three" Heading(2, Content![@Space, Text("three")])); t!("### three" Heading(2, Template![@Space, Text("three")]));
t!("###### six" Heading(5, Content![@Space, Text("six")])); t!("###### six" Heading(5, Template![@Space, Text("six")]));
// Start of heading. // Start of heading.
t!("/**/#" Heading(0, Content![@])); t!("/**/#" Heading(0, Template![@]));
t!("[f][# ok]" Call!("f", Args![Content![Heading(0, Content![ t!("[f][# ok]" Call!("f", Args![Template![Heading(0, Template![
@Space, Text("ok") @Space, Text("ok")
])]])); ])]]));
// End of heading. // End of heading.
t!("# a\nb" Heading(0, Content![@Space, Text("a")]), Space, Text("b")); t!("# a\nb" Heading(0, Template![@Space, Text("a")]), Space, Text("b"));
// Continued heading. // Continued heading.
t!("# a{\n1\n}b" Heading(0, Content![ t!("# a{\n1\n}b" Heading(0, Template![
@Space, Text("a"), Block(Int(1)), Text("b") @Space, Text("a"), Block(Int(1)), Text("b")
])); ]));
t!("# a[f][\n\n]d" Heading(0, Content![@ t!("# a[f][\n\n]d" Heading(0, Template![@
Space, Text("a"), Call!("f", Args![Content![Parbreak]]), Text("d"), Space, Text("a"), Call!("f", Args![Template![Parbreak]]), Text("d"),
])); ]));
// No heading. // No heading.
@ -260,7 +260,7 @@ fn test_parse_headings() {
// Too many hashtags. // Too many hashtags.
t!("####### seven" t!("####### seven"
nodes: [Heading(5, Content![@Space, Text("seven")])], nodes: [Heading(5, Template![@Space, Text("seven")])],
warnings: [S(0..7, "section depth should not exceed 6")]); warnings: [S(0..7, "section depth should not exceed 6")]);
} }
@ -293,13 +293,9 @@ fn test_parse_escape_sequences() {
#[test] #[test]
fn test_parse_groups() { fn test_parse_groups() {
// Test paren group. // Test paren group.
t!("{([v 1) + 3}" t!("{({1) + 3}"
nodes: [Block(Binary( nodes: [Block(Binary(Int(1), Add, Int(3)))],
Content![Call!("v", Args![Int(1)])], errors: [S(4..4, "expected closing brace")]);
Add,
Int(3),
))],
errors: [S(6..6, "expected closing bracket")]);
// Test bracket group. // Test bracket group.
t!("[)" t!("[)"
@ -307,19 +303,18 @@ fn test_parse_groups() {
errors: [S(1..2, "expected function name, found closing paren"), errors: [S(1..2, "expected function name, found closing paren"),
S(2..2, "expected closing bracket")]); S(2..2, "expected closing bracket")]);
t!("[v {*]_" t!("[v [*]"
nodes: [Call!("v", Args![Content![Strong]]), Emph], nodes: [Call!("v", Args![Template![Strong]])],
errors: [S(5..5, "expected closing brace")]); errors: [S(6..6, "expected closing bracket")]);
// Test brace group. // Test brace group.
t!("{1 + [}" t!("{1 + [}"
nodes: [Block(Binary(Int(1), Add, Content![Call!("")]))], nodes: [Block(Binary(Int(1), Add, Template![]))],
errors: [S(6..6, "expected function name"), errors: [S(6..6, "expected closing bracket")]);
S(6..6, "expected closing bracket")]);
// Test subheader group. // Test subheader group.
t!("[v (|u )]" t!("[v (|u )]"
nodes: [Call!("v", Args![Array![], Content![Call!("u")]])], nodes: [Call!("v", Args![Array![], Template![Call!("u")]])],
errors: [S(4..4, "expected closing paren"), errors: [S(4..4, "expected closing paren"),
S(7..8, "expected expression, found closing paren")]); S(7..8, "expected expression, found closing paren")]);
} }
@ -331,7 +326,7 @@ fn test_parse_blocks() {
// Function calls. // Function calls.
t!("{f()}" Call!("f")); t!("{f()}" Call!("f"));
t!("{[f]}" Block(Content![Call!("f")])); t!("{[[f]]}" Block(Template![Call!("f")]));
// Missing or bad value. // Missing or bad value.
t!("{}{1u}" t!("{}{1u}"
@ -353,15 +348,15 @@ fn test_parse_bracket_funcs() {
t!("[ v ]" Call!("v")); t!("[ v ]" Call!("v"));
// Body and no body. // Body and no body.
t!("[v][[f]]" Call!("v", Args![Content![Call!("f")]])); t!("[v][[f]]" Call!("v", Args![Template![Call!("f")]]));
t!("[v][v][v]" Call!("v", Args![Content![Text("v")]]), Call!("v")); t!("[v][v][v]" Call!("v", Args![Template![Text("v")]]), Call!("v"));
t!("[v] [f]" Call!("v"), Space, Call!("f")); t!("[v] [f]" Call!("v"), Space, Call!("f"));
// Spans. // Spans.
t!("[v 1][📐]" t!("[v 1][📐]"
nodes: [S(0..11, Call!(S(1..2, "v"), S(3..4, Args![ nodes: [S(0..11, Call!(S(1..2, "v"), S(3..4, Args![
S(3..4, Int(1)), S(3..4, Int(1)),
S(5..11, Content![S(6..10, Text("📐"))]), S(5..11, Template![S(6..10, Text("📐"))]),
])))], ])))],
spans: true); spans: true);
@ -389,35 +384,35 @@ fn test_parse_bracket_funcs() {
// Raw in body eats closing bracket. // Raw in body eats closing bracket.
t!("[v][`a]`" t!("[v][`a]`"
nodes: [Call!("v", Args![Content![Raw(None, &["a]"], true)]])], nodes: [Call!("v", Args![Template![Raw(None, &["a]"], true)]])],
errors: [S(8..8, "expected closing bracket")]); errors: [S(8..8, "expected closing bracket")]);
} }
#[test] #[test]
fn test_parse_chaining() { fn test_parse_chaining() {
// Basic. // Basic.
t!("[a | b]" Call!("a", Args![Content![Call!("b")]])); t!("[a | b]" Call!("a", Args![Template![Call!("b")]]));
t!("[a|b|c]" Call!("a", Args![Content![ t!("[a|b|c]" Call!("a", Args![Template![
Call!("b", Args![Content![Call!("c")]]) Call!("b", Args![Template![Call!("c")]])
]])); ]]));
// With body and spans. // With body and spans.
t!("[a|b][💕]" t!("[a|b][💕]"
nodes: [S(0..11, Call!(S(1..2, "a"), S(2..2, Args![ nodes: [S(0..11, Call!(S(1..2, "a"), S(2..2, Args![
S(3..11, Content![S(3..11, Call!(S(3..4, "b"), S(4..4, Args![ S(3..11, Template![S(3..11, Call!(S(3..4, "b"), S(4..4, Args![
S(5..11, Content![S(6..10, Text("💕"))]) S(5..11, Template![S(6..10, Text("💕"))])
])))]) ])))])
])))], ])))],
spans: true); spans: true);
// No name in second subheader. // No name in second subheader.
t!("[a 1|]" t!("[a 1|]"
nodes: [Call!("a", Args![Int(1), Content![Call!("")]])], nodes: [Call!("a", Args![Int(1), Template![Call!("")]])],
errors: [S(5..5, "expected function name")]); errors: [S(5..5, "expected function name")]);
// No name in first subheader. // No name in first subheader.
t!("[|a true]" t!("[|a true]"
nodes: [Call!("", Args![Content![Call!("a", Args![Bool(true)])]])], nodes: [Call!("", Args![Template![Call!("a", Args![Bool(true)])]])],
errors: [S(1..1, "expected function name")]); errors: [S(1..1, "expected function name")]);
} }
@ -534,12 +529,12 @@ fn test_parse_dictionaries() {
errors: [S(8..9, "expected named pair, found expression")]); errors: [S(8..9, "expected named pair, found expression")]);
// Dictionary marker followed by more stuff. // Dictionary marker followed by more stuff.
t!("{(:1 b:2, true::)}" t!("{(:1 b:[], true::)}"
nodes: [Block(Dict!["b" => Int(2)])], nodes: [Block(Dict!["b" => Template![]])],
errors: [S(3..4, "expected named pair, found expression"), errors: [S(3..4, "expected named pair, found expression"),
S(4..4, "expected comma"), S(4..4, "expected comma"),
S(10..14, "name must be identifier"), S(11..15, "name must be identifier"),
S(15..16, "expected expression, found colon")]); S(16..17, "expected expression, found colon")]);
} }
#[test] #[test]
@ -615,7 +610,11 @@ fn test_parse_values() {
errors: [S(1..4, "invalid color")]); errors: [S(1..4, "invalid color")]);
// Content. // Content.
t!("{{*Hi*}}" Block(Content![Strong, Text("Hi"), Strong])); t!("{[*Hi*]}" Block(Template![Strong, Text("Hi"), Strong]));
// Nested blocks.
t!("{{1}}" Block(Int(1)));
t!("{{{1+2}}}" Block(Binary(Int(1), Add, Int(2))));
// Invalid tokens. // Invalid tokens.
t!("{1u}" t!("{1u}"

View File

@ -562,7 +562,7 @@ mod tests {
} }
#[test] #[test]
fn test_tokenize_body_symbols() { fn test_tokenize_markup_symbols() {
// Test markup tokens. // Test markup tokens.
t!(Markup[" a1"]: "*" => Star); t!(Markup[" a1"]: "*" => Star);
t!(Markup: "_" => Underscore); t!(Markup: "_" => Underscore);
@ -572,7 +572,7 @@ mod tests {
} }
#[test] #[test]
fn test_tokenize_header_symbols() { fn test_tokenize_code_symbols() {
// Test all symbols. // Test all symbols.
t!(Code: "," => Comma); t!(Code: "," => Comma);
t!(Code: ":" => Colon); t!(Code: ":" => Colon);
@ -653,6 +653,27 @@ mod tests {
t!(Code[" /"]: "falser" => Ident("falser")); t!(Code[" /"]: "falser" => Ident("falser"));
} }
#[test]
fn test_tokenize_whitespace() {
// Test basic whitespace.
t!(Both["a1/"]: "" => );
t!(Both["a1/"]: " " => Space(0));
t!(Both["a1/"]: " " => Space(0));
t!(Both["a1/"]: "\t" => Space(0));
t!(Both["a1/"]: " \t" => Space(0));
t!(Both["a1/"]: "\u{202F}" => Space(0));
// Test newline counting.
t!(Both["a1/"]: "\n" => Space(1));
t!(Both["a1/"]: "\n " => Space(1));
t!(Both["a1/"]: " \n" => Space(1));
t!(Both["a1/"]: " \n " => Space(1));
t!(Both["a1/"]: "\r\n" => Space(1));
t!(Both["a1/"]: " \n\t \n " => Space(2));
t!(Both["a1/"]: "\n\r" => Space(2));
t!(Both["a1/"]: " \r\r\n \x0D" => Space(3));
}
#[test] #[test]
fn test_tokenize_text() { fn test_tokenize_text() {
// Test basic text. // Test basic text.
@ -738,27 +759,6 @@ mod tests {
t!(Markup: r"\u{1🏕}" => UnicodeEscape("1", false), Text("🏕"), RightBrace); t!(Markup: r"\u{1🏕}" => UnicodeEscape("1", false), Text("🏕"), RightBrace);
} }
#[test]
fn test_tokenize_whitespace() {
// Test basic whitespace.
t!(Both["a1/"]: "" => );
t!(Both["a1/"]: " " => Space(0));
t!(Both["a1/"]: " " => Space(0));
t!(Both["a1/"]: "\t" => Space(0));
t!(Both["a1/"]: " \t" => Space(0));
t!(Both["a1/"]: "\u{202F}" => Space(0));
// Test newline counting.
t!(Both["a1/"]: "\n" => Space(1));
t!(Both["a1/"]: "\n " => Space(1));
t!(Both["a1/"]: " \n" => Space(1));
t!(Both["a1/"]: " \n " => Space(1));
t!(Both["a1/"]: "\r\n" => Space(1));
t!(Both["a1/"]: " \n\t \n " => Space(2));
t!(Both["a1/"]: "\n\r" => Space(2));
t!(Both["a1/"]: " \r\r\n \x0D" => Space(3));
}
#[test] #[test]
fn test_tokenize_idents() { fn test_tokenize_idents() {
// Test valid identifiers. // Test valid identifiers.

View File

@ -3,8 +3,8 @@
pub use crate::diag::{Feedback, Pass}; pub use crate::diag::{Feedback, Pass};
#[doc(no_inline)] #[doc(no_inline)]
pub use crate::eval::{ pub use crate::eval::{
Args, CastResult, Eval, EvalContext, Value, ValueAny, ValueArray, ValueContent, Args, CastResult, Eval, EvalContext, Value, ValueAny, ValueArray, ValueDict,
ValueDict, ValueTemplate,
}; };
pub use crate::geom::*; pub use crate::geom::*;
#[doc(no_inline)] #[doc(no_inline)]

View File

@ -38,8 +38,8 @@ pub enum Expr {
Array(ExprArray), Array(ExprArray),
/// A dictionary expression: `(color: #f79143, pattern: dashed)`. /// A dictionary expression: `(color: #f79143, pattern: dashed)`.
Dict(ExprDict), Dict(ExprDict),
/// A content expression: `{*Hello* there!}`. /// A template expression: `[*Hi* there!]`.
Content(ExprContent), Template(ExprTemplate),
} }
impl Pretty for Expr { impl Pretty for Expr {
@ -60,24 +60,16 @@ impl Pretty for Expr {
Self::Binary(binary) => binary.pretty(p), Self::Binary(binary) => binary.pretty(p),
Self::Array(array) => array.pretty(p), Self::Array(array) => array.pretty(p),
Self::Dict(dict) => dict.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. /// Pretty print a template in an expression context.
pub fn pretty_content_expr(tree: &Tree, p: &mut Printer) { pub fn pretty_template_expr(tree: &Tree, p: &mut Printer) {
if let [Spanned { v: Node::Expr(Expr::Call(call)), .. }] = tree.as_slice() { p.push_str("[");
// 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); tree.pretty(p);
p.push_str("}"); p.push_str("]");
}
} }
/// An invocation of a function: `[foo ...]`, `foo(...)`. /// 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. // Find out whether this can be written with a body or as a chain.
// //
// Example: Transforms "[v {Hi}]" => "[v][Hi]". // Example: Transforms "[v [Hi]]" => "[v][Hi]".
if let [head @ .., Argument::Pos(Spanned { v: Expr::Content(content), .. })] = if let [head @ .., Argument::Pos(Spanned { v: Expr::Template(template), .. })] =
call.args.v.as_slice() call.args.v.as_slice()
{ {
// Previous arguments. // 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. // Find out whether this can written as a chain.
// //
// Example: Transforms "[v][[f]]" => "[v | f]". // 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); return pretty_bracket_call(call, p, true);
} else { } else {
p.push_str("]["); p.push_str("][");
content.pretty(p); template.pretty(p);
} }
} else if !call.args.v.is_empty() { } else if !call.args.v.is_empty() {
p.push_str(" "); p.push_str(" ");
@ -291,8 +283,8 @@ impl Pretty for ExprDict {
} }
} }
/// A content expression: `{*Hello* there!}`. /// A template expression: `[*Hi* there!]`.
pub type ExprContent = Tree; pub type ExprTemplate = Tree;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -301,8 +293,7 @@ mod tests {
#[test] #[test]
fn test_pretty_print_chaining() { fn test_pretty_print_chaining() {
// All equivalent. // 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]");
test_pretty("[v | f]", "[v | f]"); test_pretty("[v | f]", "[v | f]");
} }
@ -321,9 +312,8 @@ mod tests {
test_pretty("{(:)}", "{(:)}"); test_pretty("{(:)}", "{(:)}");
test_pretty("{(percent: 5%)}", "{(percent: 5%)}"); test_pretty("{(percent: 5%)}", "{(percent: 5%)}");
// Content expression without unncessary braces. // Content expression.
test_pretty("[v [f], 1]", "[v [f], 1]"); test_pretty("[v [[f]], 1]", "[v [[f]], 1]");
test_pretty("(func: {[f]})", "(func: [f])");
} }
#[test] #[test]

View File

@ -3,18 +3,18 @@ use super::*;
/// A syntax node, encompassing a single logical entity of parsed source code. /// A syntax node, encompassing a single logical entity of parsed source code.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Node { pub enum Node {
/// Plain text. /// Strong text was enabled / disabled.
Text(String), Strong,
/// Emphasized text was enabled / disabled.
Emph,
/// Whitespace containing less than two newlines. /// Whitespace containing less than two newlines.
Space, Space,
/// A forced line break. /// A forced line break.
Linebreak, Linebreak,
/// A paragraph break. /// A paragraph break.
Parbreak, Parbreak,
/// Strong text was enabled / disabled. /// Plain text.
Strong, Text(String),
/// Emphasized text was enabled / disabled.
Emph,
/// A section heading. /// A section heading.
Heading(NodeHeading), Heading(NodeHeading),
/// An optionally syntax-highlighted raw block. /// An optionally syntax-highlighted raw block.
@ -26,12 +26,12 @@ pub enum Node {
impl Pretty for Node { impl Pretty for Node {
fn pretty(&self, p: &mut Printer) { fn pretty(&self, p: &mut Printer) {
match self { 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::Space => p.push_str(" "),
Self::Linebreak => p.push_str(r"\"), Self::Linebreak => p.push_str(r"\"),
Self::Parbreak => p.push_str("\n\n"), Self::Parbreak => p.push_str("\n\n"),
Self::Strong => p.push_str("*"), Self::Text(text) => p.push_str(&text),
Self::Emph => p.push_str("_"),
Self::Heading(heading) => heading.pretty(p), Self::Heading(heading) => heading.pretty(p),
Self::Raw(raw) => raw.pretty(p), Self::Raw(raw) => raw.pretty(p),
Self::Expr(expr) => pretty_expr_node(expr, 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. // Remove unncessary nesting of content and expression blocks.
// //
// Example: Transforms "{{Hi}}" => "Hi". // Example: Transforms "{[Hi]}" => "Hi".
Expr::Content(content) => content.pretty(p), Expr::Template(template) => template.pretty(p),
_ => { _ => {
p.push_str("{"); p.push_str("{");
@ -179,9 +179,9 @@ mod tests {
#[test] #[test]
fn test_pretty_print_removes_nesting() { fn test_pretty_print_removes_nesting() {
// Even levels of nesting do not matter. // Nesting does not matter.
test_pretty("{{Hi}}", "Hi"); test_pretty("{{x}}", "{x}");
test_pretty("{{{{Hi}}}}", "Hi"); test_pretty("{{{x}}}", "{x}");
} }
#[test] #[test]
@ -189,8 +189,7 @@ mod tests {
// All reduces to a simple bracket call. // 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_pretty("{[[v]]}", "[v]");
test_pretty("{{[v]}}", "[v]");
} }
#[test] #[test]