masaori335 commented on code in PR #13699: URL: https://github.com/apache/trafficserver/pull/13699#discussion_r4034668897
########## tools/hrw4u/tests/ast_unparse.py: ########## @@ -0,0 +1,144 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Render an AST back to hrw4u source. Test-only; see test_ast_roundtrip.py for why. + +Whitespace is not reproduced: the emitter derives its own indentation and never reads the +source's. +""" + +from __future__ import annotations + +from hrw4u.ast_nodes import * + +INDENT = " " + + +def unparse(ast: HRW4UAST) -> str: + return "\n".join(_top_level(item) for item in ast.body) + "\n" + + +def _top_level(node: TopLevelNode) -> str: + match node: + case Comment(): + return node.text + case UseDirective(): + return f"use {node.spec}" + case ProcedureDecl(): + params = ", ".join(_proc_param(p) for p in node.params) + return _braced(f"procedure {node.name}({params})", [_body_item(b, 1) for b in node.body]) + case VarSection(): + keyword = "SESSION_VARS" if node.scope == "session" else "VARS" + return _braced(keyword, [_var_item(d) for d in node.declarations]) + case Section(): + return _braced(node.type, [_body_item(b, 1) for b in node.body]) + raise ValueError(f"unparse: unhandled top-level node {type(node).__name__}") + + +def _braced(header: str, lines: list[str]) -> str: + return "\n".join([f"{header} {{", *(f"{INDENT}{line}" for line in lines), "}"]) + + +def _proc_param(p: ProcParam) -> str: + return f"${p.name}" if p.default is None else f"${p.name}={_value(p.default)}" + + +def _var_item(node: VarDecl | Comment) -> str: + if isinstance(node, Comment): + return node.text + slot = "" if node.slot is None else f" @{node.slot}" + return f"{node.name}: {node.type_name}{slot};" + + +def _body_item(node: BodyNode, depth: int) -> str: + match node: + case Comment(): + return node.text + case Break(): + return "break;" + case FunctionCall(): + return f"{_call(node)};" Review Comment: `no-op;` / `skip-remap;` parse but never compile, so nothing that produces output loses anything here. Out of scope for this PR: the conflation is unchanged from the branch point (`src/ast_visitor.py:118-119`, already pinned by `test_ast_visitor.py`), and a node/flag only means something once we decide whether to drop the grammar alternative or make the bare form work — a language change, not a test change. Filing that separately. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
