知识卡片
属性宏参数的结构化解析
自定义语法决定解析入口。属性宏的 exclude(fourth, third) 可先解析成 MetaList,验证路径名为 exclude,再用 Punctuated<Ident, Token![,]>::parse_terminated 解析组内标识符。另一种 syn 2 写法是用 syn::meta::parser 处理整个参数流,回调内再用 parse_nested_meta 进入列表,并对未知键返回定位错误。该方案允许空输入作为缺省,坏输入仍可以诊断。syn 1 的 AttributeArgs 与 MetaList.nested 是另一主版本的接口。
相关卡片 [[属性宏的两个输入]]、[[属性形状决定解析方法]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Builder with attributes 属性宏输入与参数解析 · 查看原书依据
A more up-to-date alternative to our current approach is to use syn::meta ::parser. The implementation in listing 8.39 is quite similar to a documentation example. We parse_macro_input!(attr with attr_parser) to pass in a custom parser for the attributes, which we defined one line above using syn::meta::parser. Actual parsing is a task for our custom struct, which checks whether we have an exclude attribute in the path property, throwing an error if we receive something else.