Skip to content

Instantly share code, notes, and snippets.

@alecchen
Created January 14, 2010 08:55
Show Gist options
  • Save alecchen/276992 to your computer and use it in GitHub Desktop.
Save alecchen/276992 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
my $self = shift;
my $input = shift;
my $in_block = 0;
my $in_function = 0;
my $block_name = q{};
my $result = q{};
my $mark = $self->mark;
open FH, "<$input"
or die "Can't open $input for reading: $!";
foreach my $line (<FH>) {
chomp($line);
# comments
next if $line =~ /^\s*--[^!]/;
$line =~ s/--[^!].*//;
$line =~ s{$mark}{///};
if ($line =~ m{^\s*///}) {
$result .= "$line\n";
}
elsif ($line =~ /==/) {
next;
}
# function
elsif ($line =~ /^function/) {
$line .= q{;};
$in_function = 1;
$result .= "$line\n";
}
elsif ($in_function == 1 && $line =~ /^end/) {
$in_function = 0;
}
# block start
elsif ($in_function == 0 && $line =~ /^(\S+)\s*=\s*{/ && $line !~ /}/) {
$block_name = $1; $in_block = 1;
next;
}
elsif ($in_function == 0 && $line =~ /^\s*}/ && $in_block == 1) {
$block_name = q{};
$in_block = 0;
next;
}
# variables
elsif ($in_function == 0 && $line =~ /=/) {
$line =~ s/(?=\S)/$block_name./ if $block_name;
$line =~ s{,?(\s*)(?=///|$)}{;$1};
$result .= "$line\n";
}
else {
next;
}
}
close FH;
return $result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment