--- "bou, hou (GE Money, consultant)" <[EMAIL PROTECTED]> 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/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to