Skip to main content

php_printer/
lib.rs

1//! Pretty printer for PHP AST — converts parsed AST back to PHP source code.
2//!
3//! # Example
4//!
5//! ```
6//! let arena = bumpalo::Bump::new();
7//! let result = php_rs_parser::parse(&arena, "<?php echo 1 + 2;");
8//! let output = php_printer::pretty_print(&result.program);
9//! assert_eq!(output, "echo 1 + 2;");
10//! ```
11
12mod precedence;
13mod printer;
14
15pub use printer::{Indent, PrinterConfig};
16
17use php_ast::Program;
18
19/// Pretty-print a program's statements (without `<?php` header).
20pub fn pretty_print(program: &Program) -> String {
21    pretty_print_with_config(program, &PrinterConfig::default())
22}
23
24/// Pretty-print a complete PHP file (prepends `<?php\n\n`).
25pub fn pretty_print_file(program: &Program) -> String {
26    let mut out = String::from("<?php\n\n");
27    out.push_str(&pretty_print(program));
28    out.push('\n');
29    out
30}
31
32/// Pretty-print with custom configuration.
33pub fn pretty_print_with_config(program: &Program, config: &PrinterConfig) -> String {
34    let mut p = printer::Printer::new(config);
35    p.print_program(program);
36    p.into_output()
37}