Indexer/Config.pm untested

filter: Statementstmt 0.0% 0 / 163 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 86 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 394 CC 32 SCAR 35.9
CC
32
Cov
0%
_tokenize
110.0
_term_body
72.0
_mkprop
30.0
CRAP 36.1
1 package MARC::Indexer::Config;
2  
3 use strict;
4 use warnings;
5  
6 use constant SPACE => 1 << 0;
7 use constant PLAIN => 1 << 1;
8 use constant QUOTE => 1 << 2;
9 use constant GROUP => 1 << 3;
10 use constant COMMA => 1 << 4;
11 use constant SEMIC => 1 << 5;
12 use constant ASTER => 1 << 6;
13 use constant NEWLN => 1 << 7;
14 use constant REGEX => 1 << 8;
15  
16 sub new {
17 my ($cls, $f) = @_;
18 bless {
19 'file' => $f,
20 }, $cls;
21 }
22  
23 sub parse {
24 my ($self, $f) = @_;
25 $f = $self->{'file'} || die "No file given"
26 if !defined $f;
27 my $fh;
28 if (ref $f) {
29 $fh = $f;
30 }
31 else {
32 open $fh, '<', $f or die "Can't open file $f: $!";
33 }
34 local $/;
35 my $src = $self->{'source'} = <$fh>;
36 $src =~ s/^\s*#.*//mg;
37 my @tok = _tokenize($src);
38 my %term;
39 $self->{'tokens'} = \@tok;
40 $self->{'terms'} = \%term;
41 while (@tok) {
42 _wsp(\@tok);
43 _docid(\@tok, \$self->{'docid'})
44 || _defaults(\@tok, $self)
45 || _term(\@tok, \%term)
46 || _fatal("Unparseable", $tok[0][2]);
47 }
48 return $self;
49 }
50  
51 sub _wspafter {
52 my ($tok) = @_;
53 my $n = 0;
54 $n++, pop @$tok while @$tok && $tok->[-1][0] == SPACE;
55 $n;
56 }
57  
58 sub _wsp {
59 my ($tok) = @_;
60 my $n = 0;
61 $n++, shift @$tok while @$tok && $tok->[0][0] == SPACE;
62 $n;
63 }
64  
65 sub _last {
66 my ($tok, $typ, $val) = @_;
67 _wspafter($tok);
68 return if !@$tok
69 || defined($typ) && !($tok->[-1][0] & $typ)
70 || ref($val) && $tok->[-1][1] !~ $val
71 || defined($val) && $tok->[-1][1] ne $val;
72 return @{ pop @$tok };
73 }
74  
75 sub _next {
76 my ($tok, $typ, $val) = @_;
77 _wsp($tok);
78 return if !@$tok
79 || defined($typ) && !($tok->[0][0] & $typ)
80 || ref($val) && $tok->[0][1] !~ $val
81 || defined($val) && $tok->[0][1] ne $val;
82 return @{ shift @$tok };
83 }
84  
85 sub _docid {
86 # docid FIELD;
87 my ($tok, $ref) = @_;
88 return if @$tok < 3;
89 my ($typ, $val, $lno);
90 ($typ, $val, $lno) = _next($tok, PLAIN, 'docid') or return;
91 ($typ, $val, $lno) = _next($tok, PLAIN) or return;
92 $$ref = $val;
93 ($typ, $val, $lno) = _next($tok, SEMIC) or fatal("Junk after docid declaration");
94 }
95  
96 sub _defaults {
97 # defaults { foo; bar; }
98 my ($tok, $self) = @_;
99 return if @$tok < 3;
100 my ($typ, $val, $lno, %def);
101 ($typ, $val, $lno) = _next($tok, PLAIN, 'defaults') or return;
102 $self->{'defaults'} = \%def;
103 _term_body($tok, \%def);
104 1;
105 }
106  
107 sub _term {
108 # term * { foo; bar; }
109 # term foo "bar" { baz; qux; }
110 my ($tok, $terms) = @_;
111 return if @$tok < 4;
112 my ($typ, $val, $lno);
113 ($typ, $val, $lno) = _next($tok, PLAIN, 'term') or return;
114 ($typ, $val, $lno) = _next($tok, ASTER|PLAIN ) or return;
115 my %term;
116 $terms->{$val} = \%term;
117 $term{'description'} = $val if ($typ, $val, $lno) = _next($tok, QUOTE);
118 _term_body($tok, \%term);
119 1;
120 }
121  
122 sub _term_body {
123 my ($tok, $term) = @_;
124 _wsp($tok);
125 my ($typ, $val, $lno);
126 ($typ, $val, $lno) = _next($tok, GROUP, '{') or die;
127 my @prop;
128 while (1) {
129 _wsp($tok);
130 ($typ, $val, $lno) = _next($tok) or die;
131 if ($typ == GROUP && $val eq '}') {
132 if (@prop) {
133 my ($pkey, $pval) = _mkprop(@prop);
134 $term->{$pkey} = $pval;
135 }
136 last;
137 }
138 elsif ($typ == SEMIC) {
139 next if !@prop;
140 my ($pkey, $pval) = _mkprop(@prop);
141 $term->{$pkey} = $pval;
142 @prop = ();
143 }
144 elsif (!@prop && $typ == PLAIN) {
145 push @prop, $val;
146 }
147 elsif (@prop) {
148 push @prop, [$typ, $val];
149 }
150 else {
151 die;
152 }
153 }
154 $term->{'norm'} = [ list($term->{'norm'}) ];
155 $term->{'prefix'} = [ list($term->{'prefix'}) ];
156 $term->{'match'} = qr/$term->{'match'}/ if defined $term->{'match'};
157 }
158  
159 sub _mkprop {
160 my $pkey = shift;
161 if (!@_) {
162 return ($pkey, '') if $pkey =~ s/^no//;
163 return ($pkey, 1);
164 }
165 if (_next(\@_, GROUP, '[')) {
166 _last(\@_, GROUP, ']') or _fatal("Unclosed list");
167 my @list;
168 while (1) {
169 _wsp(\@_);
170 my ($typ, $val, $lno) = _next(\@_, PLAIN|QUOTE)
171 or _fatal("Unrecognized list element");
172 push @list, $val;
173 _next(\@_, COMMA) or last;
174 }
175 _wsp(\@_);
176 _fatal("Junk at end of list") if @_;
177 return ($pkey, \@list);
178 }
179 return ($pkey, join(' ', map { $_->[1] } @_));
180 }
181  
182 sub _fatal {
183 my ($err, $lno) = @_;
184 print STDERR "FATAL: $err at line $lno\n";
185 exit 2;
186 }
187  
188 sub _tokenize {
189 local $_ = shift;
190 my @tok;
191 my $lno = 1;
192 while (!/\G\z/gc) {
193 $lno++, next if /\G\n/gc;
194 push @tok,
195 /\G(\s+)/gc ? [ SPACE, $1, $lno ] :
196 /\G([:.\w][-:.\$\/\w]*)/gc ? [ PLAIN, $1, $lno ] :
197 /\G"((?:\\.|[^"])*)"/gc ? [ QUOTE, _unesc($1), $lno ] :
198 /\G(,)/gc ? [ COMMA, $1, $lno ] :
199 /\G(;)/gc ? [ SEMIC, $1, $lno ] :
200 /\G(\*)/gc ? [ ASTER, $1, $lno ] :
201 /\G([(){}\[\]])/gc ? [ GROUP, $1, $lno ] :
202 /\G\/((?:\\.|[^\/])+)\//gc ? [ REGEX, $1, $lno ] :
203 _fatal("Unrecognized", $lno)
204 ;
205 }
206 return @tok;
207 }
208  
209 sub _unesc {
210 local $_ = shift;
211 s/\\(.)|(.)/defined($1) ? $1 : $2/eg;
212 return $_;
213 }
214  
215 sub list {
216 my ($v) = @_;
217 return if !defined $v;
218 my $r = ref $v;
219 return @$v if $r eq 'ARRAY';
220 return %$v if $r eq 'HASH';
221 return $v;
222 }
223  
224 1;