/[suikacvs]/webroot/regexp/visualizer/regexp.cgi
Suika

Contents of /webroot/regexp/visualizer/regexp.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download)
Tue Dec 9 02:41:39 2008 UTC (15 years, 11 months ago) by wakaba
Branch: MAIN
Changes since 1.4: +35 -1 lines
++ swe/visualizer/ChangeLog	9 Dec 2008 02:41:30 -0000
2008-12-09  Wakaba  <wakaba@suika.fam.cx>

	* regexp.cgi: Output errors and warnings as HTML, not plain text
	messages.

++ swe/lib/Regexp/Parser/ChangeLog	9 Dec 2008 02:40:27 -0000
2008-12-09  Wakaba  <wakaba@suika.fam.cx>

	* Perl58.pm: Added support for custom error handling hook.
	(nextchar): Don't skip (?#) if it is not supported in the regexp
	dialect.

++ swe/lib/Regexp/Visualize/ChangeLog	9 Dec 2008 02:41:01 -0000
2008-12-09  Wakaba  <wakaba@suika.fam.cx>

	* Simple.pm (_add_to_graph): Added support for \p{} outside
	character classes.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24