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 $footer = q[ |
29 |
<footer> |
30 |
[<a href="input">Input</a>] |
31 |
[<a href="../doc/readme">Source</a>] |
32 |
</footer> |
33 |
]; |
34 |
|
35 |
my @error; |
36 |
$parser->onerror (sub { |
37 |
my %args = @_; |
38 |
my $r = '<li>'; |
39 |
if ($args{level} eq 'w') { |
40 |
$r .= '<strong>Warning</strong>: '; |
41 |
} else { |
42 |
$r .= '<strong>Error</strong>: '; |
43 |
} |
44 |
|
45 |
$r .= htescape sprintf $args{type}, @{$args{args}}; |
46 |
|
47 |
$r .= ': <code>'; |
48 |
$r .= htescape substr ${$args{valueref}}, 0, $args{pos_start}; |
49 |
$r .= '<mark>'; |
50 |
$r .= htescape substr ${$args{valueref}}, |
51 |
$args{pos_start}, $args{pos_end} - $args{pos_start}; |
52 |
$r .= '</mark>'; |
53 |
$r .= htescape substr ${$args{valueref}}, $args{pos_end}; |
54 |
$r .= '</code></li>'; |
55 |
|
56 |
push @error, $r; |
57 |
}); |
58 |
|
59 |
eval { |
60 |
$parser->parse ($regexp); |
61 |
}; |
62 |
|
63 |
if ($parser->errnum) { |
64 |
binmode STDOUT, ':encoding(utf-8)'; |
65 |
print "Content-Type: text/html; charset=utf-8\n\n"; |
66 |
print q[<!DOCTYPE HTML><html lang=en> |
67 |
<title>Regular expression visualizer: ], $eregexp, q[</title> |
68 |
<link rel="stylesheet" href="/www/style/html/xhtml"> |
69 |
|
70 |
<h1>Regular expression visualizer</h1> |
71 |
|
72 |
<p>Input: <code>], $eregexp, q[</code></p> |
73 |
|
74 |
<p>Error: |
75 |
<ul>]; |
76 |
print join '', @error; |
77 |
print q[</ul>]; |
78 |
|
79 |
print $footer; |
80 |
|
81 |
exit; |
82 |
} |
83 |
|
84 |
require Regexp::Visualize::Simple; |
85 |
my $v = Regexp::Visualize::Simple->new; |
86 |
$v->push_regexp_node ($parser->root); |
87 |
|
88 |
binmode STDOUT, ':encoding(utf-8)'; |
89 |
print "Content-Type: application/xhtml+xml; charset=utf-8\n\n"; |
90 |
|
91 |
print q[<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> |
92 |
<head><title>Regular expression visualizer: ], $eregexp, q[</title> |
93 |
<link rel="stylesheet" href="/www/style/html/xhtml"/> |
94 |
</head> |
95 |
<body> |
96 |
<h1>Regular expression visualizer</h1> |
97 |
|
98 |
<p>Input: <code>], $eregexp, q[</code></p>]; |
99 |
|
100 |
if (@error) { |
101 |
print q[<ul>]; |
102 |
print join '', @error; |
103 |
print q[</ul>]; |
104 |
} |
105 |
|
106 |
while ($v->has_regexp_node) { |
107 |
my ($g, $index) = $v->next_graph; |
108 |
|
109 |
print "<section><h2>Regexp #$index</h2>\n\n"; |
110 |
print $g->as_svg; |
111 |
print "</section>\n"; |
112 |
} |
113 |
|
114 |
print $footer; |
115 |
|
116 |
print q[</body></html>]; |