blib/lib/Decl/DefaultParsers.pm

filter: Statementstmt 97.7% 256 / 262 Branchbran 91.2% 73 / 80 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 21 / 21 Podpod 100% 6 / 6 totaltotal 95.9% 359 / 374 CC 47 SCAR 24.5
CC
47
Cov
96%
__ANON__
23.1
__ANON__
12.0
__ANON__
7.0
CRAP 11.6
« blib/lib/Decl/Node.pm ↑ Summary blib/lib/Decl/Semantics/Code.pm »
1 package Decl::DefaultParsers;
2
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Decl::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Decl::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Decl::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10
11 =head1 NAME
12
13 Decl::DefaultParsers - implements the default parsers for the Declarative language.
14
15 =head1 VERSION
16
17 Version 0.01
18
19 =cut
20
21 13 our $VERSION = '0.01';
22
23
24 =head1 SYNOPSIS
25
26 This isn't really an object module; it's just a convenient place to stash the default parsers we use, in order to make it easier to work with the Decl code.
27
28 =head2 init_default_line_parser(), init_default_body_parser(), init_locator_parser(), including locally defined is_blank, is_blank_or_comment, and line_indentation
29
30 These are called by C<init_parsers> to initialize our various sublanguage parsers. You don't need to call them.
31
32 =cut
33
34 13 ▶sub init_default_line_parser {
Subroutine init_default_line_parser: called CC=1 SCAR=0.0
Pod documented
35 13 my ($self) = @_;
36
37 # Default line parser.
38 13 my $p = Decl::Parser->new();
39
40 13 $p->add_tokenizer ('CODEBLOCK'); # TODO: parameterizable, perhaps.
41 $p->add_tokenizer ('STRING', "'(?:\\.|[^'])*'|\"(?:\\.|[^\"])*\"",
42 107 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 107 my $s = shift;
44 107 $s =~ s/.//;
45 107 $s =~ s/.$//;
46 107 $s =~ s/\\(['"])/$1/g;
47 107 $s =~ s/\\\\/\\/g;
48 107 $s =~ s/\\n/\n/g;
49 107 $s =~ s/\\t/\t/g;
50 107 ['STRING', $s]
51 13 }); # TODO: this should be globally available.
52 13 $p->add_tokenizer ('BRACKET', '{.*');
53 13 $p->add_tokenizer ('COMMENT', '#.*');
54 13 $p->add_tokenizer ('WHITESPACE*', '\s+');
55 13 $p->add_tokenizer ('EQUALS', '=');
56 13 $p->add_tokenizer ('COMMA', ',');
57 13 $p->add_tokenizer ('LPAREN', '\(');
58 13 $p->add_tokenizer ('RPAREN', '\)');
59 13 $p->add_tokenizer ('LBRACK', '\[');
60 13 $p->add_tokenizer ('RBRACK', '\]');
61 13 $p->add_tokenizer ('LT', '<');
62
63 13 $p->add_rule ('line', 'p_and(optional(<name>), optional(<parmlist>), optional(<optionlist>), optional (<label>), optional(<parser>), optional(<code>), optional(<bracket>), optional(<comment>), \&end_of_input)');
64 13 $p->add_rule ('name', 'one_or_more(\&word)');
65 13 $p->add_rule ('parmlist', 'p_and(token_silent(["LPAREN"]), list_of(<parm>, "COMMA*"), token_silent(["RPAREN"]))');
66 13 $p->add_rule ('parm', 'p_or(<parmval>, one_or_more(\&word))');
67 13 $p->add_rule ('parmval', 'p_and(\&word, token_silent(["EQUALS"]), <value>)');
68 13 $p->add_rule ('value', 'p_or(\&word, token(["STRING"]))');
69 13 $p->add_rule ('optionlist', 'p_and(token_silent(["LBRACK"]), list_of(<parm>, "COMMA*"), token_silent(["RBRACK"]))');
70 13 $p->add_rule ('label', 'token(["STRING"])');
71 13 $p->add_rule ('parser', 'p_and(\&word, token_silent(["LT"]))');
72 13 $p->add_rule ('code', 'token(["CODEBLOCK"])');
73 13 $p->add_rule ('bracket', 'token(["BRACKET"])');
74 13 $p->add_rule ('comment', 'token(["COMMENT"])');
75
76 982 ▶ $p->action ('input', sub {
Subroutine __ANON__: called CC=4 SCAR=16.4
77 982 my ($parser, $node, $input) = @_;
78 982 ▶ if (not ref $node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $node) 0 982 982
79 0 ▶ $node = 'tag' unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
80 0 $node = Decl::Node->new($node);
81 }
82 982 $parser->{user}->{node} = $node;
83 982 ▶ $input = $node->line() unless $input;
Branch: 1 branch
BranchTrueFalseTotal
unless $input 981 1 982
84 13 });
85 982 ▶ $p->action ('output', sub {
Subroutine __ANON__: called CC=12 SCAR=24.8
86 982 my ($parse_result, $parser) = @_;
87 982 my $node = $parser->{user}->{node};
88 982 ▶ if (defined $parse_result and car($parse_result) eq 'line') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parse_result and &car($parse_result) eq "line") 980 2 982
Condition66%2/3
defined $parse_result and &car($parse_result) eq "line"
!ll&&!rl&&r
20980
MC/DC50%1/2
defined $parse_result and &car($parse_result) eq "line"
defined $parse_result &car($parse_result) eq "line"
Truth table66%2/3
A and B
A= defined $parse_resultB= &car($parse_result) eq "line"
ABresult
0X0
100
111
89 980 foreach my $piece (@{$parse_result->[1]}) {
90 372 ▶ if (car($piece) eq 'name') {
Branch: 4 branches
BranchTrueFalseTotal
if (&car($piece) eq 'name') { } 88 284 372
elsif (&car($piece) eq 'parmlist') { } 166 118 284
elsif (&car($piece) eq 'optionlist') { } 6 112 118
elsif (&car($piece) eq 'parser') { } 1 111 112
91 88 my @names = map { cdr $_ } @{cdr($piece)};
92 88 $node->{name} = $names[0];
93 88 $node->{namelist} = \@names;
94 } elsif (car($piece) eq 'parmlist') {
95 166 my @parmlist = ();
96 166 foreach my $parm (@{cdr($piece)}) {
97 201 my $value = cdr($parm);
98 201 ▶ if (car($value) eq 'parmval') {
Branch: 1 branch
BranchTrueFalseTotal
if (&car($value) eq 'parmval') { } 188 13 201
99 188 my $parameter = cdr(car(cdr($value)));
100 188 my $val = cdr(cdr(cdr(cdr($value))));
101 188 push @parmlist, $parameter;
102 188 $node->{parameters}->{$parameter} = $val;
103 } else {
104 13 my @words = map { cdr $_ } @$value;
105 13 my $parameter = join ' ', @words;
106 13 push @parmlist, $parameter;
107 13 $node->{parameters}->{$parameter} = 'yes';
108 }
109 }
110 166 $node->{parmlist} = \@parmlist;
111 } elsif (car($piece) eq 'optionlist') {
112 6 my @parmlist = ();
113 6 foreach my $parm (@{cdr($piece)}) {
114 13 my $value = cdr($parm);
115 13 ▶ if (car($value) eq 'parmval') {
Branch: 1 branch
BranchTrueFalseTotal
if (&car($value) eq 'parmval') { } 1 12 13
116 1 my $parameter = cdr(car(cdr($value)));
117 1 my $val = cdr(cdr(cdr(cdr($value))));
118 1 push @parmlist, $parameter;
119 1 $node->{options}->{$parameter} = $val;
120 } else {
121 12 my @words = map { cdr $_ } @$value;
122 12 my $parameter = join ' ', @words;
123 12 push @parmlist, $parameter;
124 12 $node->{options}->{$parameter} = 'yes';
125 }
126 }
127 6 $node->{optionlist} = \@parmlist;
128 } elsif (car($piece) eq 'parser') {
129 1 $node->{parser} = cdr car cdr $piece;
130 } else {
131 111 $node->{car($piece)} = cdr(cdr($piece)); # Elegance! We likes it, precioussss.
132 }
133 }
134 }
135 982 return $node;
136 13 });
137
138 13 $p->build();
139 13 return $p;
140 }
141
142 13 ▶sub init_default_body_parser {
Subroutine init_default_body_parser: called CC=1 SCAR=0.0
Pod documented
143 13 my ($self) = @_;
144
145 # Default body parser.
146 13 my $p = Decl::Parser->new();
147
148 13 $p->add_tokenizer ('BLANKLINE', '\n\n+');
149 13 $p->add_tokenizer ('NEWLINE*', '\n');
150 13 $p->add_rule ('body', 'series(p_or(\&word, token("BLANKLINE")))');
151 774 ▶ $p->action ('input', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
152 774 my ($parser, $context, $parent, $input) = @_;
153 774 $input
154 13 });
155 774 ▶ $p->action ('output', sub {
Subroutine __ANON__: called CC=23 SCAR=31.4
156 774 my ($parse_result, $parser, $context, $parent, $input) = @_;
157 774 my @results = ();
158 774 my @nodes_made = ();
159 774 my $root = $parent->root();
160 774 ▶ return () unless popcar($parse_result) eq 'body';
Branch: 1 branch
BranchTrueFalseTotal
unless &popcar($parse_result) eq "body" 0 774 774
161 774 my $indent = 0;
162 774 my $lineindent = 0;
163 774 my $thisindent = 0;
164 774 my $curtext = '';
165 774 my $tag = '';
166 774 my $blanks = '';
167 774 my $firstcode = '';
168 774 my $rest;
169 774 my $spaces = '';
170 774 my $bracket = '';
171
172 969 ▶ my $starttag = sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
173 969 my ($line) = @_;
174 969 ▶ if ($line =~ /^(\s+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\s+)/) { } 0 969 969
175 0 $lineindent = length ($1);
176 0 $line =~ s/^\s*//; # Discard any indentation before the tag line
177 } else {
178 969 $lineindent = 0;
179 }
180 969 ▶ if ($curtext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curtext) 18 951 969
181 18 push @results, $curtext;
182 }
183 969 $curtext = $line . "\n";
184 969 ($tag, $rest) = split /\s+/, $line, 2;
185 969 $indent = 0;
186 774 };
187
188 969 ▶ my $concludetag = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
189 # print STDERR "---- concludetag: $tag\n";
190 969 my $newnode = $context->makenode($parent, $tag, $curtext);
191 969 $newnode->{parent} = $parent;
192 969 push @results, $newnode;
193 969 push @nodes_made, $newnode;
194 969 $tag = '';
195 969 $curtext = '';
196 969 $indent = 0;
197 774 };
198 1423 ▶ sub is_blank { $_[0] =~ /^(\s|\n)*$/ };
Subroutine is_blank: called CC=1 SCAR=0.0
Pod documented
199 995 ▶ sub is_blank_or_comment {
Subroutine is_blank_or_comment: called CC=2 SCAR=6.9
Pod documented
200 995 $_ = shift;
201 995 ▶ /^\s*#/ || is_blank ($_)
Branch: 1 branch
BranchTrueFalseTotal
/^\s*#/ or is_blank($_) 3 992 995
202 };
203 431 ▶ sub line_indentation {
Subroutine line_indentation: called CC=2 SCAR=6.9
Pod documented
204 431 ▶ if ($_[0] =~ /^(\s+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^(\s+)/) { } 195 236 431
205 195 length($1)
206 } else {
207 236 0
208 }
209 }
210
211 # print STDERR "\n\n----- Starting " . $parent->tag . " with:\n$input-----------------------\n";
212 774 foreach (@$parse_result) {
213 1245 my ($type, $line) = splitcar ($_);
214 1245 my $testline = $line;
215 1245 $testline =~ s/\n/\\n/g;
216 # print STDERR "$testline : ";
217 1245 ▶ $line =~ s/\n*// if $type; # If we have a BLANKLINE token, there are one too many \n's in there.
Branch: 1 branch
BranchTrueFalseTotal
if $type 48 1197 1245
218 1245 ▶ if (not $tag) { # We're in a blank-and-comment stretch
Branch: 1 branch
BranchTrueFalseTotal
if (not $tag) { } 814 431 1245
219 814 ▶ if (is_blank_or_comment($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_blank_or_comment($line)) { } 25 789 814
220 # print STDERR "blank-or-comment\n";
221 25 $curtext .= $line . "\n";
222 } else {
223 # print STDERR "start tag\n";
224 789 $starttag->($line);
225 }
226 } else { # We're in a tag
227 431 ▶ if (not $indent) { # We just started it, though.
Branch: 1 branch
BranchTrueFalseTotal
if (not $indent) { } 254 177 431
228 254 $indent = line_indentation($line);
229 254 ▶ if ($indent <= $lineindent) { # And the first line after the starting line is already back-indented!
Branch: 2 branches
BranchTrueFalseTotal
if ($indent <= $lineindent) { } 177 77 254
elsif (is_blank($line)) { } 6 71 77
230 177 ▶ if (is_blank($line)) { # This is a blank line, though, so it may not count as indented.
Branch: 1 branch
BranchTrueFalseTotal
if (is_blank($line)) { } 18 159 177
231 # print STDERR "blank line at start of tag\n";
232 18 $blanks .= $line; # We'll stash it and try again.
233 18 $indent = 0;
234 } else { # It's not a blank; it's either a new tag, or a comment.
235 159 $concludetag->();
236 159 ▶ if (is_blank_or_comment($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_blank_or_comment($line)) { } 0 159 159
237 # print STDERR "blank-or-comment\n";
238 0 $curtext = $blanks . $line . "\n";
239 0 $blanks = '';
240 } else {
241 159 ▶ if ($blanks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blanks) 3 156 159
242 # print STDERR "(had some leftover blanks) ";
243 3 push @results, $blanks;
244 3 $blanks = '';
245 }
246 # print STDERR ("starting new tag\n");
247 159 $starttag->($line);
248 }
249 }
250 } elsif (is_blank ($line)) {
251 # print STDERR "blank line at start of tag with longer indent\n";
252 6 $blanks .= $line; # Stash it and keep going.
253 6 $indent = $lineindent; # 2010-07-24 - and don't let 'indent' get updated
254 } else { # This is the first line of the body, because it's indented further than the opening line.
255 71 $spaces = ' ' x $indent;
256 71 $line =~ s/^$spaces//;
257 71 ▶ if ($blanks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blanks) 1 70 71
258 # print STDERR "(had blanks) ";
259 1 $curtext .= $blanks;
260 1 $blanks = '';
261 }
262 # print STDERR "first line of body\n";
263 71 $curtext .= $line . "\n";
264 }
265 } else {
266 177 ▶ if (line_indentation ($line) < $indent) { # A new back-indentation!
Branch: 2 branches
BranchTrueFalseTotal
if (line_indentation($line) < $indent) { } 67 110 177
elsif (is_blank($line)) { } 15 95 110
267 67 ▶ if (is_blank($line)) { # If this is blank, we don't add it to the body until there's more to add.
Branch: 3 branches
BranchTrueFalseTotal
if (is_blank($line)) { } 18 49 67
elsif ($line =~ /^\s*}/) { } 27 22 49
elsif (is_blank_or_comment($line)) { } 1 21 22
268 # print STDERR ("stash blank line\n");
269 18 $blanks .= $line . "\n";
270 } elsif ($line =~ /^\s*}/) { # Closing bracket; we don't check for matching brackets; the closing bracket is really just a sort of comment.
271 # print STDERR ("closing bracket\n");
272 27 $concludetag->();
273 } elsif (is_blank_or_comment($line)) { # Comment; this by definition belongs to the parent.
274 # print STDERR ("back-indented comment, denoting end of last tag\n");
275 1 $concludetag->();
276 1 $curtext = $blanks . $line . "\n";
277 1 $blanks = '';
278 } else { # Next tag line.
279 21 $concludetag->();
280 21 ▶ if ($blanks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blanks) 14 7 21
281 # print STDERR "(had some blanks) ";
282 14 push @results, $blanks;
283 14 $blanks = '';
284 }
285 # print STDERR "starting tag!\n";
286 21 $starttag->($line);
287 }
288 } elsif (is_blank ($line)) { # This blank line may fall between nodes, or be part of the current one.
289 # print STDERR "stash blank line within body\n";
290 15 $blanks .= $line . "\n";
291 } else { # Normal body line; toss it into the mix.
292 95 $line =~ s/^$spaces//;
293 95 ▶ if ($blanks) { # If we've stashed some blanks, add them back.
Branch: 1 branch
BranchTrueFalseTotal
if ($blanks) 8 87 95
294 # print STDERR "(had some blanks) ";
295 8 $curtext .= $blanks;
296 8 $blanks = '';
297 }
298 # print STDERR "body line >> $line\n";
299 95 $curtext .= $line . "\n";
300 }
301 }
302 }
303 }
304 774 ▶ if ($curtext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curtext) 767 7 774
305 767 ▶ if ($tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag) { } 761 6 767
306 # print STDERR "FINAL: had a tag\n";
307 761 $concludetag->();
308 } else {
309 # print STDERR "FINAL: extra text\n";
310 6 push @results, $curtext;
311 }
312 }
313 774 ▶ if ($blanks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blanks) 12 762 774
314 # print STDERR "FINAL: extra blanks\n";
315 12 push @results, $blanks;
316 }
317 774 $parent->{elements} = [$parent->elements, @results];
318 @nodes_made
319 13 });
320
321 13 $p->build(); # Forgetting this cost me several hours of debugging...
322 13 return $p;
323 }
324
325 13 ▶sub init_locator_parser {
Subroutine init_locator_parser: called CC=1 SCAR=0.0
Pod documented
326 13 my ($self) = @_;
327
328 13 my $p = Decl::Parser->new();
329
330 $p->add_tokenizer ('STRING', "'(?:\\.|[^'])*'|\"(?:\\.|[^\"])*\"",
331 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
332 4 my $s = shift;
333 4 $s =~ s/.//;
334 4 $s =~ s/.$//;
335 4 $s =~ s/\\(['"])/$1/g;
336 4 $s =~ s/\\\\/\\/g;
337 4 $s =~ s/\\n/\\n/g;
338 4 $s =~ s/\\t/\\t/g;
339 4 ['STRING', $s]
340 13 });
341 13 $p->add_tokenizer ('WHITESPACE*', '\s+');
342 13 $p->add_tokenizer ('MATCHES', '=~');
343 13 $p->add_tokenizer ('EQUALS', '=');
344 13 $p->add_tokenizer ('SEPARATOR', '[.:/]');
345 13 $p->add_tokenizer ('LPAREN', '\(');
346 13 $p->add_tokenizer ('RPAREN', '\)');
347 13 $p->add_tokenizer ('LBRACK', '\[');
348 13 $p->add_tokenizer ('RBRACK', '\]');
349
350 13 $p->add_rule ('locator', 'list_of(<tag>, "SEPARATOR*")');
351 13 $p->add_rule ('tag', 'p_and(\&word, p_or (<attribute>, <match>, <offset>, <name>, \&nothing))');
352 13 $p->add_rule ('name', 'p_and(token_silent(["LBRACK"]), one_or_more(\&word), token_silent(["RBRACK"]))');
353 13 $p->add_rule ('attribute', 'p_and(token_silent(["LBRACK"]), \&word, token_silent(["EQUALS"]), p_or(\&word, token (["STRING"])), token_silent(["RBRACK"]))');
354 13 $p->add_rule ('match', 'p_and(token_silent(["LBRACK"]), \&word, token_silent(["MATCHES"]), p_or(\&word, token (["STRING"])), token_silent(["RBRACK"]))');
355 13 $p->add_rule ('offset', 'p_and(token_silent(["LPAREN"]), \&word, token_silent(["RPAREN"]))');
356
357 320 ▶ $p->action ('output', sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
358 320 my ($parse_result, $parser) = @_;
359 320 my $list = cdr $parse_result;
360 320 my @pieces = ();
361 320 foreach (@$list) {
362 339 my $t = cdr $_;
363 339 my $tag = cdr car $t;
364 339 my $rest = cdr $t;
365 339 ▶ if (defined $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rest) { } 17 322 339
366 17 my ($type, $spec) = @$rest;
367 17 ▶ if ($type eq 'name') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'name') { } 14 3 17
elsif ($type eq 'attribute') { } 1 2 3
elsif ($type eq 'match') { } 1 1 2
elsif ($type eq 'offset') { } 1 0 1
368 14 my @names = map { cdr $_ } @$spec;
369 14 push @pieces, [$tag, @names];
370 } elsif ($type eq 'attribute') {
371 1 push @pieces, [$tag, ['a', cdr car $spec, cdr cdr $spec]];
372 } elsif ($type eq 'match') {
373 1 push @pieces, [$tag, ['m', cdr car $spec, cdr cdr $spec]];
374 } elsif ($type eq 'offset') {
375 1 push @pieces, [$tag, ['o', cdr car $spec]];
376 }
377 } else {
378 322 push @pieces, $tag;
379 }
380 }
381 320 return \@pieces;
382 13 });
383
384 13 $p->build();
385 13 return $p;
386 }
387 =head1 AUTHOR
388
389 Michael Roberts, C<< <michael at vivtek.com> >>
390
391 =head1 BUGS
392
393 Please report any bugs or feature requests to C<bug-decl at rt.cpan.org>, or through
394 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Decl>. I will be notified, and then you'll
395 automatically be notified of progress on your bug as I make changes.
396
397 =head1 LICENSE AND COPYRIGHT
398
399 Copyright 2011 Michael Roberts.
400
401 This program is free software; you can redistribute it and/or modify it
402 under the terms of either: the GNU General Public License as published
403 by the Free Software Foundation; or the Artistic License.
404
405 See http://dev.perl.org/licenses/ for more information.
406
407 =cut
408
409 13 1; # End of Decl::DefaultParsers
« blib/lib/Decl/Node.pm ↑ Summary blib/lib/Decl/Semantics/Code.pm »