-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPP.pm
424 lines (277 loc) · 8.99 KB
/
CPP.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package Text::CPP;
use strict;
eval { require warnings; };
use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS %LANGUAGE);
use Exporter;
require DynaLoader;
$VERSION = "0.15";
@ISA = qw(Exporter DynaLoader);
@EXPORT_OK = ();
%EXPORT_TAGS = (
all => \@EXPORT_OK,
);
bootstrap Text::CPP;
# Some of these choices are rather arbitrary
%LANGUAGE = (
C => CLK_GNUC99(),
C89 => CLK_STDC89(),
C94 => CLK_STDC94(),
C99 => CLK_STDC99(),
GNUC => CLK_GNUC99(),
GNUC89 => CLK_GNUC89(),
GNUC99 => CLK_GNUC99(),
STDC => CLK_STDC99(),
STDC89 => CLK_STDC89(),
STDC94 => CLK_STDC94(),
STDC99 => CLK_STDC99(),
'C++' => CLK_GNUCXX(),
'C++98' => CLK_CXX98(),
'GNUC++' => CLK_GNUCXX(),
ASM => CLK_ASM(),
ASSEMBLER => CLK_ASM(),
ASSEMBLY => CLK_ASM(),
CLK_GNUC89() => CLK_GNUC89(),
CLK_GNUC99() => CLK_GNUC99(),
CLK_STDC89() => CLK_STDC89(),
CLK_STDC94() => CLK_STDC94(),
CLK_STDC99() => CLK_STDC99(),
CLK_GNUCXX() => CLK_GNUCXX(),
CLK_CXX98() => CLK_CXX98(),
CLK_ASM() => CLK_ASM(),
);
sub new {
my $class = shift;
my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ };
if (exists $self->{Language}) {
my $language = $LANGUAGE{$self->{Language}};
unless ($language) {
my @languages = keys %LANGUAGE;
push(@languages, grep { /^CLK_/ } @EXPORT_OK);
die "Invalid language $self->{Language}. " .
"Try one of @languages";
}
$self->{Language} = $language;
}
my $language = (exists $self->{Language})
? $self->{Language}
: CLK_GNUC99();
my $builtins = (exists $self->{Builtins})
? $self->{Builtins}
: { };
my $options = (exists $self->{Options})
? $self->{Options}
: $self; # Allow for laziness
my $callbacks = (exists $self->{Callbacks})
? $self->{Callbacks}
: { };
if (exists $options->{Define}) {
# Convert defined macros into a canonical form.
if (ref($options->{Define}) eq 'HASH') {
my %opts = %{ $options->{Define} };
$options->{Define} = [ map { "$_=$opts{$_}" } keys %opts ];
}
}
foreach (qw(IncludePath -I
SystemIncludePath -isystem
AfterIncludePath -idirafter
Include -include
IncludeMacros -imacros)) {
# We promote some strings to arrays for convenience
if (exists $options->{$_} && (ref($options->{$_}) ne 'ARRAY')) {
$options->{$_} = [ $options->{$_} ];
}
}
return Text::CPP::_create($class, $language,
$builtins,
$options,
$callbacks);
}
=head1 NAME
Text::CPP - A full C Preprocessor in XS
=head1 SYNOPSIS
use Text::CPP;
my $reader = new Text::CPP(
Language => CLK_GNUC99,
Options => {
...
},
Builtins => {
foo => 'this',
bar => 'that',
...
},
Callbacks => {
...
},
);
$reader->read("file.c");
while (my $token = $reader->token) {
print "Token: $token\n";
}
$reader->data->{MyKey} = $MyData;
=head1 DESCRIPTION
A fast C preprocessor in XS. This does not require an external C
preprocessor, and will not fork() or exec() any external process.
=head1 USAGE
The following methods have been implemented, allowing the use of
this module as a pure C preprocessor, or as a lexer for a C, C++
or Assembler-like language.
=over 4
=item new Text::CPP(...)
Takes a hash or hashref with the following possible keys:
=over 4
=item Language
Defines the source language to preprocess and/or tokenise. It may
be any of:
=over 4
=item CLK_GNUC89 - GNU C89
=item CLK_GNUC99 - GNU C99
=item CLK_STDC89 - Standard C89
=item CLK_STDC94 - Standard C94
=item CLK_STDC99 - Standard C99
=item CLK_GNUCXX - GNU C++
=item CLK_CXX98 - Standard C++ 98
=item CLK_ASM - Assembler
=back
=item Options
A hashref of options for the preprocessor. Valid entries are given
with alternative forms (from GNU cpp) in brackets.
=over 4
=item Define (-D): array of strings or hash
Strings should be of the form NAME=VALUE.
=item Undef (-U): array of strings
=item DiscardComments (-C): boolean
=item DiscardCommentsInMacroExp (-CC): boolean
=item PrintIncludeNames (-H): boolean
=item NoLineCommands (-P): boolean
=item WarnComments (-Wcomment -Wcomments): boolean
=item WarnDeprecated (-Wdeprecated): boolean
=item WarningsAreErrors (-Werror): boolean
=item WarnImport (-Wimport): boolean
=item WarnMultichar (-Wmultichar): boolean
=item WarnSystemHeaders (-Wsystem-headers): boolean
Ignore errors in system header files.
=item WarnTraditional (-Wtraditional): boolean
=item WarnTrigraphs (-Wtrigraphs): boolean
=item WarnUnusedMacros (-Wunused-macros): boolean
=item Pedantic (-pedantic): boolean
=item PedanticErrors (-pedantic-errors): boolean
Implies, and overrides, Pedantic.
=item Remap (-remap): boolean
Deal with some brokennesses of MSDOS. Untested.
=item Trigraphs (-trigraphs): boolean
=item Traditional (-traditional): boolean
=item NoWarnings (-w): boolean
=item IncludePrefix (-iprefix): string
=item SystemRoot (-isysroot): string
=item Include (-include): array of strings
Include the specified files before reading the main file to be
processed.
=item IncludeMacros (-imacros): array of strings
Include the specified files before reading the main file to be
processed. Output from preprocessing these files is discarded. Files
specified by IncludeMacros are processed before files specified
by Include.
=item IncludePath (-I): array of strings
This include path is searched first.
=item SystemIncludePath (-isystem): array of strings
Specify the standard system include path, searched second.
=item AfterIncludePath (-idirafter): array of strings
This include path is searched after the system include path.
=back
=item Builtins
A hashref of predefined macros. The values must be strings or integers.
Macros in this hash will be defined before preprocessing starts. These
correspond to true "builtin" macros. You should probably prefer to
use the 'Define' option.
=item Callbacks
The preprocessor makes callbacks when certain events occur.
=over 4
=item LineChange
Called when the line in the source file changes. Arguments are (XXX see
BUGS)
=item Ident
=item Define
=item Undef
=back
=back
=item $text = $reader->token
=item ($text, $type, $flags) = $reader->token
Return the next available preprocessed token. Some tokens are not
stringifiable. These include tokens of type CPP_MACRO_ARG, CPP_PADDING
and CPP_EOF. Text::CPP returns a dummy string in the 'text' field
for these tokens. Tokens of type CPP_EOF should never actually be
returned. Instead, an empty list is returned in list context, or
undef in scalar context.
=item @tokens = $reader->tokens
Preprocess and return a list of tokens. This is approximately
equivalent to:
push(@tokens, $_) while ($_ = $reader->token);
=item $reader->type($type)
Return a human readable name for a token type, as returned by
$reader->token.
=item $reader->data
Returns a hashref in which user data may be stored by subclasses.
This hashref is created with a new Text::CPP object, and is ignored
for all functional purposes. The user may do with it as he wishes.
=item $reader->errors
In scalar context, returns the fatal error count. In list context,
returns a list of warnings and errors encountered by the preprocessor.
Thus scalar(@errors) >= $errors, since @errors will also contain
the warnings.
=back
=head1 BUGS
Fewer than in the last release, and hopefully not very many.
This documentation is probably incomplete. There are many important
functions in the source which are not yet documented.
It is not possible to instantiate multiple Text::CPP objects, since
the underlying library uses many global variables.
C99 may not implement variadic macros correctly according to the ISO
standard. I must check this. If anyone knows, please tell me.
The -M option is not yet handled.
Callbacks are not yet fully implemented.
=head1 CAVEATS
Memory for hash tables, token names, etc is only freed when the reader
is destroyed.
Assertions are a deprecated feature, and thus the -A flag is not
supported.
It does not seem necessary to support the following flags:
=item -x
=item -iwithprefix
=item -iwithprefixbefore
=item -fpreprocessed
The following options may be requested, but seem boring, so are not
currently supported.
=item -std
=item -ansi
=item -foperator-names
=item -fshow-column
=item -ftabstop
=head1 TODO
=item Dependency output
=item Lots more tests
=item Remaining callbacks
=item Virtual file support
=item Multiplicity
=head1 SUPPORT
Mail the author at <[email protected]>
=head1 AUTHOR
Shevek
CPAN ID: SHEVEK
http://www.anarres.org/projects/
=head1 COPYRIGHT
Copyright (c) 2002 Shevek. All rights reserved.
This program is free software; but parts of it have been borrowed
from, or based on, parts of the GNU C Compiler version 3.3.2. You may
therefore redistribute and/or modify this code under the terms of the
GNU GENERAL PUBLIC LICENSE. I am unable to release this code under the
usual Perl license because that license includes the Artistic License,
and I cannot rerelease GPL code under the Artistic License. Sorry.
The full text of the license can be found in the
COPYING file included with this module.
=head1 SEE ALSO
perl(1).
=cut
1;
__END__