printer

package
v0.0.0-...-4b5f16b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	PrinterStateHTML printerState = iota
	PrinterStatePHP
)

Variables

This section is empty.

Functions

func NewPrinter

func NewPrinter(output io.Writer) *printer

Types

type Printer

type Printer = printer
Example
package main

import (
	"os"

	"github.com/Demooon86/php-parser/internal/php7"
	"github.com/Demooon86/php-parser/pkg/ast"
	"github.com/Demooon86/php-parser/pkg/conf"
	"github.com/Demooon86/php-parser/pkg/version"
	"github.com/Demooon86/php-parser/pkg/visitor/printer"
)

func main() {
	src := `<?php

namespace Foo;

abstract class Bar extends Baz
{
    public function greet()
    {
        echo "Hello";
        // some comment
    }
}
	`

	// parse

	config := conf.Config{
		Version: &version.Version{
			Major: 7,
			Minor: 4,
		},
	}
	lexer := php7.NewLexer([]byte(src), config)
	php7parser := php7.NewParser(lexer, config)
	php7parser.Parse()

	rootNode := php7parser.GetRootNode()

	// change namespace

	parts := &rootNode.(*ast.Root).Stmts[0].(*ast.StmtNamespace).Name.(*ast.Name).Parts
	*parts = append(*parts, &ast.NamePart{Value: []byte("Quuz")})

	// print

	p := printer.NewPrinter(os.Stdout)
	rootNode.Accept(p)

}
Output:
<?php

namespace Foo\Quuz;

abstract class Bar extends Baz
{
    public function greet()
    {
        echo "Hello";
        // some comment
    }
}

type PrinterPHP8

type PrinterPHP8 = printer
Example
package main

import (
	"os"

	"github.com/Demooon86/php-parser/internal/php8"
	"github.com/Demooon86/php-parser/pkg/ast"
	"github.com/Demooon86/php-parser/pkg/conf"
	"github.com/Demooon86/php-parser/pkg/version"
	"github.com/Demooon86/php-parser/pkg/visitor/printer"
)

func main() {
	src := `<?php

namespace Foo;

abstract class Bar extends Baz
{
    public function greet()
    {
        echo "Hello";
        // some comment
    }
}
	`

	// parsePHP8

	config := conf.Config{
		Version: &version.Version{
			Major: 8,
			Minor: 0,
		},
	}
	lexer := php8.NewLexer([]byte(src), config)
	php8parser := php8.NewParser(lexer, config)
	php8parser.Parse()

	rootNode := php8parser.GetRootNode()

	// change namespace

	parts := &rootNode.(*ast.Root).Stmts[0].(*ast.StmtNamespace).Name.(*ast.Name).Parts
	*parts = append(*parts, &ast.NamePart{Value: []byte("Quuz")})

	// print

	p := printer.NewPrinter(os.Stdout)
	rootNode.Accept(p)

}
Output:
<?php

namespace Foo\Quuz;

abstract class Bar extends Baz
{
    public function greet()
    {
        echo "Hello";
        // some comment
    }
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL