Acme-AsciiArtinator
view release on metacpan or search on metacpan
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
my ($i, $j, $Q, @tokens, $token, $sigil, @contexts, @blocks);
$sigil = 0;
for ($i = 0; $i < @INPUT; $i++) {
$_ = $INPUT[$i];
$Q = "@INPUT[$i..$#INPUT]";
print STDERR "\$Q = ", substr($Q,0,8), "... SIGIL=$sigil\n" if $_ eq "q" && $DEBUG;
# $# could be "the output format of printed numbers"
# or it could be the start of an expression like $#X or $#{@$X}
# in the latter case we need $# + one more token to be contiguous
if ($Q =~ /^\$\#\{/ || $Q =~ /^\$\#\w+/) {
$token = $&;
push @tokens, $token;
push @contexts, "\$# operator";
$i = $i - 1 + length $token;
$sigil = 0;
next;
}
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
push @contexts, "misanalyzed regex or operator";
} elsif ($_ eq "/") {
push @contexts, "regular expression C ///";
} else {
push @contexts, "quoted string";
}
$i = $j;
} elsif (!$sigil && $Q =~ /^[0-9]*\.{0,1}[0-9]+([eE][-+]?[0-9]+)?/) {
# if first char starts a numeric literal, include all characters
# from the number in the token
$token = $&;
push @tokens, $token;
push @contexts, "numeric literal A";
$i = $i - 1 + length $token;
} elsif (!$sigil && $Q =~ /^[0-9]+\.{0,1}[0-9]*([eE][-+]?[0-9]+)?/) {
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
push @contexts, "regular expression x /$terminator/";
for (my $t=$t1+1; $t<=$t2; $t++) {
if (substr($regex,$t,1) =~ /\S/) {
push @tokens, substr($regex,$t,1);
push @contexts, "content of regex/x";
}
}
$i -= length($token) + length($regex) - $t2 - 1;
# positions $i to the start of the 2nd pattern,
# which can be tokenized as a perl expression.
# Hopefully the terminator can be recognized
} elsif ($token =~ /x/) {
pop @tokens;
pop @contexts;
my $regex = pop @tokens;
my $regex_context = pop @contexts;
my $terminator2 = $terminator;
$terminator2 =~ tr/])}>/[({</;
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
strings in a void context) that may be put to use in future versions
of this module.
When all tokens from the code are successfully aligned with the
blocks of darkspace from the artwork, we can paste the code on top
of the art and write the output file.
Sometimes we insert too many characters without successfully
aligning the tokens and darkspace blocks (and actually in the
spider example, this happens about 90% of the time). If this
happens, we will start over and retry up to 100 times.
=head1 BEST PRACTICES
Certain coding practices will increase the chance that
C<Acme::AsciiArtinator> will be able to embed your code
in the artwork of your choice. In no particular order,
here are some suggestions:
=over 4
t/10-tokenize.t view on Meta::CPAN
ok(@tokens == 6);
ok($tokens[5] eq "CDE");
@tokens = Acme::AsciiArtinator::tokenize_code('"$A$B$CDE"');
ok(@tokens == 1, "quoted string is one token");
@tokens = Acme::AsciiArtinator::tokenize_code('qq{$A$B;$CDE}');
ok(@tokens == 1, "qq{string} is one token");
@tokens = Acme::AsciiArtinator::tokenize_code('$".Hello.world."$/$/"');
ok(@tokens == 8, "\$\" does not start a quoted string");
@tokens = Acme::AsciiArtinator::tokenize_code('$z=$r//$s//($t||=$u)');
ok(@tokens == 16, "dipthongs are tokenized correctly");
ok($tokens[5] eq "//", "perl5.10 dipthongs are tokenized");
ok($tokens[12] eq "||=", "3 character dipthongs are tokenized");
ok(1);ok(1);ok(1);ok(1);ok(1);ok(1);
( run in 0.231 second using v1.01-cache-2.11-cpan-0d8aa00de5b )