知识卡片
令牌流与语法树的分工
词法分析把文本切成令牌,1 + 11 分成两个字面量和加号。令牌树还能包含界定符分组,令牌流是一列令牌树。解析解释它们怎样构成表达式或类型,语法树则提供名字、字段和子表达式等结构。过程宏入口使用 proc_macro::TokenStream,内部可用 proc_macro2 和解析库。Span 记录代码位置,可把出错令牌指回来源。
相关卡片 [[解析与插值生成实现]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- A “Hello, World” procedural macro 令牌、语法树和返回协议 · 查看原书依据
Parsing turns this information into an Abstract Syntax Tree (AST; http://mng.bz/5lKZ), a tree-like representation of all relevant data in your program. This AST makes it easier for the Rust compiler (as well as compilers in other languages) to do its work (i.e., creating an executable that computers can understand).
参考来源
- 位置:《Write Powerful Rust Macros》第03章《A “Hello, World” procedural macro》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-3.xhtml#p37)
- 结论依据:原文以 1 + 11 比较词法与语法表示,后续定义 TokenTree 并说明 proc_macro2 的转换。
- 原始内容:Parsing turns this information into an Abstract Syntax Tree (AST; http://mng.bz/5lKZ), a tree-like representation of all relevant data in your program. This AST makes it easier for the Rust compiler (as well as compilers in other languages) to do its work (i.e., creating an executable that computers can understand).