summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
authorMartin <mhaug@live.de>2020-08-29 13:58:30 +0200
committerGitHub <noreply@github.com>2020-08-29 13:58:30 +0200
commit5e1fd35926b094b6e4630181e8cf8ee5c3d2c22f (patch)
tree1142d3e9fd4d9c84b756fa138c99063af2ae9366 /src/syntax
parentc2b6f2dc359d3b5c5b09996b8902c09e27271b4c (diff)
parent236750c35fbad916b63774df917cbc436f1d1a8c (diff)
Merge branch 'master' into code-blocks
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/parsing.rs141
-rw-r--r--src/syntax/tree.rs4
2 files changed, 61 insertions, 84 deletions
diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs
index e9bbf2e5..0e6fba5e 100644
--- a/src/syntax/parsing.rs
+++ b/src/syntax/parsing.rs
@@ -50,27 +50,20 @@ impl<'s> Parser<'s> {
impl Parser<'_> {
fn parse_body_contents(&mut self) -> SyntaxTree {
let mut tree = SyntaxTree::new();
- let mut par = SyntaxTree::new();
while let Some(token) = self.peek() {
- par.push(match token.v {
+ tree.push(match token.v {
// Starting from two newlines counts as a paragraph break, a single
// newline does not.
- Token::Space(newlines) => if newlines < 2 {
- self.with_span(SyntaxNode::Spacing)
+ Token::Space(newlines) => self.with_span(if newlines < 2 {
+ SyntaxNode::Spacing
} else {
- // End the current paragraph if it is not empty.
- if let (Some(first), Some(last)) = (par.first(), par.last()) {
- let span = Span::merge(first.span, last.span);
- let node = SyntaxNode::Par(std::mem::take(&mut par));
- tree.push(Spanned::new(node, span));
- }
- self.eat();
- continue;
- }
+ SyntaxNode::Parbreak
+ }),
+
Token::LineComment(_) | Token::BlockComment(_) => {
self.eat();
- continue
+ continue;
}
Token::LeftBracket => {
@@ -134,12 +127,6 @@ impl Parser<'_> {
});
}
- if let (Some(first), Some(last)) = (par.first(), par.last()) {
- let span = Span::merge(first.span, last.span);
- let node = SyntaxNode::Par(par);
- tree.push(Spanned::new(node, span));
- }
-
tree
}
}
@@ -753,6 +740,7 @@ mod tests {
use SyntaxNode::{
Spacing as S,
Linebreak as L,
+ Parbreak as P,
ToggleItalic as I,
ToggleBolder as B,
};
@@ -765,6 +753,7 @@ mod tests {
};
}
+
fn Lang(text: &str) -> Option<Spanned<Ident>> { Some(Spanned::zero(Ident(text.to_string()))) }
macro_rules! C {
@@ -772,11 +761,7 @@ mod tests {
SyntaxNode::CodeBlock(CodeBlockExpr { raw: vec![$($line.to_string()) ,*], lang: $lang })
};
}
-
- macro_rules! P {
- ($($tts:tt)*) => { SyntaxNode::Par(Tree![@$($tts)*]) };
- }
-
+
macro_rules! F {
($($tts:tt)*) => { SyntaxNode::Call(Call!(@$($tts)*)) }
}
@@ -860,7 +845,7 @@ mod tests {
// Test expressions.
macro_rules! v {
($src:expr => $($tts:tt)*) => {
- t!(concat!("[val: ", $src, "]") => P![F!("val"; $($tts)*)]);
+ t!(concat!("[val: ", $src, "]") => F!("val"; $($tts)*));
}
}
@@ -945,16 +930,17 @@ mod tests {
#[test]
fn test_parse_simple_nodes() {
t!("" => );
- t!("hi" => P![T("hi")]);
- t!("*hi" => P![B, T("hi")]);
- t!("hi_" => P![T("hi"), I]);
- t!("hi you" => P![T("hi"), S, T("you")]);
- t!("\n\n\nhello" => P![T("hello")]);
- t!(r"a\ b" => P![T("a"), L, S, T("b")]);
- t!("`py`" => P![R!["py"]]);
- t!("`hi\nyou" => P![R!["hi", "you"]]);
+ t!("hi" => T("hi"));
+ t!("*hi" => B, T("hi"));
+ t!("hi_" => T("hi"), I);
+ t!("hi you" => T("hi"), S, T("you"));
+ t!("\n\n\nhello" => P, T("hello"));
+ t!(r"a\ b" => T("a"), L, S, T("b"));
+ t!("`py`" => R!["py"]);
+ t!("`hi\nyou" => R!["hi", "you"]);
e!("`hi\nyou" => s(1,3, 1,3, "expected backtick"));
- t!("`hi\\`du`" => P![R!["hi`du"]]);
+ t!("`hi\\`du`" => R!["hi`du"]);
+
t!("```java System.out.print```" => P![
C![Lang("java"), "System.out.print"]
]);
@@ -968,28 +954,23 @@ mod tests {
e!("```🌍 hi\nyou```" => s(0,3, 0,4, "expected language to be a valid identifier"));
t!("💜\n\n 🌍" => P![T("💜")], P![T("🌍")]);
- ts!("hi" => s(0,0, 0,2, P![s(0,0, 0,2, T("hi"))]));
- ts!("*Hi*" => s(0,0, 0,4, P![
- s(0,0, 0,1, B), s(0,1, 0,3, T("Hi")), s(0,3, 0,4, B),
- ]));
- ts!("💜\n\n 🌍" =>
- s(0,0, 0,1, P![s(0,0, 0,1, T("💜"))]),
- s(2,1, 2,2, P![s(2,1, 2,2, T("🌍"))]),
- );
+ ts!("hi" => s(0,0, 0,2, T("hi")));
+ ts!("*Hi*" => s(0,0, 0,1, B), s(0,1, 0,3, T("Hi")), s(0,3, 0,4, B));
+ ts!("💜\n\n 🌍" => s(0,0, 0,1, T("💜")), s(0,1, 2,1, P), s(2,1, 2,2, T("🌍")));
}
#[test]
fn test_parse_comments() {
// In body.
- t!("hi// you\nw" => P![T("hi"), S, T("w")]);
- t!("first//\n//\nsecond" => P![T("first"), S, S, T("second")]);
- t!("first//\n \nsecond" => P![T("first")], P![T("second")]);
- t!("first/*\n \n*/second" => P![T("first"), T("second")]);
+ t!("hi// you\nw" => T("hi"), S, T("w"));
+ t!("first//\n//\nsecond" => T("first"), S, S, T("second"));
+ t!("first//\n \nsecond" => T("first"), P, T("second"));
+ t!("first/*\n \n*/second" => T("first"), T("second"));
e!("🌎\n*/n" => s(1,0, 1,2, "unexpected end of block comment"));
// In header.
- t!("[val:/*12pt*/]" => P![F!("val")]);
- t!("[val \n /* \n */:]" => P![F!("val")]);
+ t!("[val:/*12pt*/]" => F!("val"));
+ t!("[val \n /* \n */:]" => F!("val"));
e!("[val \n /* \n */:]" => );
e!("[val : 12, /* \n */ 14]" => );
}
@@ -1006,7 +987,7 @@ mod tests {
#[test]
fn test_parse_function_names() {
// No closing bracket.
- t!("[" => P![F!("")]);
+ t!("[" => F!(""));
e!("[" => s(0,1, 0,1, "expected function name"),
s(0,1, 0,1, "expected closing bracket"));
@@ -1016,8 +997,8 @@ mod tests {
s(0,3, 0,3, "expected closing bracket"));
// A valid name.
- t!("[hi]" => P![F!("hi")]);
- t!("[ f]" => P![F!("f")]);
+ t!("[hi]" => F!("hi"));
+ t!("[ f]" => F!("f"));
// An invalid name.
e!("[12]" => s(0,1, 0,3, "expected function name, found number"));
@@ -1025,12 +1006,16 @@ mod tests {
}
#[test]
- fn test_parse_subgroups() {
+ fn test_parse_chaining() {
// Things the parser has to make sense of
- t!("[hi: (5.0, 2.1 >> you]" => P![F!("hi"; Table![Num(5.0), Num(2.1)], Tree![F!("you")])]);
- t!("[bold: 400, >> emph >> sub: 1cm]" => P![F!("bold"; Num(400.0), Tree![F!("emph"; Tree!(F!("sub"; Len(Length::cm(1.0)))))])]);
- t!("[box >> pad: 1pt][Hi]" => P![F!("box"; Tree![F!("pad"; Len(Length::pt(1.0)), Tree!(P![T("Hi")]))])]);
- t!("[box >>][Hi]" => P![F!("box"; Tree![P![T("Hi")]])]);
+ t!("[hi: (5.0, 2.1 >> you]" => F!("hi"; Table![Num(5.0), Num(2.1)], Tree![F!("you")]));
+ t!("[box >>][Hi]" => F!("box"; Tree![T("Hi")]));
+ t!("[box >> pad: 1pt][Hi]" => F!("box"; Tree![
+ F!("pad"; Len(Length::pt(1.0)), Tree!(T("Hi")))
+ ]));
+ t!("[bold: 400, >> emph >> sub: 1cm]" => F!("bold"; Num(400.0), Tree![
+ F!("emph"; Tree!(F!("sub"; Len(Length::cm(1.0)))))
+ ]));
// Errors for unclosed / empty predecessor groups
e!("[hi: (5.0, 2.1 >> you]" => s(0, 15, 0, 15, "expected closing paren"));
@@ -1043,7 +1028,7 @@ mod tests {
e!("[val:]" => );
// Wrong token.
- t!("[val=]" => P![F!("val")]);
+ t!("[val=]" => F!("val"));
e!("[val=]" => s(0,4, 0,4, "expected colon"));
e!("[val/🌎:$]" => s(0,4, 0,4, "expected colon"));
@@ -1056,27 +1041,25 @@ mod tests {
#[test]
fn test_parse_function_bodies() {
- t!("[val: 1][*Hi*]" => P![F!("val"; Num(1.0), Tree![P![B, T("Hi"), B]])]);
+ t!("[val: 1][*Hi*]" => F!("val"; Num(1.0), Tree![B, T("Hi"), B]));
e!(" [val][ */ ]" => s(0,8, 0,10, "unexpected end of block comment"));
// Raw in body.
- t!("[val][`Hi]`" => P![F!("val"; Tree![P![R!["Hi]"]]])]);
+ t!("[val][`Hi]`" => F!("val"; Tree![R!["Hi]"]]));
e!("[val][`Hi]`" => s(0,11, 0,11, "expected closing bracket"));
// Crazy.
- t!("[v][[v][v][v]]" => P![F!("v"; Tree![P![
- F!("v"; Tree![P![T("v")]]), F!("v")
- ]])]);
+ t!("[v][[v][v][v]]" => F!("v"; Tree![F!("v"; Tree![T("v")]), F!("v")]));
// Spanned.
- ts!(" [box][Oh my]" => s(0,0, 0,13, P![
+ ts!(" [box][Oh my]" =>
s(0,0, 0,1, S),
s(0,1, 0,13, F!(s(0,2, 0,5, "box");
- s(0,6, 0,13, Tree![s(0,7, 0,12, P![
+ s(0,6, 0,13, Tree![
s(0,7, 0,9, T("Oh")), s(0,9, 0,10, S), s(0,10, 0,12, T("my"))
- ])])
+ ])
))
- ]));
+ );
}
#[test]
@@ -1097,7 +1080,7 @@ mod tests {
v!("\"a\n[]\\\"string\"" => Str("a\n[]\"string"));
// Content.
- v!("{_hi_}" => Tree![P![I, T("hi"), I]]);
+ v!("{_hi_}" => Tree![I, T("hi"), I]);
e!("[val: {_hi_}]" => );
v!("[hi]" => Tree![F!["hi"]]);
e!("[val: [hi]]" => );
@@ -1115,9 +1098,7 @@ mod tests {
s(0,13, 0,13, "expected closing bracket"));
// Spanned.
- ts!("[val: 1.4]" => s(0,0, 0,10, P![
- s(0,0, 0,10, F!(s(0,1, 0,4, "val"); s(0,6, 0,9, Num(1.4))))
- ]));
+ ts!("[val: 1.4]" => s(0,0, 0,10, F!(s(0,1, 0,4, "val"); s(0,6, 0,9, Num(1.4)))));
}
#[test]
@@ -1147,17 +1128,15 @@ mod tests {
v!("3/4*5" => Mul(Div(Num(3.0), Num(4.0)), Num(5.0)));
// Spanned.
- ts!("[val: 1 + 3]" => s(0,0, 0,12, P![s(0,0, 0,12, F!(
+ ts!("[val: 1 + 3]" => s(0,0, 0,12, F!(
s(0,1, 0,4, "val"); s(0,6, 0,11, Add(
s(0,6, 0,7, Num(1.0)),
s(0,10, 0,11, Num(3.0)),
))
- ))]));
+ )));
// Span of parenthesized expression contains parens.
- ts!("[val: (1)]" => s(0,0, 0,10, P![
- s(0,0, 0,10, F!(s(0,1, 0,4, "val"); s(0,6, 0,9, Num(1.0))))
- ]));
+ ts!("[val: (1)]" => s(0,0, 0,10, F!(s(0,1, 0,4, "val"); s(0,6, 0,9, Num(1.0)))));
// Invalid expressions.
v!("4pt--" => Len(Length::pt(4.0)));
@@ -1184,12 +1163,10 @@ mod tests {
d!("[val: f(key=hi)]" => s(0,8, 0,11, TableKey));
// Spanned with spacing around keyword arguments.
- ts!("[val: \n hi \n = /* //\n */ \"s\n\"]" => s(0,0, 4,2, P![
- s(0,0, 4,2, F!(
- s(0,1, 0,4, "val");
- s(1,1, 1,3, "hi") => s(3,4, 4,1, Str("s\n"))
- ))
- ]));
+ ts!("[val: \n hi \n = /* //\n */ \"s\n\"]" => s(0,0, 4,2, F!(
+ s(0,1, 0,4, "val");
+ s(1,1, 1,3, "hi") => s(3,4, 4,1, Str("s\n"))
+ )));
e!("[val: \n hi \n = /* //\n */ \"s\n\"]" => );
}
diff --git a/src/syntax/tree.rs b/src/syntax/tree.rs
index ace5ad8e..313e76a4 100644
--- a/src/syntax/tree.rs
+++ b/src/syntax/tree.rs
@@ -23,6 +23,8 @@ pub enum SyntaxNode {
Spacing,
/// A forced line break.
Linebreak,
+ /// A paragraph break.
+ Parbreak,
/// Italics were enabled / disabled.
ToggleItalic,
/// Bolder was enabled / disabled.
@@ -33,8 +35,6 @@ pub enum SyntaxNode {
Raw(Vec<String>),
/// An optionally highlighted multi-line code block.
CodeBlock(CodeBlockExpr),
- /// A paragraph of child nodes.
- Par(SyntaxTree),
/// A function call.
Call(CallExpr),
}