Parser.pm

filter: Statementstmt 7.8% 7 / 89 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 14.2% 2 / 14 Podpod 0.0% 0 / 12 totaltotal 4.2% 9 / 214 CC 37 SCAR 36.8
CC
37
Cov
4%
parse_phrases
90.0
find_match
72.0
parse_blocks
30.0
CRAP 39.6
↑ Summary AST.pm »
1 ## Base Class for Creating Text Format Parsers
2 #
3 # Document::Parser is a base class that you can use to easily generate a
4 # parser for text document markups (like Wiki or POD markups).
5 #
6 # See this parser as an example:
7 #
8 # http://svn.kwiki.org/kwiki/trunk/src/core/Spork/lib/Spork/Parser.pm
9 #
10 # And this module for usage of the parser:
11 #
12 # http://svn.kwiki.org/kwiki/trunk/src/core/Spork/lib/Spork/Formatter2.pm
13 #
14 # Copyright (c) 2007. Ingy döt Net. All rights reserved.
15 #
16 # Licensed under the same terms as Perl itself.
17 ##
18 package Document::Parser;
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 ## Synopsis:
23 #
24 # package MyParser;
25 # use base 'Document::Parser';
26 #
27 # sub create_grammar {
28 # return {
29 # # ... define a grammar hash here ...
30 # };
31 # }
32 ##
33  
34 ##------------------------------------------------------------------------------
35 # Parser object constructor/initializer
36 ##------------------------------------------------------------------------------
37 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
38 0 my $class = shift;
39 0 ▶ return bless { @_ }, ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
40 }
41  
42 ##------------------------------------------------------------------------------
43 # $parsed = $parser->parse($wikitext);
44 ##------------------------------------------------------------------------------
45 0 ▶sub parse {
Subroutine parse: not called CC=4 SCAR=30.0
Pod undocumented
46 0 my $self = shift;
47 0 ▶ $self->{input} ||= shift;
Condition0%0/3
$self->{"input"} ||= shift()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"input"} ||= shift()
$self->{"input"} shift()
Truth table0%0/3
A ||= B
A= $self->{"input"}B= shift()
ABresult
1X1
011
000
48 0 ▶ $self->{grammar} ||= $self->set_grammar;
Condition0%0/3
$self->{"grammar"} ||= $self->set_grammar
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"grammar"} ||= $self->set_grammar
$self->{"grammar"} $self->set_grammar
Truth table0%0/3
A ||= B
A= $self->{"grammar"}B= $self->set_grammar
ABresult
1X1
011
000
49 0 ▶ $self->{receiver} ||= $self->set_receiver;
Condition0%0/3
$self->{"receiver"} ||= $self->set_receiver
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"receiver"} ||= $self->set_receiver
$self->{"receiver"} $self->set_receiver
Truth table0%0/3
A ||= B
A= $self->{"receiver"}B= $self->set_receiver
ABresult
1X1
011
000
50 0 $self->{receiver}->init;
51 0 $self->parse_blocks('top');
52 0 return $self->{receiver}->content;
53 }
54  
55 ##
56 # Call `set_receiver` to reset the receiver for a new parse.
57 0 ▶sub set_receiver {
Subroutine set_receiver: not called CC=2 SCAR=17.9
Pod undocumented
58 0 my $self = shift;
59 0 ▶ $self->{receiver} = shift || $self->create_receiver;
Condition0%0/3
shift() || $self->create_receiver
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->create_receiver
shift() $self->create_receiver
Truth table0%0/3
A || B
A= shift()B= $self->create_receiver
ABresult
1X1
011
000
60 }
61  
62 0 ▶sub set_grammar {
Subroutine set_grammar: not called CC=2 SCAR=17.9
Pod undocumented
63 0 my $self = shift;
64 0 ▶ $self->{grammar} = shift || $self->create_grammar;
Condition0%0/3
shift() || $self->create_grammar
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->create_grammar
shift() $self->create_grammar
Truth table0%0/3
A || B
A= shift()B= $self->create_grammar
ABresult
1X1
011
000
65 }
66  
67 #-------------------------------------------------------------------------------
68 # Parse input into a series of blocks. With each iteration the parser must
69 # match a block at position 0 of the text, and remove that block from the
70 # input reparse it further. This continues until there is no input left.
71 #-------------------------------------------------------------------------------
72 0 ▶sub parse_blocks {
Subroutine parse_blocks: not called CC=5 SCAR=34.0
Pod undocumented
73 0 my $self = shift;
74 0 my $container_type = shift;
75 0 my $types = $self->{grammar}{$container_type}{blocks};
76 0 ▶ while (my $length = length $self->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $length = length $self->{"input"}) 0 0 0
77 0 for my $type (@$types) {
78 0 ▶ my $matched = $self->find_match(matched_block => $type) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $matched = $self->find_match("matched_block", $type) or next 0 0 0
79 0 substr($self->{input}, 0, $matched->{end}, '');
80 0 $self->handle_match($type, $matched);
81 0 last;
82 }
83 die $self->reduction_error
84 0 ▶ unless length($self->{input}) < $length;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"input"} < $length 0 0 0
85 }
86 0 return;
87 }
88  
89 #-------------------------------------------------------------------------------
90 # This code parses a chunk into interleaved pieces of plain text and
91 # phrases. It repeatedly tries to match every possible phrase and
92 # then takes the match closest to the start. Everything before a
93 # match is written as text. Matched phrases are subparsed according
94 # to their rules. This continues until the input is all eaten.
95 #-------------------------------------------------------------------------------
96 0 ▶sub parse_phrases {
Subroutine parse_phrases: not called CC=9 SCAR=45.0
Pod undocumented
97 0 my $self = shift;
98 0 my $container_type = shift;
99 0 my $types = $self->{grammar}{$container_type}{phrases};
100 0 ▶ while (length $self->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"input"}) 0 0 0
101 0 my $match;
102 0 for my $type (@$types) {
103 0 ▶ my $matched = $self->find_match(matched_phrase => $type) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $matched = $self->find_match("matched_phrase", $type) or next 0 0 0
104 0 ▶ if (not defined $match or $matched->{begin} < $match->{begin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $match or $matched->{"begin"} < $match->{"begin"}) 0 0 0
Condition0%0/3
not defined $match or $matched->{"begin"} < $match->{"begin"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $match or $matched->{"begin"} < $match->{"begin"}
not defined $match $matched->{"begin"} < $match->{"begin"}
Truth table0%0/3
not(A) or B
A= not defined $matchB= $matched->{"begin"} < $match->{"begin"}
ABresult
1X1
011
000
105 0 $match = $matched;
106 0 $match->{type} = $type;
107 0 ▶ last if $match->{begin} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $match->{"begin"} == 0 0 0 0
108 }
109 }
110 0 ▶ if (! $match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($match) 0 0 0
111 0 $self->{receiver}->text_node($self->{input});
112 0 last;
113 }
114 0 my ($begin, $end, $type) = @{$match}{qw(begin end type)};
115 0 ▶ $self->{receiver}->text_node(substr($self->{input}, 0, $begin))
Branch: 1 branch
BranchTrueFalseTotal
unless $begin == 0 0 0 0
116 unless $begin == 0;
117 0 substr($self->{input}, 0, $end, '');
118 0 $type = $match->{type};
119 0 $self->handle_match($type, $match);
120 }
121 0 return;
122 }
123  
124 0 ▶sub find_match {
Subroutine find_match: not called CC=8 SCAR=42.8
Pod undocumented
125 0 my ($self, $matched_func, $type) = @_;
126 0 my $matched;
127 0 ▶ if (my $regexp = $self->{grammar}{$type}{match}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regexp = $self->{'grammar'}{$type}{'match'}) { } 0 0 0
128 0 ▶ if (ref($regexp) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $regexp eq 'ARRAY') { } 0 0 0
129 0 for my $re (@$regexp) {
130 0 ▶ if ($self->{input} =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"input"} =~ /$re/) 0 0 0
131 0 $matched = $self->$matched_func;
132 0 last;
133 }
134 }
135 0 ▶ return unless $matched;
Branch: 1 branch
BranchTrueFalseTotal
unless $matched 0 0 0
136 }
137 else {
138 0 ▶ return unless $self->{input} =~ $regexp;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"input"} =~ /$regexp/ 0 0 0
139 0 $matched = $self->$matched_func;
140 }
141 }
142 else {
143 0 my $func = "match_$type";
144 0 ▶ $matched = $self->$func or return;
Branch: 1 branch
BranchTrueFalseTotal
$matched = $self->$func or return 0 0 0
145 }
146 0 return $matched;
147 }
148  
149 0 ▶sub handle_match {
Subroutine handle_match: not called CC=4 SCAR=30.0
Pod undocumented
150 0 my ($self, $type, $match) = @_;
151 0 my $func = "handle_$type";
152 0 ▶ if ($self->can($func)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($func)) { } 0 0 0
153 0 $self->$func($match, $type);
154 }
155 else {
156 0 my $grammar = $self->{grammar}{$type};
157 my $parse = $grammar->{blocks}
158 0 ▶ ? 'parse_blocks'
Branch: 1 branch
BranchTrueFalseTotal
$grammar->{'blocks'} ? : 0 0 0
159 : 'parse_phrases';
160 my @filter = $grammar->{filter}
161 ? ($grammar->{filter})
162 0 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
$grammar->{'filter'} ? : 0 0 0
163 0 $self->subparse($parse, $match, $type, @filter);
164 }
165 }
166  
167 0 ▶sub subparse {
Subroutine subparse: not called CC=2 SCAR=17.9
Pod undocumented
168 0 my ($self, $func, $match, $type, $filter) = @_;
169 0 $self->{receiver}->begin_node($type);
170 my $parser = $self->new(
171 grammar => $self->{grammar},
172 receiver => $self->{receiver}->new,
173 input => $filter
174 0 ? do { $_ = $match->{text}; &$filter(); $_ }
175 : $match->{text},
176 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$filter ? : 0 0 0
177 0 $parser->$func($type);
178 0 $self->{receiver}->insert($parser->{receiver});
179 0 $self->{receiver}->end_node($type);
180 }
181  
182 #-------------------------------------------------------------------------------
183 # Helper functions
184 #
185 # These are the odds and ends called by the code above.
186 #-------------------------------------------------------------------------------
187  
188 0 ▶sub reduction_error {
Subroutine reduction_error: not called CC=1 SCAR=6.9
Pod undocumented
189 0 my $self = shift;
190 0 return ref($self) . qq[ reduction error for:\n"$self->{input}"];
191 }
192  
193 0 ▶sub matched_block {
Subroutine matched_block: not called CC=5 SCAR=34.0
Pod undocumented
194 0 ▶ my $begin = defined $_[2] ? $_[2] : $-[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
195 0 ▶ die "All blocks must match at position 0"
Branch: 1 branch
BranchTrueFalseTotal
if "$begin" ne 0 0 0 0
196 if "$begin" ne "0";
197  
198 return +{
199 0 ▶ text => ($_[1] || $1),
Condition0%0/6
$_[1] || $1
l!l&&r!l&&!r
000
$_[3] || $+[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] || $1
$_[1] $1
MC/DC0%0/2
$_[3] || $+[0]
$_[3] $+[0]
Truth table0%0/3
A || B
A= $_[1]B= $1
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $_[3]B= $+[0]
ABresult
1X1
011
000
200 end => ($_[3] || $+[0]),
201 };
202 }
203  
204 0 ▶sub matched_phrase {
Subroutine matched_phrase: not called CC=4 SCAR=30.0
Pod undocumented
205 return +{
206 0 ▶ text => ($_[1] || $1),
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
Condition0%0/6
$_[1] || $1
l!l&&r!l&&!r
000
$_[3] || $+[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] || $1
$_[1] $1
MC/DC0%0/2
$_[3] || $+[0]
$_[3] $+[0]
Truth table0%0/3
A || B
A= $_[1]B= $1
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $_[3]B= $+[0]
ABresult
1X1
011
000
207 begin => (defined $_[2] ? $_[2] : $-[0]),
208 end => ($_[3] || $+[0]),
209 };
210 }
211  
212 1 1;
213  
214 =for perldoc
215 This POD generated by Perldoc-0.21.
216 DO NOT EDIT. Your changes will be lost.
217  
218 =encoding utf8
219  
220 =head1 NAME
221  
222 Document::Parser - Base Class for Creating Text Format Parsers
223  
224 =head1 SYNOPSIS
225  
226 package MyParser;
227 use base 'Document::Parser';
228  
229 sub create_grammar {
230 return {
231 # ... define a grammar hash here ...
232 };
233 }
234  
235 =head1 DESCRIPTION
236  
237 Document::Parser is a base class that you can use to easily generate a
238 parser for text document markups (like Wiki or POD markups).
239  
240 See this parser as an example:
241  
242 http://svn.kwiki.org/kwiki/trunk/src/core/Spork/lib/Spork/Parser.pm
243  
244 And this module for usage of the parser:
245  
246 http://svn.kwiki.org/kwiki/trunk/src/core/Spork/lib/Spork/Formatter2.pm
247  
248 =head1 AUTHOR
249  
250 Ingy döt Net
251  
252 =head1 COPYRIGHT
253  
254 Copyright (c) 2007. Ingy döt Net. All rights reserved.
255  
256 This program is free software; you can redistribute it and/or modify it
257 under the same terms as Perl itself.
258  
259 See http://www.perl.com/perl/misc/Artistic.html
260  
261 =cut
↑ Summary AST.pm »