尝试为 Markdown 定义语法时的 raku 语法问题

标签 raku

我正在使用以下代码来解析 Markdown 文本的子集:

#!/usr/bin/perl6

use v6;

my @tests = '',         #should print OK
            "\n\n\n",   #should print OK
            'alpha',    #should print OK
            '1234',     #should print OK
            'alpha123', #should print OK
            'a simple test without a heading and whitespaces and 123 numbers',                                                  #should print OK
            'a simple test without a heading with punctuation, whitespaces and 123 numbers.',                                   #should print OK
            'a simple test without a heading with punctuation, whitespaces, 123 numbers, quotes\' ", braces (){}<>[], \/.',     #should print OK
            'a simple test with a # in the middle',                                                                             #should print OK
            "#heading",         #should print FAILED
            "#heading ",        #should print FAILED
            "#heading\n",       #should print OK
            "#heading \n",      #should print OK
            "#heading\nand a text",                     #should print OK
            "#heading\nand\na\ntext\nwith\nnewlines",   #should print OK
            "#heading1\nand text\n#heading2\nand text"  #should print OK
            ;

grammar markdown_ltd {
    rule TOP {
        [ <text> | <headingandtext>+ ]
    }
    rule text {
        <textline>*<lasttextline>?
    }
    rule textline {
        <textlinecontent>? [\n]
    }
    rule lasttextline {
        <textlinecontent>
    }
    rule textlinecontent {
        <[\N]-[\#]> [\N]*
    }
    rule headingandtext {
        <heading> [\n] <text>
    }
    rule heading {
        [\#] [\N]+
    }
}
for @tests -> $test {
    my $result = markdown_ltd.parse($test);
    say "\nTesting '" ~ $test ~ "' " ~ ($result ?? 'OK' !! 'FAILED');
}
此语法的目的是检测标题和常规文本。
但是,代码中存在一些错误,因此包含的测试不会导致预期的行为。当我运行脚本时,我得到以下输出:
Testing '' OK

Testing '


' OK

Testing 'alpha' FAILED

Testing '1234' FAILED

Testing 'alpha123' FAILED

Testing 'a simple test without a heading and whitespaces and 123 numbers' OK

Testing 'a simple test without a heading with punctuation, whitespaces and 123 numbers.' OK

Testing 'a simple test without a heading with punctuation, whitespaces, 123 numbers, quotes' ", braces (){}<>[], \/.' OK

Testing 'a simple test with a # in the middle' OK

Testing '#heading' FAILED

Testing '#heading ' FAILED

Testing '#heading
' FAILED

Testing '#heading 
' FAILED

Testing '#heading
and a text' FAILED

Testing '#heading
and
a
text
with
newlines' FAILED

Testing '#heading1
and text
#heading2
and text' FAILED
这与我预期的输出不同。

最佳答案

感谢@donaldh 为我指明了正确的方向!
通过更改所有出现的 ruletoken修复了问题!

关于尝试为 Markdown 定义语法时的 raku 语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66348287/

相关文章:

Raku:如何为 CArray[WCHAR] 赋值?

module - 使用完全限定名称调用模块子例程

type-conversion - index() 错误消息对于第三个参数不正确?

raku - 重新编译nqp后为`Missing or wrong version of dependency`

regex - 如何在汉字和英文字符之间插入一个空格?

module - 如何在与程序相同的文件中声明和使用 Perl 6 模块?

concurrency - Perl 6 中是否有快速并行 "for"循环?

grammar - 乐语法 : Use named regex without consuming matching string

raku - Apocalypse #1 中引入的 "semantic model"是什么?

slice - 数组切片中的多个组件 - 相当于 perl5 : @a[0. .1,3]