1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* based on ap_expr_parse.y from mod_ssl */
19 /* _________________________________________________________________
22 ** _________________________________________________________________
28 %lex-param { void *yyscanner }
29 %parse-param { ap_expr_parse_ctx_t *ctx }
32 #include "util_expr_private.h"
42 %token T_FALSE "false"
44 %token T_EXPR_BOOL "boolean expression"
45 %token T_EXPR_STRING "string expression"
47 %token <cpVal> T_ERROR "error token"
49 %token <cpVal> T_DIGIT "number"
50 %token <cpVal> T_ID "identifier"
51 %token <cpVal> T_STRING "cstring"
52 %token <cpVal> T_REGEX "regex"
53 %token <cpVal> T_REGEX_I "case-indendent regex"
54 %token <num> T_REGEX_BACKREF "regex back reference"
55 %token <cpVal> T_OP_UNARY "unary operator"
56 %token <cpVal> T_OP_BINARY "binary operator"
58 %token T_STR_BEGIN "start of string"
59 %token T_STR_END "end of string"
60 %token T_VAR_BEGIN "start of variable name"
61 %token T_VAR_END "end of variable name"
63 %token T_OP_EQ "integer equal"
64 %token T_OP_NE "integer not equal"
65 %token T_OP_LT "integer less than"
66 %token T_OP_LE "integer less or equal"
67 %token T_OP_GT "integer greater than"
68 %token T_OP_GE "integer greater or equal"
69 %token T_OP_REG "regex match"
70 %token T_OP_NRE "regex non-match"
71 %token T_OP_IN "contained in"
72 %token T_OP_STR_EQ "string equal"
73 %token T_OP_STR_NE "string not equal"
74 %token T_OP_STR_LT "string less than"
75 %token T_OP_STR_LE "string less or equal"
76 %token T_OP_STR_GT "string greater than"
77 %token T_OP_STR_GE "string greater or equal"
78 %token T_OP_CONCAT "string concatenation"
80 %token T_OP_OR "logical or"
81 %token T_OP_AND "logical and"
82 %token T_OP_NOT "logical not"
90 %type <exVal> comparison
91 %type <exVal> strfunccall "function"
92 %type <exVal> lstfunccall "listfunction"
95 %type <exVal> wordlist
98 %type <exVal> strpart "stringpart"
99 %type <exVal> var "variable"
100 %type <exVal> backref "rebackref"
103 #include "util_expr_private.h"
104 #define yyscanner ctx->scanner
106 int ap_expr_yylex(YYSTYPE *lvalp, void *scanner);
112 root : T_EXPR_BOOL expr { ctx->expr = $2; }
113 | T_EXPR_STRING string { ctx->expr = $2; }
114 | T_ERROR { YYABORT; }
117 expr : T_TRUE { $$ = ap_expr_make(op_True, NULL, NULL, ctx); }
118 | T_FALSE { $$ = ap_expr_make(op_False, NULL, NULL, ctx); }
119 | T_OP_NOT expr { $$ = ap_expr_make(op_Not, $2, NULL, ctx); }
120 | expr T_OP_OR expr { $$ = ap_expr_make(op_Or, $1, $3, ctx); }
121 | expr T_OP_AND expr { $$ = ap_expr_make(op_And, $1, $3, ctx); }
122 | comparison { $$ = ap_expr_make(op_Comp, $1, NULL, ctx); }
123 | T_OP_UNARY word { $$ = ap_expr_unary_op_make( $1, $2, ctx); }
124 | word T_OP_BINARY word { $$ = ap_expr_binary_op_make($2, $1, $3, ctx); }
125 | '(' expr ')' { $$ = $2; }
126 | T_ERROR { YYABORT; }
129 comparison: word T_OP_EQ word { $$ = ap_expr_make(op_EQ, $1, $3, ctx); }
130 | word T_OP_NE word { $$ = ap_expr_make(op_NE, $1, $3, ctx); }
131 | word T_OP_LT word { $$ = ap_expr_make(op_LT, $1, $3, ctx); }
132 | word T_OP_LE word { $$ = ap_expr_make(op_LE, $1, $3, ctx); }
133 | word T_OP_GT word { $$ = ap_expr_make(op_GT, $1, $3, ctx); }
134 | word T_OP_GE word { $$ = ap_expr_make(op_GE, $1, $3, ctx); }
135 | word T_OP_STR_EQ word { $$ = ap_expr_make(op_STR_EQ, $1, $3, ctx); }
136 | word T_OP_STR_NE word { $$ = ap_expr_make(op_STR_NE, $1, $3, ctx); }
137 | word T_OP_STR_LT word { $$ = ap_expr_make(op_STR_LT, $1, $3, ctx); }
138 | word T_OP_STR_LE word { $$ = ap_expr_make(op_STR_LE, $1, $3, ctx); }
139 | word T_OP_STR_GT word { $$ = ap_expr_make(op_STR_GT, $1, $3, ctx); }
140 | word T_OP_STR_GE word { $$ = ap_expr_make(op_STR_GE, $1, $3, ctx); }
141 | word T_OP_IN wordlist { $$ = ap_expr_make(op_IN, $1, $3, ctx); }
142 | word T_OP_REG regex { $$ = ap_expr_make(op_REG, $1, $3, ctx); }
143 | word T_OP_NRE regex { $$ = ap_expr_make(op_NRE, $1, $3, ctx); }
146 wordlist : lstfunccall { $$ = $1; }
147 | '{' words '}' { $$ = $2; }
150 words : word { $$ = ap_expr_make(op_ListElement, $1, NULL, ctx); }
151 | words ',' word { $$ = ap_expr_make(op_ListElement, $3, $1, ctx); }
154 string : string strpart { $$ = ap_expr_make(op_Concat, $1, $2, ctx); }
155 | strpart { $$ = $1; }
156 | T_ERROR { YYABORT; }
159 strpart : T_STRING { $$ = ap_expr_make(op_String, $1, NULL, ctx); }
161 | backref { $$ = $1; }
164 var : T_VAR_BEGIN T_ID T_VAR_END { $$ = ap_expr_var_make($2, ctx); }
165 | T_VAR_BEGIN T_ID ':' string T_VAR_END { $$ = ap_expr_str_func_make($2, $4, ctx); }
168 word : T_DIGIT { $$ = ap_expr_make(op_Digit, $1, NULL, ctx); }
169 | word T_OP_CONCAT word { $$ = ap_expr_make(op_Concat, $1, $3, ctx); }
171 | backref { $$ = $1; }
172 | strfunccall { $$ = $1; }
173 | T_STR_BEGIN string T_STR_END { $$ = $2; }
174 | T_STR_BEGIN T_STR_END { $$ = ap_expr_make(op_String, "", NULL, ctx); }
179 if ((regex = ap_pregcomp(ctx->pool, $1,
180 AP_REG_EXTENDED|AP_REG_NOSUB)) == NULL) {
181 ctx->error = "Failed to compile regular expression";
184 $$ = ap_expr_make(op_Regex, regex, NULL, ctx);
188 if ((regex = ap_pregcomp(ctx->pool, $1,
189 AP_REG_EXTENDED|AP_REG_NOSUB|AP_REG_ICASE)) == NULL) {
190 ctx->error = "Failed to compile regular expression";
193 $$ = ap_expr_make(op_Regex, regex, NULL, ctx);
197 backref : T_REGEX_BACKREF {
198 int *n = apr_palloc(ctx->pool, sizeof(int));
200 $$ = ap_expr_make(op_RegexBackref, n, NULL, ctx);
204 lstfunccall : T_ID '(' word ')' { $$ = ap_expr_list_func_make($1, $3, ctx); }
207 strfunccall : T_ID '(' word ')' { $$ = ap_expr_str_func_make($1, $3, ctx); }
212 void yyerror(ap_expr_parse_ctx_t *ctx, const char *s)
214 /* s is allocated on the stack */
215 ctx->error = apr_pstrdup(ctx->ptemp, s);