知识卡片
分隔语法简化属性解析
明确的分隔符可以缩小解析器需要判断的范围。书中把 Lambda 属性写成 lambda (name = my_name, mem = 1024, time = 15)。syn::parenthesized! 先取出括号内的 token 流,Punctuated::<KeyValue, Token!(,)>::parse_terminated 再负责消费逗号分隔的属性列表。
每个 KeyValue 消费键名、= 和相应的值。字符串键便于按已知属性检查和分派,name 先读取标识符,数值属性先读取整数 token。若中间表示统一保存字符串,组装实际资源时仍要转换数值并检查缺失项。书中的简化清单用 unwrap 执行部分转换,完整实现可在这些位置继续返回带 span 的诊断。
相关卡片 [[自定义关键字驱动解析]]、[[类型化属性汇入构建器]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Writing an infrastructure DSL 分隔键值与类型化属性 · 查看原书依据
When we have the content, we know the properties are separated by commas, with every property consisting of a key separated by a value. That kind of repetition calls for Punctuated. So we use it to parse the properties. For the name = value part, we create a struct called KeyValue (as in key-value pairs), which we will discuss in a moment.