Skip to content

Instantly share code, notes, and snippets.

@cooldaemon
Created August 18, 2009 05:52
Show Gist options
  • Save cooldaemon/169567 to your computer and use it in GitHub Desktop.
Save cooldaemon/169567 to your computer and use it in GitHub Desktop.
This is a command that makes the keyword file for Objective-C.
$ ./objc_dict_maker.pl -in /Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/Foundation.framework/Versions/C/Headers
or
$ ./objc_dict_maker.pl -in /Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Versions/C/Headers
#!/usr/bin/env perl
package ObjcDictMaker;
use feature qw(:5.10);
use version; our $VERSION = qv('0.0.1');
use FindBin;
use List::MoreUtils qw(uniq);
use Any::Moose;
use Any::Moose 'X::Types::Path::Class';
with any_moose('X::Getopt');
has 'in' => (
is => 'ro',
isa => 'Path::Class::Dir',
required => 1,
coerce => 1,
documentation => 'analyzed directory.',
);
has 'out' => (
is => 'ro',
isa => 'Path::Class::File',
default => $FindBin::RealBin . '/objc.dict',
coerce => 1,
documentation => 'dictionary file name.',
);
__PACKAGE__->meta->make_immutable;
no Any::Moose;
sub run {
my $self = shift;
my @keywords;
$self->in->recurse(callback => sub {
my $file = shift;
return if !-f $file;
return if $file !~ /\.h$/;
push @keywords, @{$self->_analyze($file)};
return;
});
my $out_fh = $self->out->openw or die $!;
$out_fh->print(join("\n", (sort {$a cmp $b} uniq @keywords),), "\n",);
$out_fh->close;
return;
}
sub _analyze {
my $self = shift;
my ($file,) = @_;
say $file->basename;
my @keywords;
my $fh = $file->openr or return;
while (my $line = $fh->getline) {
# $line =~ s/^\s*//;
$line =~ s/\n$//;
if ($line =~ /^[-+]/) {
push @keywords, $self->_analyze_method($line);
} elsif ($line =~ /^\@interface/) {
push @keywords, $self->_analyze_class($line);
}
}
$fh->close;
return \@keywords;
}
sub _analyze_method {
my $self = shift;
my ($line,) = @_;
$line =~ s/^([^;]+);.*$/$1/;
$line =~ s/^[-+]\s*\(\s*[^)]+\s*\)\s*//;
$line =~ s/\(\s*[^)]+\s*\)//g;
return grep {/^[A-Za-z]*$/} map {$_ =~ s/:[^:]+$//; $_} split(/\s+/, $line);
}
sub _analyze_class {
my $self = shift;
my ($line,) = @_;
if ($line =~ /^\@interface\s+([^\s:(]+)/) {
return $1;
}
return;
}
package main;
my $objc_dict_maker = eval {ObjcDictMaker->new_with_options()}
or die 'usage: ', $FindBin::RealScript, ' -in [analyzed directory]';
$objc_dict_maker->run();
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment