diff options
author | Yann Ylavic <ylavic@apache.org> | 2017-10-04 18:18:46 +0200 |
---|---|---|
committer | Yann Ylavic <ylavic@apache.org> | 2017-10-04 18:18:46 +0200 |
commit | e172ca33e2ab5f97b08ec96bc6d138ba021a3f02 (patch) | |
tree | 3348b47782f97bd4189a66410aed3681d52177b7 /server/util_expr_parse.y | |
parent | On the trunk: (diff) | |
download | apache2-e172ca33e2ab5f97b08ec96bc6d138ba021a3f02.tar.xz apache2-e172ca33e2ab5f97b08ec96bc6d138ba021a3f02.zip |
ap_expr: follow up to r1810605.
The "split" and "join" operators are now a prefix, ala perl.
Add the "sub" operator for string substitutions, prefix still.
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1811104 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'server/util_expr_parse.y')
-rw-r--r-- | server/util_expr_parse.y | 159 |
1 files changed, 71 insertions, 88 deletions
diff --git a/server/util_expr_parse.y b/server/util_expr_parse.y index 84eb054e6f..b6d586c614 100644 --- a/server/util_expr_parse.y +++ b/server/util_expr_parse.y @@ -50,12 +50,12 @@ %token <cpVal> T_ID "identifier" %token <cpVal> T_STRING "string literal" -%token T_REGEX "matching regex" -%token T_REGSUB "substitution regex" +%token T_REGEX "start of matching regex" +%token T_REGSUB "start of substitution regex" %token <cpVal> T_REG_MATCH "pattern of the regex" -%token <cpVal> T_REG_SUBST "replacement of the regex" +%token <cpVal> T_REG_SUBST "substitution of the regex" %token <cpVal> T_REG_FLAGS "pattern flags of the regex" -%token <num> T_REG_REF "capture reference in the regex" +%token <num> T_BACKREF "regex back reference" %token <cpVal> T_OP_UNARY "unary operator" %token <cpVal> T_OP_BINARY "binary operator" @@ -85,33 +85,35 @@ %token T_OP_CONCAT "string concatenation" -%token T_OP_SPLIT "split operator" %token T_OP_JOIN "join operator" +%token T_OP_SPLIT "split operator" +%token T_OP_SUB "substitute operator" %token T_OP_OR "logical or" %token T_OP_AND "logical and" %token T_OP_NOT "logical not" -%right T_OP_OR -%right T_OP_AND +%left T_OP_OR +%left T_OP_AND %right T_OP_NOT %right T_OP_CONCAT %type <exVal> cond "condition" %type <exVal> comp "comparison" %type <exVal> strfunc "string function" -%type <exVal> lstfunc "list function" -%type <exVal> wordlist "list of words" -%type <exVal> words "tuple of words" +%type <exVal> listfunc "list function" +%type <exVal> list "list" +%type <exVal> words "words" %type <exVal> word "word" %type <exVal> string "string" %type <exVal> substr "substring" %type <exVal> var "variable" -%type <exVal> regex "regex match" -%type <exVal> regsub "regex substitution" -%type <exVal> regsplit "regex split" -%type <exVal> regany "regex any" -%type <exVal> regref "regex capture reference" +%type <exVal> regex "match regex" +%type <exVal> regsub "substitution regex" +%type <exVal> regany "any regex" +%type <exVal> split "split" +%type <exVal> join "join" +%type <exVal> sub "sub" %{ #include "util_expr_private.h" @@ -123,8 +125,13 @@ int ap_expr_yylex(YYSTYPE *lvalp, void *scanner); %% -root : T_EXPR_BOOL cond { ctx->expr = $2; } - | T_EXPR_STRING string { ctx->expr = $2; } +expr : T_EXPR_STRING string { ctx->expr = $2; } + | T_EXPR_BOOL cond { ctx->expr = $2; } + | T_ERROR { YYABORT; } + ; + +string : substr { $$ = $1; } + | string substr { $$ = ap_expr_concat_make($1, $2, ctx); } | T_ERROR { YYABORT; } ; @@ -152,60 +159,69 @@ comp : word T_OP_EQ word { $$ = ap_expr_make(op_EQ, $1, $3, | word T_OP_STR_LE word { $$ = ap_expr_make(op_STR_LE, $1, $3, ctx); } | word T_OP_STR_GT word { $$ = ap_expr_make(op_STR_GT, $1, $3, ctx); } | word T_OP_STR_GE word { $$ = ap_expr_make(op_STR_GE, $1, $3, ctx); } - | word T_OP_IN wordlist { $$ = ap_expr_make(op_IN, $1, $3, ctx); } | word T_OP_REG regex { $$ = ap_expr_make(op_REG, $1, $3, ctx); } | word T_OP_NRE regex { $$ = ap_expr_make(op_NRE, $1, $3, ctx); } + | word T_OP_IN list { $$ = ap_expr_make(op_IN, $1, $3, ctx); } ; -wordlist : lstfunc { $$ = $1; } - | word T_OP_REG regsplit { $$ = ap_expr_list_regex_make($1, $3, ctx); } - | wordlist T_OP_REG regany { $$ = ap_expr_list_regex_make($1, $3, ctx); } - | '{' words '}' { $$ = $2; } - | '(' wordlist ')' { $$ = $2; } - ; - -words : word { $$ = ap_expr_make(op_ListElement, $1, NULL, ctx); } - | word ',' words { $$ = ap_expr_make(op_ListElement, $1, $3, ctx); } +word : T_DIGIT { $$ = ap_expr_make(op_Digit, $1, NULL, ctx); } + | T_STR_BEGIN T_STR_END { $$ = ap_expr_make(op_String, "", NULL, ctx); } + | T_STR_BEGIN string T_STR_END { $$ = $2; } + | word T_OP_CONCAT word { $$ = ap_expr_make(op_Concat, $1, $3, ctx); } + | var { $$ = $1; } + | sub { $$ = $1; } + | join { $$ = $1; } + | strfunc { $$ = $1; } + | '(' word ')' { $$ = $2; } ; -string : substr { $$ = $1; } - | string substr { $$ = ap_expr_concat_make($1, $2, ctx); } - | T_ERROR { YYABORT; } +list : split { $$ = $1; } + | listfunc { $$ = $1; } + | '{' words '}' { $$ = $2; } + | '(' list ')' { $$ = $2; } ; substr : T_STRING { $$ = ap_expr_make(op_String, $1, NULL, ctx); } | var { $$ = $1; } - | regref { $$ = $1; } ; var : T_VAR_BEGIN T_ID T_VAR_END { $$ = ap_expr_var_make($2, ctx); } | T_VAR_BEGIN T_ID ':' string T_VAR_END { $$ = ap_expr_str_func_make($2, $4, ctx); } - | T_VAREXP_BEGIN word T_VAREXP_END { $$ = ap_expr_str_word_make($2, ctx); } - | T_VAREXP_BEGIN cond T_VAREXP_END { $$ = ap_expr_str_bool_make($2, ctx); } + | T_VAREXP_BEGIN cond T_VAREXP_END { $$ = ap_expr_make(op_Bool, $2, NULL, ctx); } + | T_VAREXP_BEGIN word T_VAREXP_END { $$ = ap_expr_make(op_Word, $2, NULL, ctx); } + | T_BACKREF { $$ = ap_expr_backref_make($1, ctx); } ; -word : T_DIGIT { $$ = ap_expr_make(op_Digit, $1, NULL, ctx); } - | T_STR_BEGIN T_STR_END { $$ = ap_expr_make(op_String, "", NULL, ctx); } - | T_STR_BEGIN string T_STR_END { $$ = $2; } - | word T_OP_CONCAT word { $$ = ap_expr_make(op_Concat, $1, $3, ctx); } - | word T_OP_REG regsub { $$ = ap_expr_make(op_Regsub, $1, $3, ctx); } - | var { $$ = $1; } - | regref { $$ = $1; } - | strfunc { $$ = $1; } - | T_OP_JOIN wordlist { - $$ = ap_expr_make(op_Join, $2, NULL, ctx); - } - | T_OP_JOIN wordlist ',' word { - $$ = ap_expr_make(op_Join, $2, $4, ctx); - } - | T_OP_JOIN '(' wordlist ',' word ')' { - $$ = ap_expr_make(op_Join, $3, $5, ctx); - } - | '(' word ')' { $$ = $2; } +strfunc : T_ID '(' word ')' { $$ = ap_expr_str_func_make($1, $3, ctx); } + | T_ID '(' words ')' { $$ = ap_expr_str_func_make($1, $3, ctx); } + ; + +listfunc : T_ID '(' word ')' { $$ = ap_expr_list_func_make($1, $3, ctx); } + /* | T_ID '(' words ')' { $$ = ap_expr_list_func_make($1, $3, ctx); } */ + ; + +sub : T_OP_SUB regsub ',' word { $$ = ap_expr_make(op_Sub, $4, $2, ctx); } + | T_OP_SUB '(' regsub ',' word ')' { $$ = ap_expr_make(op_Sub, $5, $3, ctx); } + ; + +join : T_OP_JOIN list { $$ = ap_expr_make(op_Join, $2, NULL, ctx); } + | T_OP_JOIN '(' list ')' { $$ = ap_expr_make(op_Join, $3, NULL, ctx); } + | T_OP_JOIN list ',' word { $$ = ap_expr_make(op_Join, $2, $4, ctx); } + | T_OP_JOIN '(' list ',' word ')' { $$ = ap_expr_make(op_Join, $3, $5, ctx); } + ; + +split : T_OP_SPLIT regany ',' list { $$ = ap_expr_make(op_Split, $4, $2, ctx); } + | T_OP_SPLIT '(' regany ',' list ')' { $$ = ap_expr_make(op_Split, $5, $3, ctx); } + | T_OP_SPLIT regany ',' word { $$ = ap_expr_make(op_Split, $4, $2, ctx); } + | T_OP_SPLIT '(' regany ',' word ')' { $$ = ap_expr_make(op_Split, $5, $3, ctx); } + ; + +words : word { $$ = ap_expr_make(op_ListElement, $1, NULL, ctx); } + | word ',' words { $$ = ap_expr_make(op_ListElement, $1, $3, ctx); } ; regex : T_REGEX T_REG_MATCH T_REG_FLAGS { - ap_expr_t *e = ap_expr_regex_make($2, $3, NULL, 0, ctx); + ap_expr_t *e = ap_expr_regex_make($2, NULL, $3, ctx); if (!e) { ctx->error = "Failed to compile regular expression"; YYERROR; @@ -214,7 +230,7 @@ regex : T_REGEX T_REG_MATCH T_REG_FLAGS { } ; regsub : T_REGSUB T_REG_MATCH string T_REG_FLAGS { - ap_expr_t *e = ap_expr_regex_make($2, $4, $3, 0, ctx); + ap_expr_t *e = ap_expr_regex_make($2, $3, $4, ctx); if (!e) { ctx->error = "Failed to compile regular expression"; YYERROR; @@ -222,41 +238,8 @@ regsub : T_REGSUB T_REG_MATCH string T_REG_FLAGS { $$ = e; } ; -regsplit : T_OP_SPLIT T_REG_MATCH string T_REG_FLAGS { - /* Returns a list: - * <word> ~= split/:// - * => split around ':', replace it with empty - * <word> ~= split/:/\n/ - * => split around ':', replace it with '\n' - * <list> ~= split/.*?Ip Address:([^,]+)/$1/ - * => split around the whole match, replace it with $1 - */ - ap_expr_t *e = ap_expr_regex_make($2, $4, $3, 1, ctx); - if (!e) { - ctx->error = "Failed to compile regular expression"; - YYERROR; - } - $$ = e; - } - ; -regany : regex { $$ = $1; } - | regsub { $$ = $1; } - | regsplit { $$ = $1; } - ; - -regref : T_REG_REF { - int *n = apr_palloc(ctx->pool, sizeof(int)); - *n = $1; - $$ = ap_expr_make(op_Regref, n, NULL, ctx); - } - ; - -lstfunc : T_ID '(' word ')' { $$ = ap_expr_list_func_make($1, $3, ctx); } - /* | T_ID '(' words ')' { $$ = ap_expr_list_func_make($1, $3, ctx); } */ - ; - -strfunc : T_ID '(' word ')' { $$ = ap_expr_str_func_make($1, $3, ctx); } - | T_ID '(' words ')' { $$ = ap_expr_str_func_make($1, $3, ctx); } +regany : regex { $$ = $1; } + | regsub { $$ = $1; } ; %% |