z7zmey / php-parser

PHP parser written in Go
https://php-parser.com
MIT License
941 stars 63 forks source link

Fix usage example in README #88

Closed whisk closed 5 years ago

whisk commented 5 years ago

Usage example in README.md is broken and outputs compile error:

./go_parser.go:25:11: unknown field 'Comments' in struct literal of type visitor.Dumper
./go_parser.go:26:12: unknown field 'Positions' in struct literal of type visitor.Dumper
./go_parser.go:30:15: cannot use visitor (type visitor.Dumper) as type walker.Visitor in argument to rootNode.Walk:
    visitor.Dumper does not implement walker.Visitor (EnterChildList method has pointer receiver)

Working example should be like:

package main

import (
    "bytes"
    "fmt"
    "os"

    "github.com/z7zmey/php-parser/php7"
    "github.com/z7zmey/php-parser/visitor"
)

func main() {
    src := bytes.NewBufferString(`<? echo "Hello world";`)

    parser := php7.NewParser(src, "example.php")
    parser.Parse()

    for _, e := range parser.GetErrors() {
        fmt.Println(e)
    }

    visitor := visitor.Dumper{
        Writer: os.Stdout,
        Indent: "",
    }

    rootNode := parser.GetRootNode()
    rootNode.Walk(&visitor)
}