blib/lib/Grammar/Formal.pm

filter: Statementstmt 86.3% 203 / 235 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 54 / 66 Podpod 0.0% 0 / 14 totaltotal 80.0% 257 / 321 CC 4 SCAR 5.7
CC
4
Cov
84%
owner_grammar
12.0
expand
6.0
BUILD
2.0
CRAP 1.8
↑ Summary
1 #####################################################################
2 # Base package for operators
3 #####################################################################
4 package Grammar::Formal::Pattern;
5 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use MooseX::SetOnce;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 3 has 'parent' => (
10 is => 'ro',
11 required => 0,
12 isa => 'Maybe[Grammar::Formal::Pattern]',
13 writer => '_set_parent',
14 traits => [qw/SetOnce/],
15 weak_ref => 1,
16 );
17
18 3 has 'user_data' => (
19 is => 'rw',
20 required => 0,
21 );
22
23 3 has 'position' => (
24 is => 'ro',
25 isa => 'Maybe[Int]',
26 required => 0,
27 );
28
29 0 ▶sub owner_grammar {
Subroutine owner_grammar: not called CC=3 SCAR=24.8
Pod undocumented
30 0 my ($self) = @_;
31
32 0 ▶ for (my $p = $self->parent; $p; $p = $p->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) 0 0 0
33 0 ▶ next unless $p->isa('Grammar::Formal::Grammar');
Branch: 1 branch
BranchTrueFalseTotal
unless $p->isa("Grammar::Formal::Grammar") 0 0 0
34 0 return $p;
35 }
36
37 0 die "Called owner_grammar on orphan pattern";
38 }
39
40 #####################################################################
41 # Base package for unary operators
42 #####################################################################
43 package Grammar::Formal::Unary;
44 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
46
47 3 extends 'Grammar::Formal::Pattern';
48
49 3 has 'p' => (
50 is => 'ro',
51 required => 1,
52 isa => 'Grammar::Formal::Pattern'
53 );
54
55 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
56 0 my $self = shift;
57 0 $self->p->_set_parent($self);
58 }
59
60 #####################################################################
61 # Base package for binary operators
62 #####################################################################
63 package Grammar::Formal::Binary;
64 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
66
67 3 extends 'Grammar::Formal::Pattern';
68
69 3 has 'p1' => (
70 is => 'ro',
71 required => 1,
72 isa => 'Grammar::Formal::Pattern'
73 );
74
75 3 has 'p2' => (
76 is => 'ro',
77 required => 1,
78 isa => 'Grammar::Formal::Pattern'
79 );
80
81 2 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
82 2 my $self = shift;
83 2 $self->p1->_set_parent($self);
84 1 $self->p2->_set_parent($self);
85 }
86
87 #####################################################################
88 # Group
89 #####################################################################
90 package Grammar::Formal::Group;
91 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 3 extends 'Grammar::Formal::Binary';
94
95 #####################################################################
96 # Choice
97 #####################################################################
98 package Grammar::Formal::Choice;
99 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 3 extends 'Grammar::Formal::Binary';
102
103 #####################################################################
104 # OrderedChoice
105 #####################################################################
106 package Grammar::Formal::OrderedChoice;
107 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 3 extends 'Grammar::Formal::Binary';
110
111 #####################################################################
112 # Conjunction
113 #####################################################################
114 package Grammar::Formal::Conjunction;
115 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 3 extends 'Grammar::Formal::Binary';
118
119 #####################################################################
120 # OrderedConjunction
121 #####################################################################
122 package Grammar::Formal::OrderedConjunction;
123 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 3 extends 'Grammar::Formal::Binary';
126
127 #####################################################################
128 # Subtraction
129 #####################################################################
130 package Grammar::Formal::Subtraction;
131 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
133 3 extends 'Grammar::Formal::Binary';
134
135 #####################################################################
136 # Empty
137 #####################################################################
138 package Grammar::Formal::Empty;
139 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 3 extends 'Grammar::Formal::Pattern';
142
143 #####################################################################
144 # NotAllowed
145 #####################################################################
146 package Grammar::Formal::NotAllowed;
147 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 3 extends 'Grammar::Formal::Pattern';
150
151 #####################################################################
152 # ZeroOrMore
153 #####################################################################
154 package Grammar::Formal::ZeroOrMore;
155 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 3 extends 'Grammar::Formal::Unary';
158
159 #####################################################################
160 # OneOrMore
161 #####################################################################
162 package Grammar::Formal::OneOrMore;
163 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 3 extends 'Grammar::Formal::Unary';
166
167 #####################################################################
168 # SomeOrMore
169 #####################################################################
170 package Grammar::Formal::SomeOrMore;
171 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 3 extends 'Grammar::Formal::Unary';
174
175 3 has 'min' => (
176 is => 'ro',
177 required => 1,
178 isa => 'Int'
179 );
180
181 #####################################################################
182 # BoundedRepetition
183 #####################################################################
184 package Grammar::Formal::BoundedRepetition;
185 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
187 3 extends 'Grammar::Formal::Unary';
188
189 3 has 'min' => (
190 is => 'ro',
191 required => 1,
192 isa => 'Int'
193 );
194
195 3 has 'max' => (
196 is => 'ro',
197 required => 1,
198 isa => 'Int'
199 );
200
201 #####################################################################
202 # Reference
203 #####################################################################
204 package Grammar::Formal::Reference;
205 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
206 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
207 3 extends 'Grammar::Formal::Pattern';
208
209 3 has 'name' => (
210 is => 'ro',
211 required => 1,
212 isa => 'Str'
213 );
214
215 0 ▶sub expand {
Subroutine expand: not called CC=2 SCAR=17.9
Pod undocumented
216 0 my ($self) = @_;
217
218 0 my $p = $self->owner_grammar;
219
220 return $p->rules->{$self->name}
221 0 ▶ if $p->rules->{$self->name};
Branch: 1 branch
BranchTrueFalseTotal
if $p->rules->{$self->name} 0 0 0
222
223 0 warn "rule expansion for " . $self->name . " failed.";
224
225 0 return;
226 }
227
228 #####################################################################
229 # Rule
230 #####################################################################
231 package Grammar::Formal::Rule;
232 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
233 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
234 3 extends 'Grammar::Formal::Unary';
235
236 3 has 'name' => (
237 is => 'ro',
238 required => 1,
239 isa => 'Str'
240 );
241
242 #####################################################################
243 # Grammar
244 #####################################################################
245 package Grammar::Formal::Grammar;
246 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
247 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
248 3 extends 'Grammar::Formal::Pattern';
249
250 3 has 'start' => (
251 is => 'ro',
252 required => 0,
253 isa => 'Maybe[Str]',
254 );
255
256 has 'rules' => (
257 is => 'ro',
258 required => 1,
259 isa => 'HashRef[Grammar::Formal::Rule]',
260 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
261 3 );
262
263 # TODO: lock the rules hashref against external access?
264
265 1 ▶sub set_rule {
Subroutine set_rule: called CC=1 SCAR=0.0
Pod undocumented
266 1 my ($self, $name, $value) = @_;
267 1 $self->rules->{$name} = $value;
268 1 $value->_set_parent($self);
269 }
270
271 # TODO: validate that rules include start symbol?
272
273 #####################################################################
274 # Factory methods
275 #####################################################################
276
277 # FIXME(bh): better alternative for this?
278
279 0 ▶sub NotAllowed {
Subroutine NotAllowed: not called CC=1 SCAR=6.9
Pod undocumented
280 0 my ($self, @o) = @_;
281 0 Grammar::Formal::NotAllowed->new(@o);
282 }
283
284 0 ▶sub Empty {
Subroutine Empty: not called CC=1 SCAR=6.9
Pod undocumented
285 0 my ($self, @o) = @_;
286 0 Grammar::Formal::Empty->new(@o);
287 }
288
289 0 ▶sub Choice {
Subroutine Choice: not called CC=1 SCAR=6.9
Pod undocumented
290 0 my ($self, $p1, $p2, @o) = @_;
291 0 Grammar::Formal::Choice->new(p1 => $p1, p2 => $p2, @o);
292 }
293
294 0 ▶sub Group {
Subroutine Group: not called CC=1 SCAR=6.9
Pod undocumented
295 0 my ($self, $p1, $p2, @o) = @_;
296 0 Grammar::Formal::Group->new(p1 => $p1, p2 => $p2, @o);
297 }
298
299 0 ▶sub Optional {
Subroutine Optional: not called CC=1 SCAR=6.9
Pod undocumented
300 0 my ($self, $p, @o) = @_;
301 0 $self->Choice($self->Empty, $p, @o);
302 }
303
304 0 ▶sub OneOrMore {
Subroutine OneOrMore: not called CC=1 SCAR=6.9
Pod undocumented
305 0 my ($self, $p, @o) = @_;
306 0 Grammar::Formal::OneOrMore->new(p => $p, @o);
307 }
308
309 0 ▶sub ZeroOrMore {
Subroutine ZeroOrMore: not called CC=1 SCAR=6.9
Pod undocumented
310 0 my ($self, $p, @o) = @_;
311 0 Grammar::Formal::ZeroOrMore->new(p => $p, @o);
312 }
313
314 #####################################################################
315 # CaseSensitiveString
316 #####################################################################
317 package Grammar::Formal::CaseSensitiveString;
318 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
319 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
320 3 extends 'Grammar::Formal::Pattern';
321
322 3 has 'value' => (
323 is => 'ro',
324 required => 1,
325 isa => 'Str'
326 );
327
328 #####################################################################
329 # ASCII-Insensitive string
330 #####################################################################
331 package Grammar::Formal::AsciiInsensitiveString;
332 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
333 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
334 3 extends 'Grammar::Formal::Pattern';
335
336 3 has 'value' => (
337 is => 'ro',
338 required => 1,
339 isa => 'Str'
340 );
341
342 #####################################################################
343 # prose values
344 #####################################################################
345 package Grammar::Formal::ProseValue;
346 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
347 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
348 3 extends 'Grammar::Formal::Pattern';
349
350 3 has 'value' => (
351 is => 'ro',
352 required => 1,
353 isa => 'Str'
354 );
355
356 #####################################################################
357 # Range
358 #####################################################################
359 package Grammar::Formal::Range;
360 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
361 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
362 3 extends 'Grammar::Formal::Pattern';
363
364 3 has 'min' => (
365 is => 'ro',
366 required => 1,
367 isa => 'Int'
368 );
369
370 3 has 'max' => (
371 is => 'ro',
372 required => 1,
373 isa => 'Int'
374 );
375
376 # TODO: add check min <= max
377
378 #####################################################################
379 # Character class
380 #####################################################################
381 package Grammar::Formal::CharClass;
382 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
383 3 ▶use Set::IntSpan;
Subroutine BEGIN: called CC=1 SCAR=0.0
384 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
385 3 extends 'Grammar::Formal::Pattern';
386
387 3 has 'spans' => (
388 is => 'ro',
389 required => 1,
390 isa => 'Set::IntSpan'
391 );
392
393 0 ▶sub from_numbers {
Subroutine from_numbers: not called CC=1 SCAR=6.9
Pod undocumented
394 0 my ($class, @numbers) = @_;
395 0 my $spans = Set::IntSpan->new([@numbers]);
396 0 return $class->new(spans => $spans);
397 }
398
399 0 ▶sub from_numbers_pos {
Subroutine from_numbers_pos: not called CC=1 SCAR=6.9
Pod undocumented
400 0 my ($class, $pos, @numbers) = @_;
401 0 my $spans = Set::IntSpan->new([@numbers]);
402 0 return $class->new(spans => $spans, position => $pos);
403 }
404
405 #####################################################################
406 # Grammar::Formal
407 #####################################################################
408 package Grammar::Formal;
409 3 ▶use 5.012000;
Subroutine BEGIN: called CC=1 SCAR=0.0
410 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
411 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
412
413 3 extends 'Grammar::Formal::Grammar';
414
415 3 our $VERSION = '0.20';
416
417 3 1;
418
419
420 __END__
↑ Summary