parsing - 如何编写可定制的语法?

标签 parsing grammar raku

对于我正在编写的聊天机器人,我希望使其解析器可定制,这样人们就不需要修改机器人本身来为他们想要的任何类型的聊天消息添加 Hook 。解析器使用语法。目前,我用一个类似这样的类来处理这个问题:

class Rule {
    has Regex:D $.matcher is required;
    has         &.parser  is required;

    method new(::?CLASS:_: Regex:D $matcher, &parser) {
        self.bless: :$matcher, :&parser
    }

    method match(::?CLASS:D: Str:D $target --> Replier:_) {
        $target ~~ $!matcher;
        $/.defined ?? &!parser(self, $/) !! Nil
    }
}

然后将从解析器的操作类中循环遍历这些数组。这允许人们为解析器添加自己的“规则”,这解决了我的问题,但这很笨重,而且正在重新发明语法!我真正想要的是人们能够为我的解析器编写诸如俚语之类的东西。虽然可以使用 augment 来实现此目的,但在这种情况下它没有用处,因为用户可能希望在运行时更改其增强解析器的方式,但是 augment在编译时处理。如何做到这一点?

最佳答案

所有这些都需要 5 或 10 行样板文件,具体取决于您是否使用操作类。

如果您查看 Metamodel::GrammarHOW,在撰写本文时,您会发现:

class Perl6::Metamodel::GrammarHOW
    is Perl6::Metamodel::ClassHOW
    does Perl6::Metamodel::DefaultParent
{
}

语法是类(class)的延伸!这意味着可以在其中声明元方法。建立在 How can classes be made parametric in Perl 6? 之上,如果用户为语法和 Action 类提供角色,则可以在通过参数化进行解析之前将它们混合在一起。如果您以前写过俚语,这可能听起来很熟悉;像这样混合角色就是 $*LANG.refine_slang 的工作原理!

如果您希望语法中的标记是可扩展的,您可以将其设为原始标记。之后所需要的只是一个混合在其参数中的参数化元方法,这将是某种角色:

grammar Foo::Grammar {
    token TOP { <foo> }

    proto token foo          {*}
          token foo:sym<foo> { <sym> }

    method ^parameterize(Foo::Grammar:U $this is raw, Mu $grammar-role is raw --> Foo::Grammar:U) {
        my Foo::Grammar:U $mixin := $this.^mixin: $grammar-role;
        $mixin.^set_name: $this.^name ~ '[' ~ $grammar-role.^name ~ ']';
        $mixin
    }
}

class Foo::Actions {
    method TOP($/) { make $<foo>.made; }

    method foo:sym<foo>($/) { make ~$<sym>; }

    method ^parameterize(Foo::Actions:U $this is raw, Mu $actions-role is raw --> Foo::Actions:U) {
        my Foo::Actions:U $mixin := $this.^mixin: $actions-role;
        $mixin.^set_name: $this.^name ~ '[' ~ $actions-role.^name ~ ']';
        $mixin
    }
}

然后可以像这样声明要混合的角色:

role Bar::Grammar {
    token foo:sym<bar> { <sym> }
}

role Bar::Actions {
    method foo:sym<bar>($/) { make ~$<sym>; }
}

现在,如果需要,可以在解析之前用 Bar 增强 Foo:

Foo::Grammar.subparse: 'foo', actions => Foo::Actions.new;
say $/ && $/.made; # OUTPUT: foo
Foo::Grammar.subparse: 'bar', actions => Foo::Actions.new;
say $/ && $/.made; # OUTPUT: #<failed match>

Foo::Grammar[Bar::Grammar].subparse: 'foo', actions => Foo::Actions[Bar::Actions].new;
say $/ && $/.made; # OUTPUT: foo
Foo::Grammar[Bar::Grammar].subparse: 'bar', actions => Foo::Actions[Bar::Actions].new;
say $/ && $/.made; # OUTPUT: bar

编辑:mixin 元方法可以接受任意数量的角色作为参数,并且参数化可以使用任何签名。这意味着,如果稍微调整 parameterize 元方法,您就可以使语法或操作类的参数化接受任意数量的角色:

method ^parameterize(Mu $this is raw, *@roles --> Mu) {
    my Mu $mixin := $this.^mixin: |@roles;
    $mixin.^set_name: $this.^name ~ '[' ~ @roles.map(*.^name).join(', ') ~ ']';
    $mixin
}

关于parsing - 如何编写可定制的语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59426167/

相关文章:

php - 如何解析由 php 中 json_enocoded 的 url 接收的 json 数据

Linux shell脚本将列读入变量,然后添加属性

php - 处理变量中的敏感信息,使用后上传

c# - C# 和 Java 语法是 LALR(x) 吗?

c - 用柠檬解析条件

raku - 在Perl 6中的方法和函数调用中使用冒号

c# - C# 语言规范 4.0 中是否正确定义了基本访问权限?

grammar - *可以在符号 token 中使用多个字符吗?

arrays - 在Perl 6中如何知道多值迭代期间的缺失值?

thread-safety - Lock.protect和callame