-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp.cgi
executable file
·119 lines (93 loc) · 2.63 KB
/
regexp.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use Path::Tiny;
use lib path (__FILE__)->parent->parent->child ('lib')->stringify;
use Web::Encoding;
sub htescape ($) {
my $s = shift;
$s =~ s/&/&/g;
$s =~ s/</</g;
$s =~ s/"/"/g;
return $s;
} # htescape
my %qp = map { s/%([0-9A-Fa-f]{2})/pack 'C', hex $1/ge; $_ } map { split /=/, $_, 2 } split /&/, $ENV{QUERY_STRING} // '';
my $regexp = decode_web_utf8 $qp{s} // '';
$regexp = '(?:)' unless length $regexp;
my $eregexp = htescape $regexp;
my $lang = $qp{l} // 'perl58';
my $class = $lang eq 'js'
? 'Regexp::Parser::JavaScript'
: 'Regexp::Parser::Perl58';
eval qq{ require $class } or die $@;
my $parser = $class->new;
my $footer = q[
<footer>
[<a href="input">Input</a>]
[<a href="../doc/readme">Source</a>]
</footer>
];
my @error;
$parser->onerror (sub {
my %args = @_;
my $r = '<li>';
if ($args{level} eq 'w') {
$r .= '<strong>Warning</strong>: ';
} else {
$r .= '<strong>Error</strong>: ';
}
$r .= htescape sprintf $args{type}, @{$args{args}};
$r .= ': <code>';
$r .= htescape substr ${$args{valueref}}, 0, $args{pos_start};
$r .= '<mark>';
$r .= htescape substr ${$args{valueref}},
$args{pos_start}, $args{pos_end} - $args{pos_start};
$r .= '</mark>';
$r .= htescape substr ${$args{valueref}}, $args{pos_end};
$r .= '</code></li>';
push @error, $r;
});
eval {
$parser->parse ($regexp);
};
if ($parser->errnum) {
binmode STDOUT, ':encoding(utf-8)';
print "Content-Type: text/html; charset=utf-8\n\n";
print q[<!DOCTYPE HTML><html lang=en>
<title>Regular expression visualizer: ], $eregexp, q[</title>
<link rel="stylesheet" href="/www/style/html/xhtml">
<h1>Regular expression visualizer</h1>
<p>Input: <code>], $eregexp, q[</code></p>
<p>Error:
<ul>];
print join '', @error;
print q[</ul>];
print $footer;
exit;
}
require Regexp::Visualize::Simple;
my $v = Regexp::Visualize::Simple->new;
$v->push_regexp_node ($parser->root);
binmode STDOUT, ':encoding(utf-8)';
print "Content-Type: application/xhtml+xml; charset=utf-8\n\n";
print q[<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head><title>Regular expression visualizer: ], $eregexp, q[</title>
<link rel="stylesheet" href="/www/style/html/xhtml"/>
</head>
<body>
<h1>Regular expression visualizer</h1>
<p>Input: <code>], $eregexp, q[</code></p>];
if (@error) {
print q[<ul>];
print join '', @error;
print q[</ul>];
}
while ($v->has_regexp_node) {
my ($g, $index) = $v->next_graph;
print "<section><h2>Regexp #$index</h2>\n\n";
print $g->as_svg;
print "</section>\n";
}
print $footer;
print q[</body></html>];