如何编写一个生成模块的Rust编译器插件?

骑士42

我正在编写一个可扩展的Rust编译器插件

choose! {
    test_a
    test_b
}

#[cfg(feature = "a")]
mod test_a;
#[cfg(feature = "b")]
mod test_b;

现在几乎完成了,但是该模块在最终扩展的代码中不包含任何内容。我想原因是跨度没有覆盖模块文件。

use syntax::ast;
use syntax::ptr::P;
use syntax::codemap;
use syntax::parse::token;
use syntax::tokenstream::TokenTree;
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
use syntax::ext::build::AstBuilder;
use syntax_pos::Span;
use rustc_plugin::Registry;
use syntax::util::small_vector::SmallVector;

// Ideally, it will expand
//
// ```rust
// choose! {
//   test_a
//   test_b
// }
// ```
// to
// ```rust
// #[cfg(feature = "a")]
// mod test_a;
// #[cfg(feature = "b")]
// mod test_b;
// ```
//
// but the modules contain nothing in the expanded code at present

fn choose(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<MacResult + 'static> {
    let mut test_mods: SmallVector<P<ast::Item>> = SmallVector::many(vec![]);
    for arg in args {
        let mut attrs = vec![];
        let text = match arg {
            &TokenTree::Token(_, token::Ident(s)) => s.to_string(),
            _ => {
                return DummyResult::any(sp);
            }
        };
        let cfg_str = token::InternedString::new("cfg");
        let feat_str = token::InternedString::new("feature");
        attrs.push(cx.attribute(sp,
                                cx.meta_list(sp,
                                             cfg_str,
                                             vec![cx.meta_name_value(sp,
                                                                     feat_str,
                                                                     ast::LitKind::Str(token::intern_and_get_ident(text.trim_left_matches("test_")), ast::StrStyle::Cooked))])));
        test_mods.push(P(ast::Item {
            ident: cx.ident_of(text.as_str()),
            attrs: attrs,
            id: ast::DUMMY_NODE_ID,
            node: ast::ItemKind::Mod(
                // === How to include the specified module file here? ===
                ast::Mod {
                    inner: codemap::DUMMY_SP,
                    items: vec![],
                }
            ),
            vis: ast::Visibility::Inherited,
            span: sp,
        }))
    }

    MacEager::items(test_mods)
}

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
    reg.register_macro("choose", choose);
}

要点

骑士42

2016-08-25更新: 用于libsyntax::parse::new_parser_from_source_str避免手动设置模块路径。 new_parser_from_source_str 只会在CWD定位模块,这是意外的。

正如@Francis所指出的,模块文件的真实路径可能类似于 foo/mod.rs,我发现了一个名为的函数new_parser_from_source_str,该函数可以从中的源字符串创建一个新的解析器libsyntax::parse,因此我决定要求编译器为我处理这种情况所以我必须手动处理这种情况 更新的代码:

fn choose(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<MacResult + 'static> {
    let mut test_mods = SmallVector::zero();
    let cfg_str = intern("cfg");
    let ftre_str = intern("feature");
    for arg in args {
        let mut attrs = vec![];
        let mod_name = match arg {
            &TokenTree::Token(_, token::Ident(s)) => s.to_string(),
            _ => {
                return DummyResult::any(sp);
            }
        };
        attrs.push(cx.attribute(sp,
                                cx.meta_list(sp,
                                             // simply increase the reference counter
                                             cfg_str.clone(),
                                             vec![cx.meta_name_value(sp,
                                                                     ftre_str.clone(),
                                                                     ast::LitKind::Str(intern(mod_name.trim_left_matches("test_")), ast::StrStyle::Cooked))])));

        let mut mod_path = PathBuf::from(&cx.codemap().span_to_filename(sp));
        let dir = mod_path.parent().expect("no parent directory").to_owned();
        let default_path = dir.join(format!("{}.rs", mod_name.as_str()));
        let secondary_path = dir.join(format!("{}/mod.rs", mod_name.as_str()));
        match (default_path.exists(), secondary_path.exists()) {
            (false, false) => {
                cx.span_err(sp, &format!("file not found for module `{}`", mod_name.as_str()));
                return DummyResult::any(sp);
            }
            (true, true) => {
                cx.span_err(sp, &format!("file for module `{}` found at both {} and {}", mod_name.as_str(), default_path.display(), secondary_path.display()));
                return DummyResult::any(sp);
            }
            (true, false) => mod_path = default_path,
            (false, true) => mod_path = secondary_path,
        }

        test_mods.push(P(ast::Item {
            ident: cx.ident_of(mod_name.as_str()),
            attrs: attrs,
            id: ast::DUMMY_NODE_ID,
            node: ast::ItemKind::Mod(
                ast::Mod {
                    inner: sp,
                    items: expand_include(cx, sp, &mod_path),
                }
            ),
            vis: ast::Visibility::Inherited,
            span: sp,
        }))
    }

    MacEager::items(test_mods)
}

终于我找到了解决方案!\ o /

Rust处理模块文件的过程就像include!结果,我查看了宏的实现include!,可以在此处找到该宏,并将其重写为适合我的需求的宏

use ::std::path::Path;
use ::std::path::PathBuf;
use syntax::parse::{self, token};
use syntax::errors::FatalError;
macro_rules! panictry {
    ($e:expr) => ({
        match $e {
            Ok(e) => e,
            Err(mut e) => {
                e.emit();
                panic!(FatalError);
            }
        }
    })
}

pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, file: &Path) -> Vec<P<ast::Item>> {
    let mut p = parse::new_sub_parser_from_file(cx.parse_sess(), cx.cfg(), file, true, None, sp);
    let mut ret = vec![];
    while p.token != token::Eof {
        match panictry!(p.parse_item()) {
            Some(item) => ret.push(item),
            None => {
                panic!(p.diagnostic().span_fatal(p.span,
                                                 &format!("expected item, found `{}`", p.this_token_to_string())))
            }
        }
    }
    ret
}

要从模块文件中获取项目,我们必须找出真实的模块路径:

let mut mod_path = PathBuf::from(&cx.codemap().span_to_filename(sp));
mod_path.set_file_name(mod_name.as_str());
mod_path.set_extension("rs");

然后我们可以像这样构造我们的模块节点:

P(ast::Item {
    ident: cx.ident_of(mod_name.as_str()),
    attrs: attrs,
    id: ast::DUMMY_NODE_ID,
    node: ast::ItemKind::Mod(ast::Mod {
        inner: sp,
        items: expand_include(cx, sp, &mod_path),
    }),
    vis: ast::Visibility::Inherited,
    span: sp,
})

总而言之,应将插件重写如下:

#![feature(plugin_registrar, rustc_private)]

extern crate syntax;
extern crate rustc_plugin;

use syntax::ast;
use syntax::ptr::P;
use syntax::codemap::Span;
use syntax::parse::{self, token};
use syntax::tokenstream::TokenTree;
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
use syntax::errors::FatalError;
use syntax::ext::build::AstBuilder;
use rustc_plugin::Registry;
use syntax::util::small_vector::SmallVector;

use ::std::path::Path;
use ::std::path::PathBuf;

macro_rules! panictry {
    ($e:expr) => ({
        match $e {
            Ok(e) => e,
            Err(mut e) => {
                e.emit();
                panic!(FatalError);
            }
        }
    })
}

pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, file: &Path) -> Vec<P<ast::Item>> {
    let mut p = parse::new_sub_parser_from_file(cx.parse_sess(), cx.cfg(), file, true, None, sp);
    let mut ret = vec![];
    while p.token != token::Eof {
        match panictry!(p.parse_item()) {
            Some(item) => ret.push(item),
            None => {
                panic!(p.diagnostic().span_fatal(p.span,
                                                 &format!("expected item, found `{}`", p.this_token_to_string())))
            }
        }
    }
    ret
}

fn intern(s: &str) -> token::InternedString {
    token::intern_and_get_ident(s)
}

fn choose(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<MacResult + 'static> {
    let mut test_mods = SmallVector::zero();
    let cfg_str = intern("cfg");
    let feat_str = intern("feature");
    for arg in args {
        let mut attrs = vec![];
        let mod_name = match arg {
            &TokenTree::Token(_, token::Ident(s)) => s.to_string(),
            _ => {
                return DummyResult::any(sp);
            }
        };
        attrs.push(cx.attribute(sp,
                                cx.meta_list(sp,
                                             // simply increase the reference counter
                                             cfg_str.clone(),
                                             vec![cx.meta_name_value(sp,
                                                                     feat_str.clone(),
                                                                     ast::LitKind::Str(intern(mod_name.trim_left_matches("test_")), ast::StrStyle::Cooked))])));

        let mut mod_path = PathBuf::from(&cx.codemap().span_to_filename(sp));
        mod_path.set_file_name(mod_name.as_str());
        mod_path.set_extension("rs");

        test_mods.push(P(ast::Item {
            ident: cx.ident_of(mod_name.as_str()),
            attrs: attrs,
            id: ast::DUMMY_NODE_ID,
            node: ast::ItemKind::Mod(
                ast::Mod {
                    inner: sp,
                    items: expand_include(cx, sp, &mod_path),
                }
            ),
            vis: ast::Visibility::Inherited,
            span: sp,
        }))
    }

    MacEager::items(test_mods)
}

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
    reg.register_macro("choose", choose);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

第一个编译器是如何编写的?

第一个编译器是如何编译的?

如何编译一个编译器阶段的输出?

如何告诉编译器“忽略”一个类?

如何编写一个带迭代器的Rust函数?

Grails插件是一个很好的闭包编译器或uglify-js-minified-resources

如何让Rust编译器转储生成的LLVM?

Web编译器通过较少的文件生成了一个空的CSS

使编译器为std :: function生成一个空的默认函数

打字稿编译器给出了一个包含生成器函数的代码

为什么 Rust 在性能上比 Ocaml 快,即使第一个 Rust 编译器是在 Ocaml 中实现的

编写和编译一个简单的jQuery插件

这是一个Java编译器错误?

一个Makefile中有多个编译器

如何运行一个文件源代码的Java程序时,通过编译器选项?

是否有一个官方的C编译器,如何安装?

如何为正在构建的每个端口添加一个编译器选项?

如何从另一个Go应用程序运行Go编译器

如何仅显示Microsoft Typescript编译器的第一个错误?

如何使用 hanami 编译器将 scss 文件导入另一个 scss?

如何基于编译器指令-D自动选择一个include.h文件?

比较两个time_t变量会生成一个编译器警告

如何获取Rust编译器插件中绑定的特征的完整路径?

如何编写一个Symfony 2.5插件

同一程序在一个编译器中而不是其他编译器中给出编译器错误

为什么编译器会发出一个stloc和一个ldloca?

OpenCl 是一个库还是一个编译器?

用C#编写编译器,生成C与IL?

如何编写一个可以读写缓存的 rust 函数?