In the Hammer parser combinator, how do you implement a rule that refers to itself?

Viewed 180

I'm getting a "Deep recursion on subroutine" warning when I just refer to the name as shown in the code below. use 5.016 … __SUB__->() does not help, either.

Build hints: git clone ; scons bindings=perl test ; cd build/opt/src/bindings/perl ; $EDITOR h.pl

use 5.024;
use strictures;
use blib;
use hammer qw();

# digits = "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9"
sub digits { hammer::many hammer::in 0..9 }

# product = digits "*" digits
sub product {
    hammer::sequence digits, hammer::ch('*'), digits
}

product->parse('23*42'); # [[2, 3], '*', [4, 2]]


# mproduct = digits "*" mproduct
sub mproduct;
sub mproduct {
    hammer::sequence digits, hammer::ch('*'), mproduct
}
mproduct->parse('8*23*42');
# Deep recursion on subroutine "main::mproduct" at h.pl line 21.
1 Answers
Related