File Coverage

blib/lib/Sub/Lambda/Grammar.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 19 19 100.0


line stmt bran cond sub pod time code
1             package Sub::Lambda::Grammar;
2              
3 2     2   12 use warnings;
  2         4  
  2         55  
4 2     2   11 use strict;
  2         3  
  2         52  
5 2     2   10532 use Parse::RecDescent;
  2         73555  
  2         15  
6              
7 2     2   92 use base qw(Exporter);
  2         3  
  2         462  
8             our @EXPORT_OK = qw(parse);
9              
10             my $grammar = q{
11              
12             Parse : /^/ Expression /$/ { $item[2] }
13              
14             Expression : Unit(s)
15             { (join "->", map {qq(($_))} @{$item[1]}) }
16              
17             Unit : Code | Lambda | Parens | Symbol
18              
19             Parens : '(' Expression ')' { $item[2] }
20              
21             Lambda : /[\\\\]/ Pattern /\-\>/ Expression
22             { qq( sub { my $item[2] = \@_; $item[4] } ) }
23              
24             Pattern : (PWords | Words) { qq(($item[1])) }
25             Symbol : DWord | Word
26              
27             Code : '{' Block(s?) '}'
28             { my $x = join ' ', @{$item[2]}; "do {$x}" }
29             NoBlock : /[^{}]+/
30             Block : NoBlock | '{' Block(s?) '}'
31             { my $x = join ' ', @{$item[2]}; "{$x}" }
32              
33             PWords : '(' Words ')' { $item[2] }
34             Words : DWord | MWords
35             MWords : Word(s) DWord(?) { join ',', @{$item[1]}, @{$item[2]} }
36             Word : /[a-zA-Z]\w*/ { '$' . $item[1] }
37             DWord : '-' Word { '@' . substr($item[2],1) }
38              
39             };
40              
41             our $parser = new Parse::RecDescent($grammar);
42              
43             =head1 NAME
44              
45             Sub::Lambda::Grammar
46              
47             =head1 DESCRIPTION
48              
49             Defines the lambda grammar for L.
50              
51             =head2 METHODS
52              
53             =over
54              
55             =item parse($string)
56              
57             Runs a L parser on its in put with a grammar defined in this
58             module. In effect, translates embedded lambda expressions to Perl code.
59              
60             =cut
61              
62 30     30 1 296 sub parse ($) { $parser->Parse($_[0]) }
63              
64             1;
65              
66             __END__