And below is how to do it with the optional subrule. It even provides better error messages.
#!/usr/bin/env perl use strict; use warnings; use feature qw( say ); use Parse::RecDescent qw( ); my $grammar = <<'__EOI__'; myrule : <rulevar: local $failed = 1> myrule : 'stuff' mysubrule(?) <reject:$failed> mysubrule : 'ID' <commit> '[' ']' | <error?> { $failed = 0; } <reject> __EOI__ my $parser = Parse::RecDescent->new($grammar) or die; for my $text ("stuff ID something", "stuff something") { say "===================="; say "$text"; say "--------------------"; say $parser->myrule($text) ? 'pass' : 'fail'; }
==================== stuff ID something -------------------- ERROR (line 1): Invalid mysubrule: Was expecting '[' but found "something" instead fail ==================== stuff something -------------------- pass
In reply to Re^2: Parse::Recdescent optional subrule commit
by ikegami
in thread Parse::Recdescent optional subrule commit
by campugnatus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |