修复“没有规则预期令牌”宏错误

时间:2016-01-09 13:09:48

标签: macros rust

我正在尝试编写一个用于解构BSON数据的宏,如下所示:

let bson: Document = ...;
let (id, hash, name, path, modification_time, size, metadata, commit_data) = bson_destructure! {
    get id = from (bson), optional, name ("_id"), as ObjectId;
    get hash = from (bson), as String, through (|s| ContentHash::from_str(&s));
    get name = from (bson), as String;
    get path = from (bson), as Bson, through (PathBuf::from_bson);
    get modification_time = from (bson), as UtcDatetime, through (FileTime);
    get size = from (bson), as I64, through (|n| n as u64);
    get metadata = from (bson), as Document, through (Metadata::from_bson);
    get commit_data = from (bson), optional, as Document, through (CommitData::from_bson);
    ret (id, hash, name, path, modification_time, size, metadata, commit_data)
};

我为它编写了以下宏(非常大):

macro_rules! bson_destructure {
    // required field
    (
        @collect req,
        [$target:ident, $source:expr, $field:expr, Bson, $f:expr],
        [];
        $($rest:tt)*
    ) => {{
        let $target = try!(match $source.remove($field) {
            Some(v) => $f(v),
            None => Err(BsonDestructureError::MissingField {
                field_name: $field,
                expected: "Bson"
            }),
        });
        bson_destructure!($($rest)*)
    }};
    (
        @collect req,
        [$target:ident, $source:expr, $field:expr, $variant:ident, $f:expr],
        [];
        $($rest:tt)*
    ) => {{
        let $target = try!(match $source.remove($field) {
            Some(v) => match v {
                ::ejdb::bson::Bson::$variant(v) => $f(v),
                v => Err(BsonDestructureError::InvalidType {
                    field_name: $field,
                    expected: stringify!($variant),
                    actual: v
                })
            },
            None => Err(BsonDestructureError::MissingField {
                field_name: $field,
                expected: stringify!($variant)
            }),
        });
        bson_destructure!($($rest)*)
    }};

    // optional field
    (
        @collect opt,
        [$target:ident, $source:expr, $field:expr, Bson, $f:expr],
        [];
        $($rest:tt)*
    ) => {{
        let $target = try!(match $source.remove($field) {
            Some(v) => $f(v).map(Some),
            None => Ok(None),
        });
        bson_destructure!($($rest)*)
    }};
    (
        @collect opt,
        [$target:ident, $source:expr, $field:expr, $variant:ident, $f:expr],
        [];
        $($rest:tt)*
    ) => {{
        let $target = try!(match $source.remove($field) {
            Some(v) => match v {
                ::ejdb::bson::Bson::$variant(v) => $f(v).map(Some),
                v => Err(BsonDestructureError::InvalidType {
                    field_name: $field,
                    expected: stringify!($variant),
                    actual: v
                })
            },
            None => Ok(None),
        });
        bson_destructure!($($rest)*)
    }};

    // change variant name
    (
        @collect $k:tt,
        [$target:ident, $source:expr, $field:expr, $variant:ident, $f:expr],
        [as $nv:ident, $($word:ident $arg:tt),*];
        $($rest:tt)*
    ) => {
        bson_destructure!(
            @collect $k,
            [$target, $source, $field, $nv, $f],
            [$($word $arg),*];
            $($rest)*
        )
    };

    // change final mapping function
    (
        @collect $k:tt,
        [$target:ident, $source:expr, $field:expr, $variant:ident, $f:expr],
        [through ($nf:expr), $($word:ident $arg:tt),*];
        $($rest:tt)*
    ) => {
        bson_destructure!(
            @collect $k,
            [$target, $source, $field, $variant, $nf],
            [$($word $arg),*];
            $($rest)*
        )
    };

    // change field name
    (
        @collect $k:tt,
        [$target:ident, $source:expr, $field:expr, $variant:ident, $f:expr],
        [name ($nn:expr), $($word:ident $arg:tt),*];
        $($rest:tt)*
    ) => {
        bson_destructure!(
            @collect $k,
            [$target, $source, $nn, $variant, $f],
            [$($word $arg),*];
            $($rest)*
        )
    };

    // main forms
    (get $target:ident = from ($source:expr), $($word:ident $arg:tt),*; $($rest:tt)*) => {
        bson_destructure!(
            @collect req,
            [$target, $source, stringify!($target), Bson, Ok],
            [$($word $arg),*];
            $($rest)*
        )
    };
    (get $target:ident = from ($source:expr), optional, $($word:ident $arg:tt),*; $($rest:tt)*) => {
        bson_destructure!(
            @collect opt,
            [$target, $source, stringify!($target), Bson, Ok],
            [$($word $arg),*];
            $($rest)*
        )
    };

    // final form
    (ret $e:expr) => { $e }
}

