1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
56 use constant VALUE => 1;
57 use constant AGGREG => 2;
59 use constant OPEN_PAREN => 8;
60 use constant CLOSE_PAREN => 16;
61 use constant KEYWORD => 32;
62 my @tokens = qw[VALUE AGGREGATOR OPERATOR OPEN_PAREN CLOSE_PAREN KEYWORD];
64 use Regexp::Common qw /delimited/;
65 my $re_aggreg = qr[(?i:AND|OR)];
66 my $re_delim = qr[$RE{delimited}{-delim=>qq{\'\"}}];
67 my $re_value = qr[[+-]?\d+|(?i:NULL)|$re_delim];
68 my $re_keyword = qr[[{}\w\.]+|$re_delim];
69 my $re_op = qr[=|!=|>=|<=|>|<|(?i:IS NOT)|(?i:IS)|(?i:NOT LIKE)|(?i:LIKE)|(?i:NOT STARTSWITH)|(?i:STARTSWITH)|(?i:NOT ENDSWITH)|(?i:ENDSWITH)]; # long to short
70 my $re_open_paren = qr[\(];
71 my $re_close_paren = qr[\)];
76 my ($tree, $node, @pnodes);
80 $callback{'OpenParen'} = sub { push @pnodes, $node; $node = []; push @{ $pnodes[-1] }, $node };
81 $callback{'CloseParen'} = sub { $node = pop @pnodes };
82 $callback{'EntryAggregator'} = sub { push @$node, $_[0] };
83 $callback{'Condition'} = sub { push @$node, { key => $_[0], op => $_[1], value => $_[2] } };
85 Parse($string, \%callback);
90 my ($string, $cb) = @_;
91 my $loc = sub {HTML::Mason::Commands::loc(@_)};
92 $string = '' unless defined $string;
94 my $want = KEYWORD | OPEN_PAREN;
98 my ($key,$op,$value) = ("","","");
100 # order of matches in the RE is important.. op should come early,
101 # because it has spaces in it. otherwise "NOT LIKE" might be parsed
102 # as a keyword or value.
115 # Highest priority is last
117 $current = OP if ($want & OP) && $match =~ /^$re_op$/io;
118 $current = VALUE if ($want & VALUE) && $match =~ /^$re_value$/io;
119 $current = KEYWORD if ($want & KEYWORD) && $match =~ /^$re_keyword$/io;
120 $current = AGGREG if ($want & AGGREG) && $match =~ /^$re_aggreg$/io;
121 $current = OPEN_PAREN if ($want & OPEN_PAREN) && $match =~ /^$re_open_paren$/io;
122 $current = CLOSE_PAREN if ($want & CLOSE_PAREN) && $match =~ /^$re_close_paren$/io;
125 unless ($current && $want & $current) {
126 my $tmp = substr($string, 0, pos($string)- length($match));
127 $tmp .= '>'. $match .'<--here'. substr($string, pos($string));
128 my $msg = $loc->("Wrong query, expecting a [_1] in '[_2]'", _BitmaskToString($want), $tmp);
129 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
135 # Parens are highest priority
136 if ( $current & OPEN_PAREN ) {
137 $cb->{'OpenParen'}->();
139 $want = KEYWORD | OPEN_PAREN;
141 elsif ( $current & CLOSE_PAREN ) {
142 $cb->{'CloseParen'}->();
145 $want |= CLOSE_PAREN if $depth;
147 elsif ( $current & AGGREG ) {
148 $cb->{'EntryAggregator'}->( $match );
149 $want = KEYWORD | OPEN_PAREN;
151 elsif ( $current & KEYWORD ) {
155 elsif ( $current & OP ) {
159 elsif ( $current & VALUE ) {
162 # Remove surrounding quotes and unescape escaped
163 # characters from $key, $match
164 for ( $key, $value ) {
165 if ( /$re_delim/o ) {
167 substr($_,-1,1) = "";
172 $cb->{'Condition'}->( $key, $op, $value );
174 ($key,$op,$value) = ("","","");
176 $want |= CLOSE_PAREN if $depth;
178 my $msg = $loc->("Query parser is lost");
179 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
186 unless( !$last || $last & (CLOSE_PAREN | VALUE) ) {
187 my $msg = $loc->("Incomplete query, last element ([_1]) is not close paren or value in '[_2]'",
188 _BitmaskToString($last),
190 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
195 my $msg = $loc->("Incomplete query, [quant,_1,unclosed paren,unclosed parens] in '[_2]'", $depth, $string);
196 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
201 sub _BitmaskToString {
205 for( my $i = 0; $i<@tokens; $i++ ) {
206 next unless $mask & (1<<$i);
207 push @res, $tokens[$i];
210 my $tmp = join ', ', splice @res, 0, -1;
211 unshift @res, $tmp if $tmp;
212 return join ' or ', @res;
215 RT::Base->_ImportOverlays();