File Coverage

blib/lib/Lingua/LinkParser/MatchPath/BuildSM.pm
Criterion Covered Total %
statement 9 72 12.5
branch 0 6 0.0
condition 0 5 0.0
subroutine 3 17 17.6
pod 0 14 0.0
total 12 114 10.5


line stmt bran cond sub pod time code
1             package Lingua::LinkParser::MatchPath::BuildSM;
2              
3 1     1   5 use strict;
  1         1  
  1         37  
4 1     1   5 use Data::Dumper;
  1         1  
  1         44  
5 1     1   5 use List::Util qw(max);
  1         2  
  1         1146  
6              
7             #open STDERR, ">/dev/null";
8 0     0 0   sub print_stat {}
9              
10             sub new {
11 0     0 0   my $class = shift;
12 0           my $template = shift;
13 0           bless {
14             _template => $template,
15             _curr_state => 0,
16             _prev_state => 0,
17             _states => { 0 => '' },
18             _state_stack => [ 0 ],
19             _arc => {
20             },
21             _fh => undef, # reserved for dumping logs to file
22             _accept_state => undef,
23             _item => [],
24             }, $class;
25             }
26              
27 0     0 0   sub template { $_[0]->{_template} }
28              
29             sub reset {
30 0     0 0   my $self = shift;
31 0           $self->{_curr_state} = 0;
32 0           $self->{_prev_state} = 0;
33 0           $self->{_failed} = undef;
34 0           $self->{_visited} = undef;
35 0           $self->{_item} = [];
36 0           $self->{_state_stack} = [ 0 ];
37 0           $self->{_wordptr} = undef;
38 0           $self->{_label_num} = undef;
39 0           $self->{_arc_stack} = undef;
40 0           $self->{_start_position} = undef;
41 0           $self->{_linkage} = undef;
42 0           $self->{_built_arc_stack} = undef;
43             }
44              
45              
46             sub dump_arc {
47 0     0 0   my $arc = shift;
48 0           foreach my $k ('next_state', sort keys %$arc){
49 0           print_stat " $k => ".Dumper($arc->{$k})."\n";
50             }
51             }
52              
53             sub dump_arcs {
54 0     0 0   my $self = shift;
55 0           local $Data::Dumper::Terse = 1;
56 0           local $Data::Dumper::Indent = 0;
57 0           foreach my $state (sort { $a <=> $b } keys %{$self->{_arc}}){
  0            
  0            
58 0           print_stat ">> STATE: $state\n";
59 0           foreach my $arc (@{$self->{_arc}->{$state}}){
  0            
60 0           print_stat dump_arc($arc), $/x2;
61             }
62             }
63             }
64              
65             sub arc_template {
66 0     0 0   my %arg = @_;
67              
68             return {
69 0   0       next_state => $arg{next_state},
70              
71             branch_type => ($arg{branch_type} || 0),
72             branch => $arg{branch},
73              
74             input_action => $arg{input_action},
75             label => ($arg{label}),
76             word => ($arg{word}),
77              
78             # this is reserved for negative branching
79             failure => $arg{failure},
80             }
81             }
82              
83              
84              
85              
86             sub add_state {
87 0     0 0   my $self = shift;
88 0           my %arg = @_;
89              
90 0           my $state = $arg{state};
91 0 0         $state = max(keys %{$self->{_states}})+1 unless defined $state;
  0            
92              
93 0           $self->{_prev_state} = $self->{_curr_state};
94 0           $self->{_curr_state} = $state;
95 0           $self->{_states}->{$state} = '';
96 0 0         $self->{_final_state} = $state if $arg{final};
97 0           print_stat "ADD STATE $state\n";
98             }
99              
100             sub push_state {
101 0     0 0   my $self = shift;
102 0           push @{$self->{_state_stack}}, $self->{_curr_state};
  0            
103             }
104              
105             sub pop_state {
106 0     0 0   my $self = shift;
107 0           $self->{_curr_state} = pop @{$self->{_state_stack}};
  0            
108             }
109              
110             sub add_arc {
111 0     0 0   my $self = shift;
112 0           my %arg = @_;
113              
114 0           print_stat "JOIN: $arg{join}\n";
115 0 0 0       foreach my $prev_state (
116 0           $arg{join} && keys %{$self->{_tojoin}} ?
117             (keys %{$self->{_tojoin}})
118             :
119             ($self->{_prev_state})
120             ){
121 0           print_stat "ADD ARC ($prev_state -> $self->{_curr_state})\n";
122              
123             push
124 0           @{$self->{_arc}
125             ->{
126 0           $prev_state
127             }},
128             arc_template(
129             'next_state' => $self->{_curr_state},
130             %arg,
131             'branch_type' => $self->{_branch_type},
132             'branch' => $self->{_branch},
133             'input_action' => $self->{_input_action},
134             'failure' => $self->{_failure},
135             );
136             }
137              
138             }
139              
140             sub restore_prev_state {
141 0     0 0   my $self = shift;
142 0           $self->{_prev_state} = $self->{_pprev_state};
143             }
144              
145             sub save_prev_state {
146 0     0 0   my $self = shift;
147 0           $self->{_pprev_state} = $self->{_prev_state};
148             }
149              
150             sub set_accept_state {
151 0     0 0   my $self = shift;
152 0           $self->{_accept_state} = $self->{_curr_state};
153             }
154              
155             1;