知识卡片
编辑距离提示相近拼写
DSL 遇到未知关键字时,可以尝试把该 token 读成标识符,比较它与合法关键字的编辑距离。Levenshtein 距离统计将一段文字变成另一段文字所需的最少插入、删除和替换次数。选择距离最小且足够接近的候选,便能在原 token 的 span 上提示可能的拼写。教学示例中 buck 补上 et 得到 bucket。
如果 token 不是标识符,或所有候选都离输入太远,就保留通用错误。距离阈值会影响提示的准确性,书中清单给出的阈值适合读代码时观察判断路径。
相关卡片 [[自定义关键字驱动解析]]、[[诊断提示正确的属性写法]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Exercise solutions 第 9 章练习:找出最像的关键字 · 查看原书依据
First, we try to parse the input as an Ident, defaulting to the original error when we encounter problems. If the parse works, we can get the string representation of the identifier and compare its distance to the keywords.
参考来源
- 位置:《Write Powerful Rust Macros》附录 A《Exercise solutions》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/appendix-a.xhtml#p157)
- 结论依据:原文介绍提取未知标识符、过滤远距词与选择最近候选,并在清单中将诊断绑定到该标识符 span。
- 原始内容:First, we try to parse the input as an Ident, defaulting to the original error when we encounter problems. If the parse works, we can get the string representation of the identifier and compare its distance to the keywords.