SCPI.pm

filter: Statementstmt 83.6% 261 / 312 Branchbran 71.8% 125 / 174 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 19 / 19 Podpod 66.6% 6 / 9 totaltotal 79.4% 420 / 529 CC 97 SCAR 29.6
CC
97
Cov
79%
scpi_parse_sequence
32.4
_gMem
32.2
_scpi_value
17.6
CRAP 19.2
« XPRESS/Sweep/Time.pm ↑ Summary Instrument/TemperatureControl/TLK43.pm »
1 package Lab::SCPI;
2 5 $Lab::SCPI::VERSION = '3.901';
3 #ABSTRACT: Match L<SCPI|http://www.ivifoundation.org/scpi/> headers and parameters against keywords
4  
5 5 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use English; # avoid editor nonsense with odd special variables
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 our @EXPORT = qw( scpi_match scpi_parse scpi_canon
16 scpi_flat scpi_parse_sequence );
17  
18 5 our $WS = qr/[\x00-\x09\x0b-\x20]/; # whitespace std488-2 7.4.1.2
19  
20  
21 24 ▶sub scpi_match {
Subroutine scpi_match: called CC=3 SCAR=11.0
Pod documented
22 24 my $header = shift;
23 24 my $keyword = shift;
24 24 my @keywords = split '\|', $keyword, -1;
25 24 for my $part (@keywords) {
26 27 ▶ if ( match_keyword( $header, $part ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (match_keyword($header, $part)) 13 14 27
27 13 return 1;
28 }
29 }
30 11 return 0;
31 }
32  
33 53 ▶sub parse_keyword {
Subroutine parse_keyword: called CC=4 SCAR=14.3
Pod undocumented
34 53 my $keyword = shift;
35  
36 # For the first part, the colon is optional.
37 53 my $start_mnemonic_regex = qr/(?<mnemonic>:?[a-z][a-z0-9_]*)/i;
38 53 my $mnemonic_regex = qr/(?<mnemonic>:[a-z][a-z0-9_]*)/i;
39 53 my $keyword_regex = qr/\[$mnemonic_regex\]|$mnemonic_regex/;
40 53 my $start_regex = qr/\[$start_mnemonic_regex\]|$start_mnemonic_regex/;
41  
42 # check if keyword is valid
43 53 ▶ if ( length($keyword) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $keyword == 0) 0 53 53
44 0 croak "keyword with empty length";
45 }
46  
47 53 ▶ if ( $keyword !~ /^${start_regex}${keyword_regex}*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keyword =~ /^$start_regex$keyword_regex*$/u) 0 53 53
48 0 croak "invalid keyword: '$keyword'";
49 }
50  
51 53 ▶ if ( $keyword !~ /\[/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keyword =~ /\[/u) 40 13 53
52  
53 # no more optional parts
54 40 return $keyword;
55 }
56  
57 #recurse
58 return (
59 13 parse_keyword( $keyword =~ s/\[(.*?)\]/$1/r ),
60 parse_keyword( $keyword =~ s/\[(.*?)\]//r )
61 );
62 }
63  
64  
65 55 ▶sub scpi_shortform {
Subroutine scpi_shortform: called CC=5 SCAR=16.4
Pod documented
66 55 my $string = shift;
67 55 $string =~ s/^${WS}*//; # strip leading spaces
68 55 ▶ if ( length($string) <= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string <= 4) 10 45 55
69 10 return $string;
70 }
71  
72 # common mnemonics start with '*' and are not shortenable
73 # note that standard IEEE488 common mnemonics are length 4,
74 # but some extensions result in longer common mnemonics
75  
76 45 ▶ if ( $string =~ /^\*/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\*/u) 0 45 45
77 0 return $string;
78 }
79  
80 # mnemonics can have following digits (ex: CHANNEL3)
81 # the digits should be kept
82 # if followed by a '?', keep that too
83  
84 # mnemonics in the form (letter)(letter|digit|underscore)*
85 # but need to separate the "digit" part at end
86  
87 45 ▶ if ( $string =~ /^([a-z]\w*[a-z_])(\d*)(\??)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^([a-z]\w*[a-z_])(\d*)(\??)/iu) { } 45 0 45
88 45 $string = substr( $1, 0, 4 );
89 45 my $n = $2;
90 45 my $q = $3;
91 45 ▶ if ( $string =~ /^...[aeiou]/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^...[aeiou]/iu) 8 37 45
92 8 $string = substr( $string, 0, 3 );
93 }
94 45 return $string . $n . $q;
95 }
96 else { # not a standard form mnemonic, bail
97 0 return $string;
98 }
99  
100 }
101  
102 # Return 1 for equal, 0 if not.
103 37 ▶sub compare_headers {
Subroutine compare_headers: called CC=5 SCAR=16.1
Pod undocumented
104 37 my $a = shift;
105 37 my $b = shift;
106  
107 37 my @a = split( /:/, $a, -1 );
108 37 my @b = split( /:/, $b, -1 );
109  
110 37 ▶ if ( @a != @b ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a != @b) 11 26 37
111 11 return 0;
112 }
113 26 ▶ while (@a) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a) 34 13 47
114 34 my $a = shift @a;
115 34 my $b = shift @b;
116 34 $a = "\L$a";
117 34 $b = "\L$b";
118 34 ▶ if ( $b ne $a and $b ne scpi_shortform($a) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b ne $a and $b ne scpi_shortform($a)) 13 21 34
Condition100%3/3
$b ne $a and $b ne scpi_shortform($a)
!ll&&!rl&&r
101113
MC/DC100%2/2
$b ne $a and $b ne scpi_shortform($a)
$b ne $a $b ne scpi_shortform($a)
Truth table100%3/3
A and B
A= $b ne $aB= $b ne scpi_shortform($a)
ABresult
0X0
100
111
119 13 return 0;
120 }
121 }
122 13 return 1;
123 }
124  
125 # Return 1 for match, 0 for no match.
126 27 ▶sub match_keyword {
Subroutine match_keyword: called CC=3 SCAR=11.0
Pod undocumented
127 27 my $header = shift;
128 27 my $keyword = shift;
129  
130 # strip leading and trailing whitespace
131 27 $header =~ s/^\s*//;
132 27 $header =~ s/\s*$//;
133  
134 27 my @combinations = parse_keyword($keyword);
135 27 for my $combination (@combinations) {
136 37 ▶ if ( compare_headers( $combination, $header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (compare_headers($combination, $header)) 13 24 37
137 13 return 1;
138 }
139 }
140 14 return 0;
141 }
142  
143  
144 4 ▶sub scpi_parse {
Subroutine scpi_parse: called CC=2 SCAR=7.0
Pod documented
145 4 my $str = shift;
146 4 my $d = shift;
147 4 ▶ $d = {} unless defined($d);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 4 0 4
148 4 _gMem( $str, 0, $d, $d );
149 4 return $d;
150 }
151  
152 # "get Mnemonic"
153 # recursive parse _gMem(string,level,treetop,treebranch)
154 # level = 0 is the top of the tree, descend as elements
155 # of the scpi command are parsed: :lev0:lev1:lev2;lev2;lev2:lev3;lev3 ...
156  
157 105 ▶sub _gMem {
Subroutine _gMem: called CC=22 SCAR=34.7
158 105 my $str = shift;
159 105 my $level = shift;
160 105 my $dtop = shift;
161 105 my $d = shift;
162  
163 105 ▶ if ( $str =~ /^${WS}*(;|\s*$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^$Lab::SCPI::WS*(;|\s*$)/u) 0 105 105
164 0 return '';
165 }
166  
167 105 while (1) {
168 284 $str =~ s/^${WS}*//;
169 284 ▶ last if $str =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*$/u 102 182 284
170  
171 182 ▶ if ( $level == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0) { } 35 147 182
172 35 ▶ if ( $str =~ /^(\*\w+\??)${WS}*(;|\s*$)/i ) { #common
Branch: 3 branches
BranchTrueFalseTotal
if ($str =~ /^(\*\w+\??)$Lab::SCPI::WS*(;|\s*$)/iu) { } 0 35 35
elsif ($str =~ /^(\*\w+\??)$Lab::SCPI::WS+/iu) { } 1 34 35
elsif ($str =~ /^:/u) { } 26 8 34
173 0 ▶ $dtop->{$1} = {} unless exists $dtop->{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $dtop->{$1} 0 0 0
174 0 $str = $POSTMATCH;
175 0 next;
176 }
177 elsif ( $str =~ /^(\*\w+\??)${WS}+/i ) { # common with params
178 1 ▶ $dtop->{$1} = {} unless exists $dtop->{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $dtop->{$1} 1 0 1
179 1 $str = _scpi_value( $POSTMATCH, $dtop->{$1} );
180 1 ▶ if ( $str =~ /^${WS}*(;|\s*$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^$Lab::SCPI::WS*(;|\s*$)/u) { } 1 0 1
181 1 $str = $POSTMATCH;
182 1 next;
183 }
184 else {
185 0 croak("parse error after common command");
186 }
187 }
188 elsif ( $str =~ /^:/ ) { # leading :
189 26 $d = $dtop;
190 26 $str =~ s/^://;
191 }
192 }
193 else {
194 147 ▶ if ( $str =~ /^\*/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\*/u) 0 147 147
195 0 croak("common command on level>0");
196 }
197 147 ▶ if ( $str =~ /^:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^:/u) 0 147 147
198 0 croak("leading : on level > 0");
199 }
200 }
201  
202 181 $str =~ s/^${WS}*//;
203 181 ▶ last if $str =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*$/u 0 181 181
204  
205 181 ▶ if ( $str =~ /^;/ ) { # another branch, same or top level
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^;/u) 77 104 181
206 77 $str =~ s/^;${WS}*//;
207 77 ▶ last if $str =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*$/u 3 74 77
208 74 my $nlev = $level;
209 74 ▶ $nlev = 0 if $str =~ /^[\*\:]/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^[\*\:]/u 23 51 74
210  
211 # print "level=$level nlev=$nlev str=$str\n";
212 74 $str = _gMem( $str, $nlev, $dtop, $d );
213 74 next;
214 }
215  
216 104 ▶ if ( $str =~ /^(\w+\??)${WS}*(;|\s*$)/i ) { # leaf, no params
Branch: 3 branches
BranchTrueFalseTotal
if ($str =~ /^(\w+\??)$Lab::SCPI::WS*(;|\s*$)/iu) { } 0 104 104
elsif ($str =~ /^(\w+)$Lab::SCPI::WS*:/iu) { } 27 77 104
elsif ($str =~ /^(\w+\??)$Lab::SCPI::WS+/iu) { } 77 0 77
217 0 ▶ $d->{$1} = {} unless exists $d->{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $d->{$1} 0 0 0
218 0 return $POSTMATCH;
219 }
220 elsif ( $str =~ /^(\w+)${WS}*:/i ) { # branch, go down a level
221 27 ▶ $d->{$1} = {} unless exists $d->{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $d->{$1} 21 6 27
222 27 $str = _gMem( $POSTMATCH, $level + 1, $dtop, $d->{$1} );
223 }
224 elsif ( $str =~ /^(\w+\??)${WS}+/i ) { # leaf with params
225 77 ▶ $d->{$1} = {} unless exists $d->{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $d->{$1} 77 0 77
226 77 $str = $POSTMATCH;
227 77 $str = _scpi_value( $str, $d->{$1} );
228 }
229 else {
230 0 croak("parse error on '$str'");
231 }
232 }
233 105 return $str;
234 }
235  
236 154 ▶sub _scpi_value {
Subroutine _scpi_value: called CC=13 SCAR=28.7
237 154 my $str = shift;
238 154 my $d = shift;
239  
240 154 $d->{_VALUE} = '';
241 154 my $lastsp = 0;
242 154 ▶ while ( $str !~ /^${WS}*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /^$Lab::SCPI::WS*$/u) 325 0 325
243 325 $str =~ s/^${WS}*//;
244  
245 325 ▶ if ( $str =~ /^;/ ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($str =~ /^;/u) { } 154 171 325
elsif ($str =~ /^\#([1-9])/u) { } 1 170 171
elsif ($str =~ /^\#0/u) { } 0 170 170
elsif ($str =~ /^(\"(?:([^\"]+|\"\")*)\")$Lab::SCPI::WS*/u) { } 10 160 170
elsif ($str =~ /^(\'(?:([^\']+|\'\')*)\')$Lab::SCPI::WS*/u) { } 0 160 160
elsif ($str =~ /^([\w\-\+\.\%\!\#\~\=\*]+)$Lab::SCPI::WS*/iu) { } 160 0 160
246 154 ▶ $d->{_VALUE} =~ s/\s*$// if $lastsp;
Branch: 1 branch
BranchTrueFalseTotal
if $lastsp 153 1 154
247  
248 154 return $str;
249 }
250 elsif ( $str =~ /^\#([1-9])/ ) { # counted arbitrary
251 1 my $nnd = $1;
252 1 my $nd = substr( $str, 2, $nnd );
253 1 $d->{_VALUE} .= substr( $str, 0, $nd + 2 + $nnd );
254 1 ▶ if ( length($str) > $nd + 2 + $nnd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > $nd + 2 + $nnd) { } 1 0 1
255 1 $str = substr( $str, $nd + 2 + $nnd );
256 }
257 else {
258 0 $str = '';
259 }
260 1 $lastsp = 0;
261 }
262 elsif ( $str =~ /^\#0/ ) { #uncounted arbitrary
263 0 $d->{_VALUE} .= $str;
264 0 $str = '';
265 0 return $str;
266 }
267 elsif ( $str =~ /^(\"(?:([^\"]+|\"\")*)\")${WS}*/ )
268 { # double q string
269 10 $d->{_VALUE} .= $1 . ' ';
270 10 $str = $POSTMATCH;
271 10 $lastsp = 1;
272 }
273 elsif ( $str =~ /^(\'(?:([^\']+|\'\')*)\')${WS}*/ )
274 { # single q string
275 0 $d->{_VALUE} .= $1 . ' ';
276 0 $str = $POSTMATCH;
277 0 $lastsp = 1;
278 }
279 elsif ( $str =~ /^([\w\-\+\.\%\!\#\~\=\*]+)${WS}*/i )
280 { #words, numbers
281 160 $d->{_VALUE} .= $1 . ' ';
282 160 $str = $POSTMATCH;
283 160 $lastsp = 1;
284 }
285 else {
286 0 croak("parse error, parameter not matched with '$str'");
287 }
288 171 ▶ if ( $str =~ /^${WS}*,/ ) { #parameter separator
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^$Lab::SCPI::WS*,/u) 14 157 171
289 14 $str = $POSTMATCH;
290 14 ▶ $d->{_VALUE} =~ s/${WS}*$// if $lastsp;
Branch: 1 branch
BranchTrueFalseTotal
if $lastsp 14 0 14
291 14 $d->{_VALUE} .= ',';
292 14 $lastsp = 0;
293 }
294 }
295 0 ▶ $d->{_VALUE} =~ s/\s*$// if $lastsp;
Branch: 1 branch
BranchTrueFalseTotal
if $lastsp 0 0 0
296 0 return $str;
297 }
298  
299  
300 3 ▶sub scpi_parse_sequence {
Subroutine scpi_parse_sequence: called CC=27 SCAR=34.8
Pod documented
301 3 my $str = shift;
302 3 my $d = shift;
303 3 ▶ $d = [] unless defined($d);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 3 0 3
304  
305 3 $str =~ s/^${WS}+//;
306 3 ▶ $str = ':' . $str unless $str =~ /^[\*:]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /^[\*:]/u 0 3 3
307 3 ▶ $str = $str . ';' unless $str =~ /;$/; # :string; form
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /;$/u 1 2 3
308  
309 3 my (@cur) = ();
310 3 my $level = 0;
311  
312 3 while (1) {
313 91 $str =~ s/^${WS}+//;
314 91 ▶ if ( $str =~ /^;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^;/u) 79 12 91
315 79 $str =~ s/^;${WS}*//;
316 79 my $ttop = {};
317 79 my $t = $ttop;
318  
319 79 ▶ for ( my $j = 0; $j <= $#cur; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $#cur) 241 3 244
320 241 my $k = $cur[$j];
321 241 ▶ if ( $k eq '_VALUE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq '_VALUE') { } 76 165 241
322 76 $t->{$k} = $cur[ $j + 1 ];
323 76 last;
324 }
325 else {
326 165 $t->{$k} = undef;
327 165 ▶ $t->{$k} = {} if $j < $#cur;
Branch: 1 branch
BranchTrueFalseTotal
if $j < $#cur 162 3 165
328 165 $t = $t->{$k};
329 }
330 }
331 79 push( @{$d}, $ttop );
332 }
333  
334 91 ▶ last if $str =~ /^\s*;?\s*$/; # handle trailing newline too
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*;?\s*$/u 3 88 91
335  
336 # print "lev=$level str='$str'\n";
337 88 ▶ if ( $level == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0) 79 9 88
338  
339 # starting from prev command
340 79 ▶ if ( $str =~ /^\w/i ) { # prev A:b or A:b:_VALUE:v
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\w/iu) { } 52 27 79
341 52 pop(@cur);
342 52 my $v = pop(@cur);
343 52 ▶ if ( defined($v) && $v eq '_VALUE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and $v eq '_VALUE') { } 52 0 52
Condition33%1/3
defined $v and $v eq "_VALUE"
!ll&&!rl&&r
0052
MC/DC0%0/2
defined $v and $v eq "_VALUE"
defined $v $v eq "_VALUE"
Truth table33%1/3
A and B
A= defined $vB= $v eq "_VALUE"
ABresult
0X0
100
111
344 52 pop(@cur);
345 }
346 else {
347 0 ▶ push( @cur, $v ) if defined($v);
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
348 }
349 52 $level = 1;
350  
351 }
352 else {
353 27 ▶ if ( $str =~ /^:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^:/u) 26 1 27
354 26 $str =~ s/^:${WS}*//;
355 }
356 27 ▶ next if $str =~ /^\s*;?\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*;?\s*$/u 0 27 27
357 27 @cur = ();
358 27 ▶ if ( $str =~ /^(\*\w+\??)${WS}*;/i ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($str =~ /^(\*\w+\??)$Lab::SCPI::WS*;/iu) { } 1 26 27
elsif ($str =~ /^(\*\w+\??)$Lab::SCPI::WS+/iu) { } 0 26 26
elsif ($str =~ /^(\w+)$Lab::SCPI::WS*:/iu) { } 19 7 26
elsif ($str =~ /^(\w+\??)$Lab::SCPI::WS*;/iu) { } 0 7 7
elsif ($str =~ /^(\w+\??)$Lab::SCPI::WS*/iu) { } 7 0 7
359  
360 # common, no arg
361 1 push( @cur, $1 );
362 1 $str = ';' . $POSTMATCH;
363  
364 }
365 elsif ( $str =~ /^(\*\w+\??)${WS}+/i ) {
366  
367 # common, arguments
368 0 push( @cur, $1 );
369 0 my $tmp = {};
370 0 $str = _scpi_value( $POSTMATCH, $tmp );
371 0 push( @cur, '_VALUE' );
372 0 push( @cur, $tmp->{_VALUE} );
373  
374 }
375 elsif ( $str =~ /^(\w+)${WS}*:/i ) {
376  
377 # start of tree, more coming
378 19 push( @cur, $1 );
379 19 $str = $POSTMATCH;
380 19 $level = 1;
381  
382 }
383 elsif ( $str =~ /^(\w+\??)${WS}*;/i ) {
384  
385 # tree end
386 0 push( @cur, "$1" );
387 0 $str = ';' . $POSTMATCH;
388  
389 }
390 elsif ( $str =~ /^(\w+\??)${WS}*/i ) {
391  
392 # tree end, args
393 7 push( @cur, $1 );
394 7 my $tmp = {};
395 7 $str = _scpi_value( $POSTMATCH, $tmp );
396 7 push( @cur, '_VALUE' );
397 7 push( @cur, $tmp->{_VALUE} );
398  
399 }
400 else {
401 0 croak("parse error str='$str'");
402 }
403 }
404  
405 }
406 88 $str =~ s/^${WS}+//;
407 88 ▶ next if $str =~ /^\s*;?\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*;?\s*$/u 0 88 88
408  
409 88 ▶ if ( $level > 0 ) { # level > 0
Branch: 1 branch
BranchTrueFalseTotal
if ($level > 0) 80 8 88
410 80 ▶ if ( $str =~ /^[\*:]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^[\*:]/u) 0 80 80
411 0 croak("common|root at level > 0");
412 }
413 80 ▶ if ( $str =~ /^(\w+)${WS}*:/i ) { #down another level
Branch: 3 branches
BranchTrueFalseTotal
if ($str =~ /^(\w+)$Lab::SCPI::WS*:/iu) { } 9 71 80
elsif ($str =~ /^(\w+\??)$Lab::SCPI::WS*;/iu) { } 2 69 71
elsif ($str =~ /^(\w+\??)$Lab::SCPI::WS+/iu) { } 69 0 69
414 9 push( @cur, $1 );
415 9 $str = $POSTMATCH;
416  
417 # $level++;
418  
419 }
420 elsif ( $str =~ /^(\w+\??)${WS}*;/i ) { # end tree
421 2 push( @cur, $1 );
422 2 $str = ';' . $POSTMATCH;
423 2 $level = 0;
424  
425 }
426 elsif ( $str =~ /^(\w+\??)${WS}+/i ) { #arguments
427  
428 69 push( @cur, $1 );
429 69 my $tmp = {};
430 69 $str = _scpi_value( $POSTMATCH, $tmp );
431 69 push( @cur, '_VALUE' );
432 69 push( @cur, $tmp->{_VALUE} );
433 69 $level = 0;
434  
435 }
436 else {
437 0 croak("parse error str='$str'");
438 }
439 }
440  
441 }
442  
443 3 return $d;
444 }
445  
446  
447 98 ▶sub scpi_canon {
Subroutine scpi_canon: called CC=15 SCAR=28.0
Pod documented
448 98 my $h = shift;
449 98 my $override = shift;
450 98 my $top = shift;
451 98 ▶ $override = {} unless defined $override;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $override 24 74 98
452 98 ▶ $top = 1 unless defined $top;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $top 23 75 98
453 98 my $n = {};
454 98 my $s;
455  
456 98 foreach my $k ( keys( %{$h} ) ) {
457  
458 119 ▶ if ( $k eq '_VALUE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq '_VALUE') { } 44 75 119
459 44 $n->{$k} = $h->{$k};
460 }
461 else {
462 75 ▶ if ($top) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top) 29 46 75
463 29 ▶ if ( $k =~ /^(\*\w+\??)/i ) { #common
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^(\*\w+\??)/iu) 0 29 29
464 0 $n->{ uc($1) } = undef;
465 0 ▶ if ( defined( $h->{$k} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h->{$k}) 0 0 0
466 0 croak("common command with subcommand");
467 }
468 0 next;
469 }
470 }
471  
472 75 ▶ if ( $k =~ /^([a-z]\w*[a-z_])${WS}*(\d*)(\??)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^([a-z]\w*[a-z_])$Lab::SCPI::WS*(\d*)(\??)/iu) { } 75 0 75
473 75 my $m = $1;
474 75 my $num = $2;
475 75 ▶ $num = '' unless defined $num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 0 75 75
476 75 my $q = $3;
477 75 ▶ $q = '' unless defined $q;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $q 0 75 75
478  
479 75 my $ov = 0;
480 75 foreach my $ko ( keys( %{$override} ) ) {
481 214 my $shorter = $ko;
482 214 $shorter =~ s/[a-z]\w*$//;
483 214 ▶ if ( uc($ko) eq uc($m) || $shorter eq uc($m) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $ko eq uc $m or $shorter eq uc $m) 49 165 214
Condition66%2/3
uc $ko eq uc $m or $shorter eq uc $m
l!l&&r!l&&!r
490165
MC/DC50%1/2
uc $ko eq uc $m or $shorter eq uc $m
uc $ko eq uc $m $shorter eq uc $m
Truth table66%2/3
A or B
A= uc $ko eq uc $mB= $shorter eq uc $m
ABresult
1X1
011
000
484 49 $m = $shorter;
485 49 $s = "$m$num$q";
486 $n->{$s}
487 49 = scpi_canon( $h->{$k}, $override->{$ko}, 0 );
488 49 $ov = 1;
489 49 last;
490 }
491 }
492 75 ▶ next if $ov;
Branch: 1 branch
BranchTrueFalseTotal
if $ov 49 26 75
493  
494 26 $s = uc( scpi_shortform($m) ) . $num . $q;
495 $n->{$s}
496 26 = scpi_canon( $h->{$k}, {}, 0 ); # no override lower too
497 }
498 else {
499 0 croak("parse error, mnemonic '$k'");
500 }
501  
502 }
503  
504 }
505 98 return $n;
506 }
507  
508  
509 23 ▶sub scpi_flat {
Subroutine scpi_flat: called CC=4 SCAR=20.5
Pod documented
510 23 my $h = shift;
511 23 my $ov = shift;
512  
513 23 ▶ if ( ref($h) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $h eq 'HASH') { } 23 0 23
elsif (ref $h eq 'ARRAY') { } 0 0 0
514 23 my $f = {};
515 23 my $c = scpi_canon( $h, $ov );
516 23 _scpi_fnode( '', $f, $c );
517 23 return $f;
518 }
519 elsif ( ref($h) eq 'ARRAY' ) {
520 0 my $fa = [];
521 0 foreach my $hx ( @{$h} ) {
522 0 my $f = {};
523 0 my $c = scpi_canon( $hx, $ov );
524 0 _scpi_fnode( '', $f, $c );
525 0 push( @{$fa}, $f );
526 }
527 0 return $fa;
528 }
529 else {
530 0 croak( "wrong type passed to scpi_flat:" . ref($h) );
531 }
532  
533 }
534  
535 142 ▶sub _scpi_fnode {
Subroutine _scpi_fnode: called CC=5 SCAR=16.1
536 142 my $fk = shift;
537 142 my $f = shift;
538 142 my $h = shift;
539  
540 142 my (@keys);
541 142 ▶ if ( ref($h) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h eq '') { } 44 98 142
542 44 $fk =~ s/\:_VALUE$//;
543 44 $f->{$fk} = $h;
544 44 return;
545 }
546 else {
547 98 @keys = keys( %{$h} );
548 98 ▶ if (@keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) { } 98 0 98
549 98 ▶ $fk .= ':' if $fk ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $fk ne "" 75 23 98
550 98 foreach my $k (@keys) {
551 119 _scpi_fnode( "$fk$k", $f, $h->{$k} );
552 }
553 }
554 else {
555 0 $f->{$fk} = undef;
556 }
557 }
558 }
559  
560 5 1;
561  
562 __END__
« XPRESS/Sweep/Time.pm ↑ Summary Instrument/TemperatureControl/TLK43.pm »