| 1 | # SigParse.yp: Parse::Yapp input to parse signatures in Sub::Multi::Tiny | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | ############################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Header | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | %{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # Imports {{{1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use Text::Balanced qw(extract_codeblock); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # Types of constraints we've seen - bit offsets | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | use enum | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # Flags set by the parser | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 8 | ▶ | 'SEEN_WHERE', # `where` clause | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 'SEEN_TYPE', # Type constraint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 'SEEN_POS', # Positional argument | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 'SEEN_NAMED', # Named argument | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # future: SEEN_LITERAL for signatures holding literal values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # instead of name matches. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # Flags set by later processing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 'HAS_MULTIPLE_ARITIES', # Set if there are at least two | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # different positional arities in a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # set of impls | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 8 | ; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # Set bits in YYData->{SEEN} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 45 | ▶ | sub _seen { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_seen: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 45 | vec($_[0]->YYData->{SEEN}, $_[$_], 1) = 1 foreach 1..$#_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # }}}1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # Documentation {{{1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | Sub::Multi::Tiny::SigParse - Parse::Yapp input to parse signatures in Sub::Multi::Tiny | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | Generate the .pm file: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | yapp -m Sub::Multi::Tiny::SigParse -o lib/Sub/Multi/Tiny/SigParse.pm support/SigParse.yp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | And then: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | use Sub::Multi::Tiny::SigParse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | my $ast = Sub::Multi::Tiny::SigParse::Parse($signature); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # }}}1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 26 | ▶ | ############################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 26 | # Token and precedence definitions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 26 | ▶ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # TODO: slurpies (prefix *, +); trailing ?, ! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # Separator (usually a comma) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | %token SEPAR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # Type before a variable name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | %token TYPE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # Parameter, named or positional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | %token PARAM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # "where BLOCK" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | %token WHERE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | %% | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | ############################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # Rules | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | signature: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | { [] } # always return arrayref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 23 | ▶ | | parameter { [ $_[1] ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 7 | ▶ | | parameter SEPAR signature { [ $_[1], @{$_[3]} ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # Permit trailing comma | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | | parameter SEPAR signature SEPAR { [ $_[1], @{$_[3]} ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | parameter: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | PARAM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 15 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 15 | ▶ | _seen $_[0], $_[1]->{named} ? SEEN_NAMED : SEEN_POS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 15 | return $_[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | | PARAM WHERE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 4 | ▶ | _seen $_[0], $_[1]->{named} ? SEEN_NAMED : SEEN_POS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 4 | _seen $_[0], SEEN_WHERE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | return +{%{$_[1]}, where=>$_[2]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | | TYPE PARAM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 6 | ▶ | _seen $_[0], $_[2]->{named} ? SEEN_NAMED : SEEN_POS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 6 | _seen $_[0], SEEN_TYPE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 6 | return +{%{$_[2]}, type => $_[1]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | | TYPE PARAM WHERE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 5 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 5 | ▶ | _seen $_[0], $_[2]->{named} ? SEEN_NAMED : SEEN_POS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 5 | _seen $_[0], SEEN_TYPE, SEEN_WHERE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | return +{%{$_[2]}, where=>$_[3], type => $_[1]} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 26 | ; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | %% | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 26 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | ############################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # Footer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # Tokenizer and error-reporting routine for Parse::Yapp {{{1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # The lexer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 155 | ▶ | sub _next_token { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_next_token: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 155 | my $parser = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 155 | my $text = $parser->YYData->{TEXT}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 155 | $$text =~ m/\G\s+/gc; # Skip H and V whitespace | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 155 | $parser->YYData->{CURR_TOK_POS} = pos($$text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 155 | ▶ | return ('', undef) unless (pos($$text)||0) < length($$text); # EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
pos $$text || 0
MC/DC100%1/1
pos $$text || 0
pos $$text
Truth table100%2/2
A
A= pos $$text
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 125 | ▶ | $$text =~ m/\G,/gc and return (SEPAR => 0); # 0 is a dummy value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 105 | ▶ | if($$text =~ m/\G([:]?)([\$\@\%\&\*]\w+)\b([?!]?)/gc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 45 | my $retval = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 45 | $retval->{name} = $2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 45 | $retval->{named} = !!$1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | $retval->{reqd} = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | ($retval->{named} && $3 eq '!') || # Named: optional unless ! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 45 | ▶ | (!$retval->{named} && $3 ne '?') # Positional: reqd unless ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%9/9
$retval->{'named'} && $3 eq '!' || !$retval->{'named'} && $3 ne '?'
$retval->{"named"} and $3 eq "!"
not $retval->{"named"} and $3 ne "?"
MC/DC75%3/4
$retval->{'named'} && $3 eq '!' || !$retval->{'named'} && $3 ne '?'
$retval->{"named"}
$3 eq "!"
not $retval->{"named"}
$3 ne "?"
Truth table57%4/7
A and B || not(C) and D
A= $retval->{"named"}B= $3 eq "!"C= not $retval->{"named"}D= $3 ne "?"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 45 | return (PARAM => $retval); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 60 | ▶ | if($$text =~ m/\Gwhere\s*\{/gci) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 18 | pos($$text) -= 1; # Get the lbrace back | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 18 | my ($block) = extract_codeblock($$text); # Updates pos() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 18 | ▶ | return (WHERE => $block) if defined $block; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 1 | die "Saw a 'where' without a valid block after it"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # Permit braced expressions for complex type checks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 42 | ▶ | if($$text =~ m/\G\{/gc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 15 | pos($$text) -= 1; # Get the lbrace back | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 15 | my ($block) = extract_codeblock($$text); # Updates pos() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 15 | ▶ | return (TYPE => $block) if defined $block; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 4 | die "Saw an opening brace without a valid block after it"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # If the next thing is a backslash, die --- prohibit backslash to start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # a typecheck as a guard against '' vs "" confusion. If you want a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # backslash, use the {} form. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 27 | ▶ | if($$text =~ m{\G\\}gc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 1 | die "Saw a backslash where I don't know what to do with it! ('' vs \"\" confusion?)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # Otherwise, assume a single word is a typecheck | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 26 | ▶ | $$text =~ m/\G(\S+)/gc and return (TYPE => $1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | die "This should never happen! Unlexable text was: " . | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | substr($$text, pos($$text)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } #_next_token() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # Report an error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 2 | ▶ | sub _report_error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_report_error: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 2 | my $parser = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 2 | my $startpos = $parser->YYData->{CURR_TOK_POS}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 2 | my $endpos = pos(${ $parser->YYData->{TEXT} }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 2 | ▶ | my $got = $parser->YYCurtok || '<end of input>'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$parser->YYCurtok || '<end of input>'
MC/DC100%1/1
$parser->YYCurtok || '<end of input>'
$parser->YYCurtok
Truth table100%2/2
A
A= $parser->YYCurtok
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 2 | my $val=''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 2 | ▶ | $val = ' (' . $parser->YYCurval . ')' if $parser->YYCurval; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 2 | my $errmsg = 'Syntax error: could not understand ' . $got . $val . | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | " at positions $startpos..$endpos"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | ▶ | if(ref($parser->YYExpect) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | $errmsg .= ".\nExpected one of: " . join(',', @{$parser->YYExpect}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 2 | $errmsg .= ':' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # Print the text and flag the error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 2 | my $copy = ${ $parser->YYData->{TEXT} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 2 | $copy =~ s/\s/ /g; # Normalize spaces so pos values line up | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 2 | $errmsg .= "\n$copy"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 2 | $errmsg .= "\n" . (' ' x $startpos) . ('^' x ($endpos-$startpos)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 2 | $errmsg .= "\n"; # No stack trace | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 2 | die $errmsg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } #_report_error() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # }}}1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # Top-level parse function {{{1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | =head2 Parse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | Parse arguments. Usage: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | my $ast = Sub::Multi::Tiny::SigParse::Parse($signature); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 26 | ▶ | sub Parse { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Parse: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 26 | my $text = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 26 | ▶ | unless(defined $text) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | Carp::croak 'Parse: Need a signature to parse'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 26 | my $parser = __PACKAGE__->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 26 | my $hrData = $parser->YYData; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # Data we use while parsing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # TEXT: The input text. Store it as a reference so pos() will | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | # be preserved across calls to _next_token. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 26 | $hrData->{TEXT} = \"$text"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | # CURR_TOK_POS: the pos() value where the current token started. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | # Used in reporting errors. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 26 | $hrData->{CURR_TOK_POS} = -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | # SEEN: bit flags for which types of things we've seen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 26 | $hrData->{SEEN} = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 26 | ▶ | my $lrParms = $parser->YYParse(yylex => \&_next_token, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | yyerror => \&_report_error, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | (@_ ? (yydebug => $_[0]) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 24 | my %retval = (seen => $hrData->{SEEN}, parms => $lrParms); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 24 | return \%retval; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | } #Parse() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | # }}}1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # Rest of the docs {{{1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | Chris White E<lt>cxw@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | Copyright (C) 2019 Chris White E<lt>cxw@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | # }}}1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | # vi: set fdm=marker: # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||