Parse-RecDescent - reporting where the parsing fails

Posted on Mon Feb 7 20:22:19 2005 by szabgab
reporting where the parsing fails

I am using Parse::RecDescent for parsing the submitted text and while I think I managed to declare the grammar correctly so far I could not figure out how to point out what exactly is the cause of failure. Hence you are seeing the meaninless Text format is not correct. error message.

I'd like to be able to give different error messages in case of
< is not allowed in plain text, please escape it and
<p> seen without a closing </p> tag

Here is the current grammar and there is a whole test suit for this code withing the source code of CPAN::Forum,
any ideas on how to provide error messages ?

$self->{grammar} = q { entry : chunk(s) eodata { $item[1] } chunk : marked_html | marked_code { $item[1] } marked_html: html(s) { qq(<div class="text">) . join("", @{$item[1]}) . q +q(</div>); } html : text { $item[1] } | block { $item[1] } | inline { $item[1] } block : open_p inline(s) close_p { "<p>" . join("", @{$item[2]}) . "</p>" } inline : text { $item[1] } | open_b text close_b { join "", @item[1..$#item] } | open_i text close_i { join "", @item[1..$#item] } | br { $item[1] } | open_a text close_a { join "", @item[1..$#item] } br : m{<br( /)?>}i { "<br />" } open_p : m{<p>}i { "<p>" } close_p : m{</p>}i { "</p>" } open_b : m{<b>}i { "<b>" } close_b : m{</b>}i { "</b>" } open_i : m{<i>}i { "<i>" } close_i : m{</i>}i { "</i>" } open_a : open_a_href urlx open_a_gt { qq(<a href="$item[2]">) } open_a_href : m{<a href=}i urlx : quote url quote {$item[2]} | url {$item[1]} url : http | mailto http : m{http://[^">]+}i { lc $item[1] } mailto : m{mailto:[^">]+}i { lc $item[1] } open_a_gt : m{>} quote : m{"} close_a : m{</a>}i { "</a>" } text : m{[\r\t\n -;=?-~]+} {$item[1] } marked_code: open_code code close_code { join("", @item[1..$#item]) } open_code : m{<xcode>} { qq(<div class="code">) } close_code : m{</xcode>} { qq(</div>) } code : m{[\r\t\n -~]+?(?=</xcode>)} { CPAN::Forum::Markup::split_rows(join "", @item[1. +.$#item]) } eodata : m{^\Z} };

I had to replace the <code> entries with <xcode> as the system did not let me publish my own code :) see also here

Write a response