1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
use warnings; |
4 |
use CGI::Carp qw(fatalsToBrowser); |
5 |
|
6 |
use FindBin; |
7 |
use lib qq[$FindBin::Bin/../lib]; |
8 |
use lib q[/home/wakaba/work/manakai2/lib]; |
9 |
use Message::CGI::Util qw/htescape/; |
10 |
use Encode; |
11 |
|
12 |
use Message::CGI::HTTP; |
13 |
my $cgi = Message::CGI::HTTP->new; |
14 |
|
15 |
my $regexp = decode 'utf-8', $cgi->get_parameter ('s') // ''; |
16 |
$regexp = '(?:)' unless length $regexp; |
17 |
my $eregexp = htescape $regexp; |
18 |
|
19 |
my $lang = $cgi->get_parameter ('l') // 'perl58'; |
20 |
my $class = $lang eq 'js' |
21 |
? 'Regexp::Parser::JavaScript' |
22 |
: 'Regexp::Parser::Perl58'; |
23 |
|
24 |
use UNIVERSAL::require; |
25 |
$class->use or die $@; |
26 |
my $parser = $class->new; |
27 |
|
28 |
my @error; |
29 |
$parser->onerror (sub { |
30 |
my %args = @_; |
31 |
my $r = '<li>'; |
32 |
if ($args{level} eq 'w') { |
33 |
$r .= '<strong>Warning</strong>: '; |
34 |
} else { |
35 |
$r .= '<strong>Error</strong>: '; |
36 |
} |
37 |
|
38 |
$r .= htescape sprintf $args{type}, @{$args{args}}; |
39 |
|
40 |
$r .= ': <code>'; |
41 |
$r .= htescape substr ${$args{valueref}}, 0, $args{pos_start}; |
42 |
$r .= '<mark>'; |
43 |
$r .= htescape substr ${$args{valueref}}, |
44 |
$args{pos_start}, $args{pos_end} - $args{pos_start}; |
45 |
$r .= '</mark>'; |
46 |
$r .= htescape substr ${$args{valueref}}, $args{pos_end}; |
47 |
$r .= '</code></li>'; |
48 |
|
49 |
push @error, $r; |
50 |
}); |
51 |
|
52 |
eval { |
53 |
$parser->parse ($regexp); |
54 |
}; |
55 |
|
56 |
if ($parser->errnum) { |
57 |
binmode STDOUT, ':encoding(utf-8)'; |
58 |
print "Content-Type: text/html; charset=utf-8\n\n"; |
59 |
print q[<!DOCTYPE HTML><html lang=en> |
60 |
<title>Regular expression visualizer: ], $eregexp, q[</title> |
61 |
<link rel="stylesheet" href="/www/style/html/xhtml"/> |
62 |
</head> |
63 |
<body> |
64 |
<h1>Regular expression visualizer</h1> |
65 |
|
66 |
<p>Input: <code>], $eregexp, q[</code></p> |
67 |
|
68 |
<p>Error: |
69 |
<ul>]; |
70 |
print join '', @error; |
71 |
print q[</ul>]; |
72 |
|
73 |
exit; |
74 |
} |
75 |
|
76 |
require Regexp::Visualize::Simple; |
77 |
my $v = Regexp::Visualize::Simple->new; |
78 |
$v->push_regexp_node ($parser->root); |
79 |
|
80 |
binmode STDOUT, ':encoding(utf-8)'; |
81 |
print "Content-Type: application/xhtml+xml; charset=utf-8\n\n"; |
82 |
|
83 |
print q[<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> |
84 |
<head><title>Regular expression visualizer: ], $eregexp, q[</title> |
85 |
<link rel="stylesheet" href="/www/style/html/xhtml"/> |
86 |
</head> |
87 |
<body> |
88 |
<h1>Regular expression visualizer</h1> |
89 |
|
90 |
<p>Input: <code>], $eregexp, q[</code></p>]; |
91 |
|
92 |
if (@error) { |
93 |
print q[<ul>]; |
94 |
print join '', @error; |
95 |
print q[</ul>]; |
96 |
} |
97 |
|
98 |
while ($v->has_regexp_node) { |
99 |
my ($g, $index) = $v->next_graph; |
100 |
|
101 |
print "<section><h2>Regexp #$index</h2>\n\n"; |
102 |
print $g->as_svg; |
103 |
print "</section>\n"; |
104 |
} |
105 |
|
106 |
print q[</body></html>]; |
107 |
|