stephens2424 / php

Parser for PHP written in Go
BSD 3-Clause "New" or "Revised" License
529 stars 77 forks source link

Comments are not available in AST #19

Open andygrunwald opened 9 years ago

andygrunwald commented 9 years ago

I got a test php file:

<?php
/* Foo   */
// Bar
$baz = "hey";

and a small go tool to parse this:

package main

import (
    "fmt"
    "github.com/stephens2424/php"
    "github.com/stephens2424/php/passes/printing"
    "io/ioutil"
    "log"
)

func main() {
    content, err := ioutil.ReadFile("./test.php")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(string(content))

    p := php.NewParser()
    a, err := p.Parse("test.php", string(content))
    if err != nil {
        log.Fatal(err)
    }

    w := printing.NewWalker()

    for _, node := range a.Nodes {
        w.Walk(node)
    }
}

The output looks like:

go run main.go
<?php
/* Foo   */
// Bar
$baz = "hey";

(ast.ExpressionStmt)=
    (ast.AssignmentExpression)=
        (*ast.Variable)$baz
            (*ast.Identifier)baz
        (*ast.Literal)Literal-string: "hey"

As you see the single line + multi line comments are nor parsed in the AST. Do i something wrong or are they not reflected as node?

stephens2424 commented 9 years ago

The comments aren't in the AST at the moment, but I have some pending work that should address this.

stephens2424 commented 9 years ago

The changes I pushed just now make the comments available in the lexed token stream, but there's still no association with AST nodes.

andygrunwald commented 9 years ago

Thank you. I think you mean https://github.com/stephens2424/php/commit/29a80da86744883c0d48919adbe7ca6acd1317f0 ?

Is your pending work this commit? If not it would be nice to push the branch :) Thanks for your project!

stephens2424 commented 9 years ago

Yep, that's the one. It's really a work in progress, though, since it's pretty difficult to use in a meaningful way. I'd like to get phpdoc comments into the ast, but I'm still pondering the approach.

andygrunwald commented 9 years ago

Thank you :)