]> granicus.if.org Git - apache/blob - server/util_expr_parse.y
event: child_main() never returns, so remove some dead code after
[apache] / server / util_expr_parse.y
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /* based on ap_expr_parse.y from mod_ssl */
18
19 /*  _________________________________________________________________
20 **
21 **  Expression Parser
22 **  _________________________________________________________________
23 */
24
25 %pure-parser
26 %error-verbose
27 %defines
28 %lex-param   { void *yyscanner }
29 %parse-param { ap_expr_parse_ctx_t *ctx }
30
31 %{
32 #include "util_expr_private.h"
33 %}
34
35 %union {
36     char      *cpVal;
37     ap_expr_t *exVal;
38     int        num;
39 }
40
41 %token  T_TRUE
42 %token  T_FALSE
43
44 %token  <cpVal> T_ERROR
45
46 %token  <cpVal> T_DIGIT
47 %token  <cpVal> T_ID
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
54
55 %token  T_STR_BEGIN
56 %token  T_STR_END
57 %token  T_VAR_BEGIN
58 %token  T_VAR_END
59
60 %token  T_OP_EQ
61 %token  T_OP_NE
62 %token  T_OP_LT
63 %token  T_OP_LE
64 %token  T_OP_GT
65 %token  T_OP_GE
66 %token  T_OP_REG
67 %token  T_OP_NRE
68 %token  T_OP_IN
69 %token  T_OP_STR_EQ
70 %token  T_OP_STR_NE
71 %token  T_OP_STR_LT
72 %token  T_OP_STR_LE
73 %token  T_OP_STR_GT
74 %token  T_OP_STR_GE
75 %token  T_OP_CONCAT
76
77 %token  T_OP_OR
78 %token  T_OP_AND
79 %token  T_OP_NOT
80
81 %left   T_OP_OR
82 %left   T_OP_AND
83 %left   T_OP_NOT
84 %left   T_OP_CONCAT
85
86 %type   <exVal>   expr
87 %type   <exVal>   comparison
88 %type   <exVal>   strfunccall
89 %type   <exVal>   lstfunccall
90 %type   <exVal>   regex
91 %type   <exVal>   words
92 %type   <exVal>   wordlist
93 %type   <exVal>   word
94 %type   <exVal>   string
95 %type   <exVal>   strpart
96 %type   <exVal>   var
97 %type   <exVal>   backref
98
99 %{
100 #include "util_expr_private.h"
101 #define yyscanner ctx->scanner
102
103 int ap_expr_yylex(YYSTYPE *lvalp, void *scanner);
104 %}
105
106
107 %%
108
109 root      : expr                         { ctx->expr = $1; }
110           | T_ERROR                      { YYABORT; }
111           ;
112
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; }
122           ;
123
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); }
139           ;
140
141 wordlist  : lstfunccall                  { $$ = $1; }
142           | '{' words '}'                { $$ = $2; }
143           ;
144
145 words     : word                         { $$ = ap_expr_make(op_ListElement, $1, NULL, ctx); }
146           | words ',' word               { $$ = ap_expr_make(op_ListElement, $3, $1,   ctx); }
147           ;
148
149 string    : string strpart               { $$ = ap_expr_make(op_Concat, $1, $2, ctx); }
150           | strpart                      { $$ = $1; }
151           ;
152
153 strpart   : T_STRING                     { $$ = ap_expr_make(op_String, $1, NULL, ctx); }
154           | var                          { $$ = $1; }
155           | backref                      { $$ = $1; }
156           ;
157
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); }
160           ;
161
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); }
164           | var                          { $$ = $1; }
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); }
169           ;
170
171 regex     : T_REGEX {
172                 ap_regex_t *regex;
173                 if ((regex = ap_pregcomp(ctx->pool, $1,
174                                          AP_REG_EXTENDED|AP_REG_NOSUB)) == NULL) {
175                     ctx->error = "Failed to compile regular expression";
176                     YYERROR;
177                 }
178                 $$ = ap_expr_make(op_Regex, regex, NULL, ctx);
179             }
180           | T_REGEX_I {
181                 ap_regex_t *regex;
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";
185                     YYERROR;
186                 }
187                 $$ = ap_expr_make(op_Regex, regex, NULL, ctx);
188             }
189           ;
190
191 backref     : T_REGEX_BACKREF   {
192                 int *n = apr_palloc(ctx->pool, sizeof(int));
193                 *n = $1;
194                 $$ = ap_expr_make(op_RegexBackref, n, NULL, ctx);
195             }
196             ;
197
198 lstfunccall : T_ID '(' word ')' { $$ = ap_expr_list_func_make($1, $3, ctx); }
199             ;
200
201 strfunccall : T_ID '(' word ')' { $$ = ap_expr_str_func_make($1, $3, ctx); }
202             ;
203
204 %%
205
206 void yyerror(ap_expr_parse_ctx_t *ctx, char *s)
207 {
208     /* s is allocated on the stack */
209     ctx->error = apr_pstrdup(ctx->ptemp, s);
210 }
211