1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
use warnings; |
4 |
use feature 'state'; |
5 |
use CGI::Carp qw(fatalsToBrowser); |
6 |
|
7 |
use FindBin; |
8 |
use lib qq[$FindBin::Bin/../lib]; |
9 |
use lib q[/home/wakaba/work/manakai2/lib]; |
10 |
use Message::CGI::Util qw/percent_decode htescape/; |
11 |
use Message::CGI::HTTP; |
12 |
|
13 |
use Regexp::Parser::Perl58; |
14 |
|
15 |
my $cgi = Message::CGI::HTTP->new; |
16 |
|
17 |
my $regexp = percent_decode $cgi->get_parameter ('s') // ''; |
18 |
$regexp = '(?:)' unless length $regexp; |
19 |
|
20 |
my $parser = Regexp::Parser::Perl58->new; |
21 |
|
22 |
eval { |
23 |
$parser->parse ($regexp); |
24 |
}; |
25 |
my $eregexp = htescape $regexp; |
26 |
|
27 |
if ($parser->errnum) { |
28 |
binmode STDOUT, ':encoding(utf-8)'; |
29 |
print "Content-Type: text/html; charset=utf-8\n\n"; |
30 |
print q[<!DOCTYPE HTML><html lang=en> |
31 |
<title>Regular expression visualizer: $eregexp</title> |
32 |
<link rel="stylesheet" href="/www/style/html/xhtml"/> |
33 |
</head> |
34 |
<body> |
35 |
<h1>Regular expression visualizer</h1> |
36 |
|
37 |
<p>Input: <code>], $eregexp, q[</code></p> |
38 |
|
39 |
<p>Error: ], htescape ($parser->errmsg); |
40 |
exit; |
41 |
} |
42 |
|
43 |
require Regexp::Visualize::Simple; |
44 |
my $v = Regexp::Visualize::Simple->new; |
45 |
$v->push_regexp_node ($parser->root); |
46 |
|
47 |
binmode STDOUT, ':encoding(utf-8)'; |
48 |
print "Content-Type: application/xhtml+xml; charset=utf-8\n\n"; |
49 |
|
50 |
print q[<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> |
51 |
<head><title>Regular expression visualizer: $eregexp</title> |
52 |
<link rel="stylesheet" href="/www/style/html/xhtml"/> |
53 |
</head> |
54 |
<body> |
55 |
<h1>Regular expression visualizer</h1> |
56 |
|
57 |
<p>Input: <code>], $eregexp, q[</code></p>]; |
58 |
|
59 |
while ($v->has_regexp_node) { |
60 |
my ($g, $index) = $v->next_graph; |
61 |
|
62 |
print "<section><h2>Regexp #$index</h2>\n\n"; |
63 |
print $g->as_svg; |
64 |
print "</section>\n"; |
65 |
} |
66 |
|
67 |
print q[</body></html>]; |
68 |
|