如何在语法中引用先前匹配的项? [英] How to refer to previously matched items in a grammar?

查看:51
本文介绍了如何在语法中引用先前匹配的项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析BibTeX作者字段,并将其拆分为单独的作者.这将帮助我重写每位作者的姓名缩写.这是一个最小的示例:

I am trying to parse a BibTeX author field, and split it into its separate authors. This will help me rewriting the initials of each author. Here is a minimal example:

use v6;

my $str = '{Rockhold, Mark L and Yarwood, RR and Selker, John S}';

grammar BibTexAuthor {
    token TOP {
        <all-text> 
    }
    token all-text {
        '{' <authors> '}' 
    }
    token authors { 
        [<author> [' and ' || <?before '}'>]]+
    }
    token author {
        [<-[\s}]> || [' ' <!before 'and '>]]+
    }
}

class BibTexAuthor-actions {
    method TOP($/) {
        say $/;
        print "First author = ";
        say $<author>.made[0];
        make $/.Str;
    }
    method all-text($/) {
        make $/.Str;
    }
    method authors($/) {
        make $/.Str;
    }
    method author($/) {
        make $/.Str;
    }
}
my $res = BibTexAuthor.parse( $str, actions => BibTexAuthor-actions.new).made;

输出:

「{Rockhold, Mark L and Yarwood, RR and Selker, John S}」
 all-text => 「{Rockhold, Mark L and Yarwood, RR and Selker, John S}」
  authors => 「Rockhold, Mark L and Yarwood, RR and Selker, John S」
   author => 「Rockhold, Mark L」
   author => 「Yarwood, RR」
   author => 「Selker, John S」
First author = Nil

为什么我不能在 TOP 方法中提取第一作者?

Why am I not able to extract the first author in the TOP method?

推荐答案

为什么我不能在 TOP 方法中提取第一作者?

因为您实际上并没有在操作方法中提取任何数据.您要做的就是将匹配项的字符串附加到 $/.made 上,而实际上这并不是最后想要的数据.

Because you are not really extracting any data in the action methods. All you do is attach the string of the match to $/.made, which is not actually the data you want in the end.

如果最后希望有单独的作者,则应使用 authors 操作方法将 make 组成一个作者数组.例如:

If you want to have separate authors in the end, you should make an array of authors in the authors action method. For example:

use v6;

my $str = '{Rockhold, Mark L and Yarwood, RR and Selker, John S}';

grammar BibTexAuthor {
    token TOP {
        <all-text> 
    }
    token all-text {
        '{' <authors> '}' 
    }
    token authors { 
        [<author> [' and ' || <?before '}'>]]+
    }
    token author {
        [<-[\s}]> || [' ' <!before 'and '>]]+
    }
}

class BibTexAuthor-actions {
    method TOP($/) {
        make { authors => $<all-text>.made };
    }
    method all-text($/) {
        make $/<authors>.made;
    }
    method authors($/) {
        make $/<author>».made;
    }
    method author($/) {
        make $/.Str;
    }
}
my $res = BibTexAuthor.parse( $str, actions => BibTexAuthor-actions.new).made;

say $res.perl;

打印

${:authors($["Rockhold, Mark L", "Yarwood, RR", "Selker, John S"])}

因此,顶层匹配的 .made 现在是一个哈希,其中 authors 键保存一个数组.如果您想访问第一作者,现在可以说

so now the .made of the top-level match is a hash, where the authors key holds an array. If you want to access the first author, you can now say

say $res<authors>[0];

获得 Rockhold,Mark L

这篇关于如何在语法中引用先前匹配的项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