Text/Shellwords/Cursor.pm

filter: Statementstmt 65.3% 113 / 173 Branchbran 53.2% 65 / 122 Conditioncond 59.2% 32 / 54 MC/DCmcdc 52.7% 19 / 36 Subroutinesub 75.0% 6 / 8 Podpod 100% 6 / 6 totaltotal 60.4% 241 / 399 CC 82 SCAR 48.3
CC
82
Cov
60%
parsel
154.4
join_line
72.0
parse_escape
56.0
CRAP 125.6
« Term/GDBUI.pm ↑ Summary
1 # Text::Shellwords::Cursor.pm
2 # Scott Bronson
3 # 27 Jan 2003
4  
5 package Text::Shellwords::Cursor;
6  
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 $VERSION = '0.81';
11  
12 =head1 NAME
13  
14 Text::Shellwords::Cursor - Parse a string into tokens
15  
16 =head1 SYNOPSIS
17  
18 use Text::Shellwords::Cursor;
19 my $parser = Text::Shellwords::Cursor->new();
20 my $str = 'ab cdef "ghi" j"k\"l "';
21 my ($tok1) = $parser->parse_line($str);
22 $tok1 = ['ab', 'cdef', 'ghi', 'j', 'k"l ']
23 my ($tok2, $tokno, $tokoff) = $parser->parse_line($str, cursorpos => 6);
24 as above, but $tokno=1, $tokoff=3 (under the 'f')
25  
26 DESCRIPTION
27  
28 This module is very similar to Text::Shellwords and Text::ParseWords.
29 However, it has one very significant difference: it keeps track of
30 a character position in the line it's parsing. For instance, if you
31 pass it ("zq fmgb", cursorpos=>6), it would return
32 (['zq', 'fmgb'], 1, 3). The cursorpos parameter
33 tells where in the input string the cursor resides
34 (just before the 'b'), and the result tells you that
35 the cursor was on token 1 ('fmgb'), character 3 ('b').
36 This is very useful when computing command-line completions
37 involving quoting, escaping, and tokenizing characters (like '(' or '=').
38  
39 A few helper utilities are included as well. You can escape a string to
40 ensure that parsing it will produce the original string (L<parse_escape>).
41 You can also reassemble the tokens with a visually pleasing amount of
42 whitespace between them (L<join_line>).
43  
44 This module started out as an integral part of Term::GDBUI using
45 code loosely based on Text::ParseWords. However,
46 it is now basically a ground-up reimplementation. It was
47 split out of Term::GDBUI for version 0.8.
48  
49 =head1 METHODS
50  
51 =over 3
52  
53 =item new
54  
55 Creates a new parser. Takes named arguments on the command line.
56  
57 =over 4
58  
59 =item keep_quotes
60  
61 Normally all unescaped, unnecessary quote marks are stripped.
62 If you specify C<keep_quotes=E<gt>1>, however, they are preserved.
63 This is useful if you need to know whether the string was quoted
64 or not (string constants) or what type of quotes was around it
65 (affecting variable interpolation, for instance).
66  
67 =item token_chars
68  
69 This argument specifies the characters that should be considered
70 tokens all by themselves. For instance, if I pass
71 token_chars=>'=', then 'ab=123' would be parsed to ('ab', '=', '123').
72 Without token_chars, 'ab=123' remains a single string.
73  
74 NOTE: you cannot change token_chars after the constructor has been
75 called! The regexps that use it are compiled once (m//o).
76 Also, until the Gnu Readline library can accept "=[]," without
77 diving into an endless loop, we will not tell history expansion
78 to use token_chars (it uses " \t\n()<>;&|" by default).
79  
80 =item debug
81  
82 Turns on rather copious debugging to try to show what the parser is
83 thinking at every step.
84  
85 =item space_none
86  
87 =item space_before
88  
89 =item space_after
90  
91 These variables affect how whitespace in the line is normalized and
92 it is reassembled into a string. See the L<join_line> routine.
93  
94 =item error
95  
96 This is a reference to a routine that should be called to display
97 a parse error. The routine takes two arguments: a reference to the
98 parser, and the error message to display as a string.
99  
100 =cut
101  
102 sub new
103 2 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
104 2 my $type = shift;
105 2 bless {
106 keep_quotes => 0,
107 token_chars => '',
108 debug => 0,
109 space_none => '(',
110 space_before => '[{',
111 space_after => ',)]}',
112 error => undef,
113 @_
114 }, $type
115 }
116  
117 =item parsebail(msg)
118  
119 If the parsel routine or any of its subroutines runs into a fatal
120 error, they call parsebail to present a very descriptive diagnostic.
121  
122 =cut
123  
124 sub parsebail
125 2 ▶{
Subroutine parsebail: called CC=1 SCAR=0.0
Pod documented
126 2 my $self = shift;
127 2 my $msg = shift;
128  
129 2 die "$msg at char " . pos() . ":\n",
130 " $_\n " . (' ' x pos()) . '^' . "\n";
131  
132 }
133  
134  
135 =item parsel
136  
137 This is the heinous routine that actually does the parsing.
138 You should never need to call it directly. Call
139 L<parse_line|/"parse_line(line, named args)">
140 instead.
141  
142 =cut
143  
144 sub parsel
145 65 ▶{
Subroutine parsel: called CC=66 SCAR=50.4
Pod documented
146 65 my $self = shift;
147 65 $_ = shift;
148 65 my $cursorpos = shift;
149 65 my $fixclosequote = shift;
150  
151 65 my $deb = $self->{debug};
152 65 my $tchrs = $self->{token_chars};
153  
154 65 ▶ my $usingcp = (defined($cursorpos) && $cursorpos ne '');
Condition66%2/3
defined $cursorpos && $cursorpos ne ''
!ll&&!rl&&r
01055
MC/DC50%1/2
defined $cursorpos && $cursorpos ne ''
defined $cursorpos $cursorpos ne ''
Truth table66%2/3
A && B
A= defined $cursorposB= $cursorpos ne ''
ABresult
0X0
100
111
155 65 my $tokno = undef;
156 65 my $tokoff = undef;
157 65 my $oldpos;
158  
159 65 my @pieces = ();
160  
161 # Need to special case the empty string. None of the patterns below
162 # will match it yet we need to return an empty token for the cursor.
163 65 ▶ return ([''], 0, 0) if $usingcp && $_ eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $usingcp and $_ eq "" 1 64 65
Condition100%3/3
$usingcp and $_ eq ""
!ll&&!rl&&r
10541
MC/DC100%2/2
$usingcp and $_ eq ""
$usingcp $_ eq ""
Truth table100%3/3
A and B
A= $usingcpB= $_ eq ""
ABresult
0X0
100
111
164  
165 64 /^/gc; # force scanning to the beginning of the line
166  
167 64 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
pos $_ >= length $_ or do { do { $deb and print '-- top, pos=' . pos($_) . ($usingcp ? " cursorpos=$cursorpos" : "") . "\n"; if (/\G(\s+)/cg) { $deb and print 'trimmed ' . length($1) . ' whitespace chars, ' . ($usingcp ? "cursorpos=$cursorpos" : "") . "\n"; if ($usingcp and pos $_ >= $cursorpos) { unless (pos $_ == $cursorpos and pos $_ < length $_) { $deb and print "adding bogus token to handle cursor.\n"; push @pieces, ""; $tokno = $#pieces; $tokoff = 0; $usingcp = 0; } ; } ; } ; $oldpos = pos $_; if (/\G(['"])/cg) { my $quote = $1; my $adjust = 0; my $s; $deb and print "Found open quote [$quote] oldpos=$oldpos\n"; if ($usingcp and pos($_) - 1 < $cursorpos) { $deb and print ' lead quote increment pos=' . pos($_) . " cursorpos=$cursorpos\n"; $adjust += 1; } ; if ($quote eq "\"") { if (/\G((?:\\.|(?!["])[^\\])*)["]/cg) { $s = $1; } else { unless ($fixclosequote) { pos $_ -= 1; $self->parsebail("need closing quote [\"]"); } ; /\G(.*)$/cg; $s = $1; if ($usingcp and pos $_ == $cursorpos) { $adjust -= 1; } ; } ; $deb and print qq[ quoted string is "$s"\n]; while ($s =~ /\\./g) { my $ps = pos($s) - 2; $deb and print " doing substr at $ps on '${s}' oldpos=$oldpos adjust=$adjust\n"; $adjust += 1 if $usingcp and $ps < $cursorpos - $oldpos - $adjust; substr($s, $ps, 1) = ""; pos $s = $ps + 1; $deb and print " s='${s}' usingcp=$usingcp pos(s)=" . pos($s) . " cursorpos=$cursorpos oldpos=$oldpos adjust=$adjust\n"; } ; } else { if (/\G((?:\\.|(?!['])[^\\])*)[']/cg) { $s = $1; } else { unless ($fixclosequote) { pos $_ -= 1; $self->parsebail("need closing quote [']"); } ; /\G(.*)$/cg; $s = $1; if ($usingcp and pos $_ == $cursorpos) { $adjust -= 1; } ; } ; $deb and print " quoted string is '${s}'\n"; while ($s =~ /\\[\\']/g) { my $ps = pos($s) - 2; $deb and print " doing substr at $ps on '${s}' oldpos=$oldpos adjust=$adjust\n"; $adjust += 1 if $usingcp and $ps < $cursorpos - $oldpos - $adjust; substr($s, $ps, 1) = ""; pos $s = $ps + 1; $deb and print " s='${s}' usingcp=$usingcp pos(s)=" . pos($s) . " cursorpos=$cursorpos oldpos=$oldpos adjust=$adjust\n"; } ; } ; if ($usingcp and pos $_ == $cursorpos) { $deb and print ' trail quote increment pos=' . pos($_) . " cursorpos=$cursorpos\n"; $adjust += 1; } ; $deb and print " Found close, pushing '${s}' oldpos=$oldpos\n"; if ($self->{"keep_quotes"}) { $adjust -= 1; $s = $quote . $s . $quote; } ; push @pieces, $s; if ($usingcp and pos $_ >= $cursorpos) { $tokno = $#pieces; $tokoff = $cursorpos - $oldpos - $adjust; $usingcp = 0; } ; } ; $oldpos = pos $_; if (/\G((?:\\.|[^\s\\"'\Q$tchrs\E])+)/cog) { my $s = $1; my $adjust = 0; $deb and print "Found unquoted string '${s}'\n"; while ($s =~ /\\./g) { my $ps = pos($s) - 2; $deb and print " doing substr at $ps on '${s}' oldpos=$oldpos adjust=$adjust\n"; $adjust += 1 if $usingcp and $ps < $cursorpos - $oldpos - $adjust; substr($s, $ps, 1) = ""; pos $s = $ps + 1; $deb and print " s='${s}' usingcp=$usingcp pos(s)=" . pos($s) . " cursorpos=$cursorpos oldpos=$oldpos adjust=$adjust\n"; } ; $deb and print " pushing '${s}'\n"; push @pieces, $s; if ($usingcp and pos $_ >= $cursorpos) { $tokno = $#pieces; $tokoff = $cursorpos - $oldpos - $adjust; $usingcp = 0; } ; } ; if (length $tchrs and /\G([\Q$tchrs\E])/cog) { my $s = $1; $deb and print " pushing '${s}'\n"; push @pieces, $s; if ($usingcp and pos $_ == $cursorpos) { $tokno = $#pieces; $tokoff = 0; $usingcp = 0; } ; } } } 62 36 98
168 100 ▶ $deb && print "-- top, pos=" . pos() .
Branch: 2 branches
BranchTrueFalseTotal
$deb and print '-- top, pos=' . pos($_) . ($usingcp ? " cursorpos=$cursorpos" : "") . "\n" 0 100 100
$usingcp ? : 0 0 0
169 ($usingcp ? " cursorpos=$cursorpos" : "") . "\n";
170  
171 # trim whitespace from the beginning
172 100 ▶ if(/\G(\s+)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G(\s+)/cg) 54 46 100
173 54 ▶ $deb && print "trimmed " . length($1) . " whitespace chars, " .
Branch: 2 branches
BranchTrueFalseTotal
$deb and print 'trimmed ' . length($1) . ' whitespace chars, ' . ($usingcp ? "cursorpos=$cursorpos" : "") . "\n" 0 54 54
$usingcp ? : 0 0 0
174 ($usingcp ? "cursorpos=$cursorpos" : "") . "\n";
175 # if pos passed cursorpos, then we know that the cursor was
176 # surrounded by ws and we need to create an empty token for it.
177 54 ▶ if($usingcp && (pos() >= $cursorpos)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usingcp and pos $_ >= $cursorpos) 12 42 54
Condition100%3/3
$usingcp and pos $_ >= $cursorpos
!ll&&!rl&&r
321012
MC/DC100%2/2
$usingcp and pos $_ >= $cursorpos
$usingcp pos $_ >= $cursorpos
Truth table100%3/3
A and B
A= $usingcpB= pos $_ >= $cursorpos
ABresult
0X0
100
111
178 # if pos == cursorpos and we're not yet at EOL, let next token accept cursor
179 12 ▶ unless(pos() == $cursorpos && pos() < length($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (pos $_ == $cursorpos and pos $_ < length $_) 8 4 12
Condition100%3/3
pos $_ == $cursorpos and pos $_ < length $_
!ll&&!rl&&r
714
MC/DC100%2/2
pos $_ == $cursorpos and pos $_ < length $_
pos $_ == $cursorpos pos $_ < length $_
Truth table100%3/3
A and B
A= pos $_ == $cursorposB= pos $_ < length $_
ABresult
0X0
100
111
180 # need to special-case at end-of-line as there are no more tokens
181 # to take care of the cursor so we must create an empty one.
182 8 ▶ $deb && print "adding bogus token to handle cursor.\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print "adding bogus token to handle cursor.\n" 0 8 8
183 8 push @pieces, '';
184 8 $tokno = $#pieces;
185 8 $tokoff = 0;
186 8 $usingcp = 0;
187 }
188 }
189 }
190  
191 # if there's a quote, then suck to the close quote
192 100 $oldpos = pos();
193 100 ▶ if(/\G(['"])/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G(['"])/cg) 40 60 100
194 40 my $quote = $1;
195 40 my $adjust = 0; # keeps track of tokoff bumps due to subs, etc.
196 40 my $s;
197  
198 40 ▶ $deb && print "Found open quote [$quote] oldpos=$oldpos\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print "Found open quote [$quote] oldpos=$oldpos\n" 0 40 40
199  
200 # adjust tokoff unless the cursor sits directly on the open quote
201 40 ▶ if($usingcp && pos()-1 < $cursorpos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usingcp and pos($_) - 1 < $cursorpos) 31 9 40
Condition100%3/3
$usingcp and pos($_) - 1 < $cursorpos
!ll&&!rl&&r
5431
MC/DC100%2/2
$usingcp and pos($_) - 1 < $cursorpos
$usingcp pos($_) - 1 < $cursorpos
Truth table100%3/3
A and B
A= $usingcpB= pos($_) - 1 < $cursorpos
ABresult
0X0
100
111
202 31 ▶ $deb && print " lead quote increment pos=".pos()." cursorpos=$cursorpos\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print ' lead quote increment pos=' . pos($_) . " cursorpos=$cursorpos\n" 0 31 31
203 31 $adjust += 1;
204 }
205  
206 40 ▶ if($quote eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($quote eq '"') { } 20 20 40
207 20 ▶ if(/\G((?:\\.|(?!["])[^\\])*)["]/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G((?:\\.|(?!["])[^\\])*)["]/cg) { } 19 1 20
208 19 $s = $1; # string without quotes
209 } else {
210 1 ▶ unless($fixclosequote) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fixclosequote) 1 0 1
211 1 pos() -= 1;
212 1 $self->parsebail("need closing quote [\"]");
213 }
214 0 /\G(.*)$/gc; # if no close quote, just suck to the end of the string
215 0 $s = $1; # string without quotes
216 0 ▶ if($usingcp && pos() == $cursorpos) { $adjust -= 1; } # make cursor think cq was there
Branch: 1 branch
BranchTrueFalseTotal
if ($usingcp and pos $_ == $cursorpos) 0 0 0
Condition0%0/3
$usingcp and pos $_ == $cursorpos
!ll&&!rl&&r
000
MC/DC0%0/2
$usingcp and pos $_ == $cursorpos
$usingcp pos $_ == $cursorpos
Truth table0%0/3
A and B
A= $usingcpB= pos $_ == $cursorpos
ABresult
0X0
100
111
217 }
218 19 ▶ $deb && print " quoted string is \"$s\"\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print qq[ quoted string is "$s"\n] 0 19 19
219 19 ▶ while($s =~ /\\./g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\\./g) 14 19 33
220 14 my $ps = pos($s) - 2; # points to the start of the sub
221 14 ▶ $deb && print " doing substr at $ps on '$s' oldpos=$oldpos adjust=$adjust\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print " doing substr at $ps on '${s}' oldpos=$oldpos adjust=$adjust\n" 0 14 14
222 14 ▶ $adjust += 1 if $usingcp && $ps < $cursorpos - $oldpos - $adjust;
Branch: 1 branch
BranchTrueFalseTotal
if $usingcp and $ps < $cursorpos - $oldpos - $adjust 9 5 14
Condition66%2/3
$usingcp and $ps < $cursorpos - $oldpos - $adjust
!ll&&!rl&&r
059
MC/DC50%1/2
$usingcp and $ps < $cursorpos - $oldpos - $adjust
$usingcp $ps < $cursorpos - $oldpos - $adjust
Truth table66%2/3
A and B
A= $usingcpB= $ps < $cursorpos - $oldpos - $adjust
ABresult
0X0
100
111
223 14 substr($s, $ps, 1) = '';
224 14 pos($s) = $ps + 1;
225 14 ▶ $deb && print " s='$s' usingcp=$usingcp pos(s)=" . pos($s) . " cursorpos=$cursorpos oldpos=$oldpos adjust=$adjust\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print " s='${s}' usingcp=$usingcp pos(s)=" . pos($s) . " cursorpos=$cursorpos oldpos=$oldpos adjust=$adjust\n" 0 14 14
226 }
227 } else {
228 20 ▶ if(/\G((?:\\.|(?!['])[^\\])*)[']/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G((?:\\.|(?!['])[^\\])*)[']/cg) { } 19 1 20
229 19 $s = $1; # string without quotes
230 } else {
231 1 ▶ unless($fixclosequote) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fixclosequote) 1 0 1
232 1 pos() -= 1;
233 1 $self->parsebail("need closing quote [']");
234 }
235 0 /\G(.*)$/gc; # if no close quote, just suck to the end of the string
236 0 $s = $1;
237 0 ▶ if($usingcp && pos() == $cursorpos) { $adjust -= 1; } # make cursor think cq was there
Branch: 1 branch
BranchTrueFalseTotal
if ($usingcp and pos $_ == $cursorpos) 0 0 0
Condition0%0/3
$usingcp and pos $_ == $cursorpos
!ll&&!rl&&r
000
MC/DC0%0/2
$usingcp and pos $_ == $cursorpos
$usingcp pos $_ == $cursorpos
Truth table0%0/3
A and B
A= $usingcpB= pos $_ == $cursorpos
ABresult
0X0
100
111
238 }
239 19 ▶ $deb && print " quoted string is '$s'\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print " quoted string is '${s}'\n" 0 19 19
240 19 ▶ while($s =~ /\\[\\']/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\\[\\']/g) 12 19 31
241 12 my $ps = pos($s) - 2; # points to the start of the sub
242 12 ▶ $deb && print " doing substr at $ps on '$s' oldpos=$oldpos adjust=$adjust\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print " doing substr at $ps on '${s}' oldpos=$oldpos adjust=$adjust\n" 0 12 12
243 12 ▶ $adjust += 1 if $usingcp && $ps < $cursorpos - $oldpos - $adjust;
Branch: 1 branch
BranchTrueFalseTotal
if $usingcp and $ps < $cursorpos - $oldpos - $adjust 7 5 12
Condition66%2/3
$usingcp and $ps < $cursorpos - $oldpos - $adjust
!ll&&!rl&&r
057
MC/DC50%1/2
$usingcp and $ps < $cursorpos - $oldpos - $adjust
$usingcp $ps < $cursorpos - $oldpos - $adjust
Truth table66%2/3
A and B
A= $usingcpB= $ps < $cursorpos - $oldpos - $adjust
ABresult
0X0
100
111
244 12 substr($s, $ps, 1) = '';
245 12 pos($s) = $ps + 1;
246 12 ▶ $deb && print " s='$s' usingcp=$usingcp pos(s)=" . pos($s) . " cursorpos=$cursorpos oldpos=$oldpos adjust=$adjust\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print " s='${s}' usingcp=$usingcp pos(s)=" . pos($s) . " cursorpos=$cursorpos oldpos=$oldpos adjust=$adjust\n" 0 12 12
247 }
248 }
249  
250 # adjust tokoff if the cursor if it sits directly on the close quote
251 38 ▶ if($usingcp && pos() == $cursorpos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usingcp and pos $_ == $cursorpos) 8 30 38
Condition100%3/3
$usingcp and pos $_ == $cursorpos
!ll&&!rl&&r
3278
MC/DC100%2/2
$usingcp and pos $_ == $cursorpos
$usingcp pos $_ == $cursorpos
Truth table100%3/3
A and B
A= $usingcpB= pos $_ == $cursorpos
ABresult
0X0
100
111
252 8 ▶ $deb && print " trail quote increment pos=".pos()." cursorpos=$cursorpos\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print ' trail quote increment pos=' . pos($_) . " cursorpos=$cursorpos\n" 0 8 8
253 8 $adjust += 1;
254 }
255  
256 38 ▶ $deb && print " Found close, pushing '$s' oldpos=$oldpos\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print " Found close, pushing '${s}' oldpos=$oldpos\n" 0 38 38
257 38 ▶ if($self->{keep_quotes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"keep_quotes"}) 0 38 38
258 0 $adjust -= 1; # need to move right 1 for opening quote
259 0 $s = $quote.$s.$quote;
260 }
261 38 push @pieces, $s;
262  
263 # Set tokno and tokoff if this token contained the cursor
264 38 ▶ if($usingcp && pos() >= $cursorpos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usingcp and pos $_ >= $cursorpos) 34 4 38
Condition100%3/3
$usingcp and pos $_ >= $cursorpos
!ll&&!rl&&r
3134
MC/DC100%2/2
$usingcp and pos $_ >= $cursorpos
$usingcp pos $_ >= $cursorpos
Truth table100%3/3
A and B
A= $usingcpB= pos $_ >= $cursorpos
ABresult
0X0
100
111
265 # Previous block contains the cursor
266 34 $tokno = $#pieces;
267 34 $tokoff = $cursorpos - $oldpos - $adjust;
268 34 $usingcp = 0;
269 }
270 }
271  
272 # suck up as much unquoted text as we can
273 98 $oldpos = pos();
274 98 ▶ if(/\G((?:\\.|[^\s\\"'\Q$tchrs\E])+)/gco) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G((?:\\.|[^\s\\"'\Q$tchrs\E])+)/cog) 40 58 98
275 40 my $s = $1; # the unquoted string
276 40 my $adjust = 0; # keeps track of tokoff bumps due to subs, etc.
277  
278 40 ▶ $deb && print "Found unquoted string '$s'\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print "Found unquoted string '${s}'\n" 0 40 40
279 40 ▶ while($s =~ /\\./g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\\./g) 9 40 49
280 9 my $ps = pos($s) - 2; # points to the start of substitution
281 9 ▶ $deb && print " doing substr at $ps on '$s' oldpos=$oldpos adjust=$adjust\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print " doing substr at $ps on '${s}' oldpos=$oldpos adjust=$adjust\n" 0 9 9
282 9 ▶ $adjust += 1 if $usingcp && $ps < $cursorpos - $oldpos - $adjust;
Branch: 1 branch
BranchTrueFalseTotal
if $usingcp and $ps < $cursorpos - $oldpos - $adjust 3 6 9
Condition100%3/3
$usingcp and $ps < $cursorpos - $oldpos - $adjust
!ll&&!rl&&r
243
MC/DC100%2/2
$usingcp and $ps < $cursorpos - $oldpos - $adjust
$usingcp $ps < $cursorpos - $oldpos - $adjust
Truth table100%3/3
A and B
A= $usingcpB= $ps < $cursorpos - $oldpos - $adjust
ABresult
0X0
100
111
283 9 substr($s, $ps, 1) = '';
284 9 pos($s) = $ps + 1;
285 9 ▶ $deb && print " s='$s' usingcp=$usingcp pos(s)=" . pos($s) . " cursorpos=$cursorpos oldpos=$oldpos adjust=$adjust\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print " s='${s}' usingcp=$usingcp pos(s)=" . pos($s) . " cursorpos=$cursorpos oldpos=$oldpos adjust=$adjust\n" 0 9 9
286 }
287 40 ▶ $deb && print " pushing '$s'\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print " pushing '${s}'\n" 0 40 40
288 40 push @pieces, $s;
289  
290 # Set tokno and tokoff if this token contained the cursor
291 40 ▶ if($usingcp && pos() >= $cursorpos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usingcp and pos $_ >= $cursorpos) 12 28 40
Condition100%3/3
$usingcp and pos $_ >= $cursorpos
!ll&&!rl&&r
23512
MC/DC100%2/2
$usingcp and pos $_ >= $cursorpos
$usingcp pos $_ >= $cursorpos
Truth table100%3/3
A and B
A= $usingcpB= pos $_ >= $cursorpos
ABresult
0X0
100
111
292 # Previous block contains the cursor
293 12 $tokno = $#pieces;
294 12 $tokoff = $cursorpos - $oldpos - $adjust;
295 12 $usingcp = 0;
296 }
297 }
298  
299 98 ▶ if(length($tchrs) && /\G([\Q$tchrs\E])/gco) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tchrs and /\G([\Q$tchrs\E])/cog) 0 98 98
Condition33%1/3
length $tchrs and /\G([\Q$tchrs\E])/cog
!ll&&!rl&&r
9800
MC/DC0%0/2
length $tchrs and /\G([\Q$tchrs\E])/cog
length $tchrs /\G([\Q$tchrs\E])/cog
Truth table33%1/3
A and B
A= length $tchrsB= /\G([\Q$tchrs\E])/cog
ABresult
0X0
100
111
300 0 my $s = $1; # the token char
301 0 ▶ $deb && print " pushing '$s'\n";
Branch: 1 branch
BranchTrueFalseTotal
$deb and print " pushing '${s}'\n" 0 0 0
302 0 push @pieces, $s;
303  
304 0 ▶ if($usingcp && pos() == $cursorpos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usingcp and pos $_ == $cursorpos) 0 0 0
Condition0%0/3
$usingcp and pos $_ == $cursorpos
!ll&&!rl&&r
000
MC/DC0%0/2
$usingcp and pos $_ == $cursorpos
$usingcp pos $_ == $cursorpos
Truth table0%0/3
A and B
A= $usingcpB= pos $_ == $cursorpos
ABresult
0X0
100
111
305 # Previous block contains the cursor
306 0 $tokno = $#pieces;
307 0 $tokoff = 0;
308 0 $usingcp = 0;
309 }
310 }
311 } until(pos() >= length($_));
312  
313 62 ▶ $deb && print "Result: (", join(", ", @pieces), ") " .
Branch: 3 branches
BranchTrueFalseTotal
$deb and print "Result: (", join(", ", @pieces), ') ' . (defined $tokno ? $tokno : "undef") . ' ' . (defined $tokoff ? $tokoff : "undef") . "\n" 0 62 62
defined $tokno ? : 0 0 0
defined $tokoff ? : 0 0 0
314 (defined($tokno) ? $tokno : 'undef') . " " .
315 (defined($tokoff) ? $tokoff : 'undef') . "\n";
316  
317 62 return ([@pieces], $tokno, $tokoff);
318 }
319  
320  
321 =item parse_line(line, I<named args>)
322  
323 This is the entrypoint to this module's parsing functionality. It converts
324 a line into tokens, respecting quoted text, escaped characters,
325 etc. It also keeps track of a cursor position on the input text,
326 returning the token number and offset within the token where that position
327 can be found in the output.
328  
329 This routine originally bore some resemblance to Text::ParseWords.
330 It has changed almost completely, however, to support keeping track
331 of the cursor position. It also has nicer failure modes, modular
332 quoting, token characters (see token_chars in L</new>), etc. This
333 routine now does much more.
334  
335 Arguments:
336  
337 =over 3
338  
339 =item line
340  
341 This is a string containing the command-line to parse.
342  
343 =back
344  
345 This routine also accepts the following named parameters:
346  
347 =over 3
348  
349 =item cursorpos
350  
351 This is the character position in the line to keep track of.
352 Pass undef (by not specifying it) or the empty string to have
353 the line processed with cursorpos ignored.
354  
355 Note that passing undef is I<not> the same as passing
356 some random number and ignoring the result! For instance, if you
357 pass 0 and the line begins with whitespace, you'll get a 0-length token at
358 the beginning of the line to represent the cursor in
359 the middle of the whitespace. This allows command completion
360 to work even when the cursor is not near any tokens.
361 If you pass undef, all whitespace at the beginning and end of
362 the line will be trimmed as you would expect.
363  
364 If it is ambiguous whether the cursor should belong to the previous
365 token or to the following one (i.e. if it's between two quoted
366 strings, say "a""b" or a token_char), it always gravitates to
367 the previous token. This makes more sense when completing.
368  
369 =item fixclosequote
370  
371 Sometimes you want to try to recover from a missing close quote
372 (for instance, when calculating completions), but usually you
373 want a missing close quote to be a fatal error. fixclosequote=>1
374 will implicitly insert the correct quote if it's missing.
375 fixclosequote=>0 is the default.
376  
377 =item messages
378  
379 parse_line is capable of printing very informative error messages.
380 However, sometimes you don't care enough to print a message (like
381 when calculating completions). Messages are printed by default,
382 so pass messages=>0 to turn them off.
383  
384 =back
385  
386 This function returns a reference to an array containing three
387 items:
388  
389 =over 3
390  
391 =item tokens
392  
393 A the tokens that the line was separated into (ref to an array of strings).
394  
395 =item tokno
396  
397 The number of the token (index into the previous array) that contains
398 cursorpos.
399  
400 =item tokoff
401  
402 The character offet into tokno of cursorpos.
403  
404 =back
405  
406 If the cursor is at the end of the token, tokoff will point to 1
407 character past the last character in tokno, a non-existant character.
408 If the cursor is between tokens (surrounded by whitespace), a zero-length
409 token will be created for it.
410  
411 =cut
412  
413 sub parse_line
414 65 ▶{
Subroutine parse_line: called CC=4 SCAR=14.1
Pod documented
415 65 my $self = shift;
416 65 my $line = shift;
417 65 my %args = (
418 messages => 1, # true if we should print errors, etc.
419 cursorpos => undef, # cursor to keep track of, undef to ignore.
420 fixclosequote => 0,
421 @_
422 );
423  
424 65 my @result = eval { $self->parsel($line,
425 65 $args{'cursorpos'}, $args{'fixclosequote'}) };
426 65 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 63 65
427 2 ▶ $self->{error}->($self, $@) if $args{'messages'} && $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"messages"} and $self->{"error"} 0 2 2
Condition33%1/3
$args{"messages"} and $self->{"error"}
!ll&&!rl&&r
200
MC/DC0%0/2
$args{"messages"} and $self->{"error"}
$args{"messages"} $self->{"error"}
Truth table33%1/3
A and B
A= $args{"messages"}B= $self->{"error"}
ABresult
0X0
100
111
428 2 @result = (undef, undef, undef);
429 }
430  
431 65 return @result;
432 }
433  
434  
435 =item parse_escape(lines)
436  
437 Escapes characters that would be otherwise interpreted by the parser.
438 Will accept either a single string or an arrayref of strings (which
439 will be modified in-place).
440  
441 =cut
442  
443 sub parse_escape
444 0 ▶{
Subroutine parse_escape: not called CC=7 SCAR=40.3
Pod documented
445 0 my $self = shift;
446 0 my $arr = shift; # either a string or an arrayref of strings
447  
448 0 my $wantstr = 0;
449 0 ▶ if(ref($arr) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arr ne "ARRAY") 0 0 0
450 0 $arr = [$arr];
451 0 $wantstr = 1;
452 }
453  
454 0 foreach(@$arr) {
455 0 my $quote;
456 0 ▶ if($self->{keep_quotes} && /^(['"])(.*)\1$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"keep_quotes"} and /^(['"])(.*)\1$/) 0 0 0
Condition0%0/3
$self->{"keep_quotes"} and /^(['"])(.*)\1$/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"keep_quotes"} and /^(['"])(.*)\1$/
$self->{"keep_quotes"} /^(['"])(.*)\1$/
Truth table0%0/3
A and B
A= $self->{"keep_quotes"}B= /^(['"])(.*)\1$/
ABresult
0X0
100
111
457 0 ($quote, $_) = ($1, $2);
458 }
459 0 s/([ \\"'])/\\$1/g;
460 0 ▶ $_ = $quote.$_.$quote if $quote;
Branch: 1 branch
BranchTrueFalseTotal
if $quote 0 0 0
461 }
462  
463 0 ▶ return $wantstr ? $arr->[0] : $arr;
Branch: 1 branch
BranchTrueFalseTotal
$wantstr ? : 0 0 0
464 }
465  
466  
467 =item join_line(tokens)
468  
469 This routine does a somewhat intelligent job of joining tokens
470 back into a command line. If token_chars (see L</new>) is empty
471 (the default), then it just escapes backslashes and quotes, and
472 joins the tokens with spaces.
473  
474 However, if token_chars is nonempty, it tries to insert a visually
475 pleasing amount of space between the tokens. For instance, rather
476 than 'a ( b , c )', it tries to produce 'a (b, c)'. It won't reformat
477 any tokens that aren't found in $self->{token_chars}, of course.
478  
479 To change the formatting, you can redefine the variables
480 $self->{space_none}, $self->{space_before}, and $self->{space_after}.
481 Each variable is a string containing all characters that should
482 not be surrounded by whitespace, should have whitespace before,
483 and should have whitespace after, respectively. Any character
484 found in token_chars, but non in any of these space_ variables,
485 will have space placed both before and after.
486  
487 =cut
488  
489 sub join_line
490 0 ▶{
Subroutine join_line: not called CC=8 SCAR=42.8
Pod documented
491 0 my $self = shift;
492 0 my $intoks = shift;
493  
494 0 my $tchrs = $self->{token_chars};
495 0 my $s_none = $self->{space_none};
496 0 my $s_before = $self->{space_before};
497 0 my $s_after = $self->{space_after};
498  
499 # copy the input array so we don't modify it
500 0 my $tokens = $self->parse_escape([@$intoks]);
501  
502 0 my $str = '';
503 0 my $sw = 0; # space if space wanted after token.
504 0 my $sf = 0; # space if space should be forced after token.
505 0 for(@$tokens) {
506 0 ▶ if(length == 1 && index($tchrs,$_) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 1 and index($tchrs, $_) != -1) 0 0 0
Condition0%0/3
length $_ == 1 and index($tchrs, $_) != -1
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ == 1 and index($tchrs, $_) != -1
length $_ == 1 index($tchrs, $_) != -1
Truth table0%0/3
A and B
A= length $_ == 1B= index($tchrs, $_) != -1
ABresult
0X0
100
111
507 0 ▶ if(index($s_none,$_) >= 0) { $str .= $_; $sw=0; next; }
Branch: 1 branch
BranchTrueFalseTotal
if (index($s_none, $_) != -1) 0 0 0
508 0 ▶ if(index($s_before,$_) >= 0) { $str .= $sw.$_; $sw=0; next; }
Branch: 1 branch
BranchTrueFalseTotal
if (index($s_before, $_) != -1) 0 0 0
509 0 ▶ if(index($s_after,$_) >= 0) { $str .= $_; $sw=1; next; }
Branch: 1 branch
BranchTrueFalseTotal
if (index($s_after, $_) != -1) 0 0 0
510 # default: force space on both sides of operator.
511 0 $str .= " $_ "; $sw = 0; next;
512 }
513 0 ▶ $str .= ($sw ? ' ' : '') . $_;
Branch: 1 branch
BranchTrueFalseTotal
$sw ? : 0 0 0
514 0 $sw = 1;
515 }
516  
517 0 return $str;
518 }
519  
520  
521 =back
522  
523 =head1 BUGS
524  
525 None known.
526  
527 =head1 LICENSE
528  
529 Copyright (c) 2003 Scott Bronson, all rights reserved.
530 This program is free software; you can redistribute it and/or modify
531 it under the same terms as Perl itself.
532  
533 =head1 AUTHOR
534  
535 Scott Bronson E<lt>bronson@rinspin.comE<gt>
536  
537 =cut
538  
539 2 1;
« Term/GDBUI.pm ↑ Summary