blib/lib/PPIx/EditorTools/Lexer.pm

filter: Statementstmt 95.0% 57 / 60 Branchbran 73.5% 25 / 34 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 82.6% 95 / 115 CC 21 SCAR 27.2
CC
21
Cov
86%
class_to_css
22.3
lexer
3.0
BEGIN
1.0
CRAP 15.2
« blib/lib/PPIx/EditorTools/RenameVariable.pm ↑ Summary blib/lib/PPIx/EditorTools/IntroduceTemporaryVariable.pm »
1 package PPIx::EditorTools::Lexer;
2 3 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: Simple Lexer used for syntax highlighting
4 3 $PPIx::EditorTools::Lexer::VERSION = '0.21';
5 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use base 'PPIx::EditorTools';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Class::XSAccessor accessors => {};
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 8 ▶sub lexer {
Subroutine lexer: called CC=3 SCAR=11.0
Pod undocumented
17 8 my ( $self, %args ) = @_;
18 8 my $markup = delete $args{highlighter};
19  
20 8 $self->process_doc(%args);
21  
22 8 my $ppi = $self->ppi;
23  
24 8 ▶ return [] unless defined $ppi;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ppi 0 8 8
25 8 $ppi->index_locations;
26  
27 8 my @tokens = $ppi->tokens;
28  
29 8 foreach my $t (@tokens) {
30  
31 91 my ( $row, $rowchar ) = @{ $t->location };
32  
33 91 my $css = class_to_css($t);
34  
35 91 my $len = $t->length;
36  
37 91 $markup->( $css, $row, $rowchar, $len );
38 }
39 }
40  
41  
42 91 ▶sub class_to_css {
Subroutine class_to_css: called CC=19 SCAR=31.1
Pod undocumented
43 91 my $Token = shift;
44  
45 91 ▶ if ( $Token->isa('PPI::Token::Word') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Token->isa("PPI::Token::Word")) 37 54 91
46  
47 # There are some words we can be very confident are
48 # being used as keywords
49 37 ▶ unless ( $Token->snext_sibling and $Token->snext_sibling->content eq '=>' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Token->snext_sibling and $Token->snext_sibling->content eq "=>") 37 0 37
Condition66%2/3
$Token->snext_sibling and $Token->snext_sibling->content eq "=>"
!ll&&!rl&&r
7300
MC/DC0%0/2
$Token->snext_sibling and $Token->snext_sibling->content eq "=>"
$Token->snext_sibling $Token->snext_sibling->content eq "=>"
Truth table66%2/3
A and B
A= $Token->snext_siblingB= $Token->snext_sibling->content eq "=>"
ABresult
0X0
100
111
50 37 ▶ if ( $Token->content =~ /^(?:sub|return)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Token->content =~ /^(?:sub|return)$/) { } 3 34 37
elsif ($Token->content =~ /^(?:undef|shift|defined|bless)$/) { } 4 30 34
51 3 return 'keyword';
52 } elsif ( $Token->content =~ /^(?:undef|shift|defined|bless)$/ ) {
53 4 return 'core';
54 }
55 }
56  
57 30 ▶ if ( $Token->previous_sibling and $Token->previous_sibling->content eq '->' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Token->previous_sibling and $Token->previous_sibling->content eq "->") 0 30 30
Condition66%2/3
$Token->previous_sibling and $Token->previous_sibling->content eq "->"
!ll&&!rl&&r
9210
MC/DC0%0/2
$Token->previous_sibling and $Token->previous_sibling->content eq "->"
$Token->previous_sibling $Token->previous_sibling->content eq "->"
Truth table66%2/3
A and B
A= $Token->previous_siblingB= $Token->previous_sibling->content eq "->"
ABresult
0X0
100
111
58 0 ▶ if ( $Token->content =~ /^(?:new)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Token->content =~ /^(?:new)$/) 0 0 0
59 0 return 'core';
60 }
61 }
62  
63 30 ▶ if ( $Token->parent->isa('PPI::Statement::Include') ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($Token->parent->isa('PPI::Statement::Include')) { } 8 22 30
elsif ($Token->parent->isa('PPI::Statement::Variable')) { } 4 18 22
elsif ($Token->parent->isa('PPI::Statement::Compound')) { } 8 10 18
elsif ($Token->parent->isa('PPI::Statement::Package')) { } 1 9 10
elsif ($Token->parent->isa('PPI::Statement::Scheduled')) { } 0 9 9
64 8 ▶ if ( $Token->content =~ /^(?:use|no)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Token->content =~ /^(?:use|no)$/) 5 3 8
65 5 return 'keyword';
66 }
67 3 ▶ if ( $Token->content eq $Token->parent->pragma ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Token->content eq $Token->parent->pragma) 2 1 3
68 2 return 'pragma';
69 }
70 } elsif ( $Token->parent->isa('PPI::Statement::Variable') ) {
71 4 ▶ if ( $Token->content =~ /^(?:my|local|our)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Token->content =~ /^(?:my|local|our)$/) 4 0 4
72 4 return 'keyword';
73 }
74 } elsif ( $Token->parent->isa('PPI::Statement::Compound') ) {
75 8 ▶ if ( $Token->content =~ /^(?:if|else|elsif|unless|for|foreach|while|my)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Token->content =~ /^(?:if|else|elsif|unless|for|foreach|while|my)$/) 8 0 8
76 8 return 'keyword';
77 }
78 } elsif ( $Token->parent->isa('PPI::Statement::Package') ) {
79 1 ▶ if ( $Token->content eq 'package' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Token->content eq "package") 1 0 1
80 1 return 'keyword';
81 }
82 } elsif ( $Token->parent->isa('PPI::Statement::Scheduled') ) {
83 0 return 'keyword';
84 }
85 }
86  
87 # Normal coloring
88 64 my $css = ref $Token;
89 64 $css =~ s/^.+:://;
90 64 $css;
91 }
92  
93  
94 3 1;
95  
96 =pod
97  
98 =encoding UTF-8
99  
100 =head1 NAME
101  
102 PPIx::EditorTools::Lexer - Simple Lexer used for syntax highlighting
103  
104 =head1 VERSION
105  
106 version 0.21
107  
108 =head1 SYNOPSIS
109  
110 PPIx::EditorTools::Lexer->new->lexer(
111 code => "package TestPackage;\nsub x { 1;\n",
112 highlighter => sub {
113 my ( $css, $row, $rowchar, $len ) = @_;
114 ...
115 },
116 );
117  
118 =head1 DESCRIPTION
119  
120 Go over the various interesting elements of a give piece
121 of code or an already process PPI tree.
122 For each token call the user supplied 'highlighter' function with
123 the follow values:
124  
125 $css - The keyword that can be used for colouring.
126 $row - The row number where the token starts
127 $rowchar - The character within that row where the token starts
128 $len - The length of the token
129  
130 =head1 METHODS
131  
132 =over 4
133  
134 =item new()
135  
136 Constructor. Generally shouldn't be called with any arguments.
137  
138 =item find( ppi => PPI::Document $ppi, highlighter => sub {...} )
139  
140 =item find( code => Str $code, highlighter => sub ...{} )
141  
142 Accepts either a C<PPI::Document> to process or a string containing
143 the code (which will be converted into a C<PPI::Document>) to process.
144 Return a reference to an array.
145  
146 =back
147  
148 =head1 SEE ALSO
149  
150 This class inherits from C<PPIx::EditorTools>.
151 Also see L<App::EditorTools>, L<Padre>, and L<PPI>.
152  
153 =head1 AUTHORS
154  
155 =over 4
156  
157 =item *
158  
159 Steffen Mueller C<smueller@cpan.org>
160  
161 =item *
162  
163 Mark Grimes C<mgrimes@cpan.org>
164  
165 =item *
166  
167 Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
168  
169 =item *
170  
171 Gabor Szabo <gabor@szabgab.com>
172  
173 =item *
174  
175 Yanick Champoux <yanick@cpan.org>
176  
177 =back
178  
179 =head1 COPYRIGHT AND LICENSE
180  
181 This software is copyright (c) 2017, 2014, 2012 by The Padre development team as listed in Padre.pm..
182  
183 This is free software; you can redistribute it and/or modify it under
184 the same terms as the Perl 5 programming language system itself.
185  
186 =cut
187  
188 __END__
« blib/lib/PPIx/EditorTools/RenameVariable.pm ↑ Summary blib/lib/PPIx/EditorTools/IntroduceTemporaryVariable.pm »