Skip to content

Commit d15c381

Browse files
committed
Roundtrip parsing
1 parent 895a769 commit d15c381

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+354
-345
lines changed

crates/rune/src/ast/attribute.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{Parse, ParseError, ParseErrorKind, Parser, Peek, Spanned, ToTokens,
33
use runestick::Span;
44

55
/// Attribute like `#[derive(Debug)]`
6-
#[derive(Debug, Clone, ToTokens, Spanned)]
6+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
77
pub struct Attribute {
88
/// The `#` character
99
pub hash: ast::Hash,
@@ -24,13 +24,13 @@ pub struct Attribute {
2424
/// # Examples
2525
///
2626
/// ```rust
27-
/// use rune::{parse_all, ast, ParseError};
27+
/// use rune::{testing, ast};
2828
///
29-
/// parse_all::<ast::Attribute>("#[foo = \"foo\"]").unwrap();
30-
/// parse_all::<ast::Attribute>("#[foo()]").unwrap();
31-
/// parse_all::<ast::Attribute>("#![foo]").unwrap();
32-
/// parse_all::<ast::Attribute>("#![cfg(all(feature = \"potato\"))]").unwrap();
33-
/// parse_all::<ast::Attribute>("#[x+1]").unwrap();
29+
/// testing::roundtrip::<ast::Attribute>("#[foo = \"foo\"]");
30+
/// testing::roundtrip::<ast::Attribute>("#[foo()]");
31+
/// testing::roundtrip::<ast::Attribute>("#![foo]");
32+
/// testing::roundtrip::<ast::Attribute>("#![cfg(all(feature = \"potato\"))]");
33+
/// testing::roundtrip::<ast::Attribute>("#[x+1]");
3434
/// ```
3535
impl Parse for Attribute {
3636
fn parse(parser: &mut Parser<'_>) -> Result<Self, ParseError> {
@@ -90,7 +90,7 @@ impl Peek for Attribute {
9090
}
9191

9292
/// Whether or not the attribute is an outer `#!` or inner `#` attribute
93-
#[derive(Debug, Copy, Clone, ToTokens)]
93+
#[derive(Debug, Clone, Copy, PartialEq, Eq, ToTokens)]
9494
pub enum AttrStyle {
9595
/// `#`
9696
Inner,

crates/rune/src/ast/block.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ast;
22
use crate::{OptionSpanned as _, Parse, ParseError, ParseErrorKind, Parser, Spanned, ToTokens};
33

44
/// A block of expressions.
5-
#[derive(Debug, Clone, ToTokens, Spanned)]
5+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
66
pub struct Block {
77
/// The close brace.
88
pub open: ast::OpenBrace,
@@ -47,27 +47,27 @@ impl Block {
4747
/// # Examples
4848
///
4949
/// ```rust
50-
/// use rune::{parse_all, ast};
50+
/// use rune::{testing, ast};
5151
///
52-
/// let block = parse_all::<ast::Block>("{}").unwrap();
52+
/// let block = testing::roundtrip::<ast::Block>("{}");
5353
/// assert_eq!(block.statements.len(), 0);
5454
/// assert!(block.produces_nothing());
5555
///
56-
/// let block = parse_all::<ast::Block>("{ foo }").unwrap();
56+
/// let block = testing::roundtrip::<ast::Block>("{ foo }");
5757
/// assert_eq!(block.statements.len(), 1);
5858
/// assert!(!block.produces_nothing());
5959
///
60-
/// let block = parse_all::<ast::Block>("{ foo; }").unwrap();
60+
/// let block = testing::roundtrip::<ast::Block>("{ foo; }");
6161
/// assert_eq!(block.statements.len(), 1);
6262
/// assert!(block.produces_nothing());
6363
///
64-
/// let block = parse_all::<ast::Block>(r#"
64+
/// let block = testing::roundtrip::<ast::Block>(r#"
6565
/// {
6666
/// let foo = 42;
6767
/// let bar = "string";
6868
/// baz
6969
/// }
70-
/// "#).unwrap();
70+
/// "#);
7171
///
7272
/// assert_eq!(block.statements.len(), 3);
7373
/// ```

crates/rune/src/ast/condition.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ast;
22
use crate::{Parse, ParseError, Parser, Spanned, ToTokens};
33

44
/// An if condition.
5-
#[derive(Debug, Clone, ToTokens, Spanned)]
5+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
66
pub enum Condition {
77
/// A regular expression.
88
Expr(Box<ast::Expr>),
@@ -15,10 +15,10 @@ pub enum Condition {
1515
/// # Examples
1616
///
1717
/// ```rust
18-
/// use rune::{parse_all, ast};
18+
/// use rune::{testing, ast};
1919
///
20-
/// parse_all::<ast::Condition>("true").unwrap();
21-
/// parse_all::<ast::Condition>("let [a, ..] = v").unwrap();
20+
/// testing::roundtrip::<ast::Condition>("true");
21+
/// testing::roundtrip::<ast::Condition>("let [a, ..] = v");
2222
/// ```
2323
impl Parse for Condition {
2424
fn parse(parser: &mut Parser) -> Result<Self, ParseError> {

crates/rune/src/ast/expr.rs

+24-33
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl ops::Deref for ExprChain {
2828
}
2929

3030
/// A rune expression.
31-
#[derive(Debug, Clone, ToTokens, Spanned)]
31+
#[derive(Debug, Clone, ToTokens, Spanned, PartialEq, Eq)]
3232
pub enum Expr {
3333
/// The `self` keyword.
3434
Self_(ast::Self_),
@@ -584,45 +584,36 @@ impl Expr {
584584
/// # Examples
585585
///
586586
/// ```rust
587-
/// use rune::{parse_all, ast};
587+
/// use rune::{testing, ast};
588588
///
589-
/// parse_all::<ast::Expr>("foo[\"foo\"]").unwrap();
590-
/// parse_all::<ast::Expr>("foo.bar()").unwrap();
591-
/// parse_all::<ast::Expr>("var()").unwrap();
592-
/// parse_all::<ast::Expr>("var").unwrap();
593-
/// parse_all::<ast::Expr>("42").unwrap();
594-
/// parse_all::<ast::Expr>("1 + 2 / 3 - 4 * 1").unwrap();
595-
/// parse_all::<ast::Expr>("foo[\"bar\"]").unwrap();
596-
/// parse_all::<ast::Expr>("let var = 42").unwrap();
597-
/// parse_all::<ast::Expr>("let var = \"foo bar\"").unwrap();
598-
/// parse_all::<ast::Expr>("var[\"foo\"] = \"bar\"").unwrap();
599-
/// parse_all::<ast::Expr>("let var = objects[\"foo\"] + 1").unwrap();
600-
/// parse_all::<ast::Expr>("var = 42").unwrap();
589+
/// testing::roundtrip::<ast::Expr>("foo[\"foo\"]");
590+
/// testing::roundtrip::<ast::Expr>("foo.bar()");
591+
/// testing::roundtrip::<ast::Expr>("var()");
592+
/// testing::roundtrip::<ast::Expr>("var");
593+
/// testing::roundtrip::<ast::Expr>("42");
594+
/// testing::roundtrip::<ast::Expr>("1 + 2 / 3 - 4 * 1");
595+
/// testing::roundtrip::<ast::Expr>("foo[\"bar\"]");
596+
/// testing::roundtrip::<ast::Expr>("let var = 42");
597+
/// testing::roundtrip::<ast::Expr>("let var = \"foo bar\"");
598+
/// testing::roundtrip::<ast::Expr>("var[\"foo\"] = \"bar\"");
599+
/// testing::roundtrip::<ast::Expr>("let var = objects[\"foo\"] + 1");
600+
/// testing::roundtrip::<ast::Expr>("var = 42");
601601
///
602-
/// let expr = parse_all::<ast::Expr>(r#"
602+
/// let expr = testing::roundtrip::<ast::Expr>(r#"
603603
/// if 1 { } else { if 2 { } else { } }
604-
/// "#).unwrap();
605-
///
606-
/// if let ast::Expr::ExprIf(..) = expr {
607-
/// } else {
608-
/// panic!("not an if statement");
609-
/// }
604+
/// "#);
605+
/// assert!(matches!(expr, ast::Expr::ExprIf(..)));
610606
///
611607
/// // Chained function calls.
612-
/// parse_all::<ast::Expr>("foo.bar.baz()").unwrap();
613-
/// parse_all::<ast::Expr>("foo[0][1][2]").unwrap();
614-
/// parse_all::<ast::Expr>("foo.bar()[0].baz()[1]").unwrap();
608+
/// testing::roundtrip::<ast::Expr>("foo.bar.baz()");
609+
/// testing::roundtrip::<ast::Expr>("foo[0][1][2]");
610+
/// testing::roundtrip::<ast::Expr>("foo.bar()[0].baz()[1]");
615611
///
616-
/// parse_all::<ast::Expr>("42 is int::int").unwrap();
617-
/// parse_all::<ast::Expr>("{ let x = 1; x }").unwrap();
612+
/// testing::roundtrip::<ast::Expr>("42 is int::int");
613+
/// testing::roundtrip::<ast::Expr>("{ let x = 1; x }");
618614
///
619-
/// let expr = parse_all::<ast::Expr>("#[cfg(debug_assertions)] { assert_eq(x, 32); }").unwrap();
620-
/// if let ast::Expr::ExprBlock(block_expr) = expr {
621-
/// assert_eq!(block_expr.attributes.len(), 1);
622-
/// assert_eq!(block_expr.block.statements.len(), 1);
623-
/// } else {
624-
/// panic!("not a block statement")
625-
/// }
615+
/// let expr = testing::roundtrip::<ast::Expr>("#[cfg(debug_assertions)] { assert_eq(x, 32); }");
616+
/// assert!(matches!(expr, ast::Expr::ExprBlock(b) if b.attributes.len() == 1 && b.block.statements.len() == 1));
626617
/// ```
627618
impl Parse for Expr {
628619
fn parse(parser: &mut Parser<'_>) -> Result<Self, ParseError> {

crates/rune/src/ast/expr_async.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ use crate::{Parse, Spanned, ToTokens};
66
/// # Examples
77
///
88
/// ```rust
9-
/// use rune::{parse_all, ast};
9+
/// use rune::{testing, ast};
1010
///
11-
/// let expr = parse_all::<ast::ExprAsync>("async {}").unwrap();
11+
/// let expr = testing::roundtrip::<ast::ExprAsync>("async {}");
1212
/// assert_eq!(expr.block.statements.len(), 0);
1313
/// assert!(expr.block.produces_nothing());
1414
///
15-
/// let expr = parse_all::<ast::ExprAsync>("async { 42 }").unwrap();
15+
/// let expr = testing::roundtrip::<ast::ExprAsync>("async { 42 }");
1616
/// assert_eq!(expr.block.statements.len(), 1);
1717
/// assert!(!expr.block.produces_nothing());
1818
///
19-
/// let expr = parse_all::<ast::ExprAsync>("#[retry] async { 42 }").unwrap();
19+
/// let expr = testing::roundtrip::<ast::ExprAsync>("#[retry] async { 42 }");
2020
/// assert_eq!(expr.block.statements.len(), 1);
2121
/// assert!(!expr.block.produces_nothing());
2222
/// assert_eq!(expr.attributes.len(), 1);
2323
/// ```
24-
#[derive(Debug, Clone, Parse, ToTokens, Spanned)]
24+
#[derive(Debug, Clone, PartialEq, Eq, Parse, ToTokens, Spanned)]
2525
pub struct ExprAsync {
2626
/// The attributes for the block.
2727
#[rune(iter, attributes)]

crates/rune/src/ast/expr_await.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ast;
22
use crate::{Parse, ParseError, Parser, Spanned, ToTokens};
33

44
/// A return statement `<expr>.await`.
5-
#[derive(Debug, Clone, ToTokens, Spanned)]
5+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
66
pub struct ExprAwait {
77
/// The expression being awaited.
88
pub expr: Box<ast::Expr>,

crates/rune/src/ast/expr_binary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use runestick::Span;
44
use std::fmt;
55

66
/// A binary expression.
7-
#[derive(Debug, Clone, ToTokens, Spanned)]
7+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
88
pub struct ExprBinary {
99
/// The left-hand side of a binary operation.
1010
pub lhs: Box<ast::Expr>,

crates/rune/src/ast/expr_block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ast;
22
use crate::{Parse, Spanned, ToTokens};
33

44
/// A block of expressions.
5-
#[derive(Debug, Clone, Parse, ToTokens, Spanned)]
5+
#[derive(Debug, Clone, PartialEq, Eq, Parse, ToTokens, Spanned)]
66
pub struct ExprBlock {
77
/// The attributes for the block.
88
#[rune(iter, attributes)]

crates/rune/src/ast/expr_break.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::{Parse, ParseError, Parser, Peek, Spanned, ToTokens};
44
/// A `break` statement: `break [expr]`.
55
///
66
/// ```rust
7-
/// use rune::{parse_all, ast};
7+
/// use rune::{testing, ast};
88
///
9-
/// parse_all::<ast::ExprBreak>("break").unwrap();
10-
/// parse_all::<ast::ExprBreak>("break 42").unwrap();
11-
/// parse_all::<ast::ExprBreak>("#[attr] break 42").unwrap();
9+
/// testing::roundtrip::<ast::ExprBreak>("break");
10+
/// testing::roundtrip::<ast::ExprBreak>("break 42");
11+
/// testing::roundtrip::<ast::ExprBreak>("#[attr] break 42");
1212
/// ```
13-
#[derive(Debug, Clone, ToTokens, Parse, Spanned)]
13+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Parse, Spanned)]
1414
pub struct ExprBreak {
1515
/// The attributes of the `break` expression
1616
#[rune(iter, attributes)]
@@ -23,7 +23,7 @@ pub struct ExprBreak {
2323
}
2424

2525
/// Things that we can break on.
26-
#[derive(Debug, Clone, ToTokens, Spanned)]
26+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
2727
pub enum ExprBreakValue {
2828
/// Breaking a value out of a loop.
2929
Expr(Box<ast::Expr>),

crates/rune/src/ast/expr_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ast;
22
use crate::{Spanned, ToTokens};
33

44
/// A function call `<expr>(<args>)`.
5-
#[derive(Debug, Clone, ToTokens, Spanned)]
5+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
66
pub struct ExprCall {
77
/// The name of the function being called.
88
pub expr: Box<ast::Expr>,

crates/rune/src/ast/expr_closure.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{Parse, ParseError, Parser, Spanned, ToTokens};
33
use runestick::Span;
44

55
/// A closure.
6-
#[derive(Debug, Clone, ToTokens, Spanned)]
6+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
77
pub struct ExprClosure {
88
/// The attributes for the async closure
99
#[rune(iter)]
@@ -71,16 +71,16 @@ impl ExprClosure {
7171
/// # Examples
7272
///
7373
/// ```rust
74-
/// use rune::{parse_all, ast};
74+
/// use rune::{testing, ast};
7575
///
76-
/// parse_all::<ast::ExprClosure>("async || 42").unwrap();
77-
/// parse_all::<ast::ExprClosure>("|| 42").unwrap();
78-
/// parse_all::<ast::ExprClosure>("|| { 42 }").unwrap();
76+
/// testing::roundtrip::<ast::ExprClosure>("async || 42");
77+
/// testing::roundtrip::<ast::ExprClosure>("|| 42");
78+
/// testing::roundtrip::<ast::ExprClosure>("|| { 42 }");
7979
///
80-
/// let expr = parse_all::<ast::ExprClosure>("#[retry(n=3)] || 43").unwrap();
80+
/// let expr = testing::roundtrip::<ast::ExprClosure>("#[retry(n=3)] || 43");
8181
/// assert_eq!(expr.attributes.len(), 1);
8282
///
83-
/// let expr = parse_all::<ast::ExprClosure>("#[retry(n=3)] async || 43").unwrap();
83+
/// let expr = testing::roundtrip::<ast::ExprClosure>("#[retry(n=3)] async || 43");
8484
/// assert_eq!(expr.attributes.len(), 1);
8585
/// ```
8686
impl Parse for ExprClosure {
@@ -90,7 +90,7 @@ impl Parse for ExprClosure {
9090
}
9191
}
9292

93-
#[derive(Debug, Clone, ToTokens)]
93+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens)]
9494
pub enum ExprClosureArgs {
9595
Empty {
9696
/// The `||` token.

crates/rune/src/ast/expr_else.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ast;
22
use crate::{Parse, Spanned, ToTokens};
33

44
/// An else branch of an if expression.
5-
#[derive(Debug, Clone, ToTokens, Parse, Spanned)]
5+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Parse, Spanned)]
66
pub struct ExprElse {
77
/// The `else` token.
88
pub else_: ast::Else,

crates/rune/src/ast/expr_else_if.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ast;
22
use crate::{Parse, Spanned, ToTokens};
33

44
/// An else branch of an if expression.
5-
#[derive(Debug, Clone, ToTokens, Parse, Spanned)]
5+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Parse, Spanned)]
66
pub struct ExprElseIf {
77
/// The `else` token.
88
pub else_: ast::Else,

crates/rune/src/ast/expr_field_access.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ast;
22
use crate::{Spanned, ToTokens};
33

44
/// The field being accessed.
5-
#[derive(Debug, Clone, ToTokens, Spanned)]
5+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
66
pub enum ExprField {
77
/// An identifier.
88
Ident(ast::Ident),
@@ -11,7 +11,7 @@ pub enum ExprField {
1111
}
1212

1313
/// A field access `<expr>.<field>`.
14-
#[derive(Debug, Clone, ToTokens, Spanned)]
14+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
1515
pub struct ExprFieldAccess {
1616
/// The expr where the field is being accessed.
1717
pub expr: Box<ast::Expr>,

crates/rune/src/ast/expr_for.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use crate::{Parse, ParseError, Parser, Spanned, ToTokens};
66
/// # Examples
77
///
88
/// ```rust
9-
/// use rune::{parse_all, ast};
9+
/// use rune::{testing, ast};
1010
///
11-
/// parse_all::<ast::ExprFor>("for i in x {}").unwrap();
12-
/// parse_all::<ast::ExprFor>("'label: for i in x {}").unwrap();
13-
/// parse_all::<ast::ExprFor>("#[attr] 'label: for i in x {}").unwrap();
11+
/// testing::roundtrip::<ast::ExprFor>("for i in x {}");
12+
/// testing::roundtrip::<ast::ExprFor>("'label: for i in x {}");
13+
/// testing::roundtrip::<ast::ExprFor>("#[attr] 'label: for i in x {}");
1414
/// ```
15-
#[derive(Debug, Clone, ToTokens, Spanned)]
15+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
1616
pub struct ExprFor {
1717
/// The attributes of the `for` loop
1818
#[rune(iter)]

crates/rune/src/ast/expr_group.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ast;
22
use crate::{Parse, Spanned, ToTokens};
33

44
/// A prioritized expression group `(<expr>)`.
5-
#[derive(Debug, Clone, ToTokens, Parse, Spanned)]
5+
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Parse, Spanned)]
66
pub struct ExprGroup {
77
/// The open parenthesis.
88
pub open: ast::OpenParen,

0 commit comments

Comments
 (0)