in reply to Re: Parse::Recdescent optional subrule commit
in thread Parse::Recdescent optional subrule commit

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

Replies are listed 'Best First'.
Re^3: Parse::Recdescent optional subrule commit
by campugnatus (Initiate) on May 03, 2012 at 08:04 UTC
    This is elegant and simple! Thank you!