但是,上面的第一个示例导致以下编译错误:

src/db/data.rs:345:22: 345:25 error: no rules expected the token `opt`
src/db/data.rs:345             @collect opt,
                                        ^~~

我有点惊讶它没有像往常一样显示错误位置(也就是说,没有指示扩展发生的地方),但是,当我评论使用宏的代码时,错误消失了。

我不明白为什么它说没有规则期望这个标记,因为有这样的规则,但也许我不明白。

我很确定这是可能的,因为这大概就是quick_error crate所做的,但似乎我的宏写作技能仍然缺乏。

我应该如何修复宏,以便它可以像我期望的那样工作?

为完整起见,以下是BsonDestructureError的定义:

#[derive(Debug, Clone)]
pub enum BsonDestructureError {
    InvalidType {
        field_name: &'static str,
        expected: &'static str,
        actual: Bson
    },
    InvalidArrayItemType {
        index: usize,
        expected: &'static str,
        actual: Bson
    },
    MissingField {
        field_name: &'static str,
        expected: &'static str
    }
}

我也在使用从bson crate重新导出的ejdb crate。 Here是一个最小的示例,可以在稳定的Rust上使用cargo script运行。

1 个答案:

答案 0 :(得分:12)

cargo script,一个递归的muncher,我最喜欢的内部规则语法;我怎么能不?

首先,通过运行cargo rustc -- -Z trace-macros可以确定确切的问题。这将在扩展时输出每个规则,为我们提供一个"回溯"经过一些手动重新格式化之后,它看起来像这样:

bson_destructure! {
    get id = from ( bson ) , optional , name ( "_id" ) , as ObjectId ;
    get hash = from ( bson ) , as String ;
    get name = from ( bson ) , as String ;
    get path = from ( bson ) , as Bson ;
    get modification_time = from ( bson ) , as UtcDatetime ;
    get size = from ( bson ) , as I64 , through ( | n | n as u64 ) ;
    get metadata = from ( bson ) , as Document ;
    get commit_data = from ( bson ) , optional , as Document ;
    ret ( id , hash , name , path , modification_time , size , metadata , commit_data )
}

bson_destructure! {
    @ collect opt ,
    [ id , bson , stringify ! ( id ) , Bson , Ok ] ,
    [ name ( "_id" ) , as ObjectId ] ;

    get hash = from ( bson ) , as String ;
    get name = from ( bson ) , as String ;
    get path = from ( bson ) , as Bson ;
    get modification_time = from ( bson ) , as UtcDatetime ;
    get size = from ( bson ) , as I64 , through ( | n | n as u64 ) ;
    get metadata = from ( bson ) , as Document ;
    get commit_data = from ( bson ) , optional , as Document ;
    ret ( id , hash , name , path , modification_time , size , metadata , commit_data )
}

bson_destructure! {
    @ collect opt ,
    [ id , bson , "_id" , Bson , Ok ] , [ as ObjectId ] ;

    get hash = from ( bson ) , as String ;
    get name = from ( bson ) , as String ;
    get path = from ( bson ) , as Bson ;
    get modification_time = from ( bson ) , as UtcDatetime ;
    get size = from ( bson ) , as I64 , through ( | n | n as u64 ) ;
    get metadata = from ( bson ) , as Document ;
    get commit_data = from ( bson ) , optional , as Document ;
    ret ( id , hash , name , path , modification_time , size , metadata , commit_data )
}

仔细阅读bson_destructure!中的规则会显示问题:没有与第三次扩展相匹配的规则。坦率地说,macro_rules!垃圾报告有关递归规则的合理错误位置;它指向opt令牌是无关紧要的。 真正的问题是它无法找到匹配规则。

特别是,违规规则是这样的:

// change variant name
(
    @collect $k:tt,
    [$target:ident, $source:expr, $field:expr, $variant:ident, $f:expr],
    [as $nv:ident, $($word:ident $arg:tt),*];
    $($rest:tt)*
) => {
    ...
};

请注意 $nv:ident后立即出现逗号。另请注意,在输入中没有这样的逗号。这可以通过移动逗号里面重复来解决,如下所示:

// change field name
(
    @collect $k:tt,
    [$target:ident, $source:expr, $field:expr, $variant:ident, $f:expr],
    [name ($nn:expr) $(, $word:ident $arg:tt)*];
    $($rest:tt)*
) => {
    ...
};

另一个替代方案(以及我通常使用的方法)是在第一次遇到输入时简单地改变输入,以确保总是一个尾随逗号。

由于本机依赖性,代码在我的机器上实际上编译但是我确实做了这个更改(这里和其他规则都有类似的问题) )允许它完成宏扩展。您可以使用cargo rustc -- -Z unstable-options --pretty=expanded检查输出看起来是否正确。