Documentation
¶
Index ¶
Examples ¶
Constants ¶
View Source
const ( PrinterStateHTML printerState = iota PrinterStatePHP )
Variables ¶
This section is empty.
Functions ¶
func NewPrinter ¶
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 } }
Click to show internal directories.
Click to hide internal directories.