Rob Kinyon
2004-12-08 13:27:36 UTC
http://www.perlmonks.org/?node_id=413087 has a discussion of
Cylcomatic Complexity, which seems to be exactly what we're trying to
do here ...
to the various elements you asked for in the tree.
http://search.cpan.org/~adamk/PPI-0.831/lib/PPI/Element.pm
The tree itself still exists and is not modified, and you can still call
any of the "going up the tree" methods.
$Element->parent # Immediate parent
$Element->top # Top of the PDOM tree (should be a ::Document)
$Element->statement # Parent statement
http://search.cpan.org/~adamk/PPI-0.831/lib/PPI/Node.pm#find_$class_|_\&condition
The entire set of search methods (find, find_any and prune) accept
anonymous sub references as well, the class name thing is just a shortcut.
The sub is passed the top element of the search, and the "current"
element, so you can do something like the following, which will strip
out everything but the significant elements.
my $Doc = PPI::Document->read( $filename );
$Doc->prune( sub { ! $_[1]->significant } );
The "significant" property is defined at its method
http://search.cpan.org/~adamk/PPI-0.831/lib/PPI/Element.pm#significant
In short, perl contains a whole bunch of things you normally don't want
to care about. If I want to find the "previous" or "next" element, I
_probably_ don't care what whitespace or comments or pod is in the way.
And so the "s" serious of methods (snext_sibling, sprevious_sibling,
schildren etc) will do what you mean, ignoring any miscellaneous things
that don't matter that happen to be in the way.
This is important because when writing the perl document back to a file,
it is VERY important that the document ends up exactly as it was when we
read it in, in the same way something like Dreamweaver will read and
write a HTML file while leaving it exactly as is (layout, broken code
and all).
And whitespace does actually matter in some instances during the
parsing. Yes, perl is REALLY crufty :)
It isn't line-based, but it is line-based for some things (Heredocs).
Whitespace isn't important, but it does need to be there or things break.
I'm still shuffling things around to make sure I'm 100% comfortable with
the API myself.
Adam
Cylcomatic Complexity, which seems to be exactly what we're trying to
do here ...
I've got the basic tokens/block thing going in an exploration script.
One thing I'm noticing is that find() flattens out your parse-tree.
Having the ability to access the parent-node may end up being nice,
but I'm still feeling my way through the problem-space, so don't code
anything based on what I say just yet.
It doesn't flatten it out as such, it just returned a list of pointersOne thing I'm noticing is that find() flattens out your parse-tree.
Having the ability to access the parent-node may end up being nice,
but I'm still feeling my way through the problem-space, so don't code
anything based on what I say just yet.
to the various elements you asked for in the tree.
http://search.cpan.org/~adamk/PPI-0.831/lib/PPI/Element.pm
The tree itself still exists and is not modified, and you can still call
any of the "going up the tree" methods.
$Element->parent # Immediate parent
$Element->top # Top of the PDOM tree (should be a ::Document)
$Element->statement # Parent statement
Some documentation on what "significant" means would be nice. Also, if
significant() and prune() could play together, that would be cool. I'm
my $doc = PPI::Document->read( $filename );
$doc->prune( "PPI::Token::$_" ) for qw( Comment Whitespace );
See the find documentation atsignificant() and prune() could play together, that would be cool. I'm
my $doc = PPI::Document->read( $filename );
$doc->prune( "PPI::Token::$_" ) for qw( Comment Whitespace );
http://search.cpan.org/~adamk/PPI-0.831/lib/PPI/Node.pm#find_$class_|_\&condition
The entire set of search methods (find, find_any and prune) accept
anonymous sub references as well, the class name thing is just a shortcut.
The sub is passed the top element of the search, and the "current"
element, so you can do something like the following, which will strip
out everything but the significant elements.
my $Doc = PPI::Document->read( $filename );
$Doc->prune( sub { ! $_[1]->significant } );
The "significant" property is defined at its method
http://search.cpan.org/~adamk/PPI-0.831/lib/PPI/Element.pm#significant
In short, perl contains a whole bunch of things you normally don't want
to care about. If I want to find the "previous" or "next" element, I
_probably_ don't care what whitespace or comments or pod is in the way.
And so the "s" serious of methods (snext_sibling, sprevious_sibling,
schildren etc) will do what you mean, ignoring any miscellaneous things
that don't matter that happen to be in the way.
This is important because when writing the perl document back to a file,
it is VERY important that the document ends up exactly as it was when we
read it in, in the same way something like Dreamweaver will read and
write a HTML file while leaving it exactly as is (layout, broken code
and all).
And whitespace does actually matter in some instances during the
parsing. Yes, perl is REALLY crufty :)
It isn't line-based, but it is line-based for some things (Heredocs).
Whitespace isn't important, but it does need to be there or things break.
I suppose I could be a good opensource user and provide a patch+tests,
huh... :-)
I'm happy to receive patches but no guarantees that it will be merged.huh... :-)
I'm still shuffling things around to make sure I'm 100% comfortable with
the API myself.
Adam