diff options
| author | Paul Buetow <paul@buetow.org> | 2008-05-25 03:21:39 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-05-25 03:21:39 +0000 |
| commit | cfba2b83a79e594f4ce5e202d8b6fd7de650ee7d (patch) | |
| tree | 3f53c0d2345e87aa9821eb8a2300aa4dad425f69 | |
| parent | 56126ce3bd8d84c005f16212d23327e98e6976aa (diff) | |
| -rw-r--r-- | scripts/sortmethods.pl | 135 |
1 files changed, 42 insertions, 93 deletions
diff --git a/scripts/sortmethods.pl b/scripts/sortmethods.pl index 29800bd..3ee19b1 100644 --- a/scripts/sortmethods.pl +++ b/scripts/sortmethods.pl @@ -7,6 +7,7 @@ use strict; use warnings; use File::Find; +use re 'eval'; my @files; @@ -14,104 +15,52 @@ sub usage () { return "Usage: perl $0 <sourcedir>\n"; } -sub process (@) { - my ($file, @input) = @_; - - my $package; - my @imports; - my $classOrInterface; - my @variables; - my @constructors; - my @methods; - - my $isMethod = 0; - my $isInnerClass = 0; - my $methodparant = 0; - - for (@input) { - if (/^package/) { - $package = $_; - - } elsif (/^import/) { - push @imports, $_; - - } elsif (/^[^ ].*class/) { - $classOrInterface = $_; - - } elsif (/^[^ ].*interface/) { - $classOrInterface = $_; - - } elsif (!$isMethod && /;/ && !/{/) { - push @variables, $_; - - } elsif (!$isMethod && !/;/ && /{/) { - $methodparant = 0; - ++$methodparant while /{/g; - --$methodparant while /}/g; - my ($name) = /(\w*?\(.*) {/i; - next unless defined $name; - $isMethod = 1; - my %method = ( - name => $name, - prototype => $_, - code => [], - ); - push @methods, \%method; - - } elsif ($isMethod) { - ++$methodparant while /{/g; - --$methodparant while /}/g; - - $isMethod = 0 if $methodparant == 0; - push @{$methods[-1]->{code}}, $_; - } - } - - die "undef package in $file\n" unless defined $package; - die "undef classOrInterface in $file\n" unless defined $classOrInterface; - - my @output = (); - - push @output, $package; - push @output, "\n"; - - if (@imports) { - push @output, sort @imports; - push @output, "\n"; - } - - push @output, $classOrInterface; - - if (@variables) { - push @output, sort @variables; - push @output, "\n"; - } - - if (@methods) { - push @output, - map { "@" .$_->{name} . "=>". $_->{prototype}, @{$_->{code}}, "\n" } - sort { $a->{name} cmp $b->{name} } @methods; - } - - push @output, "}\n"; - - return @output; +sub process ($$) { + my %class; + my $indent; + + $_[1] =~ + m< + (?{ print "Start parsing $_[0]\n" }) + (package .*?;\n) (?{ + print "Found package: $1"; + $class{package} = $1; + }) + (?: + .*? + (import .*?;\n) (?{ + print "Found import: $2"; + push @{$class{imports}}, $2; + }) + )+ + .*? + ((?:(?:class)|(?:interface)) .*?) {\n+ (?{ + print "Sorting imports\n"; + @{$class{imports}} = sort @{$class{imports}}; + print "Found class prototype: $3"; + $class{prototype} = $3; + }) + (?= + (\s+) (?{ + $indent = length $4; + print "Class indent is " . $indent . "\n"; + }) + ) + (?: + \n?(??{'\s' x $indent}) + (.+?{.*? \n(??{'\s' x $indent}) }) (?{ + print "Found [[$5]]"; + }) + )* + >xs; + (); } -my $startDir = shift || die usage(); -find(sub { push @files, $File::Find::name if /\.java$/ }, $startDir); +find(sub { push @files, $File::Find::name if /\.java$/ }, shift || die usage); for (@files) { open my $file, $_ or die "$!: $_\n"; - my @input = <$file>; + print process $_, join '', <$file>; close $file; - - my @output = process($_, @input); - print @output; - - print "=================== END $_\n"; } - -=cut - |
