blib/lib/RTF/Lexer.pm

filter: Statementstmt 89.7% 96 / 107 Branchbran 46.1% 12 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.0% 22 / 25 Podpod 42.8% 3 / 7 totaltotal 78.8% 134 / 170 CC 15 SCAR 13.4
CC
15
Cov
80%
get_token
9.6
add_stop_token
6.0
del_stop_token
6.0
CRAP 3.8
« blib/lib/RTF/HTMLConverter.pm ↑ Summary Lexer/test.pl »
1 package RTF::Lexer;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our @ISA = qw(DynaLoader Exporter);
7 1 our $VERSION = '0.03';
8  
9 1 ▶use constant CWORD => 256;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use constant CSYMB => 257;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant CUNDF => 258;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant CPARM => 259;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant CNOPR => 260;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant PTEXT => 261;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant ENTER => 262;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant LEAVE => 263;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant DESTN => 264;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant ENHEX => 265;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant ENBIN => 266;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant WRHEX => 268;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant OKEOF => 300;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant UNEOF => 301;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant UNBRC => 302;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 our @EXPORT_OK;
26 1 our %EXPORT_TAGS;
27 {
28 1 my $package = __PACKAGE__;
29 1 @EXPORT_OK = (grep { $_ } map { /^$package\::(\w+)$/ } keys %constant::declared);
30 1 %EXPORT_TAGS = (all => [@EXPORT_OK]);
31 }
32  
33 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
34 1 my $proto = shift;
35 1 ▶ my $self = bless({}, ref($proto) || $proto);
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
36 1 $self->init(@_);
37 1 return $self;
38 }
39  
40 1 ▶sub init{
Subroutine init: called CC=4 SCAR=16.1
Pod undocumented
41 1 my ($self, %opts) = @_;
42 1 $self->{RTF_Lexer_backpack} = [];
43 1 $self->{_RTF_Lexer_backpack} = undef;
44 1 ▶ if($opts{in}){
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"in"}) 1 0 1
45 1 ▶ unless(ref $opts{in}){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $opts{"in"}) 0 1 1
46 0 ▶ open my $fh, "< $opts{in}" or die "Can't open '$opts{in}': $!!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, qq[< $opts{"in"}] or die qq[Can't open '$opts{"in"}': $!!\n] 0 0 0
47 0 $opts{in} = $fh;
48 }
49 1 $self->_set_source($opts{in});
50 1 $self->{_RTF_Lexer_IN} = $opts{in};
51 }
52 }
53  
54 1 bootstrap RTF::Lexer $VERSION;
55  
56 1115 ▶sub get_token {
Subroutine get_token: called CC=8 SCAR=22.6
Pod documented
57 1115 my $self = shift;
58 1115 my $token;
59 1115 ▶ if(@{$self->{RTF_Lexer_backpack}}){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'RTF_Lexer_backpack'};}) { } 0 1115 1115
60 0 $token = shift @{$self->{RTF_Lexer_backpack}};
61 }else{
62 1115 ▶ if($self->{_RTF_Lexer_backpack}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_RTF_Lexer_backpack'}) { } 0 1115 1115
63 0 $token = $self->{_RTF_Lexer_backpack};
64 0 $self->{_RTF_Lexer_backpack} = undef;
65 }else{
66 1115 $token = [0, ''];
67 1115 $token->[0] = $self->_get_token($token->[1]);
68 }
69 1115 ▶ if($token->[0] == CWORD){
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] == 256) 641 474 1115
70 641 my ($type, $text);
71 641 $type = $self->_get_token($text);
72 641 ▶ if($type == CPARM){ # Parameter that follows
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 259) { } 371 270 641
elsif ($type != 260) { } 0 270 270
73 371 $token->[2] = $text;
74 371 $type = $self->_get_token($text); # Space delimiter if any
75 371 ▶ $self->{_RTF_Lexer_backpack} = [$type, $text] unless $type == CNOPR;
Branch: 1 branch
BranchTrueFalseTotal
unless $type == 260 0 371 371
76 }elsif($type != CNOPR){ # Not a space delimiter
77 0 $self->{_RTF_Lexer_backpack} = [$type, $text];
78 }
79 }
80 }
81 1115 ▶ return $token->[0] ? $token : undef;
Branch: 1 branch
BranchTrueFalseTotal
$token->[0] ? : 1115 0 1115
82 }
83  
84 0 ▶sub unget_token { push @{$_[0]->{RTF_Lexer_backpack}}, $_[1] }
Subroutine unget_token: not called CC=1 SCAR=6.9
Pod documented
85  
86 {
87 1 my %stop_tokens = map { $_ => 1 } (OKEOF, UNBRC, UNEOF);
88 1115 ▶ sub is_stop_token { $stop_tokens{ref($_[1]) ? $_[1]->[0] : $_[1]} }
Subroutine is_stop_token: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 1115 0 1115
89 0 ▶ sub add_stop_token { $stop_tokens{ref($_[1]) ? $_[1]->[0] : $_[1]} = 1 }
Subroutine add_stop_token: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
90 0 ▶ sub del_stop_token { delete $stop_tokens{ref($_[1]) ? $_[1]->[0] : $_[1]} }
Subroutine del_stop_token: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
91 }
92  
93  
94 1 1;
95  
96 =head1 NAME
97  
98 RTF::Lexer - Rich Text Format (RTF) lexical analyzer.
99  
100 =head1 SYNOPSIS
101  
102 use RTF::Lexer qw(:all);
103  
104 my $parser = RTF::Lexer->new(in => 'text.rtf');
105 my $token;
106 do {
107 $token = $parser->get_token();
108 } until $parser->is_stop_token($token);
109  
110  
111 =head1 DESCRIPTION
112  
113 RTF::Lexer is a low-level RTF format lexical analyzer. It splits the
114 input stream into separate tokens, which can be handled by other
115 high-level modules.
116  
117 =head1 METHODS
118  
119 =over 4
120  
121 =item new
122  
123 The constructor. Accepts the only argument C<in> which must be an input
124 file handle or a file name. In the latter case if there is a failure
125 while opening the file method C<new> throws an exception. By default
126 the input is read from C<STDIN>.
127  
128 =item get_token
129  
130 Returns the next token from the input stream. The token is a reference to
131 an array those first element is a numeric id of the token type. The second
132 element is a string representation of the token. The third element may
133 exists only if the token is a control word and represents the numerical
134 parameter of this control word.
135  
136 The following token types are recognized by RTF::Lexer, that are declared
137 as constants in this module:
138  
139 =over 8
140  
141 =item CWORD
142  
143 Control word (eg. C<\rtf1>, C<\trowd>).
144  
145 =item CSYMB
146  
147 Control symbol, mentioned in RTF Specification version 1.7.
148  
149 =item CUNDF
150  
151 Unknown control symbol (i.e. not mentioned in RTF Specification).
152  
153 =item PTEXT
154  
155 Plain text.
156  
157 =item ENTER
158  
159 Start of group (C<{>).
160  
161 =item LEAVE
162  
163 End of group (C<}>).
164  
165 =item DESTN
166  
167 End of destination group (C<}> that turns off destination mode).
168  
169 =item ENHEX
170  
171 Data in hexadecimal format that follows C<\'> control symbol.
172  
173 =item ENBIN
174  
175 End of binary data block (started by C<\bin> control word).
176  
177 =item WRHEX
178  
179 Symbol which is not a hexadecimal digit found where ENHEX token expected.
180  
181 =item OKEOF
182  
183 Normal end of input stream.
184  
185 =item UNEOF
186  
187 Unexpected end of input stream.
188  
189 =item UNBRC
190  
191 End of group that does not match any start of group.
192  
193 =back
194  
195 These constants are not exported by default. Any of them may be exported by request.
196 All of them may be exported by the use of C<:all> export tag.
197  
198 =item unget_token($token)
199  
200 Pushes back token C<$token> so the next call to C<get_token> will return it.
201  
202 =item set_destination
203  
204 Turns on the destination mode, i.e. all tokens will be ignored until the end of
205 current group.
206  
207 =back
208  
209 =head1 SEE ALSO
210  
211 RTF::Tokenizer, Rich Text Format (RTF) Specification.
212  
213 =head1 BUGS
214  
215 It is impossible to have more then one RTF::Lexer objects in a single process.
216  
217 =head1 AUTHOR
218  
219 Vadim O. Ustiansky <ustiansky@cpan.org>
220  
« blib/lib/RTF/HTMLConverter.pm ↑ Summary Lexer/test.pl »