Karel/Parser.pm

filter: Statementstmt 98.9% 94 / 95 Branchbran 100% 14 / 14 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 96.8% 31 / 32 Podpod 100% 2 / 2 totaltotal 96.7% 148 / 153 CC 11 SCAR 9.1
CC
11
Cov
98%
parse
8.0
terminals
2.0
If
2.0
CRAP 2.5
« Karel/Util.pm ↑ Summary Karel/Robot/WithGrid.pm »
1 package Karel::Parser;
2  
3 =head1 NAME
4  
5 Karel::Parser
6  
7 =head1 METHODS
8  
9 =over 4
10  
11 =cut
12  
13 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Marpa::R2;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 { package # Hide from CPAN.
21 Karel::Parser::Actions;
22  
23 9 ▶ use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 our @EXPORT_OK = qw{ def concat left forward pick drop stop repeat
25 While If first_ch negate call list defs run };
26  
27 47 ▶ sub def { [ $_[1], $_[2] ] }
Subroutine def: called CC=1 SCAR=0.0
28 80 ▶ sub concat { $_[1] . $_[2] }
Subroutine concat: called CC=1 SCAR=0.0
29 39 ▶ sub left { [ l => $_[1] ] }
Subroutine left: called CC=1 SCAR=0.0
30 6 ▶ sub forward { [ f => $_[1] ] }
Subroutine forward: called CC=1 SCAR=0.0
31 9 ▶ sub pick { [ p => $_[1] ] }
Subroutine pick: called CC=1 SCAR=0.0
32 4 ▶ sub drop { [ d => $_[1] ] }
Subroutine drop: called CC=1 SCAR=0.0
33 2 ▶ sub stop { [ q => $_[1] ] }
Subroutine stop: called CC=1 SCAR=0.0
34 25 ▶ sub repeat { complex_command( r => @_ ) }
Subroutine repeat: called CC=1 SCAR=0.0
35 7 ▶ sub While { complex_command( w => @_ ) }
Subroutine While: called CC=1 SCAR=0.0
36 19 ▶ sub If { [ i => @{ $_[1] }[ 0, 1 ],
Subroutine If: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1][2] ? : 2 17 19
37 ref $_[1][2] ? $_[1][2] : [['x']], # else
38 19 [ @{ $_[1] }[ -2, -1 ] ] ] }
39 25 ▶ sub first_ch { substr $_[1], 0, 1 }
Subroutine first_ch: called CC=1 SCAR=0.0
40 17 ▶ sub negate { '!' . $_[1] }
Subroutine negate: called CC=1 SCAR=0.0
41 32 ▶ sub call { $_[0]{ $_[1][0] } = 1;
Subroutine call: called CC=1 SCAR=0.0
42 32 [ 'c', $_[1][0], [ @{ $_[1] }[ 1, 2 ] ] ] }
43 100 ▶ sub list { [ grep defined, @_[ 1 .. $#_ ] ] }
Subroutine list: called CC=1 SCAR=0.0
44  
45 26 ▶ sub defs {
Subroutine defs: called CC=2 SCAR=6.9
46 26 my $unknown = shift;
47 26 my %h;
48 26 for my $command (@_) {
49 47 $h{ $command->[0][0] } = [ $command->[0][1], @{ $command }[ 1, 2 ] ];
50 }
51 26 return [ \%h, $unknown ]
52 }
53  
54 15 ▶ sub run {
Subroutine run: called CC=1 SCAR=0.0
55 15 shift;
56 15 $_[0][-1][0] -= length 'run ';
57 15 [ @_ ]
58 }
59  
60 32 ▶ sub complex_command {
Subroutine complex_command: called CC=1 SCAR=0.0
61 32 my $cmd = shift;
62 32 [ $cmd => @{ $_[1] }[ 0, 1 ], [ @{ $_[1] }[ 2, 3 ] ] ]
63 }
64  
65 }
66  
67 9 my %terminals = (
68 octothorpe => '#',
69 drop_mark => 'drop-mark',
70 pick_mark => 'pick-mark',
71 );
72  
73 $terminals{$_} = $_
74 9 for qw( command left forward stop repeat while if else end done
75 wall mark there a facing not North East South West x times
76 s is isn t no );
77  
78  
79 9 my $dsl = << '__DSL__';
80  
81 :default ::= action => ::undef
82 lexeme default = latm => 1
83  
84 START ::= Defs action => ::first
85 | (Run SC) Command action => run
86 Run ::= 'run' action => [ values, start, length ]
87  
88 Defs ::= Def+ separator => SC action => defs
89 Def ::= Def2 action => [ values, start, length ]
90 Def2 ::= (SCMaybe) (command) (SC) CommandDef (SC) Prog (SC) (end)
91 action => def
92 NewCommand ::= CommandDef action => [ values, start, length ]
93 CommandDef ::= alpha valid_name action => concat
94 Prog ::= Commands action => ::first
95 Commands ::= Command+ separator => SC action => list
96 Command ::= Left action => left
97 | Forward action => forward
98 | Drop_mark action => drop
99 | Pick_mark action => pick
100 | Stop action => stop
101 | Repeat action => repeat
102 | While action => While
103 | If action => If
104 | NewCommand action => call
105 Left ::= left action => [ start, length ]
106 Forward ::= forward action => [ start, length ]
107 Drop_mark ::= drop_mark action => [ start, length ]
108 Pick_mark ::= pick_mark action => [ start, length ]
109 Stop ::= stop action => [ start, length ]
110 Repeat ::= (repeat SC) Num (SC Times SC) Prog (SC done)
111 action => [ values, start, length ]
112 While ::= (while SC) Condition (SC) Prog (done)
113 action => [ values, start, length ]
114 If ::= (if SC) Condition (SC) Prog (done)
115 action => [ values, start, length ]
116 | (if SC) Condition (SC) Prog (SC else SC) Prog (done)
117 action => [ values, start, length ]
118 Condition ::= (there quote s SC a SC) Covering
119 action => ::first
120 | (there SC is SC a SC) Covering
121 action => ::first
122 | (Negation SC) Covering action => negate
123 | (facing SC) Wind action => ::first
124 | (not SC facing SC) Wind action => negate
125 Negation ::= (there SC isn quote t SC a)
126 | (there SC is SC no)
127 | (there quote s SC no)
128 Covering ::= mark action => first_ch
129 | wall action => first_ch
130 Wind ::= North action => first_ch
131 | East action => first_ch
132 | South action => first_ch
133 | West action => first_ch
134 Num ::= non_zero action => ::first
135 | non_zero digits action => concat
136 Times ::= times
137 | x
138 Comment ::= (octothorpe non_lf lf)
139 SC ::= SpComm+
140 SCMaybe ::= SpComm*
141 SpComm ::= Comment
142 || space
143  
144 alpha ~ [a-z]
145 valid_name ~ [-a-z_0-9]+
146 non_zero ~ [1-9]
147 digits ~ [0-9]+
148 space ~ [\s]+
149 quote ~ [']
150 non_lf ~ [^\n]*
151 lf ~ [\n]
152  
153 __DSL__
154 9 $dsl .= join "\n", map "$_ ~ '$terminals{$_}'", keys %terminals;
155  
156  
157 9 has parser => ( is => 'ro' );
158  
159 9 has _grammar => ( is => 'lazy' );
160  
161 27 ▶sub _dsl { $dsl }
Subroutine _dsl: called CC=1 SCAR=0.0
162  
163 39 ▶sub _action_class { 'Karel::Parser::Actions' }
Subroutine _action_class: called CC=1 SCAR=0.0
164  
165 37 ▶sub _terminals { \%terminals }
Subroutine _terminals: called CC=1 SCAR=0.0
166  
167 =item my @terminal_strings = $self->terminals(@terminals)
168  
169 Returns the strings correscponding to the given terminal symbols.
170 E.g., C<< $self->terminals('octothorpe') >> returns C<#>.
171  
172 =cut
173  
174 9 ▶sub terminals {
Subroutine terminals: called CC=2 SCAR=7.1
Pod documented
175 9 my $self = shift;
176 9 ▶ return map $self->_terminals->{$_} // $_, @_
Condition66%2/3
$self->_terminals->{$_} // $_
l!l&&r!l&&!r
51120
MC/DC0%0/2
$self->_terminals->{$_} // $_
$self->_terminals->{$_} $_
Truth table66%2/3
A // B
A= $self->_terminals->{$_}B= $_
ABresult
1X1
011
000
177 }
178  
179 34 ▶sub _build__grammar {
Subroutine _build__grammar: called CC=1 SCAR=0.0
180 34 my ($self) = @_;
181 34 my $g = 'Marpa::R2::Scanless::G'->new({ source => \$self->_dsl });
182 34 return $g
183 }
184  
185 =item my ($new_commands, $unknown) = $parser->parse($definition)
186  
187 C<$new_commands> is a hash that you can use to teach the robot:
188  
189 $robot->_learn($_, $new_commands->{$_}, $definition) for keys %$new_commands;
190  
191 C<$unknwon> is a hash whose keys are all the non-basic commands needed
192 to run the parsed programs.
193  
194 When the input starts with C<run >, it should contain just one
195 command. The robot's C<run> function uses it to parse commands you
196 run, as simple C<[[ 'c', $command ]]> doesn't work for core commands
197 (C<left>, C<forward>, etc.).
198  
199 If there's an error, an exception is thrown. It's a hash ref with the
200 following keys:
201  
202 =over 4
203  
204 =item -
205  
206 B<expected>: lists the available terminals. There are several special
207 values: C<space> (white space), C<alpha> (letter starting a word),
208 C<lf> newline, C<non_lf> (anything but a newline), C<valid_name>
209 (character that can occur in a command name starting from the 2nd
210 position: a letter, digit, underscore, or a dash), C<quote> (single
211 quote), C<non_zero> (1-9).
212  
213 =item -
214  
215 B<last_completed>: the last successfully parsed command.
216  
217 =item -
218  
219 B<pos>: position (line, column) where the parsing stopped.
220  
221 =back
222  
223 =cut
224  
225 49 ▶sub parse {
Subroutine parse: called CC=8 SCAR=20.8
Pod documented
226 49 my ($self, $input) = @_;
227 49 my $recce = 'Marpa::R2::Scanless::R'
228 ->new({ grammar => $self->_grammar,
229 semantics_package => $self->_action_class,
230 });
231  
232 49 my ($line, $column);
233 eval {
234 49 $recce->read(\$input);
235 49 ▶ 1 } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $recce->read(\$input); 1 } } or do { my $exception = $@; () = $recce->line_column } 47 2 49
236 2 my $exception = $@;
237 2 ($line, $column) = $recce->line_column;
238 };
239  
240 49 my $value = $recce->value;
241 49 ▶ if ($line || ! $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line or not $value) 9 40 49
Condition100%3/3
$line or not $value
l!l&&r!l&&!r
2740
MC/DC100%2/2
$line or not $value
$line not $value
Truth table100%3/3
A or not(B)
A= $lineB= not $value
ABresult
1X1
011
000
242 9 my ($from, $length) = $recce->last_completed('Command');
243 9 my @expected = $self->terminals(@{ $recce->terminals_expected });
244 9 my $E = bless { expected => \@expected }, ref($self) . '::Exception';
245 9 ▶ my $last = $recce->substring($from, $length) if defined $from;
Branch: 1 branch
BranchTrueFalseTotal
if defined $from 6 3 9
246 9 ▶ $E->{last_completed} = $last if $last;
Branch: 1 branch
BranchTrueFalseTotal
if $last 6 3 9
247 9 ▶ if ($line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line) { } 2 7 9
248 2 $E->{pos} = [ $line, $column ];
249 } else {
250 7 $E->{pos} = [ $recce->line_column ];
251 }
252 9 die $E
253 }
254 40 ▶ return $input =~ /^run / ? $value : @$$value
Branch: 1 branch
BranchTrueFalseTotal
$input =~ /^run / ? : 14 26 40
255 }
256  
257  
258 { package
259 Karel::Parser::Exception;
260  
261 0 ▶ use overload '""' => sub { use Data::Dumper; Dumper \@_ };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
262  
263 }
264  
265 =back
266  
267 =cut
268  
269 9 __PACKAGE__
« Karel/Util.pm ↑ Summary Karel/Robot/WithGrid.pm »