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"
44 %token <cpVal> T_ERROR
46 %token <cpVal> T_DIGIT
48 %token <cpVal> T_STRING
49 %token <cpVal> T_REGEX
50 %token <cpVal> T_REGEX_I
51 %token <num> T_REGEX_BACKREF
52 %token <cpVal> T_OP_UNARY
53 %token <cpVal> T_OP_BINARY
87 %type <exVal> comparison
88 %type <exVal> strfunccall
89 %type <exVal> lstfunccall
92 %type <exVal> wordlist
100 #include "util_expr_private.h"
101 #define yyscanner ctx->scanner
103 int ap_expr_yylex(YYSTYPE *lvalp, void *scanner);
109 root : expr { ctx->expr = $1; }
110 | T_ERROR { YYABORT; }
113 expr : T_TRUE { $$ = ap_expr_make(op_True, NULL, NULL, ctx); }
114 | T_FALSE { $$ = ap_expr_make(op_False, NULL, NULL, ctx); }
115 | T_OP_NOT expr { $$ = ap_expr_make(op_Not, $2, NULL, ctx); }
116 | expr T_OP_OR expr { $$ = ap_expr_make(op_Or, $1, $3, ctx); }
117 | expr T_OP_AND expr { $$ = ap_expr_make(op_And, $1, $3, ctx); }
118 | comparison { $$ = ap_expr_make(op_Comp, $1, NULL, ctx); }
119 | T_OP_UNARY word { $$ = ap_expr_unary_op_make( $1, $2, ctx); }
120 | word T_OP_BINARY word { $$ = ap_expr_binary_op_make($2, $1, $3, ctx); }
121 | '(' expr ')' { $$ = $2; }
124 comparison: word T_OP_EQ word { $$ = ap_expr_make(op_EQ, $1, $3, ctx); }
125 | word T_OP_NE word { $$ = ap_expr_make(op_NE, $1, $3, ctx); }
126 | word T_OP_LT word { $$ = ap_expr_make(op_LT, $1, $3, ctx); }
127 | word T_OP_LE word { $$ = ap_expr_make(op_LE, $1, $3, ctx); }
128 | word T_OP_GT word { $$ = ap_expr_make(op_GT, $1, $3, ctx); }
129 | word T_OP_GE word { $$ = ap_expr_make(op_GE, $1, $3, ctx); }
130 | word T_OP_STR_EQ word { $$ = ap_expr_make(op_STR_EQ, $1, $3, ctx); }
131 | word T_OP_STR_NE word { $$ = ap_expr_make(op_STR_NE, $1, $3, ctx); }
132 | word T_OP_STR_LT word { $$ = ap_expr_make(op_STR_LT, $1, $3, ctx); }
133 | word T_OP_STR_LE word { $$ = ap_expr_make(op_STR_LE, $1, $3, ctx); }
134 | word T_OP_STR_GT word { $$ = ap_expr_make(op_STR_GT, $1, $3, ctx); }
135 | word T_OP_STR_GE word { $$ = ap_expr_make(op_STR_GE, $1, $3, ctx); }
136 | word T_OP_IN wordlist { $$ = ap_expr_make(op_IN, $1, $3, ctx); }
137 | word T_OP_REG regex { $$ = ap_expr_make(op_REG, $1, $3, ctx); }
138 | word T_OP_NRE regex { $$ = ap_expr_make(op_NRE, $1, $3, ctx); }
141 wordlist : lstfunccall { $$ = $1; }
142 | '{' words '}' { $$ = $2; }
145 words : word { $$ = ap_expr_make(op_ListElement, $1, NULL, ctx); }
146 | words ',' word { $$ = ap_expr_make(op_ListElement, $3, $1, ctx); }
149 string : string strpart { $$ = ap_expr_make(op_Concat, $1, $2, ctx); }
150 | strpart { $$ = $1; }
153 strpart : T_STRING { $$ = ap_expr_make(op_String, $1, NULL, ctx); }
155 | backref { $$ = $1; }
158 var : T_VAR_BEGIN T_ID T_VAR_END { $$ = ap_expr_var_make($2, ctx); }
159 | T_VAR_BEGIN T_ID ':' string T_VAR_END { $$ = ap_expr_str_func_make($2, $4, ctx); }
162 word : T_DIGIT { $$ = ap_expr_make(op_Digit, $1, NULL, ctx); }
163 | word T_OP_CONCAT word { $$ = ap_expr_make(op_Concat, $1, $3, ctx); }
165 | backref { $$ = $1; }
166 | strfunccall { $$ = $1; }
167 | T_STR_BEGIN string T_STR_END { $$ = $2; }
168 | T_STR_BEGIN T_STR_END { $$ = ap_expr_make(op_String, "", NULL, ctx); }
173 if ((regex = ap_pregcomp(ctx->pool, $1,
174 AP_REG_EXTENDED|AP_REG_NOSUB)) == NULL) {
175 ctx->error = "Failed to compile regular expression";
178 $$ = ap_expr_make(op_Regex, regex, NULL, ctx);
182 if ((regex = ap_pregcomp(ctx->pool, $1,
183 AP_REG_EXTENDED|AP_REG_NOSUB|AP_REG_ICASE)) == NULL) {
184 ctx->error = "Failed to compile regular expression";
187 $$ = ap_expr_make(op_Regex, regex, NULL, ctx);
191 backref : T_REGEX_BACKREF {
192 int *n = apr_palloc(ctx->pool, sizeof(int));
194 $$ = ap_expr_make(op_RegexBackref, n, NULL, ctx);
198 lstfunccall : T_ID '(' word ')' { $$ = ap_expr_list_func_make($1, $3, ctx); }
201 strfunccall : T_ID '(' word ')' { $$ = ap_expr_str_func_make($1, $3, ctx); }
206 void yyerror(ap_expr_parse_ctx_t *ctx, char *s)
208 /* s is allocated on the stack */
209 ctx->error = apr_pstrdup(ctx->ptemp, s);