pub struct TokenStreamFormatter<S, W> { /* private fields */ }
Expand description

Helper type for correctly and reasonably “pretty”-printing any TokenStream in a grammar- and language-agnostic way. This mostly means dealing with parentheses, so that nested structures don’t end up on one single long line.

Of course, it is not possible to perform pretty-printing in a completely generic manner, but the primary purpose of this mechanism is not that – it’s merely trying to be a useful debugging tool, of which the results are less unnecessarily verbose, and therefore easier to read, than the output of #[derive(Debug)].

use parsel::util::TokenStreamFormatter;
use parsel::quote::quote;

let ts = quote!{
    [
        [
            7.43 * {
                zzz (
                    3333 + "52" - 'a / [
                        foo bar || &baz;
                    ]
                ) != 5;
                ww;
                6 <<= 78 >>= 951,
                $ foo $bar #![attribute]
            },
            x, y
        ]
    ]
};

let mut string = String::new();
let mut fmt = TokenStreamFormatter::new(&mut string);
fmt.write(ts)?;

assert_eq!(string, str::trim(r#"
[
    [
        7.43 * {
            zzz (
                3333 + "52" - 'a / [
                    foo bar || & baz ;
                ]
            )
            != 5 ;
            ww ;
            6 <<= 78 >>= 951 ,
            $ foo $ bar # ! [
                attribute
            ]
        }
        ,
        x ,
        y
    ]
]
"#));

Implementations

Constructor for indenting with arbitrary whitespace.

This returns an error when non-whitespace characters are present in the indentation.

let ok = TokenStreamFormatter::with_indent("  \t", Cursor::new(&[] as &[u8]));
assert!(ok.is_ok());

let err = TokenStreamFormatter::with_indent("  not ws ", Cursor::new(&[] as &[u8]));
assert!(err.is_err());

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.