| 1 | package Zuzu::Parser; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 78 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 78 | our $VERSION = '0.007002'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 78 | ▶ | use Zuzu::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 78 | ▶ | use Zuzu::Lexer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 78 | ▶ | use Zuzu::Parser::_Impl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 78 | ▶ | use Zuzu::AST::Visitor::TypeCheckHints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 78 | ▶ | use Zuzu::AST::Visitor::SuperHints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 78 | ▶ | use Zuzu::AST::Visitor::LexicalEnvHints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 78 | ▶ | use Zuzu::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 78 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 113 | ▶ | has 'disabled_visitors' => ( is => 'rw', default => sub { [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 78 | my @VISITOR_CLASSES = ( | |||||||||||||||||||||||||||
| 20 | TypeCheckHints => 'Zuzu::AST::Visitor::TypeCheckHints', | ||||||||||||||||||||||||||||
| 21 | SuperHints => 'Zuzu::AST::Visitor::SuperHints', | ||||||||||||||||||||||||||||
| 22 | LexicalEnvHints => 'Zuzu::AST::Visitor::LexicalEnvHints', | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | 78 | my %VISITOR_CLASS_FOR = @VISITOR_CLASSES; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub available_visitors { | ||||||||||||||||||||||||||
|
Subroutine
available_visitors: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 0 | return map { $VISITOR_CLASSES[ $_ * 2 ] } 0 .. ( @VISITOR_CLASSES / 2 ) - 1; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 3264 | ▶ | sub normalize_disabled_visitors { | ||||||||||||||||||||||||||
|
Subroutine
normalize_disabled_visitors: called CC=6 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 3264 | my ( $class, @names ) = @_; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3264 | my @out; | |||||||||||||||||||||||||||
| 34 | my %seen; | ||||||||||||||||||||||||||||
| 35 | 3264 | for my $name ( @names ) { | |||||||||||||||||||||||||||
| 36 | 2 | ▶ | next if !defined $name or $name eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $name or $name eq ""
MC/DC0%0/2
not defined $name or $name eq ""
not defined $name
$name eq ""
Truth table33%1/3
not(A) or B
A= not defined $nameB= $name eq ""
| |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | if ( !exists $VISITOR_CLASS_FOR{$name} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | my $expected = join ', ', $class->available_visitors; | |||||||||||||||||||||||||||
| 39 | 0 | die "Unknown visitor '$name' (expected one of: $expected)"; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 2 | ▶ | push @out, $name if !$seen{$name}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3264 | return @out; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 753 | ▶ | sub _disabled_visitor_set { | ||||||||||||||||||||||||||
|
Subroutine
_disabled_visitor_set: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 48 | 753 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | return { | ||||||||||||||||||||||||||||
| 51 | 1 | map { $_ => 1 } | |||||||||||||||||||||||||||
| 52 | Zuzu::Parser->normalize_disabled_visitors( | ||||||||||||||||||||||||||||
| 53 | 753 | ▶ | @{ $self->disabled_visitors // [] }, | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->disabled_visitors // []
MC/DC0%0/1
$self->disabled_visitors // []
$self->disabled_visitors
Truth table50%1/2
A
A= $self->disabled_visitors
| |||||||||||||||||||||||||||||
| 54 | ) | ||||||||||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1273 | ▶ | sub visitor_cache_key { | ||||||||||||||||||||||||||
|
Subroutine
visitor_cache_key: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 1273 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | return join ',', | ||||||||||||||||||||||||||||
| 62 | Zuzu::Parser->normalize_disabled_visitors( | ||||||||||||||||||||||||||||
| 63 | 1273 | ▶ | @{ $self->disabled_visitors // [] }, | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->disabled_visitors // []
MC/DC0%0/1
$self->disabled_visitors // []
$self->disabled_visitors
Truth table50%1/2
A
A= $self->disabled_visitors
| |||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 753 | ▶ | sub apply_visitors { | ||||||||||||||||||||||||||
|
Subroutine
apply_visitors: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 753 | my ( $self, $ast ) = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 753 | my $disabled = $self->_disabled_visitor_set; | |||||||||||||||||||||||||||
| 71 | 753 | ▶ | for ( my $i = 0; $i < @VISITOR_CLASSES; $i += 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 2259 | my ( $name, $class ) = @VISITOR_CLASSES[ $i, $i + 1 ]; | |||||||||||||||||||||||||||
| 73 | 2259 | ▶ | next if $disabled->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 2258 | $class->new->apply($ast); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 753 | return $ast; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 682 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 682 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | $self->disabled_visitors([ | ||||||||||||||||||||||||||||
| 84 | Zuzu::Parser->normalize_disabled_visitors( | ||||||||||||||||||||||||||||
| 85 | 682 | ▶ | @{ $self->disabled_visitors // [] }, | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->disabled_visitors // []
MC/DC0%0/1
$self->disabled_visitors // []
$self->disabled_visitors
Truth table50%1/2
A
A= $self->disabled_visitors
| |||||||||||||||||||||||||||||
| 86 | ), | ||||||||||||||||||||||||||||
| 87 | ]); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 682 | return; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 891 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 891 | my ($self, $src, $filename) = @_; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 891 | my $ast; | |||||||||||||||||||||||||||
| 96 | eval { | ||||||||||||||||||||||||||||
| 97 | 891 | my $lx = Zuzu::Lexer->new(src => $src, filename => $filename); | |||||||||||||||||||||||||||
| 98 | 891 | my $p = Zuzu::Parser::_Impl->new(lexer => $lx, filename => $filename); | |||||||||||||||||||||||||||
| 99 | 880 | $ast = $p->parse_program; | |||||||||||||||||||||||||||
| 100 | 718 | $self->apply_visitors($ast); | |||||||||||||||||||||||||||
| 101 | 718 | 1; | |||||||||||||||||||||||||||
| 102 | 891 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 173 | my $err = $@; | |||||||||||||||||||||||||||
| 104 | 173 | ▶ | die $err if ref($err) and eval { $err->isa('Zuzu::Error') }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $err and eval {
do {
$err->isa("Zuzu::Error")
}
}
MC/DC50%1/2
ref $err and eval {
do {
$err->isa("Zuzu::Error")
}
}
ref $err
eval {
do {
$err->isa("Zuzu::Error")
}
}
Truth table66%2/3
A and B
A= ref $errB= eval {
do {
$err->isa("Zuzu::Error")
}
}
| |||||||||||||||||||||||||||||
| 105 | 15 | die $self->_compile_error_from_parse_exception( $err, $filename ); | |||||||||||||||||||||||||||
| 106 | }; | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 718 | return $ast; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 15 | ▶ | sub _compile_error_from_parse_exception { | ||||||||||||||||||||||||||
|
Subroutine
_compile_error_from_parse_exception: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||
| 112 | 15 | my ( $self, $err, $filename ) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 15 | my $message = "$err"; | |||||||||||||||||||||||||||
| 115 | 15 | chomp $message; | |||||||||||||||||||||||||||
| 116 | 15 | $message =~ s/\s+at \S+ line \d+\.?\z//; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 15 | ▶ | my $code = $message =~ /\A(?:Unterminated|Invalid)\b/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | ? 'E_COMPILE_SYNTAX' | ||||||||||||||||||||||||||||
| 120 | : 'E_COMPILE_INTERNAL'; | ||||||||||||||||||||||||||||
| 121 | 15 | ▶ | $message = "Internal parser failure: $message" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | if $code eq 'E_COMPILE_INTERNAL'; | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 15 | ▶ | return Zuzu::Error->new_compile( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | code => $code, | ||||||||||||||||||||||||||||
| 126 | message => $message, | ||||||||||||||||||||||||||||
| 127 | file => ( defined $filename ? $filename : '<input>' ), | ||||||||||||||||||||||||||||
| 128 | line => 1, | ||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =pod | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 NAME | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Zuzu::Parser - entry point for parsing source text into an AST | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Converts source text into a C<Zuzu::AST::Program> by lexing and delegating to C<Zuzu::Parser::_Impl>. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Inherits from C<Moo::Object>. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 ROLES | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | None. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 METHODS | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 parse | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Parses source text and returns a C<Zuzu::AST::Program>. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Subclasses: none in this distribution. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 COPYRIGHT AND LICENCE | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | B<< Zuzu::Parser >> is copyright Toby Inkster. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | It is free software; you may redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 165 | the terms of either the Artistic License 1.0 or the GNU General Public | ||||||||||||||||||||||||||||
| 166 | License version 2. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 78 | 1; | |||||||||||||||||||||||||||