| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2017 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Protocol::MTProto::TLSchemaParser; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use base qw( Parser::MGC ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | Parser::MGC->VERSION( '0.16' ); # ->committed_scope_of | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Struct::Dumb qw( readonly_struct ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # C-like block and C++-like line comments | ||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use constant pattern_comment => qr{ //.*\n | /\*(?s:.*)\*/ }x; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Regular identifiers may not begin with "_" | ||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use constant pattern_ident => qr/[[:alpha:]][[:alnum:]_]*/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | C<Protocol::MTProto::TLSchemaParser> - a parser for TL schema declarations | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | my $parser = Protocol::MTProto::TLSchemaParser->new; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | my $declarations = $parser->from_file( "telegram-l57.tl" ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # $declarations is now a reference to an array of Declarations | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | This subclass of L<Parser::MGC> recognises the TL schema grammar. Or, at | ||||||||||||||||||||||||||||
| 38 | least, at this early version it recognises a subset of the grammar sufficient | ||||||||||||||||||||||||||||
| 39 | to pass its own unit tests and to parse the F<Telegram> TL schema files. It | ||||||||||||||||||||||||||||
| 40 | does not yet recognise the full TL grammar. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Details of this module should be considered very subject to change as the | ||||||||||||||||||||||||||||
| 43 | implementation progresses. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 RETURNED TYPES | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # See also | ||||||||||||||||||||||||||||
| 50 | # https://core.telegram.org/mtproto/TL | ||||||||||||||||||||||||||||
| 51 | # https://core.telegram.org/mtproto/TL-combinators | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub parse | ||||||||||||||||||||||||||||
| 54 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | local $self->{declaration_kind} = "constructor"; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # A schema is simply a sequence of declarations | ||||||||||||||||||||||||||||
| 60 | 7 | return $self->sequence_of( 'parse_decl' ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head2 Declaration | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | The toplevel parse result is a reference to an array of C<Declaration> | ||||||||||||||||||||||||||||
| 66 | instances. Each instance relates to a single declaration from the schema file. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Responds to the following accessors: | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head3 ident | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | A string containing the full identifier name, including the namespace prefix. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head3 number | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | A 32bit number giving the parsed identification hash from the schema file, or | ||||||||||||||||||||||||||||
| 77 | C<undef> if one was not given in the file. Note that this implementation does | ||||||||||||||||||||||||||||
| 78 | not generate numbers by CRC32 hashes at present. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head3 optargs | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Either C<undef>, or a reference to a non-empty array of C<Argument> instances | ||||||||||||||||||||||||||||
| 83 | relating to the optional arguments of the declaration. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head3 args | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Either C<undef>, or a reference to a non-empty array of C<Argument> instances | ||||||||||||||||||||||||||||
| 88 | relating to the required arguments of the declaration. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head3 result_type | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | A L</Type> instance giving this constructor's result type. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head3 kind | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Either the string C<constructor> if the declaration was parsed before | ||||||||||||||||||||||||||||
| 97 | encountering the C<---functions---> marker, or the string C<function> if | ||||||||||||||||||||||||||||
| 98 | parsed after it. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | readonly_struct Declaration => [qw( | |||||||||||||||||||||||||||
| 103 | ident number optargs args result_type | ||||||||||||||||||||||||||||
| 104 | kind | ||||||||||||||||||||||||||||
| 105 | )]; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub parse_decl | ||||||||||||||||||||||||||||
| 108 | 9 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_decl: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # As a special case, we accept the literal | ||||||||||||||||||||||||||||
| 112 | # "---functions---" | ||||||||||||||||||||||||||||
| 113 | # marker | ||||||||||||||||||||||||||||
| 114 | 9 | ▶ | if( $self->expect( qr/(?:---functions---)?/ ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 1 | $self->{declaration_kind} = "function"; | |||||||||||||||||||||||||||
| 116 | 1 | return (); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # A declaration is | ||||||||||||||||||||||||||||
| 120 | # NS-IDENT [ "#" NUMBER ] [ OPT-ARGS...] [ ARGS... ] "=" RESULT-TYPE | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 8 | my $ident = $self->parse_namespaced_ident; | |||||||||||||||||||||||||||
| 123 | 8 | $self->commit; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 8 | ▶ | my $number = $self->maybe( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 126 | 8 | $self->expect( "#" ); | |||||||||||||||||||||||||||
| 127 | 4 | $self->commit; | |||||||||||||||||||||||||||
| 128 | 4 | hex $self->expect( qr/[0-9a-f]{1,8}/i ); | |||||||||||||||||||||||||||
| 129 | 8 | }); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 8 | my $optargs = $self->sequence_of( 'parse_optarg' ); | |||||||||||||||||||||||||||
| 132 | 8 | ▶ | if( $optargs and @$optargs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$optargs and @$optargs
MC/DC50%1/2
$optargs and @$optargs
$optargs
@$optargs
Truth table66%2/3
A and B
A= $optargsB= @$optargs
| |||||||||||||||||||||||||||||
| 133 | # Flatten the lists | ||||||||||||||||||||||||||||
| 134 | 2 | $optargs = [ map { @$_ } @$optargs ]; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||
| 137 | 6 | undef $optargs; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 8 | my $args = $self->sequence_of( 'parse_arg' ); | |||||||||||||||||||||||||||
| 141 | 8 | ▶ | @$args or undef $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 8 | $self->expect( "=" ); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 8 | my $result_type = $self->parse_result_type; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 8 | $self->expect( ";" ); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | return Declaration( | ||||||||||||||||||||||||||||
| 150 | $ident, $number, $optargs, $args, $result_type, | ||||||||||||||||||||||||||||
| 151 | $self->{declaration_kind}, | ||||||||||||||||||||||||||||
| 152 | 8 | ); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head2 Argument | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Each element of the C<args> and C<optargs> arrays for a L</Declaration> will | ||||||||||||||||||||||||||||
| 158 | be an C<Argument> instance. Each relates to a single positional argument of | ||||||||||||||||||||||||||||
| 159 | the declaration. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Responds to the following accessors: | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head3 name | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Either C<undef> or a string giving the name of the argument. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head3 type | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | A L</Type> instance giving this argument's type. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head3 conditional_on | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Either C<undef>, or a string giving the name of an earlier argument that this | ||||||||||||||||||||||||||||
| 174 | argument is conditional on. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head3 condition_mask | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Either C<undef>, or a number giving the bitmask to apply to the earlier that | ||||||||||||||||||||||||||||
| 179 | argument this argument is conditional on. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head3 has_pling | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | True if the type of this argument was declared using the C<!> modifier. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 4 | readonly_struct Argument => [qw( | |||||||||||||||||||||||||||
| 188 | name type conditional_on condition_mask has_pling | ||||||||||||||||||||||||||||
| 189 | )]; | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 4 | readonly_struct Repetition => [qw( | |||||||||||||||||||||||||||
| 192 | multiplicity args | ||||||||||||||||||||||||||||
| 193 | )]; | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | sub parse_arg | ||||||||||||||||||||||||||||
| 196 | 21 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_arg: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 197 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # ARG is any of | ||||||||||||||||||||||||||||
| 200 | # VAR-IDENT-OPT ":" [ CONDITIONAL-DEF ] [ "!" ] TYPE-TERM | ||||||||||||||||||||||||||||
| 201 | # [ VAR-IDENT-OPT ":" ] [ MULTIPLICITY "*" ] "[" [ ARG... ] "]" | ||||||||||||||||||||||||||||
| 202 | # "(" [ VAR-IDENT-OPT... ]+ : [ "!" ] TYPE-TERM ")" -- TODO | ||||||||||||||||||||||||||||
| 203 | # [ "!" ] TYPE-TERM | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | $self->any_of( | ||||||||||||||||||||||||||||
| 206 | 21 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 207 | 21 | my $name = $self->token_var_ident( optional => 1 ); | |||||||||||||||||||||||||||
| 208 | 11 | $self->expect( ":" ); | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 10 | my $conditional_arg; | |||||||||||||||||||||||||||
| 211 | my $condition_mask; | ||||||||||||||||||||||||||||
| 212 | 10 | ▶ | $self->maybe( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 213 | 10 | $conditional_arg = $self->token_var_ident; | |||||||||||||||||||||||||||
| 214 | 10 | $self->expect( "." ); | |||||||||||||||||||||||||||
| 215 | 3 | $condition_mask = 1 << $self->token_int; | |||||||||||||||||||||||||||
| 216 | 3 | $self->expect( "?" ); | |||||||||||||||||||||||||||
| 217 | 10 | }); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 10 | my $has_pling = $self->expect( qr/!?/ ); | |||||||||||||||||||||||||||
| 220 | 10 | my $type = $self->parse_type_term; | |||||||||||||||||||||||||||
| 221 | 10 | return Argument( $name, $type, $conditional_arg, $condition_mask, $has_pling ); | |||||||||||||||||||||||||||
| 222 | }, | ||||||||||||||||||||||||||||
| 223 | 11 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 224 | # TODO -- var-ident-opt, ":" | ||||||||||||||||||||||||||||
| 225 | # TODO -- multiplicity | ||||||||||||||||||||||||||||
| 226 | my $args = $self->committed_scope_of( | ||||||||||||||||||||||||||||
| 227 | "[", | ||||||||||||||||||||||||||||
| 228 | 1 | ▶ | sub { $self->sequence_of( 'parse_arg' ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 229 | 11 | "]", | |||||||||||||||||||||||||||
| 230 | ); | ||||||||||||||||||||||||||||
| 231 | # TODO: name, multiplicity | ||||||||||||||||||||||||||||
| 232 | 1 | return Repetition( "#", $args ); | |||||||||||||||||||||||||||
| 233 | }, | ||||||||||||||||||||||||||||
| 234 | 10 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 235 | 10 | my $has_pling = $self->expect( qr/!?/ ); | |||||||||||||||||||||||||||
| 236 | 10 | return Argument( undef, $self->parse_type_term, undef, undef, $has_pling ); | |||||||||||||||||||||||||||
| 237 | }, | ||||||||||||||||||||||||||||
| 238 | 21 | ); | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | sub parse_optarg | ||||||||||||||||||||||||||||
| 242 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_optarg: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 243 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # OPT-ARGS is | ||||||||||||||||||||||||||||
| 246 | # "{" [ IDENTS... ]+ ":" [ "!" ] TYPE-EXPR "}" | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | $self->committed_scope_of( | ||||||||||||||||||||||||||||
| 249 | "{", | ||||||||||||||||||||||||||||
| 250 | 2 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 251 | 2 | my $names = $self->sequence_of( 'token_var_ident' ); | |||||||||||||||||||||||||||
| 252 | 2 | ▶ | @$names or $self->fail( "Expected at least one identifier name" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 2 | $self->expect( ":" ); | |||||||||||||||||||||||||||
| 255 | 2 | my $has_pling = $self->expect( qr/!?/ ); | |||||||||||||||||||||||||||
| 256 | 2 | my $type = $self->parse_type_expr; | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | # Expand the list of names into a list of individual argument instances | ||||||||||||||||||||||||||||
| 259 | 2 | return [ map { Argument( $_, $type, undef, undef, $has_pling ) } @$names ]; | |||||||||||||||||||||||||||
| 260 | }, | ||||||||||||||||||||||||||||
| 261 | 10 | "}" | |||||||||||||||||||||||||||
| 262 | ); | ||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 4 | *parse_type_expr = \&parse_expr; | |||||||||||||||||||||||||||
| 266 | sub parse_expr | ||||||||||||||||||||||||||||
| 267 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_expr: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 268 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | # EXPR is a sequence of SUBEXPR | ||||||||||||||||||||||||||||
| 271 | # TODO: multiple of them imply being combined together in some | ||||||||||||||||||||||||||||
| 272 | # polymorphic type but I don't quite understand how | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 3 | my $exprs = $self->sequence_of( 'parse_subexpr' ); | |||||||||||||||||||||||||||
| 275 | 3 | ▶ | return $exprs->[0] if @$exprs == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 0 | $self->fail( "TODO: combine subexprs" ); | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | sub parse_subexpr | ||||||||||||||||||||||||||||
| 281 | 13 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_subexpr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 282 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 283 | # SUBEXPR is any of | ||||||||||||||||||||||||||||
| 284 | # TERM | ||||||||||||||||||||||||||||
| 285 | # NAT-CONST "+" SUBEXPR -- TODO | ||||||||||||||||||||||||||||
| 286 | # SUBEXPR "+" NAT-CONST -- TODO | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 13 | return $self->parse_term; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 4 | *parse_type_term = \&parse_term; # TODO - check that the result definitely is a type | |||||||||||||||||||||||||||
| 292 | sub parse_term | ||||||||||||||||||||||||||||
| 293 | 33 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_term: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 294 | 33 | my $self = shift; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # TERM is any of | ||||||||||||||||||||||||||||
| 297 | # "(" EXPR ")" -- TODO | ||||||||||||||||||||||||||||
| 298 | # TYPE-IDENT [ "<" [ EXPR "," EXPR... ]+ ">" ] | ||||||||||||||||||||||||||||
| 299 | # VAR-IDENT -- TODO | ||||||||||||||||||||||||||||
| 300 | # NAT-CONST -- TODO | ||||||||||||||||||||||||||||
| 301 | # "%" TERM -- TODO | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 33 | $self->parse_type_ident( allow_polymorphic => 1 ); | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =head2 Type | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | The C<result_type> of a L</Declaration>, and the C<type> of an L</Argument> | ||||||||||||||||||||||||||||
| 309 | will be a C<Type> instance. At present, no attempt is made to intern the | ||||||||||||||||||||||||||||
| 310 | instances; comparison for equallity should be performed on the string name and | ||||||||||||||||||||||||||||
| 311 | its subtypes, not object identity. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | Responds to the following accessors: | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =head3 name | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | A string giving the name of the type. | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =head3 is_boxed | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | True if the type is boxed; that is, its name begins with a capital letter. | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =head3 is_polymorphic | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | True if the type is polymorphic. | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =head3 subtypes (optional) | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | If the type is polymorphic, a reference to an array of other L</Type> | ||||||||||||||||||||||||||||
| 330 | instances corresponding to the subtypes. If the type is not polymorphic this | ||||||||||||||||||||||||||||
| 331 | accessor will not exist. | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =cut | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 4 | readonly_struct BaseType => [qw( | |||||||||||||||||||||||||||
| 336 | name is_boxed is_polymorphic | ||||||||||||||||||||||||||||
| 337 | )]; | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 4 | readonly_struct PolymorphicType => [qw( | |||||||||||||||||||||||||||
| 340 | name is_boxed is_polymorphic subtypes | ||||||||||||||||||||||||||||
| 341 | )]; | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | sub parse_type_ident | ||||||||||||||||||||||||||||
| 344 | 41 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_type_ident: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 345 | 41 | my $self = shift; | |||||||||||||||||||||||||||
| 346 | 41 | my %args = @_; | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # TYPE-IDENT is any of | ||||||||||||||||||||||||||||
| 349 | # BOXED-TYPE-IDENT | ||||||||||||||||||||||||||||
| 350 | # LC-IDENT-NS | ||||||||||||||||||||||||||||
| 351 | # "#" | ||||||||||||||||||||||||||||
| 352 | # | ||||||||||||||||||||||||||||
| 353 | # The first two of which are covered by parse_namespaced_ident | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | my $base_type = $self->any_of( | ||||||||||||||||||||||||||||
| 356 | 41 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 357 | 41 | my $name = $self->parse_namespaced_ident; | |||||||||||||||||||||||||||
| 358 | 23 | return BaseType( $name, scalar $name =~ m/(?:\.|^)[A-Z]/, 0 ); | |||||||||||||||||||||||||||
| 359 | }, | ||||||||||||||||||||||||||||
| 360 | 18 | ▶ | sub { $self->expect( "#" ); return BaseType( "#", 0, 0 ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 361 | 41 | ); | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 25 | ▶ | return $base_type unless $args{allow_polymorphic}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 25 | ▶ | my $subtypes = $self->maybe( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 366 | $self->committed_scope_of( | ||||||||||||||||||||||||||||
| 367 | "<", | ||||||||||||||||||||||||||||
| 368 | 1 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 369 | 1 | $self->list_of( ",", 'parse_expr' ); | |||||||||||||||||||||||||||
| 370 | # TODO - check nonempty, types | ||||||||||||||||||||||||||||
| 371 | }, | ||||||||||||||||||||||||||||
| 372 | 25 | ">", | |||||||||||||||||||||||||||
| 373 | ); | ||||||||||||||||||||||||||||
| 374 | 25 | }); | |||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 25 | ▶ | return $base_type if !$subtypes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 1 | return PolymorphicType( | |||||||||||||||||||||||||||
| 379 | $base_type->name, $base_type->is_boxed, 1, $subtypes | ||||||||||||||||||||||||||||
| 380 | ); | ||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | sub parse_result_type | ||||||||||||||||||||||||||||
| 384 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_result_type: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 385 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | # RESULT-TYPE is any of | ||||||||||||||||||||||||||||
| 388 | # BOXED-TYPE-IDENT [ SUBEXPR ... ] | ||||||||||||||||||||||||||||
| 389 | # BOXED-TYPE-IDENT "<" [ SUBEXPR "," SUBEXPR... ]+ ">" | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 8 | my $type = $self->parse_type_ident( allow_polymorphic => 1 ); | |||||||||||||||||||||||||||
| 392 | 8 | ▶ | $type->is_boxed or $self->fail( "Result type must be Boxed" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | 8 | ▶ | return $type if $type->is_polymorphic; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 8 | my $subtypes = $self->sequence_of( 'parse_subexpr' ); | |||||||||||||||||||||||||||
| 397 | # TODO - check these are types | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 8 | ▶ | if( $subtypes and @$subtypes ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$subtypes and @$subtypes
MC/DC50%1/2
$subtypes and @$subtypes
$subtypes
@$subtypes
Truth table66%2/3
A and B
A= $subtypesB= @$subtypes
| |||||||||||||||||||||||||||||
| 400 | 2 | return PolymorphicType( | |||||||||||||||||||||||||||
| 401 | $type->name, $type->is_boxed, 1, $subtypes | ||||||||||||||||||||||||||||
| 402 | ); | ||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 6 | return $type; | |||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | sub parse_namespaced_ident | ||||||||||||||||||||||||||||
| 409 | 49 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_namespaced_ident: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 410 | 49 | my $self = shift; | |||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 49 | ▶ | my $namespace = $self->maybe( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 413 | 49 | my $ns = $self->token_ident; | |||||||||||||||||||||||||||
| 414 | 31 | $self->expect( "." ); | |||||||||||||||||||||||||||
| 415 | 0 | $ns; | |||||||||||||||||||||||||||
| 416 | 49 | }); | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 49 | my $ident = $self->token_ident; | |||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 31 | ▶ | return "$namespace.$ident" if defined $namespace; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | 31 | return "$ident"; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | sub token_var_ident | ||||||||||||||||||||||||||||
| 425 | 35 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
token_var_ident: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 426 | 35 | my $self = shift; | |||||||||||||||||||||||||||
| 427 | 35 | my %args = @_; | |||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 35 | my $ident = $self->token_ident; | |||||||||||||||||||||||||||
| 430 | 23 | ▶ | $self->fail( "Require a named identifier" ) if $ident eq "_" and !$args{optional}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ident eq "_" and not $args{"optional"}
MC/DC0%0/2
$ident eq "_" and not $args{"optional"}
$ident eq "_"
not $args{"optional"}
Truth table33%1/3
A and not(B)
A= $ident eq "_"B= not $args{"optional"}
| |||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 23 | return $ident; | |||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | =cut | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | 4 | 0x55AA; | |||||||||||||||||||||||||||