Re: Analize Java source file with perl?
- From: publiustemp-beginnerscgi3@xxxxxxxxx (Ovid)
- Date: Thu, 26 Oct 2006 06:45:50 -0700 (PDT)
--- "bou, hou (GE Money, consultant)" <hou.bou@xxxxxx> wrote:
For Example
a java file named AAA.java
--------------------
/* author : John Smith */
// comment
public class ClassA {
/* */
//comment
public static void main(String[] args) {
}
...
}
class ClassB {
...
}
---------------------
I want to get like this
perl ClassChecker.pl AAA.java
ClassA
ClassB
Ooh, I really shouldn't do this, but here's one way of doing it. It
takes comments and quoted strings into account. It could probably use
tweaking.
Wasn't sure why I had to diddle Regexp::Common like that. I couldn't
use both {quoted} and {comment} in the same program without that. I
probably misread the docs somewhere.
Cheers,
Ovid
#!/usr/bin/perl -l
use strict;
use warnings;
use HOP::Lexer 'string_lexer';
use Regexp::Common;
Regexp::Common->import('comment'); # ??? That was odd
my @files = @ARGV;
die "Usage: $0 files" unless @files;
sub ignore {()}
my @input_tokens = (
[ COMMENT => qr/$RE{comment}{Java}/, \&ignore ],
[ QUOTE => qr/$RE{quoted}/, \&ignore ],
[ CLASS => 'class' ],
[ INDENTIFIER => qr/\w+/ ],
[ ELSE => qr/.*/, \&ignore ],
);
foreach my $file (@files) {
open my $fh, '<', $file or die "Cannot open ($file) for
reading: $!";
my $text = do { local $/; <$fh> };
my $lexer = string_lexer( $text, @input_tokens );
while ( defined ( my $token = $lexer->() ) ) {
next unless ref $token;
if ( 'CLASS' eq $token->[0] ) {
my $next = $lexer->('peek');
print "$file: $next->[1]";
}
}
}
--
Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/
.
- Follow-Ups:
- Re: Analize Java source file with perl?
- From: Dr.Ruud
- Re: Analize Java source file with perl?
- References:
- Analize Java source file with perl?
- From: Hou Bou
- Analize Java source file with perl?
- Prev by Date: Re: Spam from this list
- Next by Date: Re: How to pass string in command line argument.
- Previous by thread: Re: Analize Java source file with perl?
- Next by thread: Re: Analize Java source file with perl?
- Index(es):