Next | Higher-Order Parsing | 15 |
Rather than write 9 similar lookfor functions, we'll have another function build them as required:
sub lookfor { my $target = shift; my $parser = sub { my $tokens = shift; my $tok = first($tokens); if (type($tok) eq $target) { return (value($tok), rest($tokens)); } else { return; # failure } }; return $parser; }
Now instead of lookfor_PLUS we just use lookfor("+")
Instead of lookfor_NUMBER we just use lookfor("NUMBER")
Next | Copyright © 2007 M. J. Dominus |