Better parser testing

This commit is contained in:
Laurenz 2021-01-01 16:33:08 +01:00
parent 4069f0744d
commit 2b6ccd8248
8 changed files with 621 additions and 637 deletions

View File

@ -122,4 +122,16 @@ mod tests {
test("233", 0x22, 0x33, 0x33, 0xff); test("233", 0x22, 0x33, 0x33, 0xff);
test("111b", 0x11, 0x11, 0x11, 0xbb); test("111b", 0x11, 0x11, 0x11, 0xbb);
} }
#[test]
fn parse_invalid_colors() {
fn test(hex: &str) {
assert_eq!(RgbaColor::from_str(hex), Err(ParseRgbaError));
}
test("12345");
test("a5");
test("14B2AH");
test("f075ff011");
}
} }

View File

@ -18,9 +18,9 @@ use std::rc::Rc;
use fontdock::FontStyle; use fontdock::FontStyle;
use crate::color::Color;
use crate::diag::Diag; use crate::diag::Diag;
use crate::diag::{Deco, Feedback, Pass}; use crate::diag::{Deco, Feedback, Pass};
use crate::color::Color;
use crate::env::SharedEnv; use crate::env::SharedEnv;
use crate::geom::{BoxAlign, Dir, Flow, Gen, Length, Linear, Relative, Sides, Size}; use crate::geom::{BoxAlign, Dir, Flow, Gen, Length, Linear, Relative, Sides, Size};
use crate::layout::{ use crate::layout::{
@ -328,6 +328,11 @@ impl Eval for SynNode {
fn eval(&self, ctx: &mut EvalContext) -> Self::Output { fn eval(&self, ctx: &mut EvalContext) -> Self::Output {
match self { match self {
SynNode::Text(text) => {
let node = ctx.make_text_node(text.clone());
ctx.push(node);
}
SynNode::Space => { SynNode::Space => {
let em = ctx.state.font.font_size(); let em = ctx.state.font.font_size();
ctx.push(Spacing { ctx.push(Spacing {
@ -336,11 +341,6 @@ impl Eval for SynNode {
}); });
} }
SynNode::Text(text) => {
let node = ctx.make_text_node(text.clone());
ctx.push(node);
}
SynNode::Linebreak => { SynNode::Linebreak => {
ctx.end_par_group(); ctx.end_par_group();
ctx.start_par_group(); ctx.start_par_group();
@ -356,10 +356,12 @@ impl Eval for SynNode {
ctx.start_par_group(); ctx.start_par_group();
} }
SynNode::Emph => ctx.state.font.emph ^= true,
SynNode::Strong => ctx.state.font.strong ^= true, SynNode::Strong => ctx.state.font.strong ^= true,
SynNode::Emph => ctx.state.font.emph ^= true,
SynNode::Heading(heading) => heading.eval(ctx), SynNode::Heading(heading) => heading.eval(ctx),
SynNode::Raw(raw) => raw.eval(ctx), SynNode::Raw(raw) => raw.eval(ctx),
SynNode::Expr(expr) => expr.eval(ctx).eval(ctx), SynNode::Expr(expr) => expr.eval(ctx).eval(ctx),
} }
} }

View File

@ -33,10 +33,10 @@ fn tree(p: &mut Parser) -> SynTree {
let mut tree = vec![]; let mut tree = vec![];
while !p.eof() { while !p.eof() {
if let Some(node) = p.span_if(|p| node(p, at_start)) { if let Some(node) = p.span_if(|p| node(p, at_start)) {
if node.v == SynNode::Parbreak { match node.v {
at_start = true; SynNode::Parbreak => at_start = true,
} else if node.v != SynNode::Space { SynNode::Space => {}
at_start = false; _ => at_start = false,
} }
tree.push(node); tree.push(node);
} }
@ -46,9 +46,7 @@ fn tree(p: &mut Parser) -> SynTree {
/// Parse a syntax node. /// Parse a syntax node.
fn node(p: &mut Parser, at_start: bool) -> Option<SynNode> { fn node(p: &mut Parser, at_start: bool) -> Option<SynNode> {
let start = p.next_start(); let node = match p.peek()? {
let node = match p.eat()? {
// Spaces.
Token::Space(newlines) => { Token::Space(newlines) => {
if newlines < 2 { if newlines < 2 {
SynNode::Space SynNode::Space
@ -56,61 +54,59 @@ fn node(p: &mut Parser, at_start: bool) -> Option<SynNode> {
SynNode::Parbreak SynNode::Parbreak
} }
} }
// Text.
Token::Text(text) => SynNode::Text(text.into()), Token::Text(text) => SynNode::Text(text.into()),
// Comments. Token::LineComment(_) | Token::BlockComment(_) => {
Token::LineComment(_) | Token::BlockComment(_) => return None, p.eat();
return None;
}
// Markup.
Token::Star => SynNode::Strong, Token::Star => SynNode::Strong,
Token::Underscore => SynNode::Emph, Token::Underscore => SynNode::Emph,
Token::Hashtag => {
if at_start {
SynNode::Heading(heading(p, start))
} else {
SynNode::Text(p.eaten_from(start).into())
}
}
Token::Tilde => SynNode::Text("\u{00A0}".into()), Token::Tilde => SynNode::Text("\u{00A0}".into()),
Token::Backslash => SynNode::Linebreak, Token::Backslash => SynNode::Linebreak,
Token::UnicodeEscape(token) => SynNode::Text(unicode_escape(p, token, start)), Token::Hashtag => {
Token::Raw(token) => SynNode::Raw(raw(p, token)), if at_start {
return Some(SynNode::Heading(heading(p)));
} else {
SynNode::Text(p.get(p.peek_span()).into())
}
}
Token::Raw(t) => SynNode::Raw(raw(p, t)),
Token::UnicodeEscape(t) => SynNode::Text(unicode_escape(p, t)),
// Functions.
Token::LeftBracket => { Token::LeftBracket => {
p.jump(start); return Some(SynNode::Expr(Expr::Call(bracket_call(p))));
SynNode::Expr(Expr::Call(bracket_call(p)))
} }
// Blocks.
Token::LeftBrace => { Token::LeftBrace => {
p.jump(start); return Some(SynNode::Expr(block_expr(p)?));
SynNode::Expr(block_expr(p)?)
} }
// Bad tokens.
_ => { _ => {
p.jump(start);
p.diag_unexpected(); p.diag_unexpected();
return None; return None;
} }
}; };
p.eat();
Some(node) Some(node)
} }
/// Parse a heading. /// Parse a heading.
fn heading(p: &mut Parser, start: Pos) -> NodeHeading { fn heading(p: &mut Parser) -> NodeHeading {
// Parse the section depth. // Count hashtags.
let mut level = p.span(|p| {
p.eat_assert(Token::Hashtag);
let mut level = 0u8; let mut level = 0u8;
while p.eat_if(Token::Hashtag) { while p.eat_if(Token::Hashtag) {
level = level.saturating_add(1); level = level.saturating_add(1);
} }
level
});
let mut level = level.span_with(start .. p.last_end());
if level.v > 5 { if level.v > 5 {
p.diag(warning!(level.span, "section depth should be at most 6")); p.diag(warning!(level.span, "section depth should not exceed 6"));
level.v = 5; level.v = 5;
} }
@ -125,25 +121,23 @@ fn heading(p: &mut Parser, start: Pos) -> NodeHeading {
NodeHeading { level, contents } NodeHeading { level, contents }
} }
/// Parse a raw block. /// Handle a raw block.
fn raw(p: &mut Parser, token: TokenRaw) -> NodeRaw { fn raw(p: &mut Parser, token: TokenRaw) -> NodeRaw {
let span = p.peek_span();
let raw = resolve::resolve_raw(token.text, token.backticks); let raw = resolve::resolve_raw(token.text, token.backticks);
if !token.terminated { if !token.terminated {
p.diag(error!(p.last_end(), "expected backtick(s)")); p.diag(error!(span.end, "expected backtick(s)"));
} }
raw raw
} }
/// Parse a unicode escape sequence. /// Handle a unicode escape sequence.
fn unicode_escape(p: &mut Parser, token: TokenUnicodeEscape, start: Pos) -> String { fn unicode_escape(p: &mut Parser, token: TokenUnicodeEscape) -> String {
let span = Span::new(start, p.last_end()); let span = p.peek_span();
let text = if let Some(c) = resolve::resolve_hex(token.sequence) { let text = if let Some(c) = resolve::resolve_hex(token.sequence) {
c.to_string() c.to_string()
} else { } else {
// Print out the escape sequence verbatim if it is // Print out the escape sequence verbatim if it is invalid.
// invalid.
p.diag(error!(span, "invalid unicode escape sequence")); p.diag(error!(span, "invalid unicode escape sequence"));
p.get(span).into() p.get(span).into()
}; };
@ -155,6 +149,24 @@ fn unicode_escape(p: &mut Parser, token: TokenUnicodeEscape, start: Pos) -> Stri
text text
} }
/// Parse a block expression.
fn block_expr(p: &mut Parser) -> Option<Expr> {
p.push_mode(TokenMode::Header);
p.start_group(Group::Brace);
let expr = expr(p);
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(|p| dict_contents(p).0);
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::Header); p.push_mode(TokenMode::Header);
@ -180,7 +192,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(Expr::Call).map(SynNode::Expr); let node = inner.map(|c| SynNode::Expr(Expr::Call(c)));
let expr = Expr::Lit(Lit::Content(vec![node])).span_with(span); let expr = Expr::Lit(Lit::Content(vec![node])).span_with(span);
top.v.args.v.0.push(LitDictEntry { key: None, expr }); top.v.args.v.0.push(LitDictEntry { key: None, expr });
inner = top; inner = top;
@ -220,44 +232,16 @@ fn bracket_body(p: &mut Parser) -> SynTree {
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(|p| dict_contents(p).0);
p.end_group();
ExprCall { name, args }
}
/// Parse a block expression.
fn block_expr(p: &mut Parser) -> Option<Expr> {
p.push_mode(TokenMode::Header);
p.start_group(Group::Brace);
let expr = expr(p);
p.pop_mode();
p.end_group();
expr
}
/// Parse the contents of a dictionary. /// Parse the contents of a dictionary.
fn dict_contents(p: &mut Parser) -> (LitDict, bool) { fn dict_contents(p: &mut Parser) -> (LitDict, bool) {
let mut dict = LitDict::new(); let mut dict = LitDict::new();
let mut missing_coma = None;
let mut comma_and_keyless = true; let mut comma_and_keyless = true;
let mut expected_comma = None;
loop { while !p.eof() {
if p.eof() { if let Some(entry) = dict_entry(p) {
break; let behind = entry.expr.span.end;
} if let Some(pos) = missing_coma.take() {
let entry = if let Some(entry) = dict_entry(p) {
entry
} else {
expected_comma = None;
p.diag_unexpected();
continue;
};
if let Some(pos) = expected_comma.take() {
p.diag_expected_at("comma", pos); p.diag_expected_at("comma", pos);
} }
@ -266,18 +250,17 @@ fn dict_contents(p: &mut Parser) -> (LitDict, bool) {
p.deco(Deco::DictKey.span_with(key.span)); p.deco(Deco::DictKey.span_with(key.span));
} }
let behind = entry.expr.span.end;
dict.0.push(entry); dict.0.push(entry);
if p.eof() { if p.eof() {
break; break;
} }
if !p.eat_if(Token::Comma) { if p.eat_if(Token::Comma) {
expected_comma = Some(behind);
}
comma_and_keyless = false; comma_and_keyless = false;
} else {
missing_coma = Some(behind);
}
}
} }
let coercible = comma_and_keyless && !dict.0.is_empty(); let coercible = comma_and_keyless && !dict.0.is_empty();
@ -291,15 +274,10 @@ fn dict_entry(p: &mut Parser) -> Option<LitDictEntry> {
// Key-value pair. // Key-value pair.
Some(Token::Colon) => { Some(Token::Colon) => {
p.eat_assert(Token::Colon); p.eat_assert(Token::Colon);
if let Some(expr) = p.span_if(expr) { p.span_if(expr).map(|expr| LitDictEntry {
Some(LitDictEntry {
key: Some(ident.map(|id| DictKey::Str(id.0))), key: Some(ident.map(|id| DictKey::Str(id.0))),
expr, expr,
}) })
} else {
p.diag_expected("value");
None
}
} }
// Function call. // Function call.
@ -318,16 +296,14 @@ fn dict_entry(p: &mut Parser) -> Option<LitDictEntry> {
expr: ident.map(|id| Expr::Lit(Lit::Ident(id))), expr: ident.map(|id| Expr::Lit(Lit::Ident(id))),
}), }),
} }
} else if let Some(expr) = p.span_if(expr) {
Some(LitDictEntry { key: None, expr })
} else { } else {
None p.span_if(expr).map(|expr| LitDictEntry { key: None, 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, "summand", term, |token| match token { binops(p, term, |token| match token {
Token::Plus => Some(BinOp::Add), Token::Plus => Some(BinOp::Add),
Token::Hyphen => Some(BinOp::Sub), Token::Hyphen => Some(BinOp::Sub),
_ => None, _ => None,
@ -336,7 +312,7 @@ fn expr(p: &mut Parser) -> Option<Expr> {
/// Parse a term: `factor (* factor)*`. /// Parse a term: `factor (* factor)*`.
fn term(p: &mut Parser) -> Option<Expr> { fn term(p: &mut Parser) -> Option<Expr> {
binops(p, "factor", factor, |token| match token { binops(p, factor, |token| match token {
Token::Star => Some(BinOp::Mul), Token::Star => Some(BinOp::Mul),
Token::Slash => Some(BinOp::Div), Token::Slash => Some(BinOp::Div),
_ => None, _ => None,
@ -346,14 +322,12 @@ fn term(p: &mut Parser) -> Option<Expr> {
/// Parse binary operations of the from `a (<op> b)*`. /// Parse binary operations of the from `a (<op> b)*`.
fn binops( fn binops(
p: &mut Parser, p: &mut Parser,
operand_name: &str,
operand: fn(&mut Parser) -> Option<Expr>, operand: fn(&mut Parser) -> Option<Expr>,
op: fn(Token) -> Option<BinOp>, op: fn(Token) -> Option<BinOp>,
) -> Option<Expr> { ) -> Option<Expr> {
let mut lhs = p.span_if(operand)?; let mut lhs = p.span_if(operand)?;
loop { while let Some(op) = p.span_if(|p| p.eat_map(op)) {
if let Some(op) = p.span_if(|p| p.eat_map(op)) {
if let Some(rhs) = p.span_if(operand) { if let Some(rhs) = p.span_if(operand) {
let span = lhs.span.join(rhs.span); let span = lhs.span.join(rhs.span);
let expr = Expr::Binary(ExprBinary { let expr = Expr::Binary(ExprBinary {
@ -362,11 +336,6 @@ fn binops(
rhs: Box::new(rhs), rhs: Box::new(rhs),
}); });
lhs = expr.span_with(span); lhs = expr.span_with(span);
} else {
let span = lhs.span.join(op.span);
p.diag(error!(span, "missing right {}", operand_name));
break;
}
} else { } else {
break; break;
} }
@ -383,12 +352,8 @@ fn factor(p: &mut Parser) -> Option<Expr> {
}; };
if let Some(op) = p.span_if(|p| p.eat_map(op)) { if let Some(op) = p.span_if(|p| p.eat_map(op)) {
if let Some(expr) = p.span_if(factor) { p.span_if(factor)
Some(Expr::Unary(ExprUnary { op, expr: Box::new(expr) })) .map(|expr| Expr::Unary(ExprUnary { op, expr: Box::new(expr) }))
} else {
p.diag(error!(op.span, "missing factor"));
None
}
} else { } else {
value(p) value(p)
} }
@ -397,28 +362,28 @@ 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 start = p.next_start(); let start = p.next_start();
Some(match p.eat()? { Some(match p.eat() {
// Bracketed function call. // Bracketed function call.
Token::LeftBracket => { Some(Token::LeftBracket) => {
p.jump(start); p.jump(start);
let node = p.span(|p| SynNode::Expr(Expr::Call(bracket_call(p)))); let node = p.span(|p| SynNode::Expr(Expr::Call(bracket_call(p))));
Expr::Lit(Lit::Content(vec![node])) Expr::Lit(Lit::Content(vec![node]))
} }
// Content expression. // Content expression.
Token::LeftBrace => { Some(Token::LeftBrace) => {
p.jump(start); p.jump(start);
Expr::Lit(Lit::Content(content(p))) Expr::Lit(Lit::Content(content(p)))
} }
// Dictionary or just a parenthesized expression. // Dictionary or just a parenthesized expression.
Token::LeftParen => { Some(Token::LeftParen) => {
p.jump(start); p.jump(start);
parenthesized(p) parenthesized(p)
} }
// Function or just ident. // Function or just ident.
Token::Ident(id) => { Some(Token::Ident(id)) => {
let ident = Ident(id.into()); let ident = Ident(id.into());
let after = p.last_end(); let after = p.last_end();
if p.peek() == Some(Token::LeftParen) { if p.peek() == Some(Token::LeftParen) {
@ -429,24 +394,25 @@ fn value(p: &mut Parser) -> Option<Expr> {
} }
} }
// Atomic values. // Basic values.
Token::Bool(b) => Expr::Lit(Lit::Bool(b)), Some(Token::Bool(b)) => Expr::Lit(Lit::Bool(b)),
Token::Int(i) => Expr::Lit(Lit::Int(i)), Some(Token::Int(i)) => Expr::Lit(Lit::Int(i)),
Token::Float(f) => Expr::Lit(Lit::Float(f)), Some(Token::Float(f)) => Expr::Lit(Lit::Float(f)),
Token::Length(val, unit) => Expr::Lit(Lit::Length(val, unit)), Some(Token::Length(val, unit)) => Expr::Lit(Lit::Length(val, unit)),
Token::Percent(p) => Expr::Lit(Lit::Percent(p)), Some(Token::Percent(p)) => Expr::Lit(Lit::Percent(p)),
Token::Hex(hex) => Expr::Lit(Lit::Color(color(p, hex, start))), Some(Token::Hex(hex)) => Expr::Lit(Lit::Color(color(p, hex, start))),
Token::Str(token) => Expr::Lit(Lit::Str(string(p, token))), Some(Token::Str(token)) => Expr::Lit(Lit::Str(str(p, token))),
// No value. // No value.
_ => { _ => {
p.jump(start); p.jump(start);
p.diag_expected("expression");
return None; return None;
} }
}) })
} }
// Parse a content expression: `{...}`. // Parse a content value: `{...}`.
fn content(p: &mut Parser) -> SynTree { fn content(p: &mut Parser) -> SynTree {
p.push_mode(TokenMode::Body); p.push_mode(TokenMode::Body);
p.start_group(Group::Brace); p.start_group(Group::Brace);
@ -487,7 +453,7 @@ fn color(p: &mut Parser, hex: &str, start: Pos) -> RgbaColor {
} }
/// Parse a string. /// Parse a string.
fn string(p: &mut Parser, token: TokenStr) -> String { fn str(p: &mut Parser, token: TokenStr) -> String {
if !token.terminated { if !token.terminated {
p.diag_expected_at("quote", p.last_end()); p.diag_expected_at("quote", p.last_end());
} }

View File

@ -10,7 +10,9 @@ pub struct Parser<'s> {
/// An iterator over the source tokens. /// An iterator over the source tokens.
tokens: Tokens<'s>, tokens: Tokens<'s>,
/// The next token. /// The next token.
/// (Only `None` if we are at the end of group or end of file). next: Option<Token<'s>>,
/// The peeked token.
/// (Same as `next` except if we are at the end of group, then `None`).
peeked: Option<Token<'s>>, peeked: Option<Token<'s>>,
/// The start position of the peeked token. /// The start position of the peeked token.
next_start: Pos, next_start: Pos,
@ -28,10 +30,11 @@ impl<'s> Parser<'s> {
/// Create a new parser for the source string. /// Create a new parser for the source string.
pub fn new(src: &'s str) -> Self { pub fn new(src: &'s str) -> Self {
let mut tokens = Tokens::new(src, TokenMode::Body); let mut tokens = Tokens::new(src, TokenMode::Body);
let peeked = tokens.next(); let next = tokens.next();
Self { Self {
tokens, tokens,
peeked, next,
peeked: next,
next_start: Pos::ZERO, next_start: Pos::ZERO,
last_end: Pos::ZERO, last_end: Pos::ZERO,
modes: vec![], modes: vec![],
@ -118,7 +121,9 @@ impl<'s> Parser<'s> {
Group::Brace => self.eat_assert(Token::LeftBrace), Group::Brace => self.eat_assert(Token::LeftBrace),
Group::Subheader => {} Group::Subheader => {}
} }
self.groups.push(group); self.groups.push(group);
self.repeek();
} }
/// Ends the parsing of a group and returns the span of the whole group. /// Ends the parsing of a group and returns the span of the whole group.
@ -130,6 +135,8 @@ impl<'s> Parser<'s> {
debug_assert_eq!(self.peek(), None, "unfinished group"); debug_assert_eq!(self.peek(), None, "unfinished group");
let group = self.groups.pop().expect("no started group"); let group = self.groups.pop().expect("no started group");
self.repeek();
let end = match group { let end = match group {
Group::Paren => Some(Token::RightParen), Group::Paren => Some(Token::RightParen),
Group::Bracket => Some(Token::RightBracket), Group::Bracket => Some(Token::RightBracket),
@ -138,7 +145,7 @@ impl<'s> Parser<'s> {
}; };
if let Some(token) = end { if let Some(token) = end {
if self.peeked == Some(token) { if self.next == Some(token) {
self.bump(); self.bump();
} else { } else {
self.diag(error!(self.next_start, "expected {}", token.name())); self.diag(error!(self.next_start, "expected {}", token.name()));
@ -203,26 +210,24 @@ impl<'s> Parser<'s> {
} }
/// Peek at the next token without consuming it. /// Peek at the next token without consuming it.
pub fn peek(&mut self) -> Option<Token<'s>> { pub fn peek(&self) -> Option<Token<'s>> {
let group = match self.peeked { self.peeked
Some(Token::RightParen) => Group::Paren,
Some(Token::RightBracket) => Group::Bracket,
Some(Token::RightBrace) => Group::Brace,
Some(Token::Pipe) => Group::Subheader,
other => return other,
};
if self.groups.contains(&group) {
return None;
} }
self.peeked /// Peek at the span of the next token.
///
/// Has length zero if `peek()` returns `None`.
pub fn peek_span(&self) -> Span {
Span::new(
self.next_start,
if self.eof() { self.next_start } else { self.tokens.pos() },
)
} }
/// Checks whether the next token fulfills a condition. /// Checks whether the next token fulfills a condition.
/// ///
/// Returns `false` if there is no next token. /// Returns `false` if there is no next token.
pub fn check<F>(&mut self, f: F) -> bool pub fn check<F>(&self, f: F) -> bool
where where
F: FnOnce(Token<'s>) -> bool, F: FnOnce(Token<'s>) -> bool,
{ {
@ -230,7 +235,7 @@ impl<'s> Parser<'s> {
} }
/// Whether the end of the source string or group is reached. /// Whether the end of the source string or group is reached.
pub fn eof(&mut self) -> bool { pub fn eof(&self) -> bool {
self.peek().is_none() self.peek().is_none()
} }
@ -284,22 +289,37 @@ impl<'s> Parser<'s> {
fn bump(&mut self) { fn bump(&mut self) {
self.last_end = self.tokens.pos(); self.last_end = self.tokens.pos();
self.next_start = self.tokens.pos(); self.next_start = self.tokens.pos();
self.peeked = self.tokens.next(); self.next = self.tokens.next();
match self.tokens.mode() { match self.tokens.mode() {
TokenMode::Body => {} TokenMode::Body => {}
TokenMode::Header => { TokenMode::Header => {
while matches!( while matches!(
self.peeked, self.next,
Some(Token::Space(_)) | Some(Token::Space(_)) |
Some(Token::LineComment(_)) | Some(Token::LineComment(_)) |
Some(Token::BlockComment(_)) Some(Token::BlockComment(_))
) { ) {
self.next_start = self.tokens.pos(); self.next_start = self.tokens.pos();
self.peeked = self.tokens.next(); self.next = self.tokens.next();
} }
} }
} }
self.repeek();
}
fn repeek(&mut self) {
self.peeked = self.next;
if self.groups.contains(&match self.next {
Some(Token::RightParen) => Group::Paren,
Some(Token::RightBracket) => Group::Bracket,
Some(Token::RightBrace) => Group::Brace,
Some(Token::Pipe) => Group::Subheader,
_ => return,
}) {
self.peeked = None;
}
} }
} }

View File

@ -18,7 +18,6 @@ pub fn resolve_string(string: &str) -> String {
match s.eat() { match s.eat() {
Some('\\') => out.push('\\'), Some('\\') => out.push('\\'),
Some('"') => out.push('"'), Some('"') => out.push('"'),
Some('n') => out.push('\n'), Some('n') => out.push('\n'),
Some('t') => out.push('\t'), Some('t') => out.push('\t'),
Some('u') if s.eat_if('{') => { Some('u') if s.eat_if('{') => {
@ -29,7 +28,7 @@ pub fn resolve_string(string: &str) -> String {
if let Some(c) = resolve_hex(sequence) { if let Some(c) = resolve_hex(sequence) {
out.push(c); out.push(c);
} else { } else {
// TODO: Feedback that escape sequence is wrong. // TODO: Feedback that unicode escape sequence is wrong.
out += s.eaten_from(start); out += s.eaten_from(start);
} }
} }
@ -126,7 +125,7 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn test_unescape_strings() { fn test_resolve_strings() {
fn test(string: &str, expected: &str) { fn test(string: &str, expected: &str) {
assert_eq!(resolve_string(string), expected.to_string()); assert_eq!(resolve_string(string), expected.to_string());
} }

View File

@ -1,58 +1,104 @@
//! Parser tests.
#![allow(non_snake_case)] #![allow(non_snake_case)]
use std::fmt::Debug; use std::fmt::Debug;
use super::parse; use super::parse;
use crate::color::RgbaColor; use crate::color::RgbaColor;
use crate::diag::Deco; use crate::diag::{Diag, Level, Pass};
use crate::eval::DictKey; use crate::eval::DictKey;
use crate::geom::Unit; use crate::geom::Unit;
use crate::syntax::*; use crate::syntax::*;
// ------------------------------ Construct Syntax Nodes ------------------------------ // use BinOp::*;
use SynNode::{Emph, Linebreak, Parbreak, Space, Strong};
use UnOp::*;
use Deco::*; macro_rules! t {
use SynNode::{Emph as E, Linebreak as L, Parbreak as P, Space as S, Strong as B}; ($src:literal
nodes: [$($node:expr),* $(,)?]
$(, errors: [$($err:expr),* $(,)?])?
$(, warnings: [$($warn:expr),* $(,)?])?
$(, spans: $spans:expr)?
$(,)?
) => {{
#[allow(unused)]
let mut spans = false;
$(spans = $spans;)?
fn T(text: &str) -> SynNode { let Pass { output, feedback } = parse($src);
SynNode::Text(text.to_string()) check($src, Content![@$($node),*], output, spans);
} check(
$src,
vec![
$($(into!($err).map(|s: &str| Diag::new(Level::Error, s)),)*)?
$($(into!($warn).map(|s: &str| Diag::new(Level::Warning, s)),)*)?
],
feedback.diags,
true,
);
}};
macro_rules! H { ($src:literal $($node:expr),* $(,)?) => {
($level:expr, $($tts:tt)*) => { t!($src nodes: [$($node),*]);
SynNode::Heading(NodeHeading {
level: Spanned::zero($level),
contents: Tree![@$($tts)*],
})
}; };
} }
macro_rules! R { /// Assert that expected and found are equal, printing both and the source of
($lang:expr, $inline:expr, $($line:expr),* $(,)?) => {{ /// the test case if they aren't.
///
/// When `cmp_spans` is false, spans are ignored.
#[track_caller]
pub fn check<T>(src: &str, exp: T, found: T, cmp_spans: bool)
where
T: Debug + PartialEq,
{
Span::set_cmp(cmp_spans);
if exp != found {
println!("source: {:?}", src);
println!("expected: {:#?}", exp);
println!("found: {:#?}", found);
panic!("test failed");
}
Span::set_cmp(true);
}
/// Shorthand for `Spanned::new`.
fn S<T>(span: impl Into<Span>, v: T) -> Spanned<T> {
Spanned::new(v, span)
}
// Enables tests to optionally specify spans.
impl<T> From<T> for Spanned<T> {
fn from(t: T) -> Self {
Spanned::zero(t)
}
}
/// Shorthand for `Into::<Spanned<_>>::into`.
macro_rules! into {
($val:expr) => {
Into::<Spanned<_>>::into($val)
};
}
fn Text(text: &str) -> SynNode {
SynNode::Text(text.into())
}
fn Heading(level: impl Into<Spanned<u8>>, contents: SynTree) -> SynNode {
SynNode::Heading(NodeHeading { level: level.into(), contents })
}
fn Raw(lang: Option<&str>, lines: &[&str], inline: bool) -> SynNode {
SynNode::Raw(NodeRaw { SynNode::Raw(NodeRaw {
lang: $lang, lang: lang.map(|id| Ident(id.into())),
lines: vec![$($line.to_string()) ,*], lines: lines.iter().map(ToString::to_string).collect(),
inline: $inline, inline,
}) })
}};
} }
fn Lang(lang: &str) -> Option<Ident> {
Some(Ident(lang.to_string()))
}
macro_rules! F {
($($tts:tt)*) => { SynNode::Expr(Expr::Call(Call!(@$($tts)*))) }
}
// ------------------------------- Construct Expressions ------------------------------ //
use BinOp::*;
use UnOp::*;
use Unit::*;
fn Id(ident: &str) -> Expr { fn Id(ident: &str) -> Expr {
Expr::Lit(Lit::Ident(Ident(ident.to_string()))) Expr::Lit(Lit::Ident(Ident(ident.to_string())))
} }
@ -85,30 +131,13 @@ fn Str(string: &str) -> Expr {
Expr::Lit(Lit::Str(string.to_string())) Expr::Lit(Lit::Str(string.to_string()))
} }
macro_rules! Call { fn Block(expr: Expr) -> SynNode {
(@$name:expr $(, $span:expr)? $(; $($tts:tt)*)?) => {{ SynNode::Expr(expr)
let name = Into::<Spanned<&str>>::into($name);
#[allow(unused)]
let mut span = Span::ZERO;
$(span = $span.into();)?
ExprCall {
name: name.map(|n| Ident(n.to_string())),
args: Dict![@$($($tts)*)?].span_with(span),
}
}};
($($tts:tt)*) => { Expr::Call(Call![@$($tts)*]) };
}
fn Unary(op: impl Into<Spanned<UnOp>>, expr: impl Into<Spanned<Expr>>) -> Expr {
Expr::Unary(ExprUnary {
op: op.into(),
expr: Box::new(expr.into()),
})
} }
fn Binary( fn Binary(
op: impl Into<Spanned<BinOp>>,
lhs: impl Into<Spanned<Expr>>, lhs: impl Into<Spanned<Expr>>,
op: impl Into<Spanned<BinOp>>,
rhs: impl Into<Spanned<Expr>>, rhs: impl Into<Spanned<Expr>>,
) -> Expr { ) -> Expr {
Expr::Binary(ExprBinary { Expr::Binary(ExprBinary {
@ -118,463 +147,418 @@ fn Binary(
}) })
} }
fn Unary(op: impl Into<Spanned<UnOp>>, expr: impl Into<Spanned<Expr>>) -> Expr {
Expr::Unary(ExprUnary {
op: op.into(),
expr: Box::new(expr.into()),
})
}
macro_rules! Dict { macro_rules! Dict {
(@dict=$dict:expr,) => {}; (@$($a:expr $(=> $b:expr)?),* $(,)?) => {
(@dict=$dict:expr, $key:expr => $expr:expr $(, $($tts:tt)*)?) => {{ LitDict(vec![$(#[allow(unused)] {
let key = Into::<Spanned<&str>>::into($key); let key: Option<Spanned<DictKey>> = None;
let key = key.map(Into::<DictKey>::into); let expr = $a;
let expr = Into::<Spanned<Expr>>::into($expr); $(
$dict.0.push(LitDictEntry { key: Some(key), expr }); let key = Some(into!($a).map(|s: &str| s.into()));
Dict![@dict=$dict, $($($tts)*)?]; let expr = $b;
}}; )?
(@dict=$dict:expr, $expr:expr $(, $($tts:tt)*)?) => { LitDictEntry { key, expr: into!(expr) }
let expr = Into::<Spanned<Expr>>::into($expr); }),*])
$dict.0.push(LitDictEntry { key: None, expr });
Dict![@dict=$dict, $($($tts)*)?];
}; };
(@$($tts:tt)*) => {{ ($($tts:tt)*) => (Expr::Lit(Lit::Dict(Dict![@$($tts)*])));
#[allow(unused)]
let mut dict = LitDict::new();
Dict![@dict=dict, $($tts)*];
dict
}};
($($tts:tt)*) => { Expr::Lit(Lit::Dict(Dict![@$($tts)*])) };
} }
macro_rules! Tree { macro_rules! Content {
(@$($node:expr),* $(,)?) => { (@$($node:expr),* $(,)?) => (vec![$(into!($node)),*]);
vec![$(Into::<Spanned<SynNode>>::into($node)),*] ($($tts:tt)*) => (Expr::Lit(Lit::Content(Content![@$($tts)*])));
};
($($tts:tt)*) => { Expr::Lit(Lit::Content(Tree![@$($tts)*])) };
} }
// ------------------------------------ Test Macros ----------------------------------- // macro_rules! Call {
(@@$name:expr) => {
// Test syntax trees with or without spans. Call!(@@$name, Args![])
macro_rules! t { ($($tts:tt)*) => {test!(@spans=false, $($tts)*)} }
macro_rules! ts { ($($tts:tt)*) => {test!(@spans=true, $($tts)*)} }
macro_rules! test {
(@spans=$spans:expr, $src:expr => $($tts:tt)*) => {
let exp = Tree![@$($tts)*];
let pass = parse($src);
check($src, exp, pass.output, $spans);
}; };
} (@@$name:expr, $args:expr) => {
ExprCall {
// Test expressions. name: into!($name).map(|s: &str| Ident(s.into())),
macro_rules! v { args: into!($args),
($src:expr => $($tts:tt)*) => {
t!(concat!("[val ", $src, "]") => F!("val"; $($tts)*));
} }
}
// Test error messages.
macro_rules! e {
($src:expr => $($tts:tt)*) => {
let exp = vec![$($tts)*];
let pass = parse($src);
let found = pass.feedback.diags.iter()
.map(|s| s.as_ref().map(|e| e.message.as_str()))
.collect::<Vec<_>>();
check($src, exp, found, true);
}; };
(@$($tts:tt)*) => (Expr::Call(Call!(@@$($tts)*)));
($($tts:tt)*) => (SynNode::Expr(Call!(@$($tts)*)));
} }
// Test decorations. macro_rules! Args {
macro_rules! d { ($($tts:tt)*) => (Dict![@$($tts)*]);
($src:expr => $($tts:tt)*) => {
let exp = vec![$($tts)*];
let pass = parse($src);
check($src, exp, pass.feedback.decos, true);
};
}
/// Assert that expected and found are equal, printing both and panicking
/// and the source of their test case if they aren't.
///
/// When `cmp_spans` is false, spans are ignored.
#[track_caller]
pub fn check<T>(src: &str, exp: T, found: T, cmp_spans: bool)
where
T: Debug + PartialEq,
{
Span::set_cmp(cmp_spans);
let equal = exp == found;
Span::set_cmp(true);
if !equal {
println!("source: {:?}", src);
if cmp_spans {
println!("expected: {:#?}", exp);
println!("found: {:#?}", found);
} else {
println!("expected: {:?}", exp);
println!("found: {:?}", found);
}
panic!("test failed");
}
}
pub fn s<T>(start: u32, end: u32, v: T) -> Spanned<T> {
v.span_with(Span::new(start, end))
}
// Enables tests to optionally specify spans.
impl<T> From<T> for Spanned<T> {
fn from(t: T) -> Self {
Spanned::zero(t)
}
}
// --------------------------------------- Tests -------------------------------------- //
#[test]
fn test_parse_groups() {
e!("[)" => s(1, 2, "expected function name, found closing paren"),
s(2, 2, "expected closing bracket"));
e!("[v {]}" => s(4, 4, "expected closing brace"),
s(5, 6, "unexpected closing brace"));
}
#[test]
fn test_parse_simple_nodes() {
t!("" => );
t!("hi" => T("hi"));
t!("*hi" => B, T("hi"));
t!("hi_" => T("hi"), E);
t!("hi you" => T("hi"), S, T("you"));
t!("special~name" => T("special"), T("\u{00A0}"), T("name"));
t!("special\\~name" => T("special"), T("~"), T("name"));
t!("\\u{1f303}" => T("🌃"));
t!("\n\n\nhello" => P, T("hello"));
t!(r"a\ b" => T("a"), L, S, T("b"));
e!("\\u{d421c809}" => s(0, 12, "invalid unicode escape sequence"));
e!("\\u{abc" => s(6, 6, "expected closing brace"));
t!("💜\n\n 🌍" => T("💜"), P, T("🌍"));
ts!("hi" => s(0, 2, T("hi")));
ts!("*Hi*" => s(0, 1, B), s(1, 3, T("Hi")), s(3, 4, B));
ts!("💜\n\n 🌍" => s(0, 4, T("💜")), s(4, 7, P), s(7, 11, T("🌍")));
}
#[test]
fn test_parse_raw() {
t!("`py`" => R![None, true, "py"]);
t!("`hi\nyou" => R![None, true, "hi", "you"]);
t!(r"`` hi\`du``" => R![None, true, r"hi\`du"]);
// More than one backtick with optional language tag.
t!("``` console.log(\n\"alert\"\n)" => R![None, false, "console.log(", "\"alert\"", ")"]);
t!("````typst \r\n Typst uses ``` to indicate code blocks````!"
=> R![Lang("typst"), false, " Typst uses ``` to indicate code blocks"], T("!"));
// Trimming of whitespace.
t!("`` a ``" => R![None, true, "a"]);
t!("`` a ``" => R![None, true, "a "]);
t!("`` ` ``" => R![None, true, "`"]);
t!("``` ` ```" => R![None, true, " ` "]);
t!("``` ` \n ```" => R![None, false, " ` "]);
// Errors.
e!("`hi\nyou" => s(7, 7, "expected backtick(s)"));
e!("``` hi\nyou" => s(10, 10, "expected backtick(s)"));
// TODO: Bring back when spans/errors are in place.
// ts!("``java out``" => s(0, 12, R![Lang(s(2, 6, "java")), true, "out"]));
// e!("```🌍 hi\nyou```" => s(3, 7, "invalid identifier"));
} }
#[test] #[test]
fn test_parse_comments() { fn test_parse_comments() {
// In body. // In body.
t!("hi// you\nw" => T("hi"), S, T("w")); t!("a// you\nb" Text("a"), Space, Text("b"));
t!("first//\n//\nsecond" => T("first"), S, S, T("second")); t!("* // \n /*\n\n*/*" Strong, Space, Space, Strong);
t!("first//\n \nsecond" => T("first"), P, T("second"));
t!("first/*\n \n*/second" => T("first"), T("second"));
e!("🌎\n*/n" => s(5, 7, "unexpected end of block comment"));
// In header. // In header.
t!("[val /*12pt*/]" => F!("val")); t!("[v /*12pt*/]" Call!("v"));
t!("[val \n /* \n */]" => F!("val")); t!("[v //\n]" Call!("v"));
e!("[val \n /* \n */]" => ); t!("[v 12, /*\n*/ size: 14]" Call!("v", Args![Int(12), "size" => Int(14)]));
e!("[val 12, /* \n */ 14]" => );
// Error.
t!("a*/b"
nodes: [Text("a"), Text("b")],
errors: [S(1..3, "unexpected end of block comment")]);
}
#[test]
fn test_parse_simple_nodes() {
// Basics.
t!("");
t!(" " Space);
t!("hi" Text("hi"));
t!("🧽" Text("🧽"));
t!("_" Emph);
t!("*" Strong);
t!("~" Text("\u{00A0}"));
t!(r"\" Linebreak);
t!("\n\n" Parbreak);
// Multiple nodes.
t!("ab c" Text("ab"), Space, Text("c"));
t!("a`hi`\r\n\r*" Text("a"), Raw(None, &["hi"], true), Parbreak, Strong);
// Spans.
t!("*🌍*"
nodes: [S(0..1, Strong), S(1..5, Text("🌍")), S(5..6, Strong)],
spans: true);
// Errors.
t!("]}"
nodes: [],
errors: [S(0..1, "unexpected closing bracket"),
S(1..2, "unexpected closing brace")]);
} }
#[test] #[test]
fn test_parse_headings() { fn test_parse_headings() {
t!("## Hello world!" => H![1, S, T("Hello"), S, T("world!")]); // Basics with spans.
t!("#a"
nodes: [S(0..2, Heading(S(0..1, 0), Content![@S(1..2, Text("a"))]))],
spans: true);
// Handle various whitespace usages. // Multiple hashtags.
t!("####Simple" => H![3, T("Simple")]); t!("###three" Heading(2, Content![@Text("three")]));
t!(" # Whitespace!" => S, H![0, S, T("Whitespace!")]); t!("###### six" Heading(5, Content![@Space, Text("six")]));
t!(" /* TODO: Improve */ ## Analysis" => S, S, H!(1, S, T("Analysis")));
t!("# Heading \n ends" => H![0, S, T("Heading")], S, T("ends"));
// Complex heading contents. // Start of heading.
t!("Some text [box][### Valuable facts]" => T("Some"), S, T("text"), S, t!("/**/#" Heading(0, Content![@]));
F!("box"; Tree![H!(2, S, T("Valuable"), S, T("facts"))]) t!("[f][#ok]" Call!("f", Args![Content![Heading(0, Content![@Text("ok")])]]));
);
t!("### Grandiose stuff [box][Get it \n\n straight]" => H![
2,
S, T("Grandiose"), S, T("stuff"), S,
F!("box"; Tree![T("Get"), S, T("it"), P, T("straight")])
]);
t!("###### Multiline \\ headings" => H![5, S, T("Multiline"), S, L, S, T("headings")]);
// Things that should not become headings. // End of heading.
t!("\\## Text" => T("#"), T("#"), S, T("Text")); t!("#a\nb" Heading(0, Content![@Text("a")]), Space, Text("b"));
t!(" ###### # Text" => S, H![5, S, T("#"), S, T("Text")]);
t!("I am #1" => T("I"), S, T("am"), S, T("#"), T("1"));
t!("[box][\n] # hi" => F!("box"; Tree![S]), S, T("#"), S, T("hi"));
// Depth warnings. // Continued heading.
e!("########" => s(0, 8, "section depth should be at most 6")); t!("#a{\n1\n}b" Heading(0, Content![@Text("a"), Block(Int(1)), Text("b")]));
t!("#a[f][\n\n]d" Heading(0, Content![@
Text("a"), Call!("f", Args![Content![Parbreak]]), Text("d"),
]));
// No heading.
t!(r"\#" Text("#"));
t!("Nr. #1" Text("Nr."), Space, Text("#"), Text("1"));
t!("[v]#" Call!("v"), Text("#"));
// Too many hashtags.
t!("####### seven"
nodes: [Heading(5, Content![@Space, Text("seven")])],
warnings: [S(0..7, "section depth should not exceed 6")]);
} }
#[test] #[test]
fn test_parse_function_names() { fn test_parse_raw() {
// No closing bracket. // Basic, mostly tested in tokenizer and resolver.
t!("[" => F!("")); t!("`py`" nodes: [S(0..4, Raw(None, &["py"], true))], spans: true);
e!("[" => s(1, 1, "expected function name"), t!("`endless"
s(1, 1, "expected closing bracket")); nodes: [Raw(None, &["endless"], true)],
errors: [S(8..8, "expected backtick(s)")]);
}
#[test]
fn test_parse_escape_sequences() {
// Basic, mostly tested in tokenizer.
t!(r"\[" Text("["));
t!(r"\u{1F3D5}" nodes: [S(0..9, Text("🏕"))], spans: true);
// Bad value.
t!(r"\u{FFFFFF}"
nodes: [Text(r"\u{FFFFFF}")],
errors: [S(0..10, "invalid unicode escape sequence")]);
// No closing brace.
t!(r"\u{41*"
nodes: [Text("A"), Strong],
errors: [S(5..5, "expected closing brace")]);
}
#[test]
fn test_parse_groups() {
// Test paren group.
t!("{([v 1) + 3}"
nodes: [Block(Binary(
Content![Call!("v", Args![Int(1)])],
Add,
Int(3),
))],
errors: [S(6..6, "expected closing bracket")]);
// Test bracket group.
t!("[)"
nodes: [Call!("")],
errors: [S(1..2, "expected function name, found closing paren"),
S(2..2, "expected closing bracket")]);
t!("[v {]}"
nodes: [Call!("v", Args![Content![]])],
errors: [S(4..4, "expected closing brace"),
S(5..6, "unexpected closing brace")]);
// Test brace group.
t!("{1 + [}"
nodes: [Block(Binary(Int(1), Add, Content![Call!("")]))],
errors: [S(6..6, "expected function name"),
S(6..6, "expected closing bracket")]);
// Test subheader group.
t!("[v (|u )]"
nodes: [Call!("v", Args![Dict![], Content![Call!("u")]])],
errors: [S(4..4, "expected closing paren"),
S(7..8, "expected expression, found closing paren")]);
}
#[test]
fn test_parse_blocks() {
// Basic with spans.
t!("{1}" nodes: [S(0..3, Block(Int(1)))], spans: true);
// Function calls.
t!("{f()}" Call!("f"));
t!("{[f]}" Block(Content![Call!("f")]));
// Missing or bad value.
t!("{}{1u}"
nodes: [],
errors: [S(1..1, "expected expression"),
S(3..5, "expected expression, found invalid token")]);
}
#[test]
fn test_parse_bracket_funcs() {
// Basic.
t!("[function]" Call!("function"));
t!("[ v ]" Call!("v"));
// Body and no body.
t!("[v][[f]]" Call!("v", Args![Content![Call!("f")]]));
t!("[v][v][v]" Call!("v", Args![Content![Text("v")]]), Call!("v"));
t!("[v] [f]" Call!("v"), Space, Call!("f"));
// Spans.
t!("[v 1][📐]"
nodes: [S(0..11, Call!(S(1..2, "v"), S(3..4, Args![
S(3..4, Int(1)),
S(5..11, Content![S(6..10, Text("📐"))]),
])))],
spans: true);
// No name and no closing bracket.
t!("["
nodes: [Call!("")],
errors: [S(1..1, "expected function name"),
S(1..1, "expected closing bracket")]);
// No name. // No name.
e!("[]" => s(1, 1, "expected function name")); t!("[]"
e!("[\"]" => s(1, 3, "expected function name, found string"), nodes: [Call!("")],
s(3, 3, "expected closing bracket")); errors: [S(1..1, "expected function name")]);
// A valid name. // Bad name.
t!("[hi]" => F!("hi")); t!("[# 1]"
t!("[ f]" => F!("f")); nodes: [Call!("", Args![Int(1)])],
errors: [S(1..2, "expected function name, found hex value")]);
// An invalid name. // String header eats closing bracket.
e!("[12]" => s(1, 3, "expected function name, found integer")); t!(r#"[v "]"#
e!("[ 🌎]" => s(3, 7, "expected function name, found invalid token")); nodes: [Call!("v", Args![Str("]")])],
errors: [S(5..5, "expected quote"),
S(5..5, "expected closing bracket")]);
// Raw in body eats closing bracket.
t!("[v][`a]`"
nodes: [Call!("v", Args![Content![Raw(None, &["a]"], true)]])],
errors: [S(8..8, "expected closing bracket")]);
} }
#[test] #[test]
fn test_parse_chaining() { fn test_parse_chaining() {
// Things the parser has to make sense of // Basic.
t!("[hi (5.0, 2.1 | you]" => F!("hi"; Dict![Float(5.0), Float(2.1)], Tree![F!("you")])); t!("[a | b]" Call!("a", Args![Content![Call!("b")]]));
t!("[box | pad: 1pt][Hi]" => F!("box"; Tree![ t!("[a | b | c]" Call!("a", Args![Content![
F!("pad"; Length(1.0, Pt), Tree!(T("Hi"))) Call!("b", Args![Content![Call!("c")]])
])); ]]));
t!("[bold 400, | emph | sub: 1cm]" => F!("bold"; Int(400), Tree![
F!("emph"; Tree!(F!("sub"; Length(1.0, Cm))))
]));
// Errors for unclosed / empty predecessor groups // With body and spans.
e!("[hi (5.0, 2.1 | you]" => s(14, 14, "expected closing paren")); t!("[a|b][💕]"
e!("[| abc]" => s(1, 1, "expected function name")); nodes: [S(0..11, Call!(S(1..2, "a"), S(2..2, Args![
e!("[box |][Hi]" => s(6, 6, "expected function name")); S(3..11, Content![S(3..11, Call!(S(3..4, "b"), S(4..4, Args![
S(5..11, Content![S(6..10, Text("💕"))])
])))])
])))],
spans: true);
// No name in second subheader.
t!("[a 1|]"
nodes: [Call!("a", Args![Int(1), Content![Call!("")]])],
errors: [S(5..5, "expected function name")]);
// No name in first subheader.
t!("[|a true]"
nodes: [Call!("", Args![Content![Call!("a", Args![Bool(true)])]])],
errors: [S(1..1, "expected function name")]);
} }
#[test] #[test]
fn test_parse_function_bodies() { fn test_parse_arguments() {
t!("[val 1][*Hi*]" => F!("val"; Int(1), Tree![B, T("Hi"), B])); // Bracket functions.
e!(" [val][ */]" => s(8, 10, "unexpected end of block comment")); t!("[v 1]" Call!("v", Args![Int(1)]));
t!("[v 1,]" Call!("v", Args![Int(1)]));
t!("[v a]" Call!("v", Args![Id("a")]));
t!("[v a,]" Call!("v", Args![Id("a")]));
t!("[v a:2]" Call!("v", Args!["a" => Int(2)]));
// Raw in body. // Parenthesized function with nested dictionary literal.
t!("[val][`Hi]`" => F!("val"; Tree![R![None, true, "Hi]"]])); t!(r#"{f(1, a: (2, 3), #004, b: "five")}"# Block(Call!(@"f", Args![
e!("[val][`Hi]`" => s(11, 11, "expected closing bracket")); Int(1),
"a" => Dict![Int(2), Int(3)],
Color(RgbaColor::new(0, 0, 0x44, 0xff)),
"b" => Str("five"),
])));
// Crazy. // Bad expression.
t!("[v][[v][v][v]]" => F!("v"; Tree![F!("v"; Tree![T("v")]), F!("v")])); t!("[v */]"
nodes: [Call!("v", Args![])],
errors: [S(3..5, "expected expression, found end of block comment")]);
// Spanned. // Missing comma between arguments.
ts!(" [box][Oh my]" => t!("[v 1 2]"
s(0, 1, S), nodes: [Call!("v", Args![Int(1), Int(2)])],
s(1, 13, F!(s(2, 5, "box"), 5 .. 5; errors: [S(4..4, "expected comma")]);
s(6, 13, Tree![
s(7, 9, T("Oh")), s(9, 10, S), s(10, 12, T("my")), // Missing expression after name.
]) t!("[v a:]"
)) nodes: [Call!("v", Args![])],
); errors: [S(5..5, "expected expression")]);
// Bad expression after name.
t!("[v a:1:]"
nodes: [Call!("v", Args!["a" => Int(1)])],
errors: [S(6..7, "expected expression, found colon")]);
// Name has to be identifier. Number parsed as positional argument.
t!("[v 1:]"
nodes: [Call!("v", Args![Int(1)])],
errors: [S(4..5, "expected expression, found colon")]);
// Parsed as two positional arguments.
t!("[v 1:2]"
nodes: [Call!("v", Args![Int(1), Int(2)])],
errors: [S(4..5, "expected expression, found colon"),
S(4..4, "expected comma")]);
} }
#[test] #[test]
fn test_parse_values() { fn test_parse_dict_literals() {
// Simple. // Basic.
v!("_" => Id("_")); t!("{()}" Block(Dict![]));
v!("name" => Id("name"));
v!("ke-bab" => Id("ke-bab"));
v!("α" => Id("α"));
v!("\"hi\"" => Str("hi"));
v!("true" => Bool(true));
v!("false" => Bool(false));
v!("1.0e-4" => Float(1e-4));
v!("3.15" => Float(3.15));
v!("50%" => Percent(50.0));
v!("4.5cm" => Length(4.5, Cm));
v!("12e1pt" => Length(12e1, Pt));
v!("#f7a20500" => Color(RgbaColor::new(0xf7, 0xa2, 0x05, 0x00)));
v!("\"a\n[]\\\"string\"" => Str("a\n[]\"string"));
// Content. // With spans.
v!("{_hi_}" => Tree![E, T("hi"), E]); t!("{(1, two: 2)}"
e!("[val {_hi_}]" => ); nodes: [S(0..13, Block(Dict![
v!("[hi]" => Tree![F!("hi")]); S(2..3, Int(1)),
e!("[val [hi]]" => ); S(5..8, "two") => S(10..11, Int(2)),
]))],
spans: true);
// Healed colors. // Unclosed.
v!("#12345" => Color(RgbaColor::new(0, 0, 0, 0xff))); t!("{(}"
e!("[val #12345]" => s(5, 11, "invalid color")); nodes: [Block(Dict![])],
e!("[val #a5]" => s(5, 8, "invalid color")); errors: [S(2..2, "expected closing paren")]);
e!("[val #14b2ah]" => s(5, 12, "invalid color"));
e!("[val #f075ff011]" => s(5, 15, "invalid color"));
// Unclosed string.
v!("\"hello" => Str("hello]"));
e!("[val \"hello]" => s(12, 12, "expected quote"),
s(12, 12, "expected closing bracket"));
// Spanned.
ts!("[val 1.4]" => s(0, 9, F!(s(1, 4, "val"), 5 .. 8; s(5, 8, Float(1.4)))));
} }
#[test] #[test]
fn test_parse_expressions() { fn test_parse_expressions() {
// Coerced dict. // Parenthesis.
v!("(hi)" => Id("hi")); t!("{(x)}" Block(Id("x")));
// Operations. // Unary operations.
v!("-1" => Unary(Neg, Int(1))); t!("{-1}" Block(Unary(Neg, Int(1))));
v!("-- 1" => Unary(Neg, Unary(Neg, Int(1)))); t!("{--1}" Block(Unary(Neg, Unary(Neg, Int(1)))));
v!("--css" => Unary(Neg, Unary(Neg, Id("css"))));
v!("3.2in + 6pt" => Binary(Add, Length(3.2, In), Length(6.0, Pt)));
v!("5 - 0.01" => Binary(Sub, Int(5), Float(0.01)));
v!("(3mm * 2)" => Binary(Mul, Length(3.0, Mm), Int(2)));
v!("12e-3cm/1pt" => Binary(Div, Length(12e-3, Cm), Length(1.0, Pt)));
// More complex. // Binary operations.
v!("(3.2in + 6pt)*(5/2-1)" => Binary( t!(r#"{"x"+"y"}"# Block(Binary(Str("x"), Add, Str("y"))));
Mul, t!("{1-2}" Block(Binary(Int(1), Sub, Int(2))));
Binary(Add, Length(3.2, In), Length(6.0, Pt)), t!("{a * b}" Block(Binary(Id("a"), Mul, Id("b"))));
Binary(Sub, Binary(Div, Int(5), Int(2)), Int(1)) t!("{12pt/.4}" Block(Binary(Length(12.0, Unit::Pt), Div, Float(0.4))));
));
v!("(6.3E+2+4* - 3.2pt)/2" => Binary(
Div,
Binary(Add, Float(6.3e2), Binary(
Mul,
Int(4),
Unary(Neg, Length(3.2, Pt))
)),
Int(2)
));
// Associativity of multiplication and division. // Associativity.
v!("3/4*5" => Binary(Mul, Binary(Div, Int(3), Int(4)), Int(5))); t!("{1+2+3}" Block(Binary(Binary(Int(1), Add, Int(2)), Add, Int(3))));
t!("{1/2*3}" Block(Binary(Binary(Int(1), Div, Int(2)), Mul, Int(3))));
// Spanned. // Precedence.
ts!("[val 1 + 3]" => s(0, 11, F!( t!("{1+2*-3}" Block(Binary(
s(1, 4, "val"), 5 .. 10; s(5, 10, Binary( Int(1), Add, Binary(Int(2), Mul, Unary(Neg, Int(3))),
s(7, 8, Add),
s(5, 6, Int(1)),
s(9, 10, Int(3))
))
))); )));
// Span of parenthesized expression contains parens. // Confusion with floating-point literal.
ts!("[val (1)]" => s(0, 9, F!(s(1, 4, "val"), 5 .. 8; s(5, 8, Int(1))))); t!("{1e-3-4e+4}" Block(Binary(Float(1e-3), Sub, Float(4e+4))));
// Invalid expressions. // Spans + parentheses winning over precedence.
v!("4pt--" => Length(4.0, Pt)); t!("{(1+2)*3}"
e!("[val 4pt--]" => s(9, 10, "missing factor"), nodes: [S(0..9, Block(Binary(
s(5, 9, "missing right summand")); S(1..6, Binary(S(2..3, Int(1)), S(3..4, Add), S(4..5, Int(2)))),
S(6..7, Mul),
S(7..8, Int(3)),
)))],
spans: true);
v!("3mm+4pt*" => Binary(Add, Length(3.0, Mm), Length(4.0, Pt))); // Errors.
e!("[val 3mm+4pt*]" => s(9, 13, "missing right factor")); t!("{-}{1+}{2*}"
nodes: [Block(Int(1)), Block(Int(2))],
errors: [S(2..2, "expected expression"),
S(6..6, "expected expression"),
S(10..10, "expected expression")]);
} }
#[test] #[test]
fn test_parse_dicts() { fn test_parse_values() {
// Okay. // Basics.
v!("()" => Dict![]); t!("{_}" Block(Id("_")));
v!("(false)" => Bool(false)); t!("{name}" Block(Id("name")));
v!("(true,)" => Dict![Bool(true)]); t!("{ke-bab}" Block(Id("ke-bab")));
v!("(key: val)" => Dict!["key" => Id("val")]); t!("{α}" Block(Id("α")));
v!("(1, 2)" => Dict![Int(1), Int(2)]); t!("{true}" Block(Bool(true)));
v!("(1, key: \"value\")" => Dict![Int(1), "key" => Str("value")]); t!("{false}" Block(Bool(false)));
t!("{1.0e-4}" Block(Float(1e-4)));
t!("{3.15}" Block(Float(3.15)));
t!("{50%}" Block(Percent(50.0)));
t!("{4.5cm}" Block(Length(4.5, Unit::Cm)));
t!("{12e1pt}" Block(Length(12e1, Unit::Pt)));
// Decorations. // Strings.
d!("[val key: hi]" => s(5, 8, DictKey)); t!(r#"{"hi"}"# Block(Str("hi")));
d!("[val (key: hi)]" => s(6, 9, DictKey)); t!(r#"{"a\n[]\"\u{1F680}string"}"# Block(Str("a\n[]\"🚀string")));
d!("[val f(key: hi)]" => s(7, 10, DictKey));
// Spanned with spacing around named arguments. // Colors.
ts!("[val \n hi \n : /* //\n */ \"s\n\"]" => s(0, 30, F!( t!("{#f7a20500}" Block(Color(RgbaColor::new(0xf7, 0xa2, 0x05, 0))));
s(1, 4, "val"), t!("{#a5}"
8 .. 29; s(8, 10, "hi") => s(25, 29, Str("s\n")) nodes: [Block(Color(RgbaColor::new(0, 0, 0, 0xff)))],
))); errors: [S(1..4, "invalid color")]);
e!("[val \n hi \n : /* //\n */ \"s\n\"]" => );
}
#[test]
fn test_parse_dicts_paren_func_calls() {
v!("empty()" => Call!("empty"));
v!("add ( 1 , 2 )" => Call!("add"; Int(1), Int(2)));
v!("items(\"fire\", #f93a6d)" => Call!("items";
Str("fire"), Color(RgbaColor::new(0xf9, 0x3a, 0x6d, 0xff))
));
// More complex.
v!(r#"css(1pt, color: rgb(90, 102, 254), stroke: "solid")"# => Call!(
"css";
Length(1.0, Pt),
"color" => Call!("rgb"; Int(90), Int(102), Int(254)),
"stroke" => Str("solid"),
));
// Unclosed.
v!("lang(中文]" => Call!("lang"; Id("中文")));
e!("[val lang(中文]" => s(16, 16, "expected closing paren"));
// Invalid name.
v!("👠(\"abc\", 13e-5)" => Dict!(Str("abc"), Float(13.0e-5)));
e!("[val 👠(\"abc\", 13e-5)]" => s(5, 9, "invalid token"));
}
#[test]
fn test_parse_dicts_nested() {
v!("(1, ( ab:(), d : (3, 14pt) )), false" =>
Dict![
Int(1),
Dict!(
"ab" => Dict![],
"d" => Dict!(Int(3), Length(14.0, Pt)),
),
],
Bool(false),
);
}
#[test]
fn test_parse_dicts_errors() {
// Expected value.
e!("[val (:)]" => s(6, 7, "unexpected colon"));
e!("[val (,)]" => s(6, 7, "unexpected comma"));
v!("(\x07 abc,)" => Dict![Id("abc")]);
e!("[val (\x07 abc,)]" => s(6, 7, "invalid token"));
e!("[val (key:,)]" => s(10, 11, "expected value, found comma"));
e!("[val hi,)]" => s(8, 9, "unexpected closing paren"));
// Expected comma.
v!("(true false)" => Dict![Bool(true), Bool(false)]);
e!("[val (true false)]" => s(10, 10, "expected comma"));
// Expected closing paren.
e!("[val (#000]" => s(10, 10, "expected closing paren"));
e!("[val (key]" => s(9, 9, "expected closing paren"));
e!("[val (key:]" => s(10, 10, "expected value"),
s(10, 10, "expected closing paren"));
// Bad key.
v!("true:you" => Bool(true), Id("you"));
e!("[val true:you]" => s(9, 10, "unexpected colon"));
// Unexpected colon.
v!("z:y:4" => "z" => Id("y"), Int(4));
e!("[val z:y:4]" => s(8, 9, "unexpected colon"));
} }

View File

@ -6,19 +6,20 @@ use super::*;
/// code. /// code.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum SynNode { pub enum SynNode {
/// Whitespace containing less than two newlines.
Space,
/// Plain text. /// Plain text.
Text(String), Text(String),
/// Whitespace containing less than two newlines.
Space,
/// A forced line break. /// A forced line break.
Linebreak, Linebreak,
/// A paragraph break. /// A paragraph break.
Parbreak, Parbreak,
/// Emphasized text was enabled / disabled.
Emph,
/// Strong text was enabled / disabled. /// Strong text was enabled / disabled.
Strong, Strong,
/// Emphasized text was enabled / disabled.
Emph,
/// A section heading. /// A section heading.
Heading(NodeHeading), Heading(NodeHeading),

View File

@ -26,16 +26,16 @@ pub enum Token<'s> {
Star, Star,
/// An underscore: `_`. /// An underscore: `_`.
Underscore, Underscore,
/// A hashtag indicating a section heading: `#`.
Hashtag,
/// A tilde: `~`. /// A tilde: `~`.
Tilde, Tilde,
/// A backslash followed by whitespace: `\`. /// A backslash followed by whitespace: `\`.
Backslash, Backslash,
/// A unicode escape sequence: `\u{1F5FA}`. /// A hashtag indicating a section heading: `#`.
UnicodeEscape(TokenUnicodeEscape<'s>), Hashtag,
/// A raw block: `` `...` ``. /// A raw block: `` `...` ``.
Raw(TokenRaw<'s>), Raw(TokenRaw<'s>),
/// A unicode escape sequence: `\u{1F5FA}`.
UnicodeEscape(TokenUnicodeEscape<'s>),
/// A left bracket: `[`. /// A left bracket: `[`.
LeftBracket, LeftBracket,