知识卡片
浅层语法改写会遗漏节点
要把 panic! 改成错误返回,语法遍历必须能到达含该宏调用的节点。附录将原本处理 if 的逻辑扩展到 Expr::While,扫描循环体的语句,把命中的宏调用变成 return Err(...)。这一做法只覆盖列出的分支及层级。
书中给出的测试保留了其识别的 while 写法,并指出该练习实现会漏过其他形式。若要在整棵语法树递归修改,应检查语句和表达式的节点入口,并考虑使用 syn 的 Visit 或 Fold 接口。
相关卡片 [[修改节点保留外围语法]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Exercise solutions 第 7 章练习:局部修改与遍历边界 · 查看原书依据
This is relatively easy; just add a check for Expr::While and get the statements from body. At this point, you probably want to extract part of the map logic, which is identical to the one for Expr::If.
参考来源
- 位置:《Write Powerful Rust Macros》附录 A《Exercise solutions》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/appendix-a.xhtml#p114)
- 结论依据:原书示范对 While 分支扫描语句,并在后文明确指出带分号形式的覆盖局限及遍历辅助工具。
- 原始内容:This is relatively easy; just add a check for Expr::While and get the statements from body. At this point, you probably want to extract part of the map logic, which is identical to the one for Expr::If.