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
2010-08-15 01:37:06
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
2010-08-15 02:23:21
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
2010-08-15 04:06:54
@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
2010-08-15 04:24:18
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
2010-08-18 13:00:08