| 1 | package Text::Balanced; | ||
| 2 | |||
| 3 | # EXTRACT VARIOUSLY DELIMITED TEXT SEQUENCES FROM STRINGS. | ||
| 4 | # FOR FULL DOCUMENTATION SEE Balanced.pod | ||
| 5 | |||
| 6 | use 5.005; | ||
| 7 | use strict; | ||
| 8 | use Exporter (); | ||
| 9 | # use SelfLoader; | ||
| 10 | |||
| 11 | use vars qw { $VERSION @ISA %EXPORT_TAGS }; | ||
| 12 | BEGIN { | ||
| 13 | $VERSION = '2.03'; | ||
| 14 | @ISA = 'Exporter'; | ||
| 15 | %EXPORT_TAGS = ( | ||
| 16 | ALL => [ qw{ | ||
| 17 | &extract_delimited | ||
| 18 | &extract_bracketed | ||
| 19 | &extract_quotelike | ||
| 20 | &extract_codeblock | ||
| 21 | &extract_variable | ||
| 22 | &extract_tagged | ||
| 23 | &extract_multiple | ||
| 24 | &gen_delimited_pat | ||
| 25 | &gen_extract_tagged | ||
| 26 | &delimited_pat | ||
| 27 | } ], | ||
| 28 | ); | ||
| 29 | } | ||
| 30 | |||
| 31 | Exporter::export_ok_tags('ALL'); | ||
| 32 | |||
| 33 | # PROTOTYPES | ||
| 34 | |||
| 35 | sub _match_bracketed($$$$$$); | ||
| 36 | sub _match_variable($$); | ||
| 37 | sub _match_codeblock($$$$$$$); | ||
| 38 | sub _match_quotelike($$$$); | ||
| 39 | |||
| 40 | # HANDLE RETURN VALUES IN VARIOUS CONTEXTS | ||
| 41 | |||
| 42 | sub _failmsg { | ||
| 43 | my ($message, $pos) = @_; | ||
| 44 | $@ = bless { | ||
| 45 | error => $message, | ||
| 46 | pos => $pos, | ||
| 47 | }, 'Text::Balanced::ErrorMsg'; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _fail { | ||
| 51 | my ($wantarray, $textref, $message, $pos) = @_; | ||
| 52 | _failmsg $message, $pos if $message; | ||
| 53 | return (undef, $$textref, undef) if $wantarray; | ||
| 54 | return undef; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _succeed { | ||
| 58 | $@ = undef; | ||
| 59 | my ($wantarray,$textref) = splice @_, 0, 2; | ||
| 60 | my ($extrapos, $extralen) = @_ > 18 | ||
| 61 | ? splice(@_, -2, 2) | ||
| 62 | : (0, 0); | ||
| 63 | my ($startlen, $oppos) = @_[5,6]; | ||
| 64 | my $remainderpos = $_[2]; | ||
| 65 | if ( $wantarray ) { | ||
| 66 | my @res; | ||
| 67 | while (my ($from, $len) = splice @_, 0, 2) { | ||
| 68 | push @res, substr($$textref, $from, $len); | ||
| 69 | } | ||
| 70 | if ( $extralen ) { # CORRECT FILLET | ||
| 71 | my $extra = substr($res[0], $extrapos-$oppos, $extralen, "\n"); | ||
| 72 | $res[1] = "$extra$res[1]"; | ||
| 73 | eval { substr($$textref,$remainderpos,0) = $extra; | ||
| 74 | substr($$textref,$extrapos,$extralen,"\n")} ; | ||
| 75 | #REARRANGE HERE DOC AND FILLET IF POSSIBLE | ||
| 76 | pos($$textref) = $remainderpos-$extralen+1; # RESET \G | ||
| 77 | } else { | ||
| 78 | pos($$textref) = $remainderpos; # RESET \G | ||
| 79 | } | ||
| 80 | return @res; | ||
| 81 | } else { | ||
| 82 | my $match = substr($$textref,$_[0],$_[1]); | ||
| 83 | substr($match,$extrapos-$_[0]-$startlen,$extralen,"") if $extralen; | ||
| 84 | my $extra = $extralen | ||
| 85 | ? substr($$textref, $extrapos, $extralen)."\n" : ""; | ||
| 86 | eval {substr($$textref,$_[4],$_[1]+$_[5])=$extra} ; #CHOP OUT PREFIX & MATCH, IF POSSIBLE | ||
| 87 | pos($$textref) = $_[4]; # RESET \G | ||
| 88 | return $match; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | # BUILD A PATTERN MATCHING A SIMPLE DELIMITED STRING | ||
| 93 | |||
| 94 | sub gen_delimited_pat($;$) # ($delimiters;$escapes) | ||
| 95 | { | ||
| 96 | my ($dels, $escs) = @_; | ||
| 97 | return "" unless $dels =~ /\S/; | ||
| 98 | $escs = '\\' unless $escs; | ||
| 99 | $escs .= substr($escs,-1) x (length($dels)-length($escs)); | ||
| 100 | my @pat = (); | ||
| 101 | my $i; | ||
| 102 | for ($i=0; $i<length $dels; $i++) | ||
| 103 | { | ||
| 104 | my $del = quotemeta substr($dels,$i,1); | ||
| 105 | my $esc = quotemeta substr($escs,$i,1); | ||
| 106 | if ($del eq $esc) | ||
| 107 | { | ||
| 108 | push @pat, "$del(?:[^$del]*(?:(?:$del$del)[^$del]*)*)$del"; | ||
| 109 | } | ||
| 110 | else | ||
| 111 | { | ||
| 112 | push @pat, "$del(?:[^$esc$del]*(?:$esc.[^$esc$del]*)*)$del"; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | my $pat = join '|', @pat; | ||
| 116 | return "(?:$pat)"; | ||
| 117 | } | ||
| 118 | |||
| 119 | *delimited_pat = \&gen_delimited_pat; | ||
| 120 | |||
| 121 | # THE EXTRACTION FUNCTIONS | ||
| 122 | |||
| 123 | sub extract_delimited (;$$$$) | ||
| 124 | { | ||
| 125 | my $textref = defined $_[0] ? \$_[0] : \$_; | ||
| 126 | my $wantarray = wantarray; | ||
| 127 | my $del = defined $_[1] ? $_[1] : qq{\'\"\`}; | ||
| 128 | my $pre = defined $_[2] ? $_[2] : '\s*'; | ||
| 129 | my $esc = defined $_[3] ? $_[3] : qq{\\}; | ||
| 130 | my $pat = gen_delimited_pat($del, $esc); | ||
| 131 | my $startpos = pos $$textref || 0; | ||
| 132 | return _fail($wantarray, $textref, "Not a delimited pattern", 0) | ||
| 133 | unless $$textref =~ m/\G($pre)($pat)/gc; | ||
| 134 | my $prelen = length($1); | ||
| 135 | my $matchpos = $startpos+$prelen; | ||
| 136 | my $endpos = pos $$textref; | ||
| 137 | return _succeed $wantarray, $textref, | ||
| 138 | $matchpos, $endpos-$matchpos, # MATCH | ||
| 139 | $endpos, length($$textref)-$endpos, # REMAINDER | ||
| 140 | $startpos, $prelen; # PREFIX | ||
| 141 | } | ||
| 142 | |||
| 143 | sub extract_bracketed (;$$$) | ||
| 144 | { | ||
| 145 | my $textref = defined $_[0] ? \$_[0] : \$_; | ||
| 146 | my $ldel = defined $_[1] ? $_[1] : '{([<'; | ||
| 147 | my $pre = defined $_[2] ? $_[2] : '\s*'; | ||
| 148 | my $wantarray = wantarray; | ||
| 149 | my $qdel = ""; | ||
| 150 | my $quotelike; | ||
| 151 | $ldel =~ s/'//g and $qdel .= q{'}; | ||
| 152 | $ldel =~ s/"//g and $qdel .= q{"}; | ||
| 153 | $ldel =~ s/`//g and $qdel .= q{`}; | ||
| 154 | $ldel =~ s/q//g and $quotelike = 1; | ||
| 155 | $ldel =~ tr/[](){}<>\0-\377/[[(({{<</ds; | ||
| 156 | my $rdel = $ldel; | ||
| 157 | unless ($rdel =~ tr/[({</])}>/) | ||
| 158 | { | ||
| 159 | return _fail $wantarray, $textref, | ||
| 160 | "Did not find a suitable bracket in delimiter: \"$_[1]\"", | ||
| 161 | 0; | ||
| 162 | } | ||
| 163 | my $posbug = pos; | ||
| 164 | $ldel = join('|', map { quotemeta $_ } split('', $ldel)); | ||
| 165 | $rdel = join('|', map { quotemeta $_ } split('', $rdel)); | ||
| 166 | pos = $posbug; | ||
| 167 | |||
| 168 | my $startpos = pos $$textref || 0; | ||
| 169 | my @match = _match_bracketed($textref,$pre, $ldel, $qdel, $quotelike, $rdel); | ||
| 170 | |||
| 171 | return _fail ($wantarray, $textref) unless @match; | ||
| 172 | |||
| 173 | return _succeed ( $wantarray, $textref, | ||
| 174 | $match[2], $match[5]+2, # MATCH | ||
| 175 | @match[8,9], # REMAINDER | ||
| 176 | @match[0,1], # PREFIX | ||
| 177 | ); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub _match_bracketed($$$$$$) # $textref, $pre, $ldel, $qdel, $quotelike, $rdel | ||
| 181 | { | ||
| 182 | my ($textref, $pre, $ldel, $qdel, $quotelike, $rdel) = @_; | ||
| 183 | my ($startpos, $ldelpos, $endpos) = (pos $$textref = pos $$textref||0); | ||
| 184 | unless ($$textref =~ m/\G$pre/gc) | ||
| 185 | { | ||
| 186 | _failmsg "Did not find prefix: /$pre/", $startpos; | ||
| 187 | return; | ||
| 188 | } | ||
| 189 | |||
| 190 | $ldelpos = pos $$textref; | ||
| 191 | |||
| 192 | unless ($$textref =~ m/\G($ldel)/gc) | ||
| 193 | { | ||
| 194 | _failmsg "Did not find opening bracket after prefix: \"$pre\"", | ||
| 195 | pos $$textref; | ||
| 196 | pos $$textref = $startpos; | ||
| 197 | return; | ||
| 198 | } | ||
| 199 | |||
| 200 | my @nesting = ( $1 ); | ||
| 201 | my $textlen = length $$textref; | ||
| 202 | while (pos $$textref < $textlen) | ||
| 203 | { | ||
| 204 | next if $$textref =~ m/\G\\./gcs; | ||
| 205 | |||
| 206 | if ($$textref =~ m/\G($ldel)/gc) | ||
| 207 | { | ||
| 208 | push @nesting, $1; | ||
| 209 | } | ||
| 210 | elsif ($$textref =~ m/\G($rdel)/gc) | ||
| 211 | { | ||
| 212 | my ($found, $brackettype) = ($1, $1); | ||
| 213 | if ($#nesting < 0) | ||
| 214 | { | ||
| 215 | _failmsg "Unmatched closing bracket: \"$found\"", | ||
| 216 | pos $$textref; | ||
| 217 | pos $$textref = $startpos; | ||
| 218 | return; | ||
| 219 | } | ||
| 220 | my $expected = pop(@nesting); | ||
| 221 | $expected =~ tr/({[</)}]>/; | ||
| 222 | if ($expected ne $brackettype) | ||
| 223 | { | ||
| 224 | _failmsg qq{Mismatched closing bracket: expected "$expected" but found "$found"}, | ||
| 225 | pos $$textref; | ||
| 226 | pos $$textref = $startpos; | ||
| 227 | return; | ||
| 228 | } | ||
| 229 | last if $#nesting < 0; | ||
| 230 | } | ||
| 231 | elsif ($qdel && $$textref =~ m/\G([$qdel])/gc) | ||
| 232 | { | ||
| 233 | $$textref =~ m/\G[^\\$1]*(?:\\.[^\\$1]*)*(\Q$1\E)/gsc and next; | ||
| 234 | _failmsg "Unmatched embedded quote ($1)", | ||
| 235 | pos $$textref; | ||
| 236 | pos $$textref = $startpos; | ||
| 237 | return; | ||
| 238 | } | ||
| 239 | elsif ($quotelike && _match_quotelike($textref,"",1,0)) | ||
| 240 | { | ||
| 241 | next; | ||
| 242 | } | ||
| 243 | |||
| 244 | else { $$textref =~ m/\G(?:[a-zA-Z0-9]+|.)/gcs } | ||
| 245 | } | ||
| 246 | if ($#nesting>=0) | ||
| 247 | { | ||
| 248 | _failmsg "Unmatched opening bracket(s): " | ||
| 249 | . join("..",@nesting)."..", | ||
| 250 | pos $$textref; | ||
| 251 | pos $$textref = $startpos; | ||
| 252 | return; | ||
| 253 | } | ||
| 254 | |||
| 255 | $endpos = pos $$textref; | ||
| 256 | |||
| 257 | return ( | ||
| 258 | $startpos, $ldelpos-$startpos, # PREFIX | ||
| 259 | $ldelpos, 1, # OPENING BRACKET | ||
| 260 | $ldelpos+1, $endpos-$ldelpos-2, # CONTENTS | ||
| 261 | $endpos-1, 1, # CLOSING BRACKET | ||
| 262 | $endpos, length($$textref)-$endpos, # REMAINDER | ||
| 263 | ); | ||
| 264 | } | ||
| 265 | |||
| 266 | sub _revbracket($) | ||
| 267 | { | ||
| 268 | my $brack = reverse $_[0]; | ||
| 269 | $brack =~ tr/[({</])}>/; | ||
| 270 | return $brack; | ||
| 271 | } | ||
| 272 | |||
| 273 | my $XMLNAME = q{[a-zA-Z_:][a-zA-Z0-9_:.-]*}; | ||
| 274 | |||
| 275 | sub extract_tagged (;$$$$$) # ($text, $opentag, $closetag, $pre, \%options) | ||
| 276 | { | ||
| 277 | my $textref = defined $_[0] ? \$_[0] : \$_; | ||
| 278 | my $ldel = $_[1]; | ||
| 279 | my $rdel = $_[2]; | ||
| 280 | my $pre = defined $_[3] ? $_[3] : '\s*'; | ||
| 281 | my %options = defined $_[4] ? %{$_[4]} : (); | ||
| 282 | my $omode = defined $options{fail} ? $options{fail} : ''; | ||
| 283 | my $bad = ref($options{reject}) eq 'ARRAY' ? join('|', @{$options{reject}}) | ||
| 284 | : defined($options{reject}) ? $options{reject} | ||
| 285 | : '' | ||
| 286 | ; | ||
| 287 | my $ignore = ref($options{ignore}) eq 'ARRAY' ? join('|', @{$options{ignore}}) | ||
| 288 | : defined($options{ignore}) ? $options{ignore} | ||
| 289 | : '' | ||
| 290 | ; | ||
| 291 | |||
| 292 | if (!defined $ldel) { $ldel = '<\w+(?:' . gen_delimited_pat(q{'"}) . '|[^>])*>'; } | ||
| 293 | $@ = undef; | ||
| 294 | |||
| 295 | my @match = _match_tagged($textref, $pre, $ldel, $rdel, $omode, $bad, $ignore); | ||
| 296 | |||
| 297 | return _fail(wantarray, $textref) unless @match; | ||
| 298 | return _succeed wantarray, $textref, | ||
| 299 | $match[2], $match[3]+$match[5]+$match[7], # MATCH | ||
| 300 | @match[8..9,0..1,2..7]; # REM, PRE, BITS | ||
| 301 | } | ||
| 302 | |||
| 303 | sub _match_tagged # ($$$$$$$) | ||
| 304 | { | ||
| 305 | my ($textref, $pre, $ldel, $rdel, $omode, $bad, $ignore) = @_; | ||
| 306 | my $rdelspec; | ||
| 307 | |||
| 308 | my ($startpos, $opentagpos, $textpos, $parapos, $closetagpos, $endpos) = ( pos($$textref) = pos($$textref)||0 ); | ||
| 309 | |||
| 310 | unless ($$textref =~ m/\G($pre)/gc) | ||
| 311 | { | ||
| 312 | _failmsg "Did not find prefix: /$pre/", pos $$textref; | ||
| 313 | goto failed; | ||
| 314 | } | ||
| 315 | |||
| 316 | $opentagpos = pos($$textref); | ||
| 317 | |||
| 318 | unless ($$textref =~ m/\G$ldel/gc) | ||
| 319 | { | ||
| 320 | _failmsg "Did not find opening tag: /$ldel/", pos $$textref; | ||
| 321 | goto failed; | ||
| 322 | } | ||
| 323 | |||
| 324 | $textpos = pos($$textref); | ||
| 325 | |||
| 326 | if (!defined $rdel) | ||
| 327 | { | ||
| 328 | $rdelspec = substr($$textref, $-[0], $+[0] - $-[0]); | ||
| 329 | unless ($rdelspec =~ s/\A([[(<{]+)($XMLNAME).*/ quotemeta "$1$2". _revbracket($1) /oes) | ||
| 330 | { | ||
| 331 | } | ||
| 332 | } | ||
| 333 | else | ||
| 334 | { | ||
| 335 | } | ||
| 336 | |||
| 337 | while (pos($$textref) < length($$textref)) | ||
| 338 | { | ||
| 339 | |||
| 340 | } | ||
| 341 | |||
| 342 | short: | ||
| 343 | $closetagpos = pos($$textref); | ||
| 344 | goto matched if $omode eq 'MAX'; | ||
| 345 | goto failed unless $omode eq 'PARA'; | ||
| 346 | |||
| 347 | if (defined $parapos) { pos($$textref) = $parapos } | ||
| 348 | else { $parapos = pos($$textref) } | ||
| 349 | |||
| 350 | return ( | ||
| 351 | $startpos, $opentagpos-$startpos, # PREFIX | ||
| 352 | $opentagpos, $textpos-$opentagpos, # OPENING TAG | ||
| 353 | $textpos, $parapos-$textpos, # TEXT | ||
| 354 | $parapos, 0, # NO CLOSING TAG | ||
| 355 | $parapos, length($$textref)-$parapos, # REMAINDER | ||
| 356 | ); | ||
| 357 | |||
| 358 | matched: | ||
| 359 | $endpos = pos($$textref); | ||
| 360 | return ( | ||
| 361 | $startpos, $opentagpos-$startpos, # PREFIX | ||
| 362 | $opentagpos, $textpos-$opentagpos, # OPENING TAG | ||
| 363 | $textpos, $closetagpos-$textpos, # TEXT | ||
| 364 | $closetagpos, $endpos-$closetagpos, # CLOSING TAG | ||
| 365 | $endpos, length($$textref)-$endpos, # REMAINDER | ||
| 366 | ); | ||
| 367 | |||
| 368 | failed: | ||
| 369 | _failmsg "Did not find closing tag", pos $$textref unless $@; | ||
| 370 | pos($$textref) = $startpos; | ||
| 371 | return; | ||
| 372 | } | ||
| 373 | __END__ |