File: demo_Lucene_query.pl

package info (click to toggle)
libregexp-grammars-perl 1.058-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,328 kB
  • sloc: perl: 53,328; makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,234 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /usr/bin/perl

use strict;
use warnings;
use 5.010;

my $query
    = q{ body:("et tu" (Brute OR Caesar)) AND published:[1900 TO 2010] NOT author:Shakespeare };

my $grammar = do{
    use Regexp::Grammars;
    qr{
        \A \s* <Query> \s* \Z

        <rule: Query>
            <[And_Clause]>+ % (OR)

        <rule: And_Clause>
            <[Term]>+ % <[And_Operator]>

        <rule: And_Operator>
            AND
          | NOT
          | \s++  <MATCH=(?{'AND'})>

        <token: Term>
            (?: <Field> : )?
            (?:
                \( <Subquery=Query> \)
              | <Range>
              | <Value=Quoted_Value>
              | <Value=Raw_Value>
            )

        <token: Field>
            <.Non_Keyword>  \w++

        <rule: Range>
            \[  <From=(.*?)>  TO  <To=(.*?)>  \]

        <rule: Quoted_Value>
            '  <MATCH=( [^'\\]*+  (?: \\. [^'\\]*+ )*+ )>  '
          | "  <MATCH=( [^"\\]*+  (?: \\. [^"\\]*+ )*+ )>  "

        <rule: Raw_Value>
            <.Non_Keyword> [^'"()][^\s()]*+

        <token: Non_Keyword>
            (?! NOT | AND | OR | \( | \) )
    }xms;
};

if ($query =~ $grammar) {
    use Data::Dumper 'Dumper';
    say Dumper \%/;
}
else {
    say 'Failed';
}