尝试为Markdown定义语法时的raku语法问题
我正在使用以下代码来解析 Markdown 文本的子集:
#!/usr/bin/perl6
use v6;
my @tests = '', #should print OK
"nnn", #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
"#headingn", #should print OK
"#heading n", #should print OK
"#headingnand a text", #should print OK
"#headingnandnantextnwithnnewlines", #should print OK
"#heading1nand textn#heading2nand 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 为我指明了正确的方向!通过更改所有出现rule来token解决问题!