I wanted to create a raku grammar that can be used to parse a reduced markdown syntax. This reduced markdown syntax must meet the following criteria:
- a header in markdown must either start with a '#' followed by a space or must be underlined with a sequence of '-' (at least 2).
- a text can not stand on it's own. it must be preceeded by a header.
To parse this syntax I created the following script:
#!/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;
}
But I have a problem with this grammar: As stated in the comments of the test-array there is something wrong with the grammar but I don't know what.