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/*                      _             _
18 *  _ __ ___   ___   __| |    ___ ___| |
19 * | '_ ` _ \ / _ \ / _` |   / __/ __| |
20 * | | | | | | (_) | (_| |   \__ \__ \ | mod_ssl - Apache Interface to OpenSSL
21 * |_| |_| |_|\___/ \__,_|___|___/___/_| http://www.modssl.org/
22 *                      |_____|
23 *  ssl_expr_parse.y
24 *  Expression LR(1) Parser
25 */
26                             /* ``What you see is all you get.''
27							              -- Brian Kernighan      */
28
29/*  _________________________________________________________________
30**
31**  Expression Parser
32**  _________________________________________________________________
33*/
34
35%{
36#include "ssl_private.h"
37%}
38
39%union {
40    char     *cpVal;
41    ssl_expr *exVal;
42}
43
44%token  T_TRUE
45%token  T_FALSE
46
47%token  <cpVal> T_DIGIT
48%token  <cpVal> T_ID
49%token  <cpVal> T_STRING
50%token  <cpVal> T_REGEX
51%token  <cpVal> T_REGEX_I
52
53%token  T_FUNC_FILE
54
55%token  T_OP_EQ
56%token  T_OP_NE
57%token  T_OP_LT
58%token  T_OP_LE
59%token  T_OP_GT
60%token  T_OP_GE
61%token  T_OP_REG
62%token  T_OP_NRE
63%token  T_OP_IN
64%token  T_OP_OID
65
66%token  T_OP_OR
67%token  T_OP_AND
68%token  T_OP_NOT
69
70%left   T_OP_OR
71%left   T_OP_AND
72%left   T_OP_NOT
73
74%type   <exVal>   expr
75%type   <exVal>   comparison
76%type   <exVal>   funccall
77%type   <exVal>   regex
78%type   <exVal>   words
79%type   <exVal>   wordlist
80%type   <exVal>   word
81
82%%
83
84root      : expr                         { ssl_expr_info.expr = $1; }
85          ;
86
87expr      : T_TRUE                       { $$ = ssl_expr_make(op_True,  NULL, NULL); }
88          | T_FALSE                      { $$ = ssl_expr_make(op_False, NULL, NULL); }
89          | T_OP_NOT expr                { $$ = ssl_expr_make(op_Not,   $2,   NULL); }
90          | expr T_OP_OR expr            { $$ = ssl_expr_make(op_Or,    $1,   $3);   }
91          | expr T_OP_AND expr           { $$ = ssl_expr_make(op_And,   $1,   $3);   }
92          | comparison                   { $$ = ssl_expr_make(op_Comp,  $1,   NULL); }
93          | '(' expr ')'                 { $$ = $2; }
94          ;
95
96comparison: word T_OP_EQ word            { $$ = ssl_expr_make(op_EQ,  $1, $3); }
97          | word T_OP_NE word            { $$ = ssl_expr_make(op_NE,  $1, $3); }
98          | word T_OP_LT word            { $$ = ssl_expr_make(op_LT,  $1, $3); }
99          | word T_OP_LE word            { $$ = ssl_expr_make(op_LE,  $1, $3); }
100          | word T_OP_GT word            { $$ = ssl_expr_make(op_GT,  $1, $3); }
101          | word T_OP_GE word            { $$ = ssl_expr_make(op_GE,  $1, $3); }
102          | word T_OP_IN wordlist        { $$ = ssl_expr_make(op_IN,  $1, $3); }
103          | word T_OP_REG regex          { $$ = ssl_expr_make(op_REG, $1, $3); }
104          | word T_OP_NRE regex          { $$ = ssl_expr_make(op_NRE, $1, $3); }
105          ;
106
107wordlist  : T_OP_OID '(' word ')'	 { $$ = ssl_expr_make(op_OidListElement, $3, NULL); }
108          | '{' words '}'                { $$ = $2 ; }
109	  ;
110
111words     : word                         { $$ = ssl_expr_make(op_ListElement, $1, NULL); }
112          | words ',' word               { $$ = ssl_expr_make(op_ListElement, $3, $1);   }
113          ;
114
115word      : T_DIGIT                      { $$ = ssl_expr_make(op_Digit,  $1, NULL); }
116          | T_STRING                     { $$ = ssl_expr_make(op_String, $1, NULL); }
117          | '%' '{' T_ID '}'             { $$ = ssl_expr_make(op_Var,    $3, NULL); }
118          | funccall                     { $$ = $1; }
119          ;
120
121regex     : T_REGEX {
122                ap_regex_t *regex;
123                if ((regex = ap_pregcomp(ssl_expr_info.pool, $1,
124                                         AP_REG_EXTENDED|AP_REG_NOSUB)) == NULL) {
125                    ssl_expr_error = "Failed to compile regular expression";
126                    YYERROR;
127                }
128                $$ = ssl_expr_make(op_Regex, regex, NULL);
129            }
130          | T_REGEX_I {
131                ap_regex_t *regex;
132                if ((regex = ap_pregcomp(ssl_expr_info.pool, $1,
133                                         AP_REG_EXTENDED|AP_REG_NOSUB|AP_REG_ICASE)) == NULL) {
134                    ssl_expr_error = "Failed to compile regular expression";
135                    YYERROR;
136                }
137                $$ = ssl_expr_make(op_Regex, regex, NULL);
138            }
139          ;
140
141funccall  : T_FUNC_FILE '(' T_STRING ')' {
142               ssl_expr *args = ssl_expr_make(op_ListElement, $3, NULL);
143               $$ = ssl_expr_make(op_Func, "file", args);
144            }
145          ;
146
147%%
148
149int yyerror(char *s)
150{
151    ssl_expr_error = s;
152    return 2;
153}
154
155