| 1 | # Copyright 1998-2019, Paul Johnson (paul@pjcj.net) | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # This software is free. It is licensed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # The latest version of this software should be available from my homepage: | ||||||||||||||||||||||||||||
| 6 | # http://www.pjcj.net | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # documentation at __END__ | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 15 | require 5.005; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | package Gedcom::Grammar; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 15 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 15 | ▶ | use Gedcom::Item 1.21; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 15 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 15 | $VERSION = "1.21"; | |||||||||||||||||||||||||||
| 22 | 15 | @ISA = qw( Gedcom::Item ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub structure { | ||||||||||||||||||||||||||||
| 25 | 708 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
structure: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 708 | my ($struct) = @_; | |||||||||||||||||||||||||||
| 27 | 708 | ▶ | unless (exists $self->{top}{structures}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | $self->{top}{structures} = { | ||||||||||||||||||||||||||||
| 29 | 319 | ▶ | map { $_->{structure} ? ($_->{structure} => $_) : () } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 11 | @{$self->{top}{items}} | |||||||||||||||||||||||||||
| 31 | }; | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | # print Dumper $self->{top}{structures}; | ||||||||||||||||||||||||||||
| 34 | 708 | $self->{top}{structures}{$struct} | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub item { | ||||||||||||||||||||||||||||
| 38 | 10468 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
item: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 10468 | my ($tag) = @_; | |||||||||||||||||||||||||||
| 40 | 10468 | ▶ | return unless defined $tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 10468 | my $valid_items = $self->valid_items; | |||||||||||||||||||||||||||
| 42 | # use Data::Dumper; print "[$tag] -- ", Dumper($self), Dumper $valid_items; | ||||||||||||||||||||||||||||
| 43 | 10468 | ▶ | return unless exists $valid_items->{$tag}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 10460 | map { $_->{grammar} } @{$valid_items->{$tag}} | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub min { | ||||||||||||||||||||||||||||
| 48 | 2408 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
min: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 2408 | ▶ | exists $self->{min} ? $self->{min} : 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub max { | ||||||||||||||||||||||||||||
| 53 | 2408 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
max: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 2408 | ▶ | exists $self->{max} ? $self->{max} eq "M" ? 0 : $self->{max} : 1 | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub items { | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
items: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 0 | keys %{$self->valid_items} | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub _valid_items { | ||||||||||||||||||||||||||||
| 63 | 769 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_valid_items: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 64 | 769 | my %valid_items; | |||||||||||||||||||||||||||
| 65 | 769 | for my $item (@{$self->{items}}) { | |||||||||||||||||||||||||||
| 66 | 2408 | my $min = $item->min; | |||||||||||||||||||||||||||
| 67 | 2408 | my $max = $item->max; | |||||||||||||||||||||||||||
| 68 | 2408 | ▶ | if ($item->{tag}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 1712 | push @{$valid_items{$item->{tag}}}, { | |||||||||||||||||||||||||||
| 70 | grammar => $item, | ||||||||||||||||||||||||||||
| 71 | min => $min, | ||||||||||||||||||||||||||||
| 72 | max => $max | ||||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||
| 75 | die "What's a " . Data::Dumper->new([$item], ["grammar"]) | ||||||||||||||||||||||||||||
| 76 | 696 | ▶ | unless my ($value) = $item->{value} =~ /<<(.*)>>/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 696 | ▶ | die "Can't find $value in Gedcom structures" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | unless my $structure = $self->structure($value); | ||||||||||||||||||||||||||||
| 79 | 696 | $item->{structure} = $structure; | |||||||||||||||||||||||||||
| 80 | 696 | ▶ | while (my($tag, $g) = each %{$structure->valid_items}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 2994 | push @{$valid_items{$tag}}, map { | |||||||||||||||||||||||||||
| 82 | grammar => $_->{grammar}, | ||||||||||||||||||||||||||||
| 83 | # min and max can be calculated by multiplication because | ||||||||||||||||||||||||||||
| 84 | # the grammar always permits multiple selection records, and | ||||||||||||||||||||||||||||
| 85 | # selection records never have compulsory records. This may | ||||||||||||||||||||||||||||
| 86 | # change in future grammars, but I would not expect it to - | ||||||||||||||||||||||||||||
| 87 | # such a grammar would seem to have little practical use. | ||||||||||||||||||||||||||||
| 88 | min => $_->{min} * $min, | ||||||||||||||||||||||||||||
| 89 | 2994 | max => $_->{max} * $max | |||||||||||||||||||||||||||
| 90 | }, @$g; | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 696 | ▶ | if (exists $item->{items} && @{$item->{items}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $item->{"items"} and @{$item->{"items"};}
MC/DC50%1/2
exists $item->{"items"} and @{$item->{"items"};}
exists $item->{"items"}
@{$item->{"items"};}
Truth table66%2/3
A and B
A= exists $item->{"items"}B= @{$item->{"items"};}
| |||||||||||||||||||||||||||||
| 93 | 11 | my $extra_items = $item->_valid_items; | |||||||||||||||||||||||||||
| 94 | 11 | ▶ | while (my ($sub_item, $sub_grammars) = each %valid_items) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 121 | for my $sub_grammar (@$sub_grammars) { | |||||||||||||||||||||||||||
| 96 | 121 | $sub_grammar->{grammar}->valid_items; | |||||||||||||||||||||||||||
| 97 | 121 | ▶ | while (my ($i, $g) = each %$extra_items) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | # print "adding $i to $sub_item\n"; | ||||||||||||||||||||||||||||
| 99 | 242 | $sub_grammar->{grammar}{_valid_items}{$i} = $g; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | # print "giving @{[keys %{$sub_grammar->{grammar}->valid_items}]}\n"; | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | # print "valid items are @{[keys %valid_items]}\n"; | ||||||||||||||||||||||||||||
| 108 | 769 | \%valid_items | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub valid_items { | ||||||||||||||||||||||||||||
| 112 | 67768 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
valid_items: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 67768 | ▶ | $self->{_valid_items} ||= $self->_valid_items | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"_valid_items"} ||= $self->_valid_items
MC/DC0%0/2
$self->{"_valid_items"} ||= $self->_valid_items
$self->{"_valid_items"}
$self->_valid_items
Truth table66%2/3
A ||= B
A= $self->{"_valid_items"}B= $self->_valid_items
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 15 | 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||