File Coverage

blib/lib/Sub/Multi/Tiny/SigParse.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             ####################################################################
2             #
3             # This file was generated using Parse::Yapp version 1.21.
4             #
5             # Don't edit this file, use source file instead.
6             #
7             # ANY CHANGE MADE HERE WILL BE LOST !
8             #
9             ####################################################################
10             use vars qw ( @ISA );
11 14     14   7819 use strict;
  14         28  
  14         783  
12 14     14   87  
  14         26  
  14         458  
13             @ISA= qw ( Parse::Yapp::Driver );
14             use Parse::Yapp::Driver;
15 14     14   6495  
  14         30793  
  14         4028  
16             #line 6 "/root/.cpan/build/Sub-Multi-Tiny-0.000013-0/support/SigParse.yp"
17              
18              
19             # Imports {{{1
20              
21             use 5.006;
22             use strict;
23             use warnings;
24              
25             use Text::Balanced qw(extract_codeblock);
26              
27             # Types of constraints we've seen - bit offsets
28             use enum
29             # Flags set by the parser
30             'SEEN_WHERE', # `where` clause
31             'SEEN_TYPE', # Type constraint
32             'SEEN_POS', # Positional argument
33             'SEEN_NAMED', # Named argument
34             # future: SEEN_LITERAL for signatures holding literal values
35             # instead of name matches.
36              
37             # Flags set by later processing
38             'HAS_MULTIPLE_ARITIES', # Set if there are at least two
39             # different positional arities in a
40             # set of impls
41             ;
42              
43             # Set bits in YYData->{SEEN}
44             vec($_[0]->YYData->{SEEN}, $_[$_], 1) = 1 foreach 1..$#_;
45             }
46              
47             # }}}1
48             # Documentation {{{1
49              
50             =head1 NAME
51              
52             Sub::Multi::Tiny::SigParse - Parse::Yapp input to parse signatures in Sub::Multi::Tiny
53              
54             =head1 SYNOPSIS
55              
56             Generate the .pm file:
57              
58             yapp -m Sub::Multi::Tiny::SigParse -o lib/Sub/Multi/Tiny/SigParse.pm support/SigParse.yp
59              
60             And then:
61              
62             use Sub::Multi::Tiny::SigParse;
63             my $ast = Sub::Multi::Tiny::SigParse::Parse($signature);
64              
65             =head1 FUNCTIONS
66              
67             =cut
68              
69             # }}}1
70              
71              
72              
73             my($class)=shift;
74             ref($class)
75             and $class=ref($class);
76              
77             my($self)=$class->SUPER::new( yyversion => '1.21',
78             yystates =>
79             [
80             {#State 0
81             ACTIONS => {
82             'PARAM' => 1,
83             'TYPE' => 3
84             },
85             DEFAULT => -1,
86             GOTOS => {
87             'parameter' => 4,
88             'signature' => 2
89             }
90             },
91             {#State 1
92             ACTIONS => {
93             'WHERE' => 5
94             },
95             DEFAULT => -5
96             },
97             {#State 2
98             ACTIONS => {
99             '' => 6
100             }
101             },
102             {#State 3
103             ACTIONS => {
104             'PARAM' => 7
105             }
106             },
107             {#State 4
108             ACTIONS => {
109             'SEPAR' => 8
110             },
111             DEFAULT => -2
112             },
113             {#State 5
114             DEFAULT => -6
115             },
116             {#State 6
117             DEFAULT => 0
118             },
119             {#State 7
120             ACTIONS => {
121             'WHERE' => 9
122             },
123             DEFAULT => -7
124             },
125             {#State 8
126             ACTIONS => {
127             'TYPE' => 3,
128             'PARAM' => 1
129             },
130             DEFAULT => -1,
131             GOTOS => {
132             'signature' => 10,
133             'parameter' => 4
134             }
135             },
136             {#State 9
137             DEFAULT => -8
138             },
139             {#State 10
140             ACTIONS => {
141             'SEPAR' => 11
142             },
143             DEFAULT => -3
144             },
145             {#State 11
146             DEFAULT => -4
147             }
148             ],
149             yyrules =>
150             [
151             [#Rule 0
152             '$start', 2, undef
153             ],
154             [#Rule 1
155             'signature', 0,
156             sub
157             #line 86 "/root/.cpan/build/Sub-Multi-Tiny-0.000013-0/support/SigParse.yp"
158             { [] }
159             ],
160             [#Rule 2
161             'signature', 1,
162             sub
163             #line 87 "/root/.cpan/build/Sub-Multi-Tiny-0.000013-0/support/SigParse.yp"
164             { [ $_[1] ] }
165             ],
166             [#Rule 3
167             'signature', 3,
168             sub
169             #line 88 "/root/.cpan/build/Sub-Multi-Tiny-0.000013-0/support/SigParse.yp"
170             { [ $_[1], @{$_[3]} ] }
171             ],
172             [#Rule 4
173             'signature', 4,
174             sub
175             #line 90 "/root/.cpan/build/Sub-Multi-Tiny-0.000013-0/support/SigParse.yp"
176             { [ $_[1], @{$_[3]} ] }
177             ],
178             [#Rule 5
179             'parameter', 1,
180             sub
181             #line 95 "/root/.cpan/build/Sub-Multi-Tiny-0.000013-0/support/SigParse.yp"
182             {
183             _seen $_[0], $_[1]->{named} ? SEEN_NAMED : SEEN_POS;
184             return $_[1];
185             }
186             ],
187             [#Rule 6
188             'parameter', 2,
189             sub
190             #line 101 "/root/.cpan/build/Sub-Multi-Tiny-0.000013-0/support/SigParse.yp"
191             {
192             _seen $_[0], $_[1]->{named} ? SEEN_NAMED : SEEN_POS;
193             _seen $_[0], SEEN_WHERE;
194             return +{%{$_[1]}, where=>$_[2]};
195             }
196             ],
197             [#Rule 7
198             'parameter', 2,
199             sub
200             #line 108 "/root/.cpan/build/Sub-Multi-Tiny-0.000013-0/support/SigParse.yp"
201             {
202             _seen $_[0], $_[2]->{named} ? SEEN_NAMED : SEEN_POS;
203             _seen $_[0], SEEN_TYPE;
204             return +{%{$_[2]}, type => $_[1]};
205             }
206             ],
207             [#Rule 8
208             'parameter', 3,
209             sub
210             #line 115 "/root/.cpan/build/Sub-Multi-Tiny-0.000013-0/support/SigParse.yp"
211             {
212             _seen $_[0], $_[2]->{named} ? SEEN_NAMED : SEEN_POS;
213             _seen $_[0], SEEN_TYPE, SEEN_WHERE;
214             return +{%{$_[2]}, where=>$_[3], type => $_[1]}
215             }
216             ]
217             ],
218             @_);
219             bless($self,$class);
220             }
221              
222             #line 122 "/root/.cpan/build/Sub-Multi-Tiny-0.000013-0/support/SigParse.yp"
223              
224              
225             #############################################################################
226             # Footer
227              
228             # Tokenizer and error-reporting routine for Parse::Yapp {{{1
229              
230             # The lexer
231             my $parser = shift;
232             my $text = $parser->YYData->{TEXT};
233              
234             $$text =~ m/\G\s+/gc; # Skip H and V whitespace
235             $parser->YYData->{CURR_TOK_POS} = pos($$text);
236              
237             return ('', undef) unless (pos($$text)||0) < length($$text); # EOF
238              
239             $$text =~ m/\G,/gc and return (SEPAR => 0); # 0 is a dummy value
240              
241             if($$text =~ m/\G([:]?)([\$\@\%\&\*]\w+)\b([?!]?)/gc) {
242             my $retval = {};
243             $retval->{name} = $2;
244             $retval->{named} = !!$1;
245             $retval->{reqd} = (
246             ($retval->{named} && $3 eq '!') || # Named: optional unless !
247             (!$retval->{named} && $3 ne '?') # Positional: reqd unless ?
248             );
249             return (PARAM => $retval);
250             }
251              
252             if($$text =~ m/\Gwhere\s*\{/gci) {
253             pos($$text) -= 1; # Get the lbrace back
254             my ($block) = extract_codeblock($$text); # Updates pos()
255             return (WHERE => $block) if defined $block;
256             die "Saw a 'where' without a valid block after it";
257             }
258              
259             # Permit braced expressions for complex type checks
260             if($$text =~ m/\G\{/gc) {
261             pos($$text) -= 1; # Get the lbrace back
262             my ($block) = extract_codeblock($$text); # Updates pos()
263             return (TYPE => $block) if defined $block;
264             die "Saw an opening brace without a valid block after it";
265             }
266              
267             # If the next thing is a backslash, die --- prohibit backslash to start
268             # a typecheck as a guard against '' vs "" confusion. If you want a
269             # backslash, use the {} form.
270             if($$text =~ m{\G\\}gc) {
271             die "Saw a backslash where I don't know what to do with it! ('' vs \"\" confusion?)";
272             }
273              
274             # Otherwise, assume a single word is a typecheck
275             $$text =~ m/\G(\S+)/gc and return (TYPE => $1);
276              
277             die "This should never happen! Unlexable text was: " .
278             substr($$text, pos($$text));
279             } #_next_token()
280              
281             # Report an error
282             my $parser = shift;
283             my $startpos = $parser->YYData->{CURR_TOK_POS};
284             my $endpos = pos(${ $parser->YYData->{TEXT} });
285             my $got = $parser->YYCurtok || '<end of input>';
286             my $val='';
287             $val = ' (' . $parser->YYCurval . ')' if $parser->YYCurval;
288              
289             my $errmsg = 'Syntax error: could not understand ' . $got . $val .
290             " at positions $startpos..$endpos";
291             if(ref($parser->YYExpect) eq 'ARRAY') {
292             $errmsg .= ".\nExpected one of: " . join(',', @{$parser->YYExpect});
293             } else {
294             $errmsg .= ':'
295             }
296              
297             # Print the text and flag the error
298             my $copy = ${ $parser->YYData->{TEXT} };
299             $copy =~ s/\s/ /g; # Normalize spaces so pos values line up
300             $errmsg .= "\n$copy";
301             $errmsg .= "\n" . (' ' x $startpos) . ('^' x ($endpos-$startpos));
302              
303             $errmsg .= "\n"; # No stack trace
304             die $errmsg;
305             } #_report_error()
306              
307             # }}}1
308             # Top-level parse function {{{1
309              
310             =head2 Parse
311              
312             Parse arguments. Usage:
313              
314             my $ast = Sub::Multi::Tiny::SigParse::Parse($signature);
315              
316             =cut
317              
318             my $text = shift;
319             unless(defined $text) {
320             require Carp;
321             Carp::croak 'Parse: Need a signature to parse';
322             }
323              
324             my $parser = __PACKAGE__->new;
325             my $hrData = $parser->YYData;
326              
327             # Data we use while parsing.
328              
329             # TEXT: The input text. Store it as a reference so pos() will
330             # be preserved across calls to _next_token.
331             $hrData->{TEXT} = \"$text";
332              
333             # CURR_TOK_POS: the pos() value where the current token started.
334             # Used in reporting errors.
335             $hrData->{CURR_TOK_POS} = -1;
336              
337             # SEEN: bit flags for which types of things we've seen
338             $hrData->{SEEN} = '';
339              
340             my $lrParms = $parser->YYParse(yylex => \&_next_token,
341             yyerror => \&_report_error,
342             (@_ ? (yydebug => $_[0]) : ()),
343             );
344             my %retval = (seen => $hrData->{SEEN}, parms => $lrParms);
345              
346             return \%retval;
347             } #Parse()
348              
349             # }}}1
350             # Rest of the docs {{{1
351              
352             =head1 AUTHOR
353              
354             Chris White E<lt>cxw@cpan.orgE<gt>
355              
356             =head1 LICENSE
357              
358             Copyright (C) 2019 Chris White E<lt>cxw@cpan.orgE<gt>
359              
360             This library is free software; you can redistribute it and/or modify
361             it under the same terms as Perl itself.
362              
363             =cut
364              
365             # }}}1
366              
367             # vi: set fdm=marker: #
368              
369             1;