NAME Wraith - Parser Combinator in Perl
SYNOPSIS use Wraith;
my ($expn, $term, $factor, $num);
wraith_rule->makerules(\$expn, \$term, \$factor, \$num);
$expn = ( (\$term >> $wraith::token->('\+') >> \$expn) ** sub { [ $_[0]->[0] + $_[0]->[2] ] } ) |
( (\$term >> $wraith::token->('-') >> \$expn) ** sub { [ $_[0]->[0] - $_[0]->[2] ] } ) |
( \$term );
$term = ( (\$factor >> $wraith::token->('\*') >> \$term) ** sub { [ $_[0]->[0] * $_[0]->[2] ] } ) |
( (\$factor >> $wraith::token->('\/') >> \$term) **
sub { $_[0]->[2] ? [ $_[0]->[0] / $_[0]->[2] ] : [] } ) |
( \$factor );
$factor = ( (\$num) ** sub { my $args = $_[0]; my $val = undef; for my $elt (@$args) { $val .= $elt; } [ $val ] } ) |
( ( $wraith::token->('\(') >> \$expn >> $wraith::token->('\)') ) ** sub { my $args = $_[0]; [ $args->[1] ] } );
$num = $wraith::token->('[1-9][0-9]*');
print $expn->('2 + (4 - 1) * 3 + 4 -2')->[0]->[0]->[0], "\n";
DESCRIPTION Wraith is a simple parser combinator library (not monadic nor memoized) inspired by Boost.Spirit. It is not complete as Spirit but the fundamental operators are implemented.
When applied with arguments, all operators/combinators return a function, which takes a string as input sentence(s) and return a reference to a list of pairs: [ $pair_1, $pair_2, ..., $pair_n ], where each pair is a reference to a two-element list: [ ref_to_list_of_results, input_unprocessed ], in which ref_to_list_of_results is a reference to a list of analysis results and input_unprocessed is a string representing the unprocessed input so far.
Basic Operators:
reference $succeed It is a curried version of operator succeed. The first parameter of succeed is the analysis result and the second parameter is the unprocessed input string.
reference $fail It takes an argument, discards it and return an empty list.
Those two operators are rarely used. Use them if you need new combinators.
reference $literal It takes one character as the only argument. The returned function match the first character of its input against the argument character and return (argument, input_left) if matched, where input_left is the input without its first character, or return an empty list if failed to match.
reference $literals Almost the same as $literal, but takes a string as the only argument and match the first character of input with each character in argument string until matched.
reference $token Takes a regex string as its first argument. The second and optional argument is a regex string of skipped strings. It matches the regex at the beginning of the input string, return (token, input_left) if matched or an empty list if failed.
Combinators:
There are four combinators: then for sequence, alt for alternative, many for kleene star and using for semantic actions. Except many, the combinators are overloaded perl operators which takes at least one operator, combinator, compsite of combinators, product, or reference to an instance of those classes as the left-hand-side operand.
The returned list of function generated by combinators is a list of tokens in the order of they appeared in the products.