This is an automated email from the ASF dual-hosted git repository. zwoop pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push: new 6ce2507e22 HRW4U: Adds support for HRW if-elif clauses (#12305) 6ce2507e22 is described below commit 6ce2507e22d3a5c8217deee380f0f988b669bdc2 Author: Leif Hedstrom <zw...@apache.org> AuthorDate: Tue Jun 24 13:49:32 2025 -0500 HRW4U: Adds support for HRW if-elif clauses (#12305) --- tools/hrw4u/grammar/hrw4u.g4 | 10 ++++++++-- tools/hrw4u/src/visitor.py | 14 +++++++++++++- tools/hrw4u/tests/data/conds/if-elif.ast.txt | 1 + tools/hrw4u/tests/data/conds/if-elif.input.txt | 11 +++++++++++ tools/hrw4u/tests/data/conds/if-elif.output.txt | 11 +++++++++++ 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/tools/hrw4u/grammar/hrw4u.g4 b/tools/hrw4u/grammar/hrw4u.g4 index 7b6d744012..72110307d4 100644 --- a/tools/hrw4u/grammar/hrw4u.g4 +++ b/tools/hrw4u/grammar/hrw4u.g4 @@ -20,8 +20,9 @@ grammar hrw4u; // ----------------------------- // Lexer Rules // ----------------------------- -VARS : 'VARS'; +VARS : 'VARS'; IF : 'if'; +ELIF : 'elif'; ELSE : 'else'; IN : 'in'; TRUE : [tT][rR][uU][eE]; @@ -119,7 +120,7 @@ statement ; conditional - : ifStatement elseClause? + : ifStatement elifClause* elseClause? ; ifStatement @@ -131,6 +132,11 @@ elseClause : ELSE block ; +elifClause + : ELIF condition block + | ELIF LPAREN condition RPAREN block + ; + block : LBRACE statement* RBRACE ; diff --git a/tools/hrw4u/src/visitor.py b/tools/hrw4u/src/visitor.py index 09fd55f749..cf51c5ec4c 100644 --- a/tools/hrw4u/src/visitor.py +++ b/tools/hrw4u/src/visitor.py @@ -258,6 +258,8 @@ class HRW4UVisitor(hrw4uVisitor): def visitConditional(self, ctx): self._debug_enter("visitConditional") self.visit(ctx.ifStatement()) + for elif_ctx in ctx.elifClause(): + self.visit(elif_ctx) if ctx.elseClause(): self.visit(ctx.elseClause()) self._debug_exit("visitConditional") @@ -274,6 +276,17 @@ class HRW4UVisitor(hrw4uVisitor): self.visit(ctx.block()) self._debug_exit("visitElseClause") + def visitElifClause(self, ctx): + self._debug_enter("visitElifClause") + self.emit_condition("elif", final=True) + self._stmt_indent += 1 + self._cond_indent += 1 + self.visit(ctx.condition()) + self.visit(ctx.block()) + self._stmt_indent -= 1 + self._cond_indent -= 1 + self._debug_exit("visitElifClause") + def visitBlock(self, ctx): self._debug_enter("visitBlock") self._stmt_indent += 1 @@ -284,7 +297,6 @@ class HRW4UVisitor(hrw4uVisitor): def visitCondition(self, ctx): self._debug_enter("visitCondition") - self._cond_indent = 0 self.emit_expression(ctx.expression(), last=True) self._flush_condition() self._debug_exit("visitCondition") diff --git a/tools/hrw4u/tests/data/conds/if-elif.ast.txt b/tools/hrw4u/tests/data/conds/if-elif.ast.txt new file mode 100644 index 0000000000..b6b7a42348 --- /dev/null +++ b/tools/hrw4u/tests/data/conds/if-elif.ast.txt @@ -0,0 +1 @@ +(program (section SEND_RESPONSE { (sectionBody (conditional (ifStatement if (condition (expression (term (factor (comparison (comparable inbound.url.path) == (value "foo")))))) (block { (statement inbound.resp.X-Path = (value "f") ;) })) (elifClause elif (condition (expression (term (factor (comparison (comparable inbound.url.path) == (value "bar")))))) (block { (statement inbound.resp.X-Path = (value "b") ;) })) (elifClause elif (condition (expression (term (factor (comparison (comparab [...] diff --git a/tools/hrw4u/tests/data/conds/if-elif.input.txt b/tools/hrw4u/tests/data/conds/if-elif.input.txt new file mode 100644 index 0000000000..63df35b7dd --- /dev/null +++ b/tools/hrw4u/tests/data/conds/if-elif.input.txt @@ -0,0 +1,11 @@ +SEND_RESPONSE { + if inbound.url.path == "foo" { + inbound.resp.X-Path = "f"; + } elif inbound.url.path == "bar" { + inbound.resp.X-Path = "b"; + } elif inbound.url.path == "hrw" { + inbound.resp.X-Path = "h"; + } else { + inbound.resp.X-path = "other"; + } +} diff --git a/tools/hrw4u/tests/data/conds/if-elif.output.txt b/tools/hrw4u/tests/data/conds/if-elif.output.txt new file mode 100644 index 0000000000..dca92b8735 --- /dev/null +++ b/tools/hrw4u/tests/data/conds/if-elif.output.txt @@ -0,0 +1,11 @@ +cond %{SEND_RESPONSE_HDR_HOOK} [AND] +cond %{CLIENT-URL:PATH} ="foo" + set-header X-Path "f" +elif + cond %{CLIENT-URL:PATH} ="bar" + set-header X-Path "b" +elif + cond %{CLIENT-URL:PATH} ="hrw" + set-header X-Path "h" +else + set-header X-path "other"