blib/lib/MarpaX/Lex/Easy.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 130 CC 12 SCAR 37.7
CC
12
Cov
0%
lex
90.0
one_earleme
12.0
finish_parse
6.0
CRAP 43.5
1 package MarpaX::Lex::Easy;
2  
3 # ABSTRACT: A simple, foolproof, scanner/driver for Marpa
4 our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY
5 our $VERSION = '0.0000001'; # TRIAL VERSION
6  
7 use Moo;
8 use MooX::Types::MooseLike::Base 0.16 qw(:all);
9 use Data::Dumper;
10  
11 has 'tokens' => (
12 is => 'ro',
13 isa => HashRef,
14 required => 1,
15 );
16  
17 has 'recognizer' => (
18 is => 'ro',
19 isa => InstanceOf['Marpa::R2::Recognizer'],
20 required => 1,
21 );
22  
23 has 'automatic_whitespace' => (
24 is => 'ro',
25 isa => Int,
26 default => sub { 0 },
27 );
28  
29 has 'whitespace_pattern' => (
30 is => 'ro',
31 isa => RegexpRef,
32 );
33  
34 has 'input' => (
35 is => 'rw',
36 isa => Str,
37 );
38  
39 has 'pos' => (
40 is => 'rw',
41 isa => Int,
42 );
43  
44 sub lex {
45 my $self = shift;
46 my ($input, $pos, @expected) = @_;
47  
48 my @matches;
49 my $whitespace_consumed = 0;
50  
51 if ($self->automatic_whitespace) {
52 pos($input) = $pos;
53 if ($input =~ $self->whitespace_pattern) {
54 $whitespace_consumed = $+[0] - $-[0];
55 $pos += $whitespace_consumed;
56 }
57 }
58  
59 my $tokens = $self->tokens;
60  
61 TOKEN: for my $token_name (@expected) {
62 my $token = $tokens->{$token_name};
63 die "Unknown token $token_name" unless defined $token;
64 next if $token eq 'passthrough';
65 my $rule = $token->[0];
66  
67 pos($input) = $pos;
68 next TOKEN unless $input =~ $rule;
69  
70 my $matched_len = $+[0] - $-[0];
71 my $matched_value = undef;
72  
73 if (defined( my $val = $token->[1] )) {
74 if (ref $val eq 'CODE') {
75 eval {
76 $matched_value = $val->();
77 1;
78 } || do {
79 next TOKEN;
80 };
81 } else {
82 $matched_value = $val;
83 }
84 } elsif ($#- > 0) { # Captured a value
85 $matched_value = $1;
86 }
87  
88 push @matches, [ $token_name, \$matched_value, $matched_len + $whitespace_consumed ];
89 }
90 return @matches;
91 }
92  
93 sub one_earleme {
94 my $self = shift;
95 my $rec = $self->recognizer;
96 my $input = $self->input;
97 my $pos = $self->pos;
98  
99 my $expected_tokens = $rec->terminals_expected;
100  
101 my @matching_tokens;
102  
103 if (@$expected_tokens) {
104 @matching_tokens = $self->lex($input, $pos, @$expected_tokens);
105 $rec->alternative( @$_ ) for @matching_tokens;
106 }
107  
108 my $ok = eval {
109 $rec->earleme_complete;
110 1;
111 };
112  
113 if (@$expected_tokens && !$ok) {
114 my $err = $@;
115 die "Parse error, expecting [@$expected_tokens], got ", Dumper(\@matching_tokens),
116 "(error $err at $pos, ", Data::Dumper::qquote(substr($input, $pos, 10)), ")\n";
117 }
118 }
119  
120 sub finish_parse {
121 my $self = shift;
122 $self->recognizer->end_input;
123 my $ret = $self->recognizer->value;
124 if (defined $ret) {
125 return $$ret;
126 } else {
127 my $expected = $self->recognizer->terminals_expected;
128 die "Parse error at EOF, expecting [@$expected]";
129 }
130 }
131  
132 sub parse {
133 my $self = shift;
134 my $length = length $self->input;
135 while ($self->pos < $length) {
136 $self->one_earleme;
137 $self->pos($self->pos + 1);
138 }
139 return $self->finish_parse;
140 }
141  
142 sub read_input {
143 my $self = shift;
144 my ($input) = @_;
145 $self->input($input);
146 $self->pos(0);
147 }
148  
149 sub read_and_parse {
150 my $self = shift;
151 $self->read_input(@_);
152 return $self->parse;
153 }
154  
155 1;
156  
157  
158 __END__