Preproc.pm

filter: Statementstmt 99.1% 119 / 120 Branchbran 93.4% 43 / 46 Conditioncond 53.3% 8 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 21 / 21 Podpod 100% 8 / 8 totaltotal 91.2% 199 / 218 CC 32 SCAR 19.4
CC
32
Cov
94%
getline
15.2
__ANON__
8.0
include
5.1
CRAP 6.9
« Preproc/Lexer.pm ↑ Summary Preproc/Token.pm »
1 # $Id: Preproc.pm,v 1.15 2013/07/26 01:57:26 Paulo Exp $
2
3 package Asm::Preproc;
4
5 #------------------------------------------------------------------------------
6
7 =head1 NAME
8
9 Asm::Preproc - Preprocessor to be called from an assembler
10
11 =cut
12
13 #------------------------------------------------------------------------------
14
15 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Asm::Preproc::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Iterator::Simple::Lookahead;
Subroutine BEGIN: called CC=1 SCAR=0.0
21
22 7 our $VERSION = '1.03';
23
24 #------------------------------------------------------------------------------
25
26 =head1 SYNOPSIS
27
28 use Asm::Preproc;
29 my $pp = Asm::Preproc->new();
30 my $pp = Asm::Preproc->new(@files);
31
32 $pp->add_path(@path); @path = $pp->path;
33 $pp->include($file); $pp->include($file, $from_line);
34 my $full_path = $pp->path_search($file);
35
36 $pp->include_list(@input);
37
38 my $iter = sub {return scalar <STDIN>};
39 $pp->include_list($iter);
40
41 my $line = $pp->getline; # isa Asm::Preproc::Line
42 my $strm = $pp->line_stream; # isa Iterator::Simple::Lookahead
43
44 =head1 DESCRIPTION
45
46 This module implements a preprocessor that reads source files
47 and handles recursive file includes.
48 It is intended to be called from inside an assembler or compiler.
49
50 =cut
51
52 # TODO: does conditional text expansion and macro substitution.
53
54 =head1 METHODS
55
56 =head2 new
57
58 Creates a new object. If an argument list is given, calls C<include>
59 for each of the file starting from the last, so that the files are
60 read in the given order.
61
62 =cut
63
64 #------------------------------------------------------------------------------
65 # Asm::Preproc::File : current file being read
66 {
67 package # hide from indexer
68 Asm::Preproc::File;
69 7 ▶ use base 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 7 __PACKAGE__->mk_accessors(
71 'iter', # iter() to read each line
72 'file', # file name
73 'line_nr', # current line number
74 'line_inc', # line number increment
75 );
76
77 30 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
78 30 my($class, $iter, $file) = @_;
79 30 bless {iter => $iter, file => $file, line_nr => 0, line_inc => 1}, $class;
80 }
81 7 };
82
83 #------------------------------------------------------------------------------
84 # Asm::Preproc : stack of stuff to read
85 7 ▶use base 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
86 7 __PACKAGE__->mk_accessors(
87 '_stack', # stack of Asm::Preproc::File
88 '_path', # path of search directories
89 );
90
91 7 ▶use constant TOP => -1; # top of stack, i.e. current input file
Subroutine BEGIN: called CC=1 SCAR=0.0
92
93 20 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
94 20 my($class, @files) = @_;
95 20 my $self = bless {_stack => [], _path => []}, $class;
96 20 $self->include($_) for reverse @files;
97 19 return $self;
98 }
99 #------------------------------------------------------------------------------
100
101 =head2 path
102
103 Returns the list of directories to search in sequence for source files.
104
105 =cut
106
107 #------------------------------------------------------------------------------
108 3 ▶sub path { @{$_[0]->_path} }
Subroutine path: called CC=1 SCAR=0.0
Pod documented
109 #------------------------------------------------------------------------------
110
111 =head2 add_path
112
113 Adds the given directories to the path searched for include files.
114
115 =cut
116
117 #------------------------------------------------------------------------------
118 2 ▶sub add_path {
Subroutine add_path: called CC=1 SCAR=0.0
Pod documented
119 2 my($self, @dirs) = @_;
120 2 push @{$self->_path}, @dirs;
121 }
122 #------------------------------------------------------------------------------
123
124 =head2 path_search
125
126 Searches for the given file in the C<path> created by C<add_path>, returns
127 the first full path name where the file can be found.
128
129 Returns the input file name if the file is found in the current directory,
130 or if it is not found in any of the C<path> directories.
131
132 =cut
133
134 #------------------------------------------------------------------------------
135 27 ▶sub path_search {
Subroutine path_search: called CC=4 SCAR=13.9
Pod documented
136 27 my($self, $file) = @_;
137
138 27 ▶ return $file if -f $file; # found
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 20 7 27
139
140 7 for my $dir (@{$self->_path}) {
141 8 my $full_path = File::Spec->catfile($dir, $file);
142 8 ▶ return $full_path if -f $full_path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $full_path 5 3 8
143 }
144
145 2 return $file; # not found
146 }
147 #------------------------------------------------------------------------------
148
149 =head2 include
150
151 Open the input file and sets-up the object to read each line in sequence.
152
153 The optional second argument is a L<Asm::Preproc::Line|Asm::Preproc::Line>
154 object pointing at the C<%include> line that included the file, to be used
155 in error messages.
156
157 An exception is raised if the input file cannot be read, or if a file is
158 included recursively, to avoid an infinite include loop.
159
160 =cut
161
162 #------------------------------------------------------------------------------
163 23 ▶sub include {
Subroutine include: called CC=5 SCAR=16.4
Pod documented
164 23 my($self, $file, $from_line) = @_;
165
166 # search include path
167 23 my $full_path = $self->path_search($file);
168
169 # check for include loop
170 23 ▶ if (grep {$_->file eq $full_path} @{$self->_stack}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->file eq $full_path;} @{$self->_stack;}) 1 22 23
171 1 ▶ ($from_line || Asm::Preproc::Line->new)
Condition33%1/3
$from_line || 'Asm::Preproc::Line'->new
l!l&&r!l&&!r
100
MC/DC0%0/2
$from_line || 'Asm::Preproc::Line'->new
$from_line 'Asm::Preproc::Line'->new
Truth table33%1/3
A || B
A= $from_lineB= 'Asm::Preproc::Line'->new
ABresult
1X1
011
000
172 ->error("%include loop")
173 }
174
175 # open the file
176 22 ▶ open(my $fh, $full_path) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $full_path or ($from_line || "Asm::Preproc::Line"->new)->error("unable to open input file '${full_path}'") 21 1 22
Condition33%1/3
$from_line || 'Asm::Preproc::Line'->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$from_line || 'Asm::Preproc::Line'->new
$from_line 'Asm::Preproc::Line'->new
Truth table33%1/3
A || B
A= $from_lineB= 'Asm::Preproc::Line'->new
ABresult
1X1
011
000
177 ($from_line || Asm::Preproc::Line->new)
178 ->error("unable to open input file '$full_path'");
179
180 # create a new iterator to read file lines
181 48 ▶ my $iter = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
182 48 ▶ return undef unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 48 48
183 48 my $text = <$fh>;
184 48 ▶ defined($text) and return $text;
Branch: 1 branch
BranchTrueFalseTotal
defined $text and return $text 30 18 48
185
186 18 undef $fh; # close fh at end of file
187 18 return undef;
188 21 };
189 21 $self->_push_iter($iter, $full_path);
190 }
191 #------------------------------------------------------------------------------
192
193 =head2 include_list
194
195 Sets-up the object to read each element of the passed input
196 list one line at a time.
197
198 Each element of the list is either a text string
199 or a code reference of an iterator.
200 The iterator may return text strings, or other iterators that will be
201 called recursively. The iterator returns C<undef> at the end of input.
202
203 The text strings are split by lines, so that each C<getline> calls returns
204 one complete line.
205
206 As the text lines are scanned for pre-processor directives, the following two
207 lines are equivalent:
208
209 $pp->include('file.asm');
210 $pp->include_list('%include <file.asm>');
211
212 =cut
213
214 #------------------------------------------------------------------------------
215 9 ▶sub include_list {
Subroutine include_list: called CC=1 SCAR=0.0
Pod documented
216 9 my($self, @input) = @_;
217
218 # create a new iterator to read text lines from iterators or strings
219 66 ▶ my $iter = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
220 66 while (1) {
221 106 ▶ return undef unless @input;
Branch: 1 branch
BranchTrueFalseTotal
unless @input 10 96 106
222
223 # call iterator to get first string, if any
224 96 ▶ if (ref $input[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input[0]) 32 64 96
225 32 my $text = $input[0]->(); # get first from iterator
226 32 ▶ if (defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) { } 29 3 32
227 29 unshift @input, $text; # insert line at head
228 }
229 else {
230 3 shift @input; # iter exhausted, drop it
231 }
232 32 next; # need to test list again
233 }
234
235 # line is a string, return each complete line
236 64 for ($input[0]) {
237 64 ▶ last unless defined $_; # skip undef lines
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 4 60 64
238 60 ▶ if (/ \G ( .*? \n | .+ ) /gcx) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ \G ( .*? \n | .+ ) /cgx) 56 4 60
239 56 ▶ shift @input if pos == length; # consumed all, drop it
Branch: 1 branch
BranchTrueFalseTotal
if pos $_ == length $_ 52 4 56
240 56 return $1;
241 }
242 }
243 8 shift @input; # end of input
244 }
245 9 };
246 9 $self->_push_iter($iter, "-");
247 }
248
249 #------------------------------------------------------------------------------
250 # prepare the object to read the given iterator and file name
251 30 ▶sub _push_iter {
Subroutine _push_iter: called CC=1 SCAR=0.0
252 30 my($self, $iter, $file) = @_;
253
254 # new file in the stack
255 30 push @{$self->_stack}, Asm::Preproc::File->new($iter, $file);
256 }
257 #------------------------------------------------------------------------------
258
259 =head2 getline
260
261 Returns the next line from the input, after doing all the pre-processing.
262 The line is returned as a L<Asm::Preproc::Line|Asm::Preproc::Line> object
263 containing the actual text, and the file and line number where the text
264 was found.
265
266 Returns C<undef> at the end of the input.
267
268 =cut
269
270 #------------------------------------------------------------------------------
271 # return next line as a Asm::Preproc::Line object
272 99 ▶sub getline {
Subroutine getline: called CC=15 SCAR=27.2
Pod documented
273 99 my($self) = @_;
274
275 99 while (1) {
276 142 ▶ return undef unless @{$self->_stack}; # no more files
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_stack;} 33 109 142
277 109 my $top = $self->_stack->[TOP];
278
279 # read line
280 109 my $text = $top->iter->();
281 109 ▶ if (! defined $text) { # file finished, read next
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 27 82 109
282 27 pop @{$self->_stack};
283 27 next;
284 }
285
286 # inc line number, save it to use as the line_nr of a multi-line
287 # continuation
288 82 my $line_nr = $top->line_nr( $top->line_nr + $top->line_inc );
289
290 # while line ends in \\, remove all blanks before it and \r \n after
291 # the line contains at most one \n, due to include_list() iterator
292 82 ▶ while ($text =~ s/ \s* \\ [\r\n]* \z / /x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/ \s* \\ [\r\n]* \z / /x) 5 81 86
293 5 my $next = $top->iter->();
294 5 $top->line_nr( $top->line_nr + $top->line_inc );
295
296 5 ▶ defined($next) or last; # no more input, ignore last \\
Branch: 1 branch
BranchTrueFalseTotal
defined $next or last 4 1 5
297 4 $text .= $next;
298 }
299
300 # normalize eol
301 82 $text =~ s/ \s* \z /\n/x; # any ending blanks replaced by \n
302
303 # line to be returned, is used in %include below
304 82 my $line = Asm::Preproc::Line->new($text, $top->file, $line_nr);
305
306 # check for pre-processor directives
307 82 ▶ if ($text =~ /^ \s* [\#\%] /gcix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^ \s* [\#\%] /cgix) { } 18 64 82
308 18 ▶ if ($text =~ / \G line /gcix) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ / \G line /cgix) { } 6 12 18
elsif ($text =~ / \G include /cgix) { } 12 0 12
309 # %line n+m file
310 # #line n "file"
311 6 ▶ if ($text =~ / \G \s+ (\d+) /gcix) { # line_nr
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ / \G \s+ (\d+) /cgix) 6 0 6
312 6 $top->line_nr( $1 );
313
314 6 ▶ if ($text =~ / \G \+ (\d+) /gcix) { # optional line_inc
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ / \G \+ (\d+) /cgix) { } 3 3 6
315 3 $top->line_inc( $1 );
316 }
317 else {
318 3 $top->line_inc( 1 );
319 }
320
321 6 ▶ if ($text =~ / \G \s+ \"? ([^\"\s]+) \"? /gcix) { # file
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ / \G \s+ \"? ([^\"\s]+) \"? /cgix) 4 2 6
322 4 $top->file( $1 );
323 }
324
325 # next line in nr+inc
326 6 $top->line_nr( $top->line_nr - $top->line_inc );
327 6 next; # get next line
328 }
329 }
330 elsif ($text =~ / \G include /gcix) {
331 # %include <file>
332 # #include 'file'
333 # %include "file"
334 # #include file
335 12 ▶ if ($text =~ / \G \s+ (?: \< ([^\>]+) \> |
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ / \G \s+ (?: \< ([^\>]+) \> | \' ([^\']+) \' | \" ([^\"]+) \" | (\S+) ) /cgix) { } 11 1 12
336 \' ([^\']+) \' |
337 \" ([^\"]+) \" |
338 (\S+)
339 ) /gcix) {
340 11 ▶ my $file = $1 || $2 || $3 || $4;
Condition66%6/9
$1 || $2 || $3 || $4
l!l&&r!l&&!r
1100
$1 or $2 or $3
l!l&&r!l&&!r
830
$1 or $2
l!l&&r!l&&!r
353
MC/DC0%0/4
$1 || $2 || $3 || $4
$1 $2 $3 $4
Truth table60%3/5
A or B or C || D
A= $1B= $2C= $3D= $4
ABCDresult
1XXX1
01XX1
001X1
00011
00000
341 11 $self->include($file, $line);
342 10 next; # get next line
343 }
344 else {
345 1 $line->error("%include expects a file name\n");
346 }
347 }
348 else {
349 # ignore other unknown directives
350 0 next; # get next line
351 }
352 }
353 else {
354 # TODO: macro expansion
355 }
356
357 # return complete line
358 64 return $line;
359 }
360 }
361 #------------------------------------------------------------------------------
362
363 =head2 line_stream
364
365 Returns a L<Iterator::Simple::Lookahead|Iterator::Simple::Lookahead> object that will
366 return the result of C<getline> on each C<next> call.
367
368 =cut
369
370 #------------------------------------------------------------------------------
371 1 ▶sub line_stream {
Subroutine line_stream: called CC=1 SCAR=0.0
Pod documented
372 1 my($self) = @_;
373 1 ▶ return Iterator::Simple::Lookahead->new(sub {$self->getline});
Subroutine __ANON__: called CC=1 SCAR=0.0
374 }
375 #------------------------------------------------------------------------------
376
377 =head1 PREPROCESSING
378
379 The following preprocessor-like lines are processed:
380
381 %line N+M FILE
382
383 nasm-like line directive, telling that the next input line is
384 line N from file FILE,
385 followed by lines N+M, N+2*M, ...
386 This information is used to generate error messages.
387 Usefull to parse a file preprocessed by nasm.
388
389 #line N "FILE"
390
391 cpp-like line directive, telling that the next input line is
392 line N from file FILE,
393 followed by lines N+1, N+2, ...
394 This information is used to generate error messages.
395 Usefull to parse a file preprocessed by cpp.
396
397 %include 'FILE'
398 %include "FILE"
399 %include <FILE>
400 %include FILE
401 #include 'FILE'
402 #include "FILE"
403 #include <FILE>
404 #include FILE
405
406 nasm/cpp-like include directive, asking to insert the contents
407 of the given file in the input stream.
408
409 All the other preprocessor-like lines are ignored, i.e. lines starting with '#' or '%'.
410
411 =head1 AUTHOR
412
413 Paulo Custodio, C<< <pscust at cpan.org> >>
414
415 =head1 BUGS and FEEDBACK
416
417 Please report any bugs or feature requests through the web interface at
418 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Asm-Preproc>.
419
420 =head1 ACKNOWLEDGEMENTS
421
422 Inspired in the Netwide Assembler, L<http://www.nasm.us/>
423
424 =head1 LICENSE and COPYRIGHT
425
426 Copyright (c) 2010 Paulo Custodio.
427
428 This program is free software; you can redistribute it and/or modify it
429 under the terms of either: the GNU General Public License as published
430 by the Free Software Foundation; or the Artistic License.
431
432 See http://dev.perl.org/licenses/ for more information.
433
434 =cut
435
436 7 1; # End of Asm::Preproc
« Preproc/Lexer.pm ↑ Summary Preproc/Token.pm »