Blame server/util_expr_parse.y

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/* based on ap_expr_parse.y from mod_ssl */
Packit 90a5c9
Packit 90a5c9
/*  _________________________________________________________________
Packit 90a5c9
**
Packit 90a5c9
**  Expression Parser
Packit 90a5c9
**  _________________________________________________________________
Packit 90a5c9
*/
Packit 90a5c9
Packit 90a5c9
%pure-parser
Packit 90a5c9
%error-verbose
Packit 90a5c9
%defines
Packit 90a5c9
%lex-param   { void *yyscanner }
Packit 90a5c9
%parse-param { ap_expr_parse_ctx_t *ctx }
Packit 90a5c9
Packit 90a5c9
%{
Packit 90a5c9
#include "util_expr_private.h"
Packit 90a5c9
%}
Packit 90a5c9
Packit 90a5c9
%union {
Packit 90a5c9
    char      *cpVal;
Packit 90a5c9
    ap_expr_t *exVal;
Packit 90a5c9
    int        num;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
%token  T_TRUE
Packit 90a5c9
%token  T_FALSE
Packit 90a5c9
Packit 90a5c9
%token  T_EXPR_BOOL
Packit 90a5c9
%token  T_EXPR_STRING
Packit 90a5c9
Packit 90a5c9
%token  <cpVal> T_ERROR
Packit 90a5c9
Packit 90a5c9
%token  <cpVal> T_DIGIT
Packit 90a5c9
%token  <cpVal> T_ID
Packit 90a5c9
%token  <cpVal> T_STRING
Packit 90a5c9
%token  <cpVal> T_REGEX
Packit 90a5c9
%token  <cpVal> T_REGEX_I
Packit 90a5c9
%token  <num>   T_REGEX_BACKREF
Packit 90a5c9
%token  <cpVal> T_OP_UNARY
Packit 90a5c9
%token  <cpVal> T_OP_BINARY
Packit 90a5c9
Packit 90a5c9
%token  T_STR_BEGIN
Packit 90a5c9
%token  T_STR_END
Packit 90a5c9
%token  T_VAR_BEGIN
Packit 90a5c9
%token  T_VAR_END
Packit 90a5c9
Packit 90a5c9
%token  T_OP_EQ
Packit 90a5c9
%token  T_OP_NE
Packit 90a5c9
%token  T_OP_LT
Packit 90a5c9
%token  T_OP_LE
Packit 90a5c9
%token  T_OP_GT
Packit 90a5c9
%token  T_OP_GE
Packit 90a5c9
%token  T_OP_REG
Packit 90a5c9
%token  T_OP_NRE
Packit 90a5c9
%token  T_OP_IN
Packit 90a5c9
%token  T_OP_STR_EQ
Packit 90a5c9
%token  T_OP_STR_NE
Packit 90a5c9
%token  T_OP_STR_LT
Packit 90a5c9
%token  T_OP_STR_LE
Packit 90a5c9
%token  T_OP_STR_GT
Packit 90a5c9
%token  T_OP_STR_GE
Packit 90a5c9
%token  T_OP_CONCAT
Packit 90a5c9
Packit 90a5c9
%token  T_OP_OR
Packit 90a5c9
%token  T_OP_AND
Packit 90a5c9
%token  T_OP_NOT
Packit 90a5c9
Packit 90a5c9
%right  T_OP_OR
Packit 90a5c9
%right  T_OP_AND
Packit 90a5c9
%right  T_OP_NOT
Packit 90a5c9
%right  T_OP_CONCAT
Packit 90a5c9
Packit 90a5c9
%type   <exVal>   expr
Packit 90a5c9
%type   <exVal>   comparison
Packit 90a5c9
%type   <exVal>   strfunccall
Packit 90a5c9
%type   <exVal>   lstfunccall
Packit 90a5c9
%type   <exVal>   regex
Packit 90a5c9
%type   <exVal>   words
Packit 90a5c9
%type   <exVal>   wordlist
Packit 90a5c9
%type   <exVal>   word
Packit 90a5c9
%type   <exVal>   string
Packit 90a5c9
%type   <exVal>   strpart
Packit 90a5c9
%type   <exVal>   var
Packit 90a5c9
%type   <exVal>   backref
Packit 90a5c9
Packit 90a5c9
%{
Packit 90a5c9
#include "util_expr_private.h"
Packit 90a5c9
#define yyscanner ctx->scanner
Packit 90a5c9
Packit 90a5c9
int ap_expr_yylex(YYSTYPE *lvalp, void *scanner);
Packit 90a5c9
%}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
%%
Packit 90a5c9
Packit 90a5c9
root      : T_EXPR_BOOL   expr           { ctx->expr = $2; }
Packit 90a5c9
          | T_EXPR_STRING string         { ctx->expr = $2; }
Packit 90a5c9
          | T_ERROR                      { YYABORT; }
Packit 90a5c9
          ;
Packit 90a5c9
Packit 90a5c9
expr      : T_TRUE                       { $$ = ap_expr_make(op_True,        NULL, NULL, ctx); }
Packit 90a5c9
          | T_FALSE                      { $$ = ap_expr_make(op_False,       NULL, NULL, ctx); }
Packit 90a5c9
          | T_OP_NOT expr                { $$ = ap_expr_make(op_Not,         $2,   NULL, ctx); }
Packit 90a5c9
          | expr T_OP_OR expr            { $$ = ap_expr_make(op_Or,          $1,   $3,   ctx); }
Packit 90a5c9
          | expr T_OP_AND expr           { $$ = ap_expr_make(op_And,         $1,   $3,   ctx); }
Packit 90a5c9
          | comparison                   { $$ = ap_expr_make(op_Comp,        $1,   NULL, ctx); }
Packit 90a5c9
          | T_OP_UNARY word              { $$ = ap_expr_unary_op_make(       $1,   $2,   ctx); }
Packit 90a5c9
          | word T_OP_BINARY word        { $$ = ap_expr_binary_op_make($2,   $1,   $3,   ctx); }
Packit 90a5c9
          | '(' expr ')'                 { $$ = $2; }
Packit 90a5c9
          | T_ERROR                      { YYABORT; }
Packit 90a5c9
          ;
Packit 90a5c9
Packit 90a5c9
comparison: word T_OP_EQ word            { $$ = ap_expr_make(op_EQ,      $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_NE word            { $$ = ap_expr_make(op_NE,      $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_LT word            { $$ = ap_expr_make(op_LT,      $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_LE word            { $$ = ap_expr_make(op_LE,      $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_GT word            { $$ = ap_expr_make(op_GT,      $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_GE word            { $$ = ap_expr_make(op_GE,      $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_STR_EQ word        { $$ = ap_expr_make(op_STR_EQ,  $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_STR_NE word        { $$ = ap_expr_make(op_STR_NE,  $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_STR_LT word        { $$ = ap_expr_make(op_STR_LT,  $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_STR_LE word        { $$ = ap_expr_make(op_STR_LE,  $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_STR_GT word        { $$ = ap_expr_make(op_STR_GT,  $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_STR_GE word        { $$ = ap_expr_make(op_STR_GE,  $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_IN wordlist        { $$ = ap_expr_make(op_IN,      $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_REG regex          { $$ = ap_expr_make(op_REG,     $1, $3, ctx); }
Packit 90a5c9
          | word T_OP_NRE regex          { $$ = ap_expr_make(op_NRE,     $1, $3, ctx); }
Packit 90a5c9
          ;
Packit 90a5c9
Packit 90a5c9
wordlist  : lstfunccall                  { $$ = $1; }
Packit 90a5c9
          | '{' words '}'                { $$ = $2; }
Packit 90a5c9
          ;
Packit 90a5c9
Packit 90a5c9
words     : word                         { $$ = ap_expr_make(op_ListElement, $1, NULL, ctx); }
Packit 90a5c9
          | words ',' word               { $$ = ap_expr_make(op_ListElement, $3, $1,   ctx); }
Packit 90a5c9
          ;
Packit 90a5c9
Packit 90a5c9
string    : string strpart               { $$ = ap_expr_make(op_Concat, $1, $2, ctx); }
Packit 90a5c9
          | strpart                      { $$ = $1; }
Packit 90a5c9
          | T_ERROR                      { YYABORT; }
Packit 90a5c9
          ;
Packit 90a5c9
Packit 90a5c9
strpart   : T_STRING                     { $$ = ap_expr_make(op_String, $1, NULL, ctx); }
Packit 90a5c9
          | var                          { $$ = $1; }
Packit 90a5c9
          | backref                      { $$ = $1; }
Packit 90a5c9
          ;
Packit 90a5c9
Packit 90a5c9
var       : T_VAR_BEGIN T_ID T_VAR_END            { $$ = ap_expr_var_make($2, ctx); }
Packit 90a5c9
          | T_VAR_BEGIN T_ID ':' string T_VAR_END { $$ = ap_expr_str_func_make($2, $4, ctx); }
Packit 90a5c9
          ;
Packit 90a5c9
Packit 90a5c9
word      : T_DIGIT                      { $$ = ap_expr_make(op_Digit,  $1, NULL, ctx); }
Packit 90a5c9
          | word T_OP_CONCAT word        { $$ = ap_expr_make(op_Concat, $1, $3,   ctx); }
Packit 90a5c9
          | var                          { $$ = $1; }
Packit 90a5c9
          | backref                      { $$ = $1; }
Packit 90a5c9
          | strfunccall                  { $$ = $1; }
Packit 90a5c9
          | T_STR_BEGIN string T_STR_END { $$ = $2; }
Packit 90a5c9
          | T_STR_BEGIN T_STR_END        { $$ = ap_expr_make(op_String, "", NULL, ctx); }
Packit 90a5c9
          ;
Packit 90a5c9
Packit 90a5c9
regex     : T_REGEX {
Packit 90a5c9
                ap_regex_t *regex;
Packit 90a5c9
                if ((regex = ap_pregcomp(ctx->pool, $1,
Packit 90a5c9
                                         AP_REG_EXTENDED|AP_REG_NOSUB)) == NULL) {
Packit 90a5c9
                    ctx->error = "Failed to compile regular expression";
Packit 90a5c9
                    YYERROR;
Packit 90a5c9
                }
Packit 90a5c9
                $$ = ap_expr_make(op_Regex, regex, NULL, ctx);
Packit 90a5c9
            }
Packit 90a5c9
          | T_REGEX_I {
Packit 90a5c9
                ap_regex_t *regex;
Packit 90a5c9
                if ((regex = ap_pregcomp(ctx->pool, $1,
Packit 90a5c9
                                         AP_REG_EXTENDED|AP_REG_NOSUB|AP_REG_ICASE)) == NULL) {
Packit 90a5c9
                    ctx->error = "Failed to compile regular expression";
Packit 90a5c9
                    YYERROR;
Packit 90a5c9
                }
Packit 90a5c9
                $$ = ap_expr_make(op_Regex, regex, NULL, ctx);
Packit 90a5c9
            }
Packit 90a5c9
          ;
Packit 90a5c9
Packit 90a5c9
backref     : T_REGEX_BACKREF   {
Packit 90a5c9
                int *n = apr_palloc(ctx->pool, sizeof(int));
Packit 90a5c9
                *n = $1;
Packit 90a5c9
                $$ = ap_expr_make(op_RegexBackref, n, NULL, ctx);
Packit 90a5c9
            }
Packit 90a5c9
            ;
Packit 90a5c9
Packit 90a5c9
lstfunccall : T_ID '(' word ')' { $$ = ap_expr_list_func_make($1, $3, ctx); }
Packit 90a5c9
            ;
Packit 90a5c9
Packit 90a5c9
strfunccall : T_ID '(' word ')' { $$ = ap_expr_str_func_make($1, $3, ctx); }
Packit 90a5c9
            ;
Packit 90a5c9
Packit 90a5c9
%%
Packit 90a5c9
Packit 90a5c9
void yyerror(ap_expr_parse_ctx_t *ctx, const char *s)
Packit 90a5c9
{
Packit 90a5c9
    /* s is allocated on the stack */
Packit 90a5c9
    ctx->error = apr_pstrdup(ctx->ptemp, s);
Packit 90a5c9
}
Packit 90a5c9