blib/lib/DDC/PP/CQueryCompiler.pm

filter: Statementstmt 89.7% 96 / 107 Branchbran 56.2% 18 / 32 Conditioncond 53.3% 8 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 84.3% 27 / 32 Podpod 0.0% 0 / 18 totaltotal 70.7% 150 / 212 CC 23 SCAR 11.7
CC
23
Cov
79%
free
6.0
fromFile
6.0
fromFh
6.0
CRAP 3.2
« blib/lib/DDC/Format/Template.pm ↑ Summary blib/lib/DDC/PP/CQFilter.pm »
1 ##-*- Mode: CPerl -*-
2 ##
3 ## File: DDC::PP::CQueryCompiler.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: pure-perl DDC query parser, top-level
6 ##======================================================================
7  
8 package DDC::PP::CQueryCompiler;
9 20 ▶use DDC::Utils qw(:escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use DDC::PP::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use DDC::PP::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use DDC::PP::CQuery;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use DDC::PP::CQCount;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use DDC::PP::CQFilter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use DDC::PP::CQueryOptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 20 ▶use DDC::PP::yyqlexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶use DDC::PP::yyqparser;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 ##======================================================================
23 ## Globals etc.
24 20 our @ISA = qw(DDC::PP::Object);
25  
26  
27 ##======================================================================
28 ## $qc = $CLASS_OR_OBJ->new(%args)
29 ## + abstract constructor
30 ## + object structure, %args:
31 ## {
32 ## ##-- DDC::XS::CQueryCompiler emulation
33 ## Query => $query, ##-- last query parsed
34 ##
35 ## ##-- guts: status flags
36 ## error => $current_errstr, ##-- false indicates no error
37 ##
38 ## ##-- guts: underlying lexer/parser pair
39 ## lexer => $yylexer, ##-- a DDC::PP::yyqlexer object
40 ## parser => $yyparser, ##-- a DDC::PP::yyqparser object
41 ## yydebug => $mask, ##-- yydebug value
42 ##
43 ## ##-- guts: closures
44 ## yylex => \&yylex, ##-- yapp-friendly lexer sub
45 ## yyerror => \&yyerror, ##-- yapp-friendly parser sub
46 ## }
47 15 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod undocumented
48 15 my $that = shift;
49 15 ▶ my $qc = bless({
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
0150
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
50 ##-- DDC::XS emulation
51 Query => undef,
52 KeepLexerComments => 0,
53  
54 ##-- guts: status flags
55 error => undef,
56  
57 ##-- guts: underlying lexer/parser pair
58 lexer => DDC::PP::yyqlexer->new(),
59 parser => DDC::PP::yyqparser->new(),
60  
61 ##-- guts: runtime data
62 qopts => undef,
63  
64 ##-- parser debugging
65 yydebug => 0, # no debug
66 #yydebug => 0x01, # lexer debug
67 #yydebug => 0x02, # state info
68 #yydebug => 0x04, # driver actions (shift/reduce/etc.)
69 #yydebug => 0x08, # stack dump
70 #yydebug => 0x10, # Error recovery trace
71 #yydebug => 0x01 | 0x02 | 0x04 | 0x08, # almost everything
72 #yydebug => 0xffffffff, ##-- pretty much everything
73  
74 ##-- User args
75 @_
76 },
77 ref($that)||$that);
78 15 $qc->getClosures();
79 15 return $qc;
80 }
81  
82 ## undef = $qc->free()
83 ## + clears $qc itself, as well as $qc->{parser}{USER}
84 ## + makes $qc subsequently useless, but destroyable
85 0 ▶sub free {
Subroutine free: not called CC=2 SCAR=17.9
Pod undocumented
86 0 my $qc = shift;
87 0 ▶ delete($qc->{parser}{USER}) if ($qc->{parser});
Branch: 1 branch
BranchTrueFalseTotal
if $qc->{"parser"} 0 0 0
88 0 %$qc = qw();
89 }
90  
91 ## $qc = $qc->getClosures()
92 ## + compiles lexer & parser closures
93 15 ▶sub getClosures {
Subroutine getClosures: called CC=1 SCAR=0.0
Pod undocumented
94 15 my $qc = shift;
95 15 delete(@$qc{qw(yylex yyerror)});
96 15 $qc->{yylex} = $qc->_yylex_sub();
97 15 $qc->{yyerror} = $qc->_yyerror_sub();
98 15 return $qc;
99 }
100  
101 ##======================================================================
102 ## DDC::XS emulation
103  
104 20 __PACKAGE__->defprop('Query');
105 20 __PACKAGE__->defprop('KeepLexerComments');
106  
107 ## undef = $qc->CleanParser()
108 ## + reset all parse-relevant data structures
109 0 ▶sub CleanParser { $_[0]->reset; }
Subroutine CleanParser: not called CC=1 SCAR=6.9
Pod undocumented
110  
111 ## $CQuery = $qc->ParseQuery($qstr)
112 211 ▶sub ParseQuery {
Subroutine ParseQuery: called CC=2 SCAR=6.9
Pod undocumented
113 211 my ($qc,$qstr) = @_;
114 211 $qc->{Query} = eval { $qc->parse(string=>\$qstr); };
115 211 ▶ die(__PACKAGE__."::ParseQuery() failed: could not parse query: $@") if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 209 211
116 209 return $qc->{Query};
117 }
118  
119 ## $s = $qc->QueryToString()
120 2 ▶sub QueryToString { return $_[0]->getQuery->toStringFull(); }
Subroutine QueryToString: called CC=1 SCAR=0.0
Pod undocumented
121  
122 ## $s = $qc->QueryToJson()
123 2 ▶sub QueryToJson {
Subroutine QueryToJson: called CC=1 SCAR=0.0
Pod undocumented
124 2 return "{\"Query\":".$_[0]->getQuery->toJson().",\"Options\":".$_[0]->getQuery->getOptions->toJson()."}";
125 }
126  
127  
128 ##======================================================================
129 ## Local API: Input selection
130  
131 ## undef = $qc->reset()
132 ## + reset all parse-relevant data structures
133 211 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
134 211 my $qc = shift;
135  
136 ##-- runtime data
137 211 delete(@$qc{qw(Query qopts)});
138  
139 ##-- lexer & parser state
140 211 $qc->{lexer}->reset();
141  
142 211 delete($qc->{parser}{USER}{hint});
143 211 $qc->{parser}{USER}{qc} = $qc;
144 211 $qc->{parser}{USER}{lex} = $qc->{lexer};
145  
146 }
147  
148 ## $qc = $qc->from($which,$src, %opts)
149 ## + wraps $qc->{lexer}->from()
150 ## + $which is one of qw(fh file string)
151 ## + $src is the actual source (default: 'string')
152 ## + %opts may contain (src=>$name)
153 211 ▶sub from {
Subroutine from: called CC=2 SCAR=7.6
Pod undocumented
154 211 ▶ return $_[0]{lexer}->from(@_[1..$#_]) ? $_[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'lexer'}->from(@_[1 .. $#_]) ? : 211 0 211
155 }
156  
157 ## $qc = $qc->fromFile($filename_or_handle,%opts)
158 ## + wraps $qc->{lexer}->fromFile()
159 0 ▶sub fromFile {
Subroutine fromFile: not called CC=2 SCAR=17.9
Pod undocumented
160 0 ▶ return $_[0]{lexer}->fromFile(@_[1..$#_]) ? $_[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'lexer'}->fromFile(@_[1 .. $#_]) ? : 0 0 0
161 }
162  
163 ## $qc = $qc->fromFh($fh,%opts)
164 ## + wraps $qc->{lexer}->fromFh()
165 0 ▶sub fromFh {
Subroutine fromFh: not called CC=2 SCAR=17.9
Pod undocumented
166 0 ▶ return $_[0]{lexer}->fromFh(@_[1..$#_]) ? $_[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'lexer'}->fromFh(@_[1 .. $#_]) ? : 0 0 0
167 }
168  
169 ## $qc = $qc->fromString($str,%opts)
170 ## $qc = $qc->fromString(\$str,%opts)
171 ## + wraps $qc->{lexer}->fromString()
172 0 ▶sub fromString {
Subroutine fromString: not called CC=2 SCAR=17.9
Pod undocumented
173 0 ▶ return $_[0]{lexer}->fromString(@_[1..$#_]) ? $_[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'lexer'}->fromString(@_[1 .. $#_]) ? : 0 0 0
174 }
175  
176  
177 ##======================================================================
178 ## Local API: High-level Parsing
179  
180 ## $query_or_undef = $qc->parse(string=>$str)
181 ## $query_or_undef = $qc->parse(string=>\$str)
182 ## $query_or_undef = $qc->parse(file=>$filename)
183 ## $query_or_undef = $qc->parse(fh=>$handle)
184 211 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.1
Pod undocumented
185 211 my $qc = shift;
186 211 $qc->reset();
187 211 $qc->from(@_);
188 211 my $result = eval { $qc->yyparse(); };
189 211 my $err = $@;
190 211 delete($qc->{parser}{qc}); ##-- chop circular reference we know how to get at...
191 211 delete($qc->{parser}{USER}{qc}); ##-- chop circular reference we know how to get at...
192  
193 ##-- adopt lexer comments
194 $result->{Options}{LexerComments} = $qc->{lexer}{comments}
195 211 ▶ if ($qc->{KeepLexerComments} && $result && $result->{Options});
Branch: 1 branch
BranchTrueFalseTotal
if $qc->{"KeepLexerComments"} and $result and $result->{"Options"} 8 203 211
Condition66%4/6
$qc->{"KeepLexerComments"} and $result and $result->{"Options"}
!ll&&!rl&&r
20308
$qc->{"KeepLexerComments"} and $result
!ll&&!rl&&r
20308
MC/DC33%1/3
$qc->{"KeepLexerComments"} and $result and $result->{"Options"}
$qc->{"KeepLexerComments"} $result $result->{"Options"}
Truth table50%2/4
A and B and C
A= $qc->{"KeepLexerComments"}B= $resultC= $result->{"Options"}
ABCresult
0XX0
10X0
1100
1111
196  
197 ##-- how'd it go?
198 211 ▶ die($err) if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 2 209 211
199 209 return $result;
200 }
201  
202 ## $query_or_undef = $qc->yyparse()
203 ## + parses from currently selected input source; no reset or error catching
204 211 ▶sub yyparse {
Subroutine yyparse: called CC=1 SCAR=0.0
Pod undocumented
205 211 my $qc = shift;
206 return $qc->{parser}->YYParse(
207 yylex => $qc->{yylex},
208 yyerror => $qc->{yyerror},
209 yydebug => $qc->{yydebug},
210 211 );
211 }
212  
213 ##======================================================================
214 ## Local API: Mid-level: Query Generation
215  
216 ## $q = $qc->newq($class,@args)
217 ## + wrapper for "DDC::PP::$class"->new(@args); called by yapp parser
218 391 ▶sub newq {
Subroutine newq: called CC=1 SCAR=0.0
Pod undocumented
219 391 return "DDC::PP::$_[1]"->new(@_[2..$#_]);
220 }
221  
222 ## $qf = $qc->newf(@args)
223 ## + wrapper for DDC::Query::Filter->new(@args); called by yapp parser
224 20 ▶sub newf {
Subroutine newf: called CC=1 SCAR=0.0
Pod undocumented
225 20 return "DDC::PP::$_[1]"->new(@_[2..$#_]);
226 }
227  
228 ## $re = $qc->newre($re,$modifiers)
229 11 ▶sub newre {
Subroutine newre: called CC=4 SCAR=17.9
Pod undocumented
230 11 my ($qc,$re,$mods) = @_;
231 11 ▶ if (($mods||'') =~ /g/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($mods || "") =~ /g/) 0 11 11
Condition50%1/2
$mods || ''
l!l
011
MC/DC0%0/1
$mods || ''
$mods
Truth table50%1/2
A
A= $mods
Aresult
11
00
232 0 $re = "^(?:${re})\$";
233 0 $mods =~ s/g//g;
234 }
235 11 ▶ return $re if (!$mods);
Branch: 1 branch
BranchTrueFalseTotal
unless $mods 11 0 11
236 0 return "(?:${mods})$re";
237 }
238  
239 ## $qo = $qc->qopts()
240 ## $qo = $qc->qopts($opts)
241 ## + get/set current query options
242 606 ▶sub qopts {
Subroutine qopts: called CC=3 SCAR=11.0
Pod undocumented
243 606 ▶ $_[0]{qopts} = $_[1] if ($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 239 367 606
244 606 ▶ $_[0]{qopts} = DDC::PP::CQueryOptions->new if (!defined($_[0]{qopts}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{"qopts"} 211 395 606
245 606 return $_[0]{qopts};
246 }
247  
248  
249 ##======================================================================
250 ## API: Low-LEVEL: Parse::Lex <-> Parse::Yapp interface
251 ##
252 ## - REQUIREMENTS on yylex() sub:
253 ## + Yapp-compatible lexing routine
254 ## + reads input and returns token values to the parser
255 ## + our only argument ($MyParser) is the YYParser object itself
256 ## + We return a list ($TOKENTYPE, $TOKENVAL) of the next tokens to the parser
257 ## + on end-of-input, we should return the list ('', undef)
258 ##
259  
260 ## \&yylex_sub = $qc->_yylex_sub()
261 ## + returns a Parse::Yapp-friendly lexer subroutine
262 15 ▶sub _yylex_sub {
Subroutine _yylex_sub: called CC=1 SCAR=0.2
263 15 my $qc = shift;
264 15 my ($type,$text,@expect);
265  
266 1125 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=15.2
267 1125 $qc->{yyexpect} = [$qc->{parser}->YYExpect];
268 1125 ($type,$text) = $qc->{lexer}->yylex();
269 1125 ▶ return ('',undef) if ($type eq '__EOF__');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "__EOF__" 209 916 1125
270  
271 ##-- un-escape single-quoted symbols (this happens in the parser)
272 # if ($type =~ /^SQ_(.*)$/) {
273 # $type = $1;
274 # $text = unescapeq($text);
275 # }
276 # elsif ($type eq 'SYMBOL') {
277 # $text = unescape($text);
278 # }
279  
280 916 ▶ if ($qc->{yydebug} & 0x01) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qc->{"yydebug"} & 1) 0 916 916
281 0 ▶ print STDERR ": yylex(): type=($type) ; text=(".(defined($text) ? $text : '-undef-')." ; state=(".($qc->{lexer}{state}).")\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 0 0 0
282 }
283  
284 916 return ($type,$text);
285 15 };
286 }
287  
288  
289 ## \&yyerror_sub = $qc->_yyerror_sub()
290 ## + returns error subroutine for the underlying Yapp parser
291 15 ▶sub _yyerror_sub {
Subroutine _yyerror_sub: called CC=1 SCAR=0.0
292 15 my $qc = shift;
293 15 my (%expect,@expecting);
294 2 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
295 2 ▶ @expect{@{$qc->{yyexpect}||[]}}=qw();
Condition50%1/2
$qc->{'yyexpect'} || []
l!l
20
MC/DC0%0/1
$qc->{'yyexpect'} || []
$qc->{'yyexpect'}
Truth table50%1/2
A
A= $qc->{'yyexpect'}
Aresult
11
00
296 2 ▶ @expect{@{$qc->{yyexpect}||[]}, $qc->{parser}->YYExpect}=qw();
Condition50%1/2
$qc->{'yyexpect'} || []
l!l
20
MC/DC0%0/1
$qc->{'yyexpect'} || []
$qc->{'yyexpect'}
Truth table50%1/2
A
A= $qc->{'yyexpect'}
Aresult
11
00
297 2 ▶ @expecting = sort map {$_ eq '' ? '$end' : $_} keys %expect;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '' ? : 2 2 4
298 die("syntax error, unexpected ".$qc->{lexer}->yytype
299 .", expecting ".join(' or ', @expecting)
300 ." at line ".$qc->{lexer}->yylineno
301 2 .", near token \`".$qc->{lexer}->yytext."'");
302 # $qc->{error} = ("syntax error in ".$qc->{lexer}->yywhere().":\n"
303 # #." > Expected one of (here): ".join(', ', map {$_ eq '' ? '__EOF__' : $_} $qc->{parser}->YYExpect)."\n"
304 # #." > Expected one of (prev): ".join(', ', map {$_ eq '' ? '__EOF__' : $_} @{$qc->{yyexpect}||['???']})."\n"
305 # ." > Expected one of: ".join(', ', sort map {$_ eq '' ? '__EOF__' : $_} keys %expect)."\n"
306 # ." > Got: ".$qc->{lexer}->yytype.' "'.$qc->{lexer}->yytext."\"\n"
307 # );
308 15 };
309 }
310  
311  
312 20 1; ##-- be happy
313  
314 __END__
« blib/lib/DDC/Format/Template.pm ↑ Summary blib/lib/DDC/PP/CQFilter.pm »