AtteanX/API/Lexer.pm

filter: Statementstmt 92.4% 73 / 79 Branchbran 64.2% 18 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 84.8% 112 / 132 CC 16 SCAR 10.5
CC
16
Cov
84%
read_length
4.4
read_word
4.2
check_for_bom
3.5
CRAP 2.9
« Test/Attean/TestSimpleQueryEvaluator.pm ↑ Summary AtteanX/Parser/SPARQLTSV.pm »
1 50 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 AtteanX::API::Lexer - Role defining common functionality for lexers.
7  
8 =head1 VERSION
9  
10 This document describes AtteanX::API::Lexer version 0.040
11  
12 =head1 DESCRIPTION
13  
14 The AtteanX::API::Lexer role provides a common interface and implementation
15 for lexer implementations, allowing line-based buffer filling, and consuming
16 of characters, constant strings, and fixed-length buffers.
17  
18 =head1 ATTRIBUTES
19  
20 =over 4
21  
22 =item C<< file >>
23  
24 =item C<< linebuffer >>
25  
26 =item C<< line >>
27  
28 =item C<< column >>
29  
30 =item C<< buffer >>
31  
32 =item C<< start_column >>
33  
34 =item C<< start_line >>
35  
36 =back
37  
38 =head1 METHODS
39  
40 =over 4
41  
42 =cut
43  
44 50 package AtteanX::API::Lexer 0.040 {
45 50 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 50 ▶ use Types::Standard qw(FileHandle Ref Str Int ArrayRef HashRef ConsumerOf InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 50 has file => ( is => 'ro', isa => FileHandle, required => 1, );
51 50 has linebuffer => ( is => 'rw', isa => Str, default => '', );
52 50 has line => ( is => 'rw', isa => Int, default => 1, );
53 50 has column => ( is => 'rw', isa => Int, default => 1, );
54 50 has buffer => ( is => 'rw', isa => Str, default => '', );
55 50 has start_column => ( is => 'rw', isa => Int, default => -1, );
56 50 has start_line => ( is => 'rw', isa => Int, default => -1, );
57  
58 115 ▶ around 'BUILDARGS' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
59 115 my $orig = shift;
60 115 my $class = shift;
61 115 ▶ return { file => shift } if (scalar(@_) == 1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 36 79 115
62 79 return $orig->( $class, @_ );
63 50 };
64  
65 =item C<< fill_buffer >>
66  
67 Fills the buffer with a new line from the underlying filehandle.
68  
69 =cut
70  
71 177 ▶ sub fill_buffer {
Subroutine fill_buffer: called CC=3 SCAR=11.0
Pod documented
72 177 my $self = shift;
73 177 ▶ unless (length($self->buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $self->buffer) 177 0 177
74 177 my $line = $self->file->getline;
75 # use Data::Dumper;
76 # warn Dumper({buffer_line => $line});
77 177 ▶ $self->{buffer} .= $line if (defined($line));
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 113 64 177
78 }
79 }
80  
81 =item C<< check_for_bom >>
82  
83 Remove a BOM character if one appears at the start of the buffer.
84  
85 =cut
86  
87 30 ▶ sub check_for_bom {
Subroutine check_for_bom: called CC=3 SCAR=12.5
Pod documented
88 30 my $self = shift;
89 30 my $c = $self->peek_char();
90 30 ▶ $self->get_char if (defined($c) and $c eq "\x{FEFF}");
Branch: 1 branch
BranchTrueFalseTotal
if defined $c and $c eq "\x{feff}" 0 30 30
Condition33%1/3
defined $c and $c eq "\x{feff}"
!ll&&!rl&&r
0300
MC/DC0%0/2
defined $c and $c eq "\x{feff}"
defined $c $c eq "\x{feff}"
Truth table33%1/3
A and B
A= defined $cB= $c eq "\x{feff}"
ABresult
0X0
100
111
91 }
92  
93 =item C<< get_char_safe( $char ) >>
94  
95 Consume the single character C<< $char >> from the buffer.
96 Throw an error if C<< $char >> is not at the start of the buffer.
97  
98 =cut
99  
100 648 ▶ sub get_char_safe {
Subroutine get_char_safe: called CC=2 SCAR=7.0
Pod documented
101 648 my $self = shift;
102 648 my $char = shift;
103 648 my $c = $self->get_char;
104 648 ▶ $self->_throw_error("Expected '$char' but got '$c'") if ($c ne $char);
Branch: 1 branch
BranchTrueFalseTotal
if $c ne $char 0 648 648
105 648 return $c;
106 }
107  
108 =item C<< get_char( $char ) >>
109  
110 Consume and return a single character from the buffer.
111  
112 =cut
113  
114 937 ▶ sub get_char {
Subroutine get_char: called CC=2 SCAR=7.6
Pod documented
115 937 my $self = shift;
116 937 my $c = substr($self->{buffer}, 0, 1, '');
117 937 ▶ if ($c eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\n") { } 0 937 937
118 # $self->{linebuffer} = '';
119 0 $self->{line} = 1+$self->{line};
120 0 $self->{column} = 1;
121 } else {
122 # $self->{linebuffer} .= $c;
123 937 $self->{column} = 1+$self->{column};
124 }
125 937 return $c;
126 }
127  
128 =item C<< peek_char( $char ) >>
129  
130 Return a single character from the start of the buffer.
131  
132 =cut
133  
134 1628 ▶ sub peek_char {
Subroutine peek_char: called CC=3 SCAR=11.0
Pod documented
135 1628 my $self = shift;
136 1628 ▶ if (length($self->{buffer}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"buffer"} == 0) 347 1281 1628
137 347 $self->fill_buffer;
138 347 ▶ return if (length($self->{buffer}) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"buffer"} == 0 317 30 347
139 }
140 1311 return substr($self->{buffer}, 0, 1);
141 }
142  
143 =item C<< read_word( $word ) >>
144  
145 Consume the string C<< $word >> from the start of the buffer.
146 Throw an error if C<< $word >> is not at the start of the buffer.
147  
148 =cut
149  
150 94 ▶ sub read_word {
Subroutine read_word: called CC=4 SCAR=14.3
Pod documented
151 94 my $self = shift;
152 94 my $word = shift;
153 94 ▶ $self->fill_buffer while (length($self->{buffer}) < length($word));
Branch: 1 branch
BranchTrueFalseTotal
length $self->{"buffer"} < length $word and do { $self->fill_buffer } 0 94 94
154 94 ▶ $self->_throw_error("Expected '$word'") if (substr($self->{buffer}, 0, length($word)) ne $word);
Branch: 1 branch
BranchTrueFalseTotal
if substr($self->{"buffer"}, 0, length $word) ne $word 0 94 94
155
156 94 my $lines = ($word =~ tr/\n//);
157 94 my $lastnl = rindex($word, "\n");
158 94 my $cols = length($word) - $lastnl - 1;
159 94 $self->{line} += $lines;
160 94 ▶ if ($lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines) { } 0 94 94
161 0 $self->{column} = $cols;
162 } else {
163 94 $self->{column} += $cols;
164 }
165 94 substr($self->{buffer}, 0, length($word), '');
166 }
167  
168 =item C<< read_length( $length ) >>
169  
170 Consume and return C<< $length >> characters from the start of the buffer.
171  
172 =cut
173  
174 1941 ▶ sub read_length {
Subroutine read_length: called CC=4 SCAR=14.8
Pod documented
175 1941 my $self = shift;
176 1941 my $len = shift;
177 1941 ▶ while (length($self->{buffer}) < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"buffer"} < $len) 0 1941 1941
178 0 my $curlen = length($self->{buffer});
179 0 $self->fill_buffer;
180 0 ▶ last if (length($self->{buffer}) == $curlen);
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"buffer"} == $curlen 0 0 0
181 }
182
183 1941 my $word = substr($self->{buffer}, 0, $len, '');
184 1941 my $lines = ($word =~ tr/\n//);
185 1941 my $lastnl = rindex($word, "\n");
186 1941 my $cols = length($word) - $lastnl - 1;
187 1941 $self->{line} += $lines;
188 1941 ▶ if ($lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines) { } 116 1825 1941
189 116 $self->{column} = $cols;
190 } else {
191 1825 $self->{column} += $cols;
192 }
193 1941 return $word;
194 }
195 }
196  
197 50 1;
198  
199 __END__
« Test/Attean/TestSimpleQueryEvaluator.pm ↑ Summary AtteanX/Parser/SPARQLTSV.pm »