Preproc/Token.pm

filter: Statementstmt 100% 44 / 44 Branchbran 100% 10 / 10 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 7 / 7 totaltotal 92.9% 79 / 85 CC 8 SCAR 8.1
CC
8
Cov
97%
_format_error_msg
5.0
error_at
2.0
warning_at
2.0
CRAP 2.2
« Preproc.pm ↑ Summary Preproc/Line.pm »
1 # $Id: Token.pm,v 1.6 2013/07/26 01:57:26 Paulo Exp $
2
3 package Asm::Preproc::Token;
4
5 #------------------------------------------------------------------------------
6
7 =head1 NAME
8
9 Asm::Preproc::Token - One token retrieved from the input
10
11 =cut
12
13 #------------------------------------------------------------------------------
14
15 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 4 our $VERSION = '1.03';
19
20 4 ▶use Data::Dump 'dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Asm::Preproc::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
22
23 #------------------------------------------------------------------------------
24
25 =head1 SYNOPSIS
26
27 use Asm::Preproc::Token;
28 my $token = Asm::Preproc::Token->new($type, $value, $line);
29 $token->type; $token->value;
30 $token->line; # isa Asm::Preproc::Line
31 my $token2 = $token->clone;
32 $token->error($message);
33 $token->warning($message);
34 Asm::Preproc::Token->error_at($token, $message);
35 Asm::Preproc::Token->warning_at($token, $message);
36
37 =head1 DESCRIPTION
38
39 This module defines the object to represent one token of input text as retrieved
40 from the preprocessed input text.
41 It contains the token type (a string), the token value (a string) and a
42 L<Asm::Preproc::Line|Asm::Preproc::Line> object with the line where the token
43 was found.
44
45 There are also utility methods for error messages.
46
47 =head1 METHODS
48
49 =head2 new
50
51 Creates a new object with the given type, value and line.
52
53 =head2 type
54
55 Get/set type.
56
57 =head2 value
58
59 Get/set file value.
60
61 =head2 line
62
63 Get/set line.
64
65 =head2 clone
66
67 Creates an identical copy as a new object.
68
69 =cut
70
71 #------------------------------------------------------------------------------
72 4 ▶use base 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
73 4 __PACKAGE__->mk_accessors(
74 'type',
75 'value',
76 '_line',
77 );
78
79 # create line on demand
80 209 ▶sub _has_line {
Subroutine _has_line: called CC=1 SCAR=0.0
81 209 my $self = shift;
82 209 return defined($self->_line);
83 }
84
85 209 ▶sub line {
Subroutine line: called CC=2 SCAR=6.9
Pod documented
86 209 my $self = shift;
87 209 ▶ $self->_has_line or $self->_line( Asm::Preproc::Line->new );
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_line or $self->_line("Asm::Preproc::Line"->new) 183 26 209
88 209 $self->_line(@_);
89 }
90
91 67 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
92 67 my($class, $type, $value, $line) = @_;
93 67 bless {type => $type, value => $value, _line => $line}, $class;
94 }
95
96 1 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
97 1 my $self = shift;
98 1 bless {type => $self->type, value => $self->value, _line => $self->line->clone}, ref($self);
99 }
100
101 #------------------------------------------------------------------------------
102
103 =head2 error
104
105 Dies with the given error message, indicating the place in the input source file
106 where the error occured as:
107
108 FILE(LINE) : error ... at TOKEN
109
110 =cut
111
112 #------------------------------------------------------------------------------
113 12 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod documented
114 12 my($self, $message) = @_;
115 12 $self->line->error($self->_format_error_msg($message));
116 }
117 #------------------------------------------------------------------------------
118
119 =head2 error_at
120
121 Same as error(), but is a class method and can receive an undef $token.
122
123 =cut
124
125 #------------------------------------------------------------------------------
126 24 ▶sub error_at {
Subroutine error_at: called CC=2 SCAR=7.0
Pod documented
127 24 my($class, $token, $message) = @_;
128 24 ▶ $token ||= $class->new();
Condition66%2/3
$token ||= $class->new
l!l&&r!l&&!r
12120
MC/DC0%0/2
$token ||= $class->new
$token $class->new
Truth table66%2/3
A ||= B
A= $tokenB= $class->new
ABresult
1X1
011
000
129 24 $token->line->error($token->_format_error_msg($message));
130 }
131 #------------------------------------------------------------------------------
132
133 =head2 warning
134
135 Warns with the given error message, indicating the place in the input source file
136 where the error occured as:
137
138 FILE(LINE) : warning ... at TOKEN
139
140 =cut
141
142 #------------------------------------------------------------------------------
143 12 ▶sub warning {
Subroutine warning: called CC=1 SCAR=0.0
Pod documented
144 12 my($self, $message) = @_;
145 12 $self->line->warning($self->_format_error_msg($message));
146 }
147 #------------------------------------------------------------------------------
148
149 =head2 warning_at
150
151 Same as warning(), but is a class method and can receive an undef $token.
152
153 =cut
154
155 #------------------------------------------------------------------------------
156 24 ▶sub warning_at {
Subroutine warning_at: called CC=2 SCAR=7.0
Pod documented
157 24 my($class, $token, $message) = @_;
158 24 ▶ $token ||= $class->new();
Condition66%2/3
$token ||= $class->new
l!l&&r!l&&!r
12120
MC/DC0%0/2
$token ||= $class->new
$token $class->new
Truth table66%2/3
A ||= B
A= $tokenB= $class->new
ABresult
1X1
011
000
159 24 $token->line->warning($token->_format_error_msg($message));
160 }
161 #------------------------------------------------------------------------------
162 # error message for error() and warning()
163 72 ▶sub _format_error_msg {
Subroutine _format_error_msg: called CC=5 SCAR=16.1
164 72 my($self, $message) = @_;
165 72 my $type = $self->type;
166
167 72 ▶ defined($message) or $message = "";
Branch: 1 branch
BranchTrueFalseTotal
defined $message or $message = "" 48 24 72
168 72 $message =~ s/\s+$//;
169 72 ▶ $message .= " " if $message ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $message ne "" 48 24 72
170 72 ▶ $message .= "at ".
Branch: 2 branches
BranchTrueFalseTotal
!defined($type) ? : 48 24 72
$type =~ /\W/ ? : 12 12 24
171 (! defined($type) ?
172 "EOF" :
173 $type =~ /\W/ ?
174 dump($type) :
175 $type
176 );
177 72 return $message;
178 }
179 #------------------------------------------------------------------------------
180
181 =head1 AUTHOR, BUGS, SUPPORT, LICENSE, COPYRIGHT
182
183 See L<Asm::Preproc|Asm::Preproc>.
184
185 =cut
186
187 #------------------------------------------------------------------------------
188
189 4 1;
« Preproc.pm ↑ Summary Preproc/Line.pm »