Tangence/Compiler/Parser.pm

filter: Statementstmt 98.1% 160 / 163 Branchbran 78.1% 25 / 32 Conditioncond 42.8% 9 / 21 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 97.0% 33 / 34 Podpod 53.3% 8 / 15 totaltotal 84.9% 237 / 279 CC 24 SCAR 16.1
CC
24
Cov
90%
parse_classblock
12.4
parse
9.5
parse_structblock
4.0
CRAP 5.0
« Tangence/Meta/Type.pm ↑ Summary Tangence/Property.pm »
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, 2011-2024 -- leonerd@leonerd.org.uk
5  
6 13 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Tangence::Compiler::Parser 0.33;
11 13 ▶class Tangence::Compiler::Parser :isa(Parser::MGC);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 13 ▶use Syntax::Keyword::Dynamically;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 13 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 ▶use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Parsing is simpler if we treat Package.Name as a simple identifier
21 13 ▶use constant pattern_ident => qr/[[:alnum:]_][\w.]*/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 13 ▶use constant pattern_comment => qr/#.*\n/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 =head1 NAME
26  
27 C<Tangence::Compiler::Parser> - parse C<Tangence> interface definition files
28  
29 =head1 DESCRIPTION
30  
31 This subclass of L<Parser::MGC> parses a L<Tangence> interface definition and
32 returns a metadata tree.
33  
34 =cut
35  
36 =head1 GRAMMAR
37  
38 The top level of an interface definition file contains C<include> directives
39 and C<class> and C<struct> definitions.
40  
41 =head2 include
42  
43 An C<include> directive imports the definitions from another file, named
44 relative to the current file.
45  
46 include "filename.tan"
47  
48 =head2 class
49  
50 A C<class> definition defines the set of methods, events and properties
51 defined by a named class.
52  
53 class N {
54 ...
55 }
56  
57 The contents of the class block will be a list of C<method>, C<event>, C<prop>
58 and C<isa> declarations.
59  
60 =head2 struct
61  
62 A C<struct> definition defines the list of fields contained within a named
63 structure type.
64  
65 struct N {
66 ...
67 }
68  
69 The contents of the struct block will be a list of C<field> declarations.
70  
71 =cut
72  
73 13 field $_package;
74  
75 # Parser::MGC version 0.20 adds this method. Before then, this workaround is
76 # known to be safe
77 13 ▶if( $Parser::MGC::VERSION < 0.20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parser::MGC::VERSION < "0.2") 0 13 13
78 0 ▶ *filename = sub ( $self ) { $self->{filename} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 }
80  
81 method parse
82 15 ▶{
Subroutine parse: called CC=9 SCAR=22.5
Pod undocumented
83 15 dynamically $_package = \my %package;
84  
85 15 ▶ while( !$self->at_eos ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->at_eos) 28 15 43
86 match( $self->token_kw(qw( class struct include )) : eq ) {
87 case( 'class' ) {
88 15 my $classname = $self->token_ident;
89  
90 15 ▶ exists $package{$classname} and
Condition33%1/3
exists $package{$classname} and $self->fail("Already have a class or struct called $classname")
!ll&&!rl&&r
1500
MC/DC0%0/2
exists $package{$classname} and $self->fail("Already have a class or struct called $classname")
exists $package{$classname} $self->fail("Already have a class or struct called $classname")
Truth table33%1/3
A and B
A= exists $package{$classname}B= $self->fail("Already have a class or struct called $classname")
ABresult
0X0
100
111
91 $self->fail( "Already have a class or struct called $classname" );
92  
93 15 my $class = $self->make_class( name => $classname );
94 15 $package{$classname} = $class;
95  
96 15 ▶ $self->scope_of( '{', sub { $self->parse_classblock( $class ) }, '}' ),
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'struct' ? : 11 2 13
97 }
98 case( 'struct' ) {
99 11 my $structname = $self->token_ident;
100  
101 11 ▶ exists $package{$structname} and
Condition33%1/3
exists $package{$structname} and $self->fail("Already have a class or struct called $structname")
!ll&&!rl&&r
1100
MC/DC0%0/2
exists $package{$structname} and $self->fail("Already have a class or struct called $structname")
exists $package{$structname} $self->fail("Already have a class or struct called $structname")
Truth table33%1/3
A and B
A= exists $package{$structname}B= $self->fail("Already have a class or struct called $structname")
ABresult
0X0
100
111
102 $self->fail( "Already have a class or struct called $structname" );
103  
104 11 my $struct = $self->make_struct( name => $structname );
105 11 $package{$structname} = $struct;
106  
107 11 ▶ $self->scope_of( '{', sub { $self->parse_structblock( $struct ) }, '}' ),
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'include' ? : 2 0 2
108 }
109 case( 'include' ) {
110 2 my $filename = dirname($self->filename) . "/" . $self->token_string;
111  
112 2 my $subparser = (ref $self)->new;
113 2 my $included = $subparser->from_file( $filename );
114  
115 2 foreach my $classname ( keys %$included ) {
116 2 ▶ exists $package{$classname} and
Branch: 1 branch
BranchTrueFalseTotal
exists $package{$classname} and $self->fail("Cannot include '${filename}' as class $classname collides") 0 2 2
117 $self->fail( "Cannot include '$filename' as class $classname collides" );
118  
119 2 $package{$classname} = $included->{$classname};
120 }
121 }
122 28 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'class' ? : 15 13 28
123 0 $self->fail( "Expected keyword, found $_" );
124 }
125 }
126 }
127  
128 15 return \%package;
129 13 }
130  
131 =head2 method
132  
133 A C<method> declaration defines one method in the class, giving its name (N)
134 and types of its arguments and and return (T).
135  
136 method N(T, T, ...) -> T;
137  
138 =head2 event
139  
140 An C<event> declaration defines one event raised by the class, giving its name
141 (N) and types of its arguments (T).
142  
143 event N(T, T, ...);
144  
145 =head2 prop
146  
147 A C<prop> declaration defines one property supported by the class, giving its
148 name (N), dimension (D) and type (T). It may be declared as a C<smashed>
149 property.
150  
151 [smashed] prop N = D of T;
152  
153 Scalar properties may omit the C<scalar of>, by supplying just the type
154  
155 [smashed] prop N = T;
156  
157 =head2 isa
158  
159 An C<isa> declaration declares a superclass of the class, by its name (C)
160  
161 isa C;
162  
163 =cut
164  
165 15 ▶method parse_classblock ( $class )
Subroutine parse_classblock: called CC=12 SCAR=25.1
Pod undocumented
166 {
167 15 my %methods;
168 my %events;
169 15 my %properties;
170 15 my @superclasses;
171  
172 15 ▶ while( !$self->at_eos ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->at_eos) 131 15 146
173 match( $_ = $self->token_kw(qw( method event prop smashed isa )) : eq ) {
174 case( 'method' ) {
175 24 my $methodname = $self->token_ident;
176  
177 24 ▶ exists $methods{$methodname} and
Condition33%1/3
exists $methods{$methodname} and $self->fail("Already have a method called $methodname")
!ll&&!rl&&r
2400
MC/DC0%0/2
exists $methods{$methodname} and $self->fail("Already have a method called $methodname")
exists $methods{$methodname} $self->fail("Already have a method called $methodname")
Truth table33%1/3
A and B
A= exists $methods{$methodname}B= $self->fail("Already have a method called $methodname")
ABresult
0X0
100
111
178 $self->fail( "Already have a method called $methodname" );
179  
180 24 my $args = $self->parse_arglist;
181 24 my $ret;
182  
183 24 ▶ $self->maybe( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
184 24 $self->expect( '->' );
185  
186 13 $ret = $self->parse_type;
187 24 } );
188  
189 24 ▶ $methods{$methodname} = $self->make_method(
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'event' ? : 13 94 107
190 class => $class,
191 name => $methodname,
192 arguments => $args,
193 ret => $ret,
194 );
195 }
196  
197 case( 'event' ) {
198 13 my $eventname = $self->token_ident;
199  
200 13 ▶ exists $events{$eventname} and
Condition33%1/3
exists $events{$eventname} and $self->fail("Already have an event called $eventname")
!ll&&!rl&&r
1300
MC/DC0%0/2
exists $events{$eventname} and $self->fail("Already have an event called $eventname")
exists $events{$eventname} $self->fail("Already have an event called $eventname")
Truth table33%1/3
A and B
A= exists $events{$eventname}B= $self->fail("Already have an event called $eventname")
ABresult
0X0
100
111
201 $self->fail( "Already have an event called $eventname" );
202  
203 13 my $args = $self->parse_arglist;
204  
205 13 ▶ $events{$eventname} = $self->make_event(
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'smashed' || $(Syntax::Keyword::Match/topic) eq 'prop' ? : 92 2 94
Condition100%3/3
$(Syntax::Keyword::Match/topic) eq "smashed" or $(Syntax::Keyword::Match/topic) eq "prop"
l!l&&r!l&&!r
24682
MC/DC100%2/2
$(Syntax::Keyword::Match/topic) eq "smashed" or $(Syntax::Keyword::Match/topic) eq "prop"
$(Syntax::Keyword::Match/topic) eq "smashed" $(Syntax::Keyword::Match/topic) eq "prop"
Truth table100%3/3
A or B
A= $(Syntax::Keyword::Match/topic) eq "smashed"B= $(Syntax::Keyword::Match/topic) eq "prop"
ABresult
1X1
011
000
206 class => $class,
207 name => $eventname,
208 arguments => $args,
209 );
210 }
211  
212 case( 'smashed' ), case( 'prop' ) {
213 92 my $smashed = 0;
214  
215 92 ▶ if( $_ eq 'smashed' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "smashed") 24 68 92
216 24 $smashed = 1;
217 24 $self->expect( 'prop' );
218 }
219  
220 92 my $propname = $self->token_ident;
221  
222 92 ▶ exists $properties{$propname} and
Condition33%1/3
exists $properties{$propname} and $self->fail("Already have a property called $propname")
!ll&&!rl&&r
9200
MC/DC0%0/2
exists $properties{$propname} and $self->fail("Already have a property called $propname")
exists $properties{$propname} $self->fail("Already have a property called $propname")
Truth table33%1/3
A and B
A= exists $properties{$propname}B= $self->fail("Already have a property called $propname")
ABresult
0X0
100
111
223 $self->fail( "Already have a property called $propname" );
224  
225 92 $self->expect( '=' );
226  
227 92 my $dim = DIM_SCALAR;
228 92 ▶ $self->maybe( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
229 92 $dim = $self->parse_dim;
230 55 $self->expect( 'of' );
231 92 } );
232  
233 92 my $type = $self->parse_type;
234  
235 92 ▶ $properties{$propname} = $self->make_property(
Condition33%1/3
$(Syntax::Keyword::Match/topic) eq "isa" and do { my $supername = $self->token_ident; my $super = $_package->{$supername} or $self->fail("Unrecognised superclass $supername"); push @superclasses, $super }
!ll&&!rl&&r
020
MC/DC0%0/2
$(Syntax::Keyword::Match/topic) eq "isa" and do { my $supername = $self->token_ident; my $super = $_package->{$supername} or $self->fail("Unrecognised superclass $supername"); push @superclasses, $super }
$(Syntax::Keyword::Match/topic) eq "isa" do { my $supername = $self->token_ident; my $super = $_package->{$supername} or $self->fail("Unrecognised superclass $supername"); push @superclasses, $super }
Truth table33%1/3
A and B
A= $(Syntax::Keyword::Match/topic) eq "isa"B= do { my $supername = $self->token_ident; my $super = $_package->{$supername} or $self->fail("Unrecognised superclass $supername"); push @superclasses, $super }
ABresult
0X0
100
111
236 class => $class,
237 name => $propname,
238 smashed => $smashed,
239 dimension => $dim,
240 type => $type,
241 );
242 }
243  
244 131 ▶ case( 'isa' ) {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'method' ? : 24 107 131
245 2 my $supername = $self->token_ident;
246  
247 2 ▶ my $super = $_package->{$supername} or
Branch: 1 branch
BranchTrueFalseTotal
my $super = $_package->{$supername} or $self->fail("Unrecognised superclass $supername") 2 0 2
248 $self->fail( "Unrecognised superclass $supername" );
249  
250 2 push @superclasses, $super;
251 }
252 }
253  
254 131 $self->expect( ';' );
255 }
256  
257 $class->define(
258 15 methods => \%methods,
259 events => \%events,
260 properties => \%properties,
261 superclasses => \@superclasses,
262 );
263 13 }
264  
265 method parse_arglist
266 37 ▶{
Subroutine parse_arglist: called CC=1 SCAR=0.0
Pod undocumented
267 return $self->scope_of(
268 "(",
269 37 ▶ sub { $self->list_of( ",", \&parse_arg ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
270 37 ")",
271 );
272 13 }
273  
274 method parse_arg
275 48 ▶{
Subroutine parse_arg: called CC=1 SCAR=0.0
Pod undocumented
276 48 my $name;
277 48 my $type = $self->parse_type;
278 48 ▶ $self->maybe( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
279 48 $name = $self->token_ident;
280 48 } );
281 48 return $self->make_argument( name => $name, type => $type );
282 13 }
283  
284 11 ▶method parse_structblock ( $struct )
Subroutine parse_structblock: called CC=4 SCAR=13.9
Pod undocumented
285 {
286 11 my @fields;
287 my %fieldnames;
288  
289 11 ▶ while( !$self->at_eos ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->at_eos) 55 11 66
290 match( $self->token_kw(qw( field )) : eq ) {
291 55 ▶ case( 'field' ) {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq "field" and do { my $fieldname = $self->token_ident; exists $fieldnames{$fieldname} and $self->fail("Already have a field called $fieldname"); $self->expect("="); my $type = $self->parse_type; push @fields, $self->make_field("name", $fieldname, "type", $type); ++$fieldnames{$fieldname} } 55 0 55
292 55 my $fieldname = $self->token_ident;
293  
294 55 ▶ exists $fieldnames{$fieldname} and
Branch: 1 branch
BranchTrueFalseTotal
exists $fieldnames{$fieldname} and $self->fail("Already have a field called $fieldname") 0 55 55
295 $self->fail( "Already have a field called $fieldname" );
296  
297 55 $self->expect( '=' );
298  
299 55 my $type = $self->parse_type;
300  
301 55 push @fields, $self->make_field(
302 name => $fieldname,
303 type => $type,
304 );
305 55 $fieldnames{$fieldname}++;
306 }
307 }
308 55 $self->expect( ';' );
309 }
310  
311 $struct->define(
312 11 fields => \@fields,
313 );
314 13 }
315  
316 =head2 Types
317  
318 The following basic type names are recognised
319  
320 bool int str obj any
321 s8 s16 s32 s64 u8 u16 u32 u64
322  
323 Aggregate types may be formed of any type (T) by
324  
325 list(T) dict(T)
326  
327 =cut
328  
329 13 my @basic_types = qw(
330 bool
331 int
332 s8 s16 s32 s64 u8 u16 u32 u64
333 float
334 float16 float32 float64
335 str
336 obj
337 any
338 );
339  
340 method parse_type
341 219 ▶{
Subroutine parse_type: called CC=1 SCAR=0.0
Pod undocumented
342 $self->any_of(
343 219 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
344 219 my $aggregate = $self->token_kw(qw( list dict ));
345  
346 11 $self->commit;
347  
348 11 my $membertype = $self->scope_of( "(", \&parse_type, ")" );
349  
350 11 return $self->make_type( $aggregate => $membertype );
351 },
352 208 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
353 208 my $typename = $self->token_ident;
354  
355 208 ▶ grep { $_ eq $typename } @basic_types or
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq $typename;} @basic_types or $self->fail("'${typename}' is not a typename") 208 0 208
356 $self->fail( "'$typename' is not a typename" );
357  
358 208 return $self->make_type( $typename );
359 },
360 219 );
361 13 }
362  
363 13 my %dimensions = (
364 scalar => DIM_SCALAR,
365 hash => DIM_HASH,
366 queue => DIM_QUEUE,
367 array => DIM_ARRAY,
368 objset => DIM_OBJSET,
369 );
370  
371 method parse_dim
372 92 ▶{
Subroutine parse_dim: called CC=1 SCAR=0.0
Pod undocumented
373 92 my $dimname = $self->token_kw( keys %dimensions );
374  
375 55 return $dimensions{$dimname};
376 13 }
377  
378 =head1 SUBCLASS METHODS
379  
380 If this class is subclassed, the following methods may be overridden to
381 customise the behaviour. They allow the subclass to return different objects
382 in the syntax tree.
383  
384 =cut
385  
386 =head2 make_class
387  
388 $class = $parser->make_class( name => $name )
389  
390 Return a new instance of L<Tangence::Meta::Class> to go in a package. The
391 parser will call C<define> on it.
392  
393 =cut
394  
395 method make_class
396 3 ▶{
Subroutine make_class: called CC=1 SCAR=0.0
Pod documented
397 3 require Tangence::Meta::Class;
398 3 return Tangence::Meta::Class->new( @_ );
399 13 }
400  
401 =head2 make_struct
402  
403 $struct = $parser->make_struct( name => $name )
404  
405 Return a new instance of L<Tangence::Meta::Struct> to go in a package. The
406 parser will call C<define> on it.
407  
408 =cut
409  
410 method make_struct
411 1 ▶{
Subroutine make_struct: called CC=1 SCAR=0.0
Pod documented
412 1 require Tangence::Meta::Struct;
413 1 return Tangence::Meta::Struct->new( @_ );
414 13 }
415  
416 =head2 make_method
417  
418 $method = $parser->make_method( %args )
419  
420 =head2 make_event
421  
422 $event = $parser->make_event( %args )
423  
424 =head2 make_property
425  
426 $property = $parser->make_property( %args )
427  
428 Return a new instance of L<Tangence::Meta::Method>, L<Tangence::Meta::Event>
429 or L<Tangence::Meta::Property> to go in a class.
430  
431 =cut
432  
433 method make_method
434 24 ▶{
Subroutine make_method: called CC=1 SCAR=0.0
Pod documented
435 24 require Tangence::Meta::Method;
436 24 return Tangence::Meta::Method->new( @_ );
437 13 }
438  
439 method make_event
440 13 ▶{
Subroutine make_event: called CC=1 SCAR=0.0
Pod documented
441 13 require Tangence::Meta::Event;
442 13 return Tangence::Meta::Event->new( @_ );
443 13 }
444  
445 method make_property
446 10 ▶{
Subroutine make_property: called CC=1 SCAR=0.0
Pod documented
447 10 require Tangence::Meta::Property;
448 10 return Tangence::Meta::Property->new( @_ );
449 13 }
450  
451 =head2 make_argument
452  
453 $argument = $parser->make_argument( %args )
454  
455 Return a new instance of L<Tangence::Meta::Argument> to use for a method
456 or event argument.
457  
458 =cut
459  
460 method make_argument
461 48 ▶{
Subroutine make_argument: called CC=1 SCAR=0.0
Pod documented
462 48 require Tangence::Meta::Argument;
463 48 return Tangence::Meta::Argument->new( @_ );
464 13 }
465  
466 =head2 make_field
467  
468 $field = $parser->make_field( %args )
469  
470 Return a new instance of L<Tangence::Meta::Field> to use for a structure type.
471  
472 =cut
473  
474 method make_field
475 55 ▶{
Subroutine make_field: called CC=1 SCAR=0.0
Pod documented
476 55 require Tangence::Meta::Field;
477 55 return Tangence::Meta::Field->new( @_ );
478 13 }
479  
480 =head2 make_type
481  
482 $type = $parser->make_type( $primitive_name )
483  
484 $type = $parser->make_type( $aggregate_name => $member_type )
485  
486 Return an instance of L<Tangence::Meta::Type> representing the given
487 primitive or aggregate type name. An implementation is allowed to use
488 singleton objects and return identical objects for the same primitive name or
489 aggregate and member type.
490  
491 =cut
492  
493 method make_type
494 24 ▶{
Subroutine make_type: called CC=1 SCAR=0.0
Pod documented
495 24 require Tangence::Meta::Type;
496 24 return Tangence::Meta::Type->make( @_ );
497 13 }
498  
499 =head1 AUTHOR
500  
501 Paul Evans <leonerd@leonerd.org.uk>
502  
503 =cut
504  
505 13 0x55AA;
« Tangence/Meta/Type.pm ↑ Summary Tangence/Property.pm »