# Access to syntax tree?

- Thread: https://externals.io/message/43464
- Original archive: https://news-web.php.net/php.internals/43464
- Started by Jeremy on 2009-03-25T17:13:44+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #43464 — Jeremy — 2009-03-25T17:13:44+00:00

URL: https://externals.io/message/43464#43464

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

---

## #43465 — Alexey Zakhlestin — 2009-03-25T17:48:58+00:00

URL: https://externals.io/message/43464#43465 · In reply to: #43464

> 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/

---

## #43466 — Jeremy — 2009-03-25T17:51:52+00:00

URL: https://externals.io/message/43464#43466 · In reply to: #43465

Alexey Zakhlestin wrote:
> 
> > […]
> 
> 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

---

## #43471 — Baptiste Autin — 2009-03-25T19:52:27+00:00

URL: https://externals.io/message/43464#43471 · In reply to: #43464

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

---

## #43474 — Manuel Pichler — 2009-03-25T20:23:54+00:00

URL: https://externals.io/message/43464#43474 · In reply to: #43464

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

---

## #43487 — Greg Beaver — 2009-03-26T13:52:35+00:00

URL: https://externals.io/message/43464#43487 · In reply to: #43464

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
