| 1 | package Perl6::Signature; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use Parse::RecDescent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Text::Balanced; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Perl6::Signature::Val; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 1 | our $VERSION = '0.04'; | |||||||||||
| 10 | |||||||||||||
| 11 | #$::RD_TRACE = 1; | ||||||||||||
| 12 | 1 | $::RD_HINT = 1; | |||||||||||
| 13 | |||||||||||||
| 14 | 1 | our $SIGNATURE_GRAMMAR = << '#\'END'; | |||||||||||
| 15 | #\ | ||||||||||||
| 16 | { | ||||||||||||
| 17 | use Text::Balanced qw(extract_bracketed); | ||||||||||||
| 18 | use Carp qw(croak); | ||||||||||||
| 19 | } | ||||||||||||
| 20 | |||||||||||||
| 21 | Sig: Sig_colon | Sig_nocolon | ||||||||||||
| 22 | |||||||||||||
| 23 | Sig_colon: ':' Sig_nocolon | ||||||||||||
| 24 | |||||||||||||
| 25 | Sig_nocolon: '(' Sigbody ')' | ||||||||||||
| 26 | { $item{Sigbody} } | ||||||||||||
| 27 | |||||||||||||
| 28 | Sigbody: Sigbody_inv | Sigbody_noinv | ||||||||||||
| 29 | |||||||||||||
| 30 | Sigbody_inv: Invocant <skip:'\s*'> ':' Sigbody_noinv | ||||||||||||
| 31 | { | ||||||||||||
| 32 | my $sig = $item{Sigbody_noinv}; | ||||||||||||
| 33 | die "invocant cannot be optional" unless $item{Invocant}->{required}; | ||||||||||||
| 34 | $sig->s_invocant( $item{Invocant}->{param} ); | ||||||||||||
| 35 | $return = $sig; | ||||||||||||
| 36 | } | ||||||||||||
| 37 | |||||||||||||
| 38 | Sigbody_noinv: Param(s? /,/) | ||||||||||||
| 39 | { | ||||||||||||
| 40 | my @params = @{ $item{'Param(s?)'} }; | ||||||||||||
| 41 | my @slurpies = map { $_->{param} } grep { $_->{slurpy} } @params; | ||||||||||||
| 42 | my @nonslurpies = grep { !$_->{slurpy} } @params; | ||||||||||||
| 43 | my @positionals = grep { $_->{style} eq 'positional' } @nonslurpies; | ||||||||||||
| 44 | my @named = grep { $_->{style} eq 'named' } @nonslurpies; | ||||||||||||
| 45 | |||||||||||||
| 46 | my $seen_optional; | ||||||||||||
| 47 | my $requiredPositionalCount = 0; | ||||||||||||
| 48 | |||||||||||||
| 49 | # calculate requiredPositionalCount, and make sure we don't have | ||||||||||||
| 50 | # :($optional?, $required!) -- that's invalid. | ||||||||||||
| 51 | for my $param (@positionals) { | ||||||||||||
| 52 | $seen_optional++ if ! $param->{required}; | ||||||||||||
| 53 | die "can't place required positional after an optional one" if | ||||||||||||
| 54 | $param->{required} && $seen_optional; | ||||||||||||
| 55 | $requiredPositionalCount++ if ! $seen_optional; | ||||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | my %slurpies = map { $_->p_sigil => $_ } @slurpies; | ||||||||||||
| 59 | |||||||||||||
| 60 | my ( $slurpy_array, $slurpy_hash ) = ( @slurpies{qw(@ %)} ); | ||||||||||||
| 61 | |||||||||||||
| 62 | croak "Only one slurpy of every type is allowed" if keys %slurpies != @slurpies; | ||||||||||||
| 63 | |||||||||||||
| 64 | my $sig = Perl6::Signature::Val::Sig->new | ||||||||||||
| 65 | ( s_requiredPositionalCount => $requiredPositionalCount | ||||||||||||
| 66 | , s_positionalList => [ map { $_->{param} } @positionals ] | ||||||||||||
| 67 | , s_namedList => [ map { $_->{param} } @named ] | ||||||||||||
| 68 | , s_requiredNames => { map { $_->{param}->p_label => 1 } grep { $_->{required} } @named } | ||||||||||||
| 69 | , ( $slurpy_array ? ( s_slurpyArray => $slurpy_array ) : () ), | ||||||||||||
| 70 | , ( $slurpy_hash ? ( s_slurpyHash => $slurpy_hash ) : () ), | ||||||||||||
| 71 | ); | ||||||||||||
| 72 | $return = $sig; | ||||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | Invocant: Param | ||||||||||||
| 76 | |||||||||||||
| 77 | Param: ParamType(s? /\|/) | ||||||||||||
| 78 | SlurpynessModifier(?) | ||||||||||||
| 79 | ParamIdentifier <skip:''> | ||||||||||||
| 80 | OptionalityModifier(?) | ||||||||||||
| 81 | <skip:'\s*'> | ||||||||||||
| 82 | Unpacking(?) | ||||||||||||
| 83 | DefaultValueSpec(?) | ||||||||||||
| 84 | <skip:'\s+'> | ||||||||||||
| 85 | Attrib(s?) | ||||||||||||
| 86 | <skip:'\s*'> | ||||||||||||
| 87 | Constraint(s?) | ||||||||||||
| 88 | { | ||||||||||||
| 89 | my ($variable, $label, $style) = | ||||||||||||
| 90 | @{$item{ParamIdentifier}}{qw/variable label style/}; | ||||||||||||
| 91 | |||||||||||||
| 92 | my ($hasAccess, $isRef, $isContext, $isLazy, @slots); | ||||||||||||
| 93 | # unfortunately, we can't use a hash and delete from it: | ||||||||||||
| 94 | # "is ro is rw" means "is rw". (Or maybe, a parse error.) | ||||||||||||
| 95 | ATTR: for (@{ $item{'Attrib(s?)'} }) { | ||||||||||||
| 96 | /^(ro|rw|copy)$/ && do { $hasAccess = $_; next ATTR }; | ||||||||||||
| 97 | /^ref$/ && do { $isRef = 1; next ATTR }; | ||||||||||||
| 98 | /^context$/ && do { $isContext = 1; next ATTR }; | ||||||||||||
| 99 | /^lazy$/ && do { $isLazy = 1; next ATTR }; | ||||||||||||
| 100 | push @slots, $_; | ||||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | my $param = Perl6::Signature::Val::SigParam->new | ||||||||||||
| 104 | ( p_types => $item{'ParamType(s?)'} | ||||||||||||
| 105 | , p_variable => $variable | ||||||||||||
| 106 | , p_label => $label | ||||||||||||
| 107 | , ($item{'Unpacking(?)'} ? (p_unpacking => $item{'Unpacking(?)'}->[0]) : ()) | ||||||||||||
| 108 | , (@{ $item{'DefaultValueSpec(?)'} } ? | ||||||||||||
| 109 | (p_default => $item{'DefaultValueSpec(?)'}->[0]) : ()) | ||||||||||||
| 110 | , (@{ $item{'Constraint(s?)'} } ? | ||||||||||||
| 111 | (p_constraints => [ @{ $item{'Constraint(s?)'} } ]) : ()) | ||||||||||||
| 112 | , ( $hasAccess ? ( p_hasAccess => $hasAccess ) : () ), | ||||||||||||
| 113 | , p_isRef => $isRef | ||||||||||||
| 114 | , p_isContext => $isContext | ||||||||||||
| 115 | , p_isLazy => $isLazy | ||||||||||||
| 116 | , p_slots => { map { $_ => 1 } @slots } | ||||||||||||
| 117 | # "is foo<42>" not supported yet. | ||||||||||||
| 118 | ); | ||||||||||||
| 119 | my $slurpy = 1 == @{ $item{'SlurpynessModifier(?)'} }; | ||||||||||||
| 120 | my $optionality = $item{'OptionalityModifier(?)'}->[0] || ''; | ||||||||||||
| 121 | my $optional = scalar @{ $item{'DefaultValueSpec(?)'} }; | ||||||||||||
| 122 | die "required parameter can't have default value" if | ||||||||||||
| 123 | $optional && $optionality eq '!'; | ||||||||||||
| 124 | $optional = 1 if $style eq 'named' && $optionality ne '!'; | ||||||||||||
| 125 | $optional = 1 if $optionality eq '?'; | ||||||||||||
| 126 | |||||||||||||
| 127 | $return = { param => $param | ||||||||||||
| 128 | , required => !$optional | ||||||||||||
| 129 | , style => $style | ||||||||||||
| 130 | , slurpy => $slurpy | ||||||||||||
| 131 | }; | ||||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | ParamType: /[a-zA-Z]\w+/ | ||||||||||||
| 135 | |||||||||||||
| 136 | ParamIdentifier: ParamIdentifier_positional | ||||||||||||
| 137 | | ParamIdentifier_named | ||||||||||||
| 138 | |||||||||||||
| 139 | # Perl 6 allows placeholder parameters, e.g. :($) - sub of arity 1 (scalar). | ||||||||||||
| 140 | ParamIdentifier_positional: Sigil <skip:''> Label(?) | ||||||||||||
| 141 | { | ||||||||||||
| 142 | my $label = @{ $item{'Label(?)'} } ? $item{'Label(?)'}->[0] : ''; | ||||||||||||
| 143 | $return = { variable => $item{Sigil} . $label | ||||||||||||
| 144 | , label => $label | ||||||||||||
| 145 | , style => 'positional' | ||||||||||||
| 146 | }; | ||||||||||||
| 147 | } | ||||||||||||
| 148 | |||||||||||||
| 149 | # TODO: L<S06/"Multiple name wrappings may be given">, whoa. | ||||||||||||
| 150 | ParamIdentifier_named: | ||||||||||||
| 151 | ':' <skip:''> Label ParamIdentifier_named_variablename | ||||||||||||
| 152 | { | ||||||||||||
| 153 | $return = { variable => $item{'ParamIdentifier_named_variablename'} | ||||||||||||
| 154 | , label => $item{Label} | ||||||||||||
| 155 | , style => 'named' | ||||||||||||
| 156 | }; | ||||||||||||
| 157 | } | ||||||||||||
| 158 | | ':' <skip:''> Sigil Label | ||||||||||||
| 159 | { | ||||||||||||
| 160 | $return = { variable => $item{Sigil} . $item{Label} | ||||||||||||
| 161 | , label => $item{Label} | ||||||||||||
| 162 | , style => 'named' | ||||||||||||
| 163 | }; | ||||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | ParamIdentifier_named_variablename: '(' <perl_variable> ')' | ||||||||||||
| 167 | { $return = $item[2]; 1; } | ||||||||||||
| 168 | |||||||||||||
| 169 | OptionalityModifier: /[!?]/ | ||||||||||||
| 170 | |||||||||||||
| 171 | SlurpynessModifier: /\*/ | ||||||||||||
| 172 | |||||||||||||
| 173 | Unpacking: Sig | ||||||||||||
| 174 | |||||||||||||
| 175 | Constraint: 'where' <perl_codeblock> | ||||||||||||
| 176 | |||||||||||||
| 177 | # default values are _unevaluated_. | ||||||||||||
| 178 | DefaultValueSpec: '=' ValueOrSomeStabAtOne | ||||||||||||
| 179 | |||||||||||||
| 180 | ValueOrSomeStabAtOne: Value_numberLiteral | ||||||||||||
| 181 | | Value_acceptableQuotelike | ||||||||||||
| 182 | | Value_variable | ||||||||||||
| 183 | | Value_looksBalanced | ||||||||||||
| 184 | | Value_looksClosure | ||||||||||||
| 185 | |||||||||||||
| 186 | # perlfaq4 ftw | ||||||||||||
| 187 | Value_numberLiteral: /([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?/ # float | ||||||||||||
| 188 | | /-?(?:\d+(?:\.\d*)?|\.\d+)/ # decimal | ||||||||||||
| 189 | | /-?\d+\.?\d*/ # real | ||||||||||||
| 190 | | /[+-]?\d+/ # +/- integer | ||||||||||||
| 191 | | /-?\d+/ # integer | ||||||||||||
| 192 | | /\d+/ # whole number | ||||||||||||
| 193 | | /0x[0-9a-fA-F]+/ # hexadecimal | ||||||||||||
| 194 | | /0b[01]+/ # binary | ||||||||||||
| 195 | # note that octals will be captured by the "whole number" | ||||||||||||
| 196 | # production. Our consumer will have to eval this (we don't | ||||||||||||
| 197 | # want to do it for them because of roundtripping. But maybe | ||||||||||||
| 198 | # we need annotation nodes anyway? | ||||||||||||
| 199 | |||||||||||||
| 200 | Value_acceptableQuotelike: <perl_quotelike> | ||||||||||||
| 201 | { | ||||||||||||
| 202 | my $op = $item[1]->[0]; # q, qq etc. | ||||||||||||
| 203 | my %whitelist = map { $_ => 1 } qw(q qq qw qr); # TODO: lift this up | ||||||||||||
| 204 | die "rejected quotelike operator: $op" unless $whitelist{$op}; | ||||||||||||
| 205 | $return = join "", @{ $item[0] }; | ||||||||||||
| 206 | 1; | ||||||||||||
| 207 | } | ||||||||||||
| 208 | |||||||||||||
| 209 | Value_variable: <perl_variable> | ||||||||||||
| 210 | |||||||||||||
| 211 | Value_looksBalanced: { extract_bracketed($text, '()') } | ||||||||||||
| 212 | | { extract_bracketed($text, '[]') } | ||||||||||||
| 213 | | { extract_bracketed($text, '{}') } | ||||||||||||
| 214 | |||||||||||||
| 215 | Value_looksClosure: 'sub' <perl_codeblock> | ||||||||||||
| 216 | |||||||||||||
| 217 | Attrib: 'is' Label | ||||||||||||
| 218 | |||||||||||||
| 219 | Sigil: /[\$\@\%]/ | ||||||||||||
| 220 | |||||||||||||
| 221 | Label: /\w+/ | ||||||||||||
| 222 | |||||||||||||
| 223 | # This one is a bummer: we don't want to provide a full parser for | ||||||||||||
| 224 | # Perl expressions here. If we are called in the context of Devel::Declare, | ||||||||||||
| 225 | # perhaps we can get a reference back to the real parser? Otherwise, we're | ||||||||||||
| 226 | # stuck with doing some half-assed parsing that would preclude e.g. | ||||||||||||
| 227 | # :($pi = 22/7) | ||||||||||||
| 228 | Literal: /\S+/ | ||||||||||||
| 229 | #'END | ||||||||||||
| 230 | |||||||||||||
| 231 | 1 | ▶ | my $parser = Parse::RecDescent->new($SIGNATURE_GRAMMAR) || die "GRAMMAR NO WORKY *CWY* *CWY*"; | ||||||||||
|
Condition50%1/2
'Parse::RecDescent'->new($SIGNATURE_GRAMMAR) || die('GRAMMAR NO WORKY *CWY* *CWY*')
MC/DC0%0/1
'Parse::RecDescent'->new($SIGNATURE_GRAMMAR) || die('GRAMMAR NO WORKY *CWY* *CWY*')
'Parse::RecDescent'->new($SIGNATURE_GRAMMAR)
Truth table50%1/2
A
A= 'Parse::RecDescent'->new($SIGNATURE_GRAMMAR)
| |||||||||||||
| 232 | |||||||||||||
| 233 | 47 | ▶ | sub parse { | ||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 234 | 47 | my($self, $sig_str) = @_; | |||||||||||
| 235 | |||||||||||||
| 236 | 47 | my $res = $parser->Sig($sig_str); | |||||||||||
| 237 | |||||||||||||
| 238 | 45 | ▶ | die "Unparsable signature" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 239 | unless $res; | ||||||||||||
| 240 | |||||||||||||
| 241 | 43 | return $res; | |||||||||||
| 242 | } | ||||||||||||
| 243 | |||||||||||||
| 244 | # These are my favorite debugging tools. Share and enjoy. | ||||||||||||
| 245 | #sub ::Y { require YAML::Syck; YAML::Syck::Dump(@_) } | ||||||||||||
| 246 | #sub ::YY { require Carp; Carp::confess(::Y(@_)) } | ||||||||||||
| 247 | |||||||||||||
| 248 | 1 | 6; | |||||||||||
| 249 | |||||||||||||
| 250 | __END__ | ||||||||||||