gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:21:00 25 4
gpt4 key购买 nike

我正在使用以下代码来解析 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com