--- trunk/lib/Frey/PPI.pm 2008/11/16 17:39:39 362 +++ trunk/lib/Frey/PPI.pm 2008/11/16 19:50:36 364 @@ -13,58 +13,68 @@ documentation => 'Name of class to parse', ); -my $ppi_doc; - -sub doc { - my ( $self ) = @_; - - if ( ! $ppi_doc ) { - warn "# parse ", $self->class; +has 'doc' => ( + is => 'ro', + isa => 'PPI::Document', + lazy => 1, + default => sub { + my ( $self ) = @_; + warn "doc from ", $self->class; my $doc = PPI::Document->new( $self->class_path( $self->class ) ) || die $!; $doc->prune('PPI::Token::Whitespace'); - $ppi_doc = $doc; - } - return $ppi_doc; -} + return $doc; + }, +); -sub attribute_order { - my ( $self ) = @_; +sub find { + my ( $self, $coderef ) = @_; my $doc = $self->doc; + $doc->find(sub { + my ( $doc,$el ) = @_; + eval { $coderef->( $doc, $el ) }; + warn "ERROR: $@" if $@; + }); +} - my @attribute_order; +sub attribute_order { + my ( $self ) = @_; - $doc->find(sub { + my $attribute_order; + $self->find(sub { my ($doc,$el) = @_; - if ( $el->isa('PPI::Statement') && $el->{children}->[0]->{content} eq 'has' ) { - warn "## has ",$el->{children}->[1]->string if $self->debug; - push @attribute_order, $el->{children}->[1]->string; - } - return 1; + return unless ( $el->isa('PPI::Statement') && $el->{children}->[0]->isa('PPI::Token::Word') && $el->{children}->[0]->literal eq 'has' ); + + warn "## has ",$el->{children}->[1]->literal if $self->debug; + push @$attribute_order, $el->{children}->[1]->literal; }); - warn "# ", $self->class, " attribute_order ", dump( @attribute_order ) if $self->debug; + warn "# ", $self->class, " attribute_order ", dump( $attribute_order ) if $self->debug; - return @attribute_order; + return $attribute_order; } -sub data { +sub includes { my $self = shift; - my $doc = $self->doc; + my $include; - $doc->find( sub { + $self->find(sub { my ($doc,$el) = @_; - if ( $el->isa('PPI::Statement::Include') ) { - warn dump( $el->module, $el->type, $el->pragma ); - push @{ $include->{ $el->type } }, $el->module - } - return 1; + return unless $el->isa('PPI::Statement::Include'); + + warn "## include ",dump( $el->module, $el->type, $el->pragma ) if $self->debug; + push @{ $include->{ $el->type } }, $el->module }); - warn "# ", $self->class, " ", dump( $include ) if $self->debug; + warn "# ", $self->class, " include ", dump( $include ) if $self->debug; + return $include; +} +sub data { + my $self = shift; return { - include => $include, - doc => $doc, + includes => $self->includes, + attribute_order => $self->attribute_order, + doc => $self->doc, }; }