raku:解析部分的降价语法

raku: markdown grammar to parse sections

我想创建一个 raku 语法,可以用来解析简化的 markdown 语法。这种简化的降价语法必须满足以下条件:

为了解析此语法,我创建了以下脚本:

#!/usr/bin/perl6

use v6;

grammar gram {
    token TOP {
        <text>
    }
    token text {
        [ <section> ]+
    }
    token section {
        <headline> <textline>*
    }
    token headline {
        ^^ [<hashheadline> | <underlineheadline>] $$
    }
    token hashheadline {
        <hashprefix> <headlinecontent>
    }
    token hashprefix {
        [\#] <space>
    }
    token underlineheadline {
        <headlinecontent> [\n] <underline>
    }
    token underline {
        [\-]**2..*
    }
    token headlinecontent {
        [\N]+
    }
    token textline {
        ^^ (<[\N]-[\#]> (<[\N]-[\ ]> [\N]*)? )? [\n] <!before [\-][\-]>
    }
}

my @tests = "",                                         #should not match and doesn't match - OK
            "test1",                                    #should not match and doesn't match - OK
            "test2\n",                                  #should not match and doesn't match - OK
            "test3\nnewline",                           #should not match and doesn't match - OK
            "test4\n----",                              #should match and does match        - OK
            "test5\n----\nnewline",                     #should match but doesn't match     - NOK
            "#test6\nnewline",                          #should not match and doesn't match - OK
            "# test7\nnewline",                         #should match but doesn't match     - NOK
            "# test8",                                  #should match and does match        - OK
            "test9\n----\nnewline\nanother\nnew line",  #should match but doesn't match     - NOK
            "# test10\nnewline\nhead\n---\nanother",    #should match but doesn't match     - NOK
            ;

for @tests -> $test {
    say gram.parse($test).perl;
}

但我对这个语法有疑问:正如 test-array 的评论中所述,语法有问题,但我不知道是什么。

textline 标记更改为:

token textline { \n* <!before <headline>> \N+ \n? }

我没有考虑过这个改变是否是你真正想要的,但这意味着你的测试按照你指定的方式工作。


一般用CommaIDE开发文法。大多数问题(如您发布的问题)的位置会立即变得显而易见。 (解决方案当然是一个独特的步骤,但查明问题通常是大部分工作。)


通常,通过生成最小示例来调试不明显的问题(请参阅我对您问题的评论中提供的 link,但跳过可重现的部分)。

这样做通常是相对快速地查明任何不明显问题所在位置的最有效方法。

这也是一款有趣的游戏,通过将您的直觉与松散的 binary chop 类方法相结合,您会更快地玩上这款游戏。


一般来说,在询问有关 SO 的问题时,首先生成一个最小示例(如刚才所讨论的),然后将其设为最小 可重现 示例(以最小示例为基础)。 (你问题中的例子是 100% 可重现的——谢谢!——但我写这个答案是为了其他读者和你。)

最小可重现示例是您自己的洞察力和效率问题,以及其他人的洞察力和效率问题。一旦我了解问题出在哪里,解决您的问题就花了我大约 1 分钟的时间。但我花了 15 分钟做了你“最好”做的事情,然后才在这里提问:

  • 最适合您,因为它很有趣(并且会稳步提高您的 bug 搜索效率)。

  • 最适合我,我本来应该享受属于你的乐趣。

  • 最适合尝试回答您问题的其他人,这样我们就不会重复不必要的工作。

  • 最适合后来的读者,他们得到的是解决真正困惑的简单问题,而不是不幸的复杂问题,这些问题掩盖了真正的问题,以至于它们对读者的价值已经丧失。

  • 最适合 Rakoons 集体,因为发布 Minimal Reproducible Examples 是版主和 Whosebug 的普通读者认为一个好问题的基本要素,这意味着他们更有可能认真对待 Raku,帮助我们调节我们的问题,并成为浣熊。

也就是说,我并不是要阻止您提问,远非如此。如果在阅读并尝试应用最小可复制示例页面中的指南后,您发现自己正在挣扎,请继续提问,并在问题中解释您在生成最小 and/or 可复制示例时遇到的任何问题例如,因为这会有所帮助。

raku 模块 Text::Markdown 可能与某些用户相关。