Is there any way to access the parse tree of a PHP file from within PHP?
I'm trying to write a code-checking utility to find namespace-related
problems in code and alert me to them. Specifically:
namespace foo\bar;
function foobar()
{
try
{
...
}
catch(Exception $e)
{
//nothing will be caught
//I forgot to qualify
//code silently fails
}
}
I didn't want to put a whole lot of work into this, so I'm trying to
avoid having to write in C/flex/ANTLR/etc. There's a bit of
grease-monkey internals stuff in PHP so I thought I would check.
Thanks,
Jeremy
Is there any way to access the parse tree of a PHP file from within PHP?
will this work for you?
http://docs.php.net/manual/en/tokenizer.examples.php
I'm trying to write a code-checking utility to find namespace-related
problems in code and alert me to them. Specifically:namespace foo\bar;
function foobar()
{
try
{
...
}
catch(Exception $e)
{
//nothing will be caught
//I forgot to qualify
//code silently fails
}
}I didn't want to put a whole lot of work into this, so I'm trying to avoid
having to write in C/flex/ANTLR/etc. There's a bit of grease-monkey
internals stuff in PHP so I thought I would check.Thanks,
Jeremy--
--
Alexey Zakhlestin
http://www.milkfarmsoft.com/
Alexey Zakhlestin wrote:
Is there any way to access the parse tree of a PHP file from within PHP?
will this work for you?
http://docs.php.net/manual/en/tokenizer.examples.php
That looks like it should work perfectly! Thanks for pointing this out;
it's almost exactly what I was after.
Jeremy
Other possibility:
The PEAR package "PHP_UML" can parse PHP files/folders, and generate the API
in the form of a global standard XML file (and/or in the form of a full
XHTML documentation).
Namespaced code is supported.
But the parsed code must be object oriented, not procedural (for now).
Perhaps this could help anyway...
http://pear.php.net/manual/en/package.php.php-uml.intro.php
Baptiste
-----Original Message-----
From: Jeremy [mailto:jeremy@pinacol.com]
Sent: mercredi 25 mars 2009 18:14
To: internals@lists.php.net
Subject: [PHP-DEV] Access to syntax tree?
Is there any way to access the parse tree of a PHP file from within PHP?
I'm trying to write a code-checking utility to find namespace-related
problems in code and alert me to them. Specifically:
namespace foo\bar;
function foobar()
{
try
{
...
}
catch(Exception $e)
{
//nothing will be caught
//I forgot to qualify
//code silently fails
}
}
I didn't want to put a whole lot of work into this, so I'm trying to
avoid having to write in C/flex/ANTLR/etc. There's a bit of
grease-monkey internals stuff in PHP so I thought I would check.
Thanks,
Jeremy
Hello Jeremy,
Is there any way to access the parse tree of a PHP file from within PHP?
I'm trying to write a code-checking utility to find namespace-related
problems in code and alert me to them. Specifically:namespace foo\bar;
function foobar()
{
try
{
...
}
catch(Exception $e)
{
//nothing will be caught
//I forgot to qualify
//code silently fails
}
}
a little bit off topic here, but PHP_Depend's[1] parser and source graph
in branches/0.9.0[2] can be used to detect such issues. It is aware of
namespaces and can detect most class/interface language constructs like
catch, new, type-hints, static calls etc.
Greetings
Manuel
[1] http://pdepend.org/
[2] http://svn.pdepend.org/branches/0.9.0/
Manuel Pichler http://manuel-pichler.de
GnuPG Key: 6A5D2258 / B4F9 AA42 925D A4AA 4733 4E39 F48B 809C 6A5D 2258
Jeremy wrote:
Is there any way to access the parse tree of a PHP file from within PHP?
I'm trying to write a code-checking utility to find namespace-related
problems in code and alert me to them. Specifically:namespace foo\bar;
function foobar()
{
try
{
...
}
catch(Exception $e)
{
//nothing will be caught
//I forgot to qualify
//code silently fails
}
}I didn't want to put a whole lot of work into this, so I'm trying to
avoid having to write in C/flex/ANTLR/etc. There's a bit of
grease-monkey internals stuff in PHP so I thought I would check.
Hi Jeremy,
The parser file needs to be updated to reflect additions in PHP 5.3, but
http://pear.php.net/PHP_Parser provides an example of a parser in which
you could actually check for every class reference at parse-time,
ignoring everything else.
It's built using the tokenizer extension as a lexer, and a port of lemon
to PHP for the parser (zend_language_parser.y is also ported into lemon
format)
Greg