tags:

views:

52

answers:

1

I need a class, preferably written in PHP, C#, or Python, that can parse PHP and return a token tree. Can anyone suggest a good, accurate one?

+4  A: 

Leave it to PHP to have a native function to do just that: http://php.net/token_get_all

SimpleCoder
I don't think it does a tree, just a token stream. The original request is for the tree.On the other hand, I am not sure the parser has an internal tree. Could be just a state machine. Certainly opening a bracket in one php block and closing it in another is not helping the tree generation. It could use skip tokens, I guess.
Alexandre Rafalovitch
It wouldn't be hard to build an OO tree that takes in a token array and converts it into a tree of objects (Basically, on the start of a block create a new object and hand the parsing to it, and then hand the parsing back once the block is done)...
ircmaxell
@ircmaxwell: yes, you could get a tree, that used tokens that way. You could define lots of trees with arbitrary internal nodes whose leaves happened to be the tokens from token_get_all. But what would the point of having any such a tree be (if it weren't some kind of parse tree)?
Ira Baxter
Basically what I will be doing is trying to write my own PHP formatter. However as I explore my options I am realizing that I probably don't need a tree.
SimpleCoder