File Coverage

blib/lib/Text/Query/ParseSimple.pm
Criterion Covered Total %
statement 44 44 100.0
branch 17 26 65.3
condition 5 12 41.6
subroutine 6 6 100.0
pod 2 2 100.0
total 74 90 82.2


line stmt bran cond sub pod time code
1             #
2             # Copyright (C) 1999 Eric Bohlman, Loic Dachary
3             #
4             # This program is free software; you can redistribute it and/or modify it
5             # under the terms of the GNU General Public License as published by the
6             # Free Software Foundation; either version 2, or (at your option) any
7             # later version. You may also use, redistribute and/or modify it
8             # under the terms of the Artistic License supplied with your Perl
9             # distribution
10             #
11             # This program is distributed in the hope that it will be useful,
12             # but WITHOUT ANY WARRANTY; without even the implied warranty of
13             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14             # GNU General Public License for more details.
15             #
16             # You should have received a copy of the GNU General Public License
17             # along with this program; if not, write to the Free Software
18             # Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19              
20             package Text::Query::ParseSimple;
21              
22 2     2   11 use strict;
  2         3  
  2         72  
23 2     2   10 use re qw/eval/;
  2         2  
  2         69  
24              
25 2     2   1583 use Text::Query::Parse;
  2         5  
  2         66  
26              
27 2     2   13 use vars qw(@ISA);
  2         4  
  2         1129  
28              
29             @ISA = qw(Text::Query::Parse);
30              
31             sub expression {
32 14     14 1 24 my($self) = shift;
33              
34 14         23 my($t, $expr);
35 14         16 foreach $t (@{$self->{'tokens'}}) {
  14         32  
36 22 50       55 warn("t 0 = $t") if($self->{-verbose} > 1);
37              
38 22 100       82 my($type) = ($t =~ s/([-+\e])//) ? $1 : '';
39              
40 22         74 $t = $self->build_literal($t);
41            
42 22 100       67 if ($type eq '-') {
    100          
43 2         15 $t = $self->build_forbiden($t);
44             } elsif($type eq '+') {
45 6         26 $t = $self->build_mandatory($t);
46             }
47              
48 22 50       61 warn("t 1 = $t") if($self->{-verbose} > 1);
49            
50 22         67 $t = $self->build_expression_finish($t);
51              
52 22 50       62 warn("t 2 = $t") if($self->{-verbose} > 1);
53              
54 22 100       82 $expr = $expr ? $self->build_expression($expr, $t) : $t;
55             }
56              
57 14         63 return $expr;
58             }
59              
60             sub parse_tokens {
61 14     14 1 49 local($^W) = 0;
62 14         24 my($self) = shift;
63 14         633 my($line) = @_;
64 14         17 my($quote, $quoted, $unquoted, $delim, $word);
65 14         35 my($quotes) = $self->{parseopts}{-quotes};
66              
67 14         19 my(@tokens) = ();
68 14         37 while (length($line)) {
69 22         1076 ($quote, $quoted, undef, $unquoted, $delim, undef) =
70             $line =~ m/^([$quotes]) # a $quote
71             ((?:\\.|(?!\1)[^\\])*) # and $quoted text
72             \1 # followed by the same quote
73             ([\000-\377]*) # and the rest
74             | # --OR--
75             ^((?:\\.|[^\\$quotes])*?) # an $unquoted text
76             (\Z(?!\n)|\s+|(?!^)(?=[$quotes])) # plus EOL, delimiter, or quote
77             ([\000-\377]*) # the rest
78             /ix; # extended layout
79              
80 22 50 33     194 last unless($quote || length($unquoted) || length($delim));
      33        
81 22         50 $line = $+;
82 22         42 $unquoted=~s/^\s+//;
83 22         41 $unquoted=~s/\s+$//;
84 22 0       57 $word .= defined($quote) ? (length($word) ? $quoted : "\e$quoted" ) : $unquoted;
    50          
85 22 50 66     128 push(@tokens,$word) if(length($word) and (length($delim) or !length($line)));
      33        
86 22 100       82 undef $word if(length($delim));
87             }
88              
89 14 50       46 warn("parsed tokens @tokens") if($self->{-verbose} > 1);
90              
91 14         103 $self->{'tokens'} = \@tokens;
92             }
93              
94             1;
95              
96             __END__