ToTokens

Trait ToTokens 

Source
pub trait ToTokens {
    type Printer: Printer;

    // Required method
    fn write(&self, printer: &mut Self::Printer);

    // Provided method
    fn to_string_formatted(&self) -> String
       where Self::Printer: Default { ... }
}
Expand description

Trait for converting AST nodes back to text.

ToTokens is the inverse of Parse - it converts parsed structures back into their textual representation. This enables:

  • Code formatting / pretty-printing
  • Source-to-source transformations
  • Round-trip testing (parse → modify → print)

§Associated Types

  • Printer: The printer implementation for formatting output

§Required Methods

  • write(&self, printer): Write this value to the printer

§Provided Methods

  • to_string_formatted(): Convenience method for getting a String

§Example

use synkit::{ToTokens, Printer};

struct BinaryExpr {
    left: Box<Expr>,
    op: &'static str,
    right: Box<Expr>,
}

impl ToTokens for BinaryExpr {
    type Printer = MyPrinter;

    fn write(&self, p: &mut Self::Printer) {
        self.left.write(p);
        p.write(" ");
        p.write(self.op);
        p.write(" ");
        self.right.write(p);
    }
}

§Blanket Implementations

  • Option<T>: Writes nothing for None, delegates for Some
  • Box<T>: Delegates to inner value
  • Vec<T>: Writes each element in sequence
  • &T: Delegates to referenced value

Required Associated Types§

Source

type Printer: Printer

The printer type for formatting output.

Required Methods§

Source

fn write(&self, printer: &mut Self::Printer)

Write this value to the printer.

§Arguments
  • printer - The printer to write to

Provided Methods§

Source

fn to_string_formatted(&self) -> String
where Self::Printer: Default,

Convert to a formatted string.

Convenience method that creates a default printer, writes to it, and returns the result as a String.

§Returns

The formatted string representation

Implementations on Foreign Types§

Source§

impl<T: ToTokens> ToTokens for Option<T>

Source§

type Printer = <T as ToTokens>::Printer

Source§

fn write(&self, p: &mut Self::Printer)

Source§

impl<T: ToTokens> ToTokens for &T

Source§

type Printer = <T as ToTokens>::Printer

Source§

fn write(&self, p: &mut Self::Printer)

Source§

impl<T: ToTokens> ToTokens for Box<T>

Source§

type Printer = <T as ToTokens>::Printer

Source§

fn write(&self, p: &mut Self::Printer)

Source§

impl<T: ToTokens> ToTokens for Vec<T>

Source§

type Printer = <T as ToTokens>::Printer

Source§

fn write(&self, p: &mut Self::Printer)

Implementors§