/lib/Foswiki/Query/Parser.pm

https://github.com/csirac2/core · Perl · 147 lines · 105 code · 35 blank · 7 comment · 3 complexity · 59a459cb76b1737fca08f87934c928a7 MD5 · raw file

  1. # See bottom of file for license and copyright information
  2. =begin TML
  3. ---+ package Foswiki::Query::Parser
  4. Parser for queries, using the Foswiki::Infix::Parser.
  5. The default node type in the generated parse tree is Foswiki::Query::Node,
  6. though you can pass your own alternative class as an option (it must implement
  7. Foswiki::Infix::Node)
  8. =cut
  9. package Foswiki::Query::Parser;
  10. use strict;
  11. use warnings;
  12. use Assert;
  13. use Foswiki::Infix::Parser ();
  14. our @ISA = ('Foswiki::Infix::Parser');
  15. use Foswiki::Query::Node ();
  16. # operator name precedence
  17. use Foswiki::Query::OP_or (); # 100
  18. use Foswiki::Query::OP_and (); # 200
  19. use Foswiki::Query::OP_not (); # 300
  20. use Foswiki::Query::OP_comma (); # 400
  21. use Foswiki::Query::OP_lte (); # 500
  22. use Foswiki::Query::OP_gt (); # 500
  23. use Foswiki::Query::OP_gte (); # 500
  24. use Foswiki::Query::OP_lt (); # 500
  25. use Foswiki::Query::OP_match (); # 500
  26. use Foswiki::Query::OP_eq (); # 500
  27. use Foswiki::Query::OP_like (); # 500
  28. use Foswiki::Query::OP_ne (); # 500
  29. use Foswiki::Query::OP_in (); # 500
  30. use Foswiki::Query::OP_plus (); # 600
  31. use Foswiki::Query::OP_minus (); # 600
  32. use Foswiki::Query::OP_times (); # 700
  33. use Foswiki::Query::OP_div (); # 700
  34. use Foswiki::Query::OP_ref (); # 800
  35. use Foswiki::Query::OP_dot (); # 800
  36. use Foswiki::Query::OP_where (); # 900
  37. use Foswiki::Query::OP_lc (); # 1000
  38. use Foswiki::Query::OP_uc (); # 1000
  39. use Foswiki::Query::OP_d2n (); # 1000
  40. use Foswiki::Query::OP_length (); # 1000
  41. use Foswiki::Query::OP_neg (); # 1000
  42. use Foswiki::Query::OP_int (); # 1000
  43. use Foswiki::Query::OP_ob (); # 1100
  44. =begin TML
  45. Query Language BNF
  46. <verbatim>
  47. expr ::= and_expr 'or' expr | and_expr;
  48. and_expr ::= not_expr 'and' and_expr | not_expr;
  49. not_expr ::= 'not' comma_expr | comma_expr;
  50. comma_expr ::= cmp_expr ',' comma_expr | cmp_expr;
  51. cmp_expr ::= add_expr cmp_op cm_expr | add_expr;
  52. cmp_op ::= '<=' | '>=' | '<' | '>' | '=' | '=~' | '~' | '!=' | 'in';
  53. add_expr ::= mul_expr add_op add_expr | mul_expr;
  54. mul_expr ::= ref_expr mul_op mul_expr | ref_expr;
  55. mul_op ::= '*' | 'div';
  56. ref_expr ::= u_expr ref_op ref_expr | u_expr;
  57. ref_op ::= '/' | '.';
  58. u_expr ::= value uop u_expr | value;
  59. uop ::= 'lc' | 'uc' | 'd2n' | 'length' | '-' | 'int' | '@';
  60. value ::= <name> | <string> | <number>;
  61. </verbatim>
  62. String and Numbers are as defined in Foswiki::Infix::Parser. Names default
  63. to =/([A-Z:][A-Z0-9_:]*|({[A-Z][A-Z0-9_]*})+)/i=.
  64. See %SYSTEMWEB%.QuerySearch for details of the query language.
  65. =cut
  66. # Each operator is implemented by a class in Foswiki::Query. Note that
  67. # OP_empty is *not* included here; it is a pseudo-operator and does
  68. # not participate in parsing.
  69. use constant OPS => qw (match and eq lc lte not ref d2n gte length lt ob
  70. uc dot gt like ne or where comma plus minus
  71. neg times div in int );
  72. sub new {
  73. my ( $class, $options ) = @_;
  74. $options->{words} ||= qr/([A-Z:][A-Z0-9_:]*|({[A-Z][A-Z0-9_]*})+)/i;
  75. $options->{nodeClass} ||= 'Foswiki::Query::Node';
  76. my $this = $class->SUPER::new($options);
  77. foreach my $op ( OPS() ) {
  78. my $on = 'Foswiki::Query::OP_' . $op;
  79. $this->addOperator( $on->new() );
  80. }
  81. return $this;
  82. }
  83. # Ensure there is at least one operand on the opstack when closing
  84. # a subexpression.
  85. sub onCloseExpr {
  86. my ( $this, $opands ) = @_;
  87. if ( !scalar(@$opands) ) {
  88. require Foswiki::Query::OP_empty;
  89. push( @$opands, $this->{node_factory}->emptyExpression() );
  90. }
  91. }
  92. 1;
  93. __END__
  94. Author: Crawford Currie http://c-dot.co.uk
  95. Foswiki - The Free and Open Source Wiki, http://foswiki.org/
  96. Copyright (C) 2008-2011 Foswiki Contributors. Foswiki Contributors
  97. are listed in the AUTHORS file in the root of this distribution.
  98. NOTE: Please extend that file, not this notice.
  99. Additional copyrights apply to some or all of the code in this
  100. file as follows:
  101. Copyright (C) 2005-2007 TWiki Contributors. All Rights Reserved.
  102. TWiki Contributors are listed in the AUTHORS file in the root
  103. of this distribution.
  104. This program is free software; you can redistribute it and/or
  105. modify it under the terms of the GNU General Public License
  106. as published by the Free Software Foundation; either version 2
  107. of the License, or (at your option) any later version. For
  108. more details read LICENSE in the root of this distribution.
  109. This program is distributed in the hope that it will be useful,
  110. but WITHOUT ANY WARRANTY; without even the implied warranty of
  111. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  112. As per the GPL, removal of this notice is prohibited.