/usr/share/perl5/MDOM/Document/Gmake.pm is in libmakefile-dom-perl 0.008-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | package MDOM::Document::Gmake;
use strict;
use warnings;
#use Smart::Comments;
#use Smart::Comments '###', '####';
use Text::Balanced qw( gen_extract_tagged );
use Makefile::DOM;
#use Data::Dump::Streamer;
use base 'MDOM::Node';
use List::MoreUtils qw( before all any );
use List::Util qw( first );
my %_map;
BEGIN {
%_map = (
COMMENT => 1, # context for parsing multi-line comments
COMMAND => 2, # context for parsing multi-line commands
RULE => 3, # context for parsing rules
VOID => 4, # void context
UNKNOWN => 5, # context for parsing unexpected constructs
);
}
use constant \%_map;
my %_rev_map = reverse %_map;
my @keywords = qw(
vpath include sinclude
ifdef ifndef else endif
define endef export unexport
);
my $extract_interp_1 = gen_extract_tagged('\$[(]', '[)]', '');
my $extract_interp_2 = gen_extract_tagged('\$[{]', '[}]', '');
sub extract_interp {
my ($res) = $extract_interp_1->($_[0]);
if (!$res) {
($res) = $extract_interp_2->($_[0]);
}
$res;
}
my ($context, $saved_context);
sub new {
my $class = ref $_[0] ? ref shift : shift;
my $input = shift;
return undef if !defined $input;
my $in;
if (ref $input) {
open $in, '<', $input or die;
} else {
open $in, $input or
die "Can't open $input for reading: $!";
}
my $self = $class->SUPER::new;
$self->_tokenize($in);
$self;
}
sub _tokenize {
my ($self, $fh) = @_;
$context = VOID;
my @tokens;
while (<$fh>) {
### Tokenizing : $_
### ...with context : $_rev_map{$context}
s/\r\n/\n/g;
$_ .= "\n" if !/\n$/s;
if ($context == VOID || $context == RULE) {
if ($context == VOID && s/(?x) ^ (\t\s*) (?= \# ) //) {
### Found comment in VOID context...
@tokens = (
MDOM::Token::Whitespace->new($1),
_tokenize_comment($_)
);
if ($tokens[-1]->isa('MDOM::Token::Continuation')) {
### Switching context to COMMENT...
$saved_context = $context;
$context = COMMENT;
$tokens[-2]->add_content("\\\n");
pop @tokens;
}
$self->__add_elements( @tokens );
}
elsif ($context == RULE and s/^\t//) {
### Found a command in RULE context...
@tokens = _tokenize_command($_);
#warn "*@tokens*";
### Tokens for the command: @tokens
unshift @tokens, MDOM::Token::Separator->new("\t");
if ($tokens[-1]->isa('MDOM::Token::Continuation')) {
### Switching context to COMMAND...
$saved_context = $context;
$context = COMMAND;
pop @tokens;
if ($tokens[-1]->class =~ /Bare$/) {
$tokens[-1]->add_content("\\\n");
} else {
push @tokens, MDOM::Token::Bare->new("\\\n");
}
}
my $cmd = MDOM::Command->new;
$cmd->__add_elements(@tokens);
$self->__add_element($cmd);
### command (post): $cmd
next;
} else {
@tokens = _tokenize_normal($_);
if (@tokens >= 2 &&
$tokens[-1]->isa('MDOM::Token::Continuation') &&
$tokens[-2]->isa('MDOM::Token::Comment')) {
### Found a trailing comment...
### Switching conext to COMMENT...
$saved_context = $context;
$context = COMMENT;
$tokens[-2]->add_content("\\\n");
pop @tokens;
$self->__add_elements( _parse_normal(@tokens) );
} elsif ($tokens[-1]->isa('MDOM::Token::Continuation')) {
### Found a line continuation...
### Switching context to UNKNOWN...
$saved_context = $context;
$context = UNKNOWN;
} else {
### Parsing it as a normal line...
$self->__add_elements( _parse_normal(@tokens) );
}
}
} elsif ($context == COMMENT) {
@tokens = _tokenize_comment($_);
if ($tokens[-1]->isa('MDOM::Token::Continuation')) {
### Slurping one more continued comment line...
$tokens[-2]->add_content("\\\n");
pop @tokens;
$self->last_token->add_content(join '', @tokens);
} else {
### Completing comment slurping...
### Switching back to context: _state_str($saved_context)
$context = $saved_context;
my $last = pop @tokens;
$self->last_token->add_content(join '', @tokens);
$self->last_token->parent->__add_element($last);
}
} elsif ($context == COMMAND) {
@tokens = _tokenize_command($_);
### more tokens for the cmd: @tokens
if ($tokens[-1]->isa('MDOM::Token::Continuation')) {
### Slurping one more continued command line...
$tokens[-2]->add_content("\\\n");
pop @tokens;
for my $token (@tokens) {
if ($token->class =~ /Interpolation/ or
$self->last_token->class =~ /Interpolation/) {
$self->last_token->parent->__add_element($token);
} else {
$self->last_token->add_content($token);
}
}
} else {
### Completing command slurping: @tokens
### Switching back to context: _state_str($saved_context)
$context = RULE;
my $last = pop @tokens;
### last_token: $self->last_token
for my $token (@tokens) {
if ($token->class =~ /Interpolation/ or
$self->last_token->class =~ /Interpolation/) {
$self->last_token->parent->__add_element($token);
} else {
$self->last_token->add_content($token);
}
}
$self->last_token->parent->__add_element($last);
}
} elsif ($context == UNKNOWN) {
push @tokens, _tokenize_normal($_);
if (@tokens >= 2 && $tokens[-1]->isa('MDOM::Token::Continuation') &&
$tokens[-2]->isa('MDOM::Token::Comment')) {
$context = COMMENT;
$tokens[-2]->add_content("\\\n");
pop @tokens;
$self->__add_elements( _parse_normal(@tokens) );
} elsif ($tokens[-1]->isa('MDOM::Token::Continuation')) {
### Do nothing here...stay in the UNKNOWN context...
} else {
$self->__add_elements( _parse_normal(@tokens) );
$context = $saved_context;
}
} else {
die "Unkown state: $context";
}
}
if ($context != RULE && $context != VOID) {
warn "unexpected end of input at line $.";
}
}
sub _tokenize_normal {
local $_ = shift;
my @tokens;
my $pending_token = '';
my $next_token;
### TOKENIZING: $_
while (1) {
# "token = $pending_token";
#warn pos;
#warn '@tokens = ', _dump_tokens2(@tokens);
if (/(?x) \G [\s\n]+ /gc) {
$next_token = MDOM::Token::Whitespace->new($&);
#push @tokens, $next_token;
}
elsif (/(?x) \G (?: :: | := | \?= | \+= | [=:;] )/gc) {
$next_token = MDOM::Token::Separator->new($&);
}
elsif (/(?x) \G \| /gc) {
# XXX This should be a separator...
$next_token = MDOM::Token::Bare->new($&);
}
elsif (my $res = extract_interp($_)) {
$next_token = MDOM::Token::Interpolation->new($res);
}
elsif (/(?x) \G \$. /gc) {
$next_token = MDOM::Token::Interpolation->new($&);
}
elsif (/(?x) \G \\ ([\#\\\n:]) /gcs) {
my $c = $1;
if ($c eq "\n") {
push @tokens, MDOM::Token::Bare->new($pending_token)
if $pending_token ne '';
push @tokens, MDOM::Token::Continuation->new("\\\n");
return @tokens;
} else {
$pending_token .= "\\$c";
}
}
elsif (/(?x) \G (\# [^\n]*) \\ \n/sgc) {
my $s = $1;
push @tokens, MDOM::Token::Bare->new($pending_token) if $pending_token ne '';
push @tokens, MDOM::Token::Comment->new($s);
push @tokens, MDOM::Token::Continuation->new("\\\n");
return @tokens;
} elsif (/(?x) \G \# [^\n]* /gc) {
$next_token = MDOM::Token::Comment->new($&);
} elsif (/(?x) \G . /gc) {
$pending_token .= $&;
} else {
last;
}
if ($next_token) {
if ($pending_token ne '') {
push @tokens, MDOM::Token::Bare->new($pending_token);
$pending_token = '';
}
push @tokens, $next_token;
$next_token = undef;
}
}
### parse_normal result: @tokens
@tokens;
}
sub _tokenize_command {
my $s = shift;
my @tokens;
my $pending_token = '';
my $next_token;
my $strlen = length $s;
while ($s =~ /(?x) \G (\s*) ([\@+\-]) /gc) {
my ($whitespace, $modifier) = ($1, $2);
if ($whitespace) {
push @tokens, MDOM::Token::Whitespace->new($whitespace);
}
push @tokens, MDOM::Token::Modifier->new($modifier);
}
while (1) {
my $last = 0;
if ($s =~ /(?x) \G \n /gc) {
$next_token = MDOM::Token::Whitespace->new("\n");
#push @tokens, $next_token;
}
elsif (my $res = extract_interp($s)) {
$next_token = MDOM::Token::Interpolation->new($res);
}
elsif ($s =~ /(?x) \G \$. /gc) {
$next_token = MDOM::Token::Interpolation->new($&);
}
elsif ($s =~ /(?x) \G \\ ([\#\\\n:]) /gcs) {
my $c = $1;
if ($c eq "\n" && pos $s == $strlen) {
$next_token = MDOM::Token::Continuation->new("\\\n");
} else {
$pending_token .= "\\$c";
}
}
elsif ($s =~ /(?x) \G . /gc) {
$pending_token .= $&;
} else {
$last = 1;
}
if ($next_token) {
if ($pending_token) {
push @tokens, MDOM::Token::Bare->new($pending_token);
$pending_token = '';
}
push @tokens, $next_token;
$next_token = undef;
}
last if $last;
}
if ($pending_token) {
push @tokens, MDOM::Token::Bare->new($pending_token);
$pending_token = '';
}
@tokens;
}
sub _tokenize_comment {
local $_ = shift;
my @tokens;
my $pending_token = '';
while (1) {
if (/(?x) \G \n /gc) {
push @tokens, MDOM::Token::Comment->new($pending_token) if $pending_token ne '';
push @tokens, MDOM::Token::Whitespace->new("\n");
return @tokens;
#push @tokens, $next_token;
}
elsif (/(?x) \G \\ ([\\\n#:]) /gcs) {
my $c = $1;
if ($c eq "\n") {
push @tokens, MDOM::Token::Comment->new($pending_token) if $pending_token ne '';
push @tokens, MDOM::Token::Continuation->new("\\\n");
return @tokens;
} else {
$pending_token .= "\\$c";
}
}
elsif (/(?x) \G . /gc) {
$pending_token .= $&;
}
else {
last;
}
}
@tokens;
}
sub _parse_normal {
my @tokens = @_;
### fed to _parse_normal: @tokens
my @sep = grep { $_->isa('MDOM::Token::Separator') } @tokens;
#### Separators: @sep
if (@tokens == 1) {
return $tokens[0];
}
# filter out significant tokens:
my ($fst, $snd) = grep { $_->significant } @tokens;
my $is_directive;
if ($fst) {
if ($fst eq '-include') {
$fst->set_content('include');
unshift @tokens, MDOM::Token::Modifier->new('-');
$is_directive = 1;
}
elsif ($fst eq 'override' && $snd && $snd eq 'define' ||
_is_keyword($fst)) {
$is_directive = 1;
}
if ($is_directive) {
##### Found directives...
my $node = MDOM::Directive->new;
$node->__add_elements(@tokens);
return $node;
}
}
if (@sep >= 2 && $sep[0] =~ /^::?$/ and $sep[1] eq ';') {
#### Found simple rule with inlined command...
my $rule = MDOM::Rule::Simple->new;
my @t = before { $_ eq ';' } @tokens;
$rule->__add_elements(@t);
splice @tokens, 0, scalar(@t);
my @prefix = shift @tokens;
if ($tokens[0] && $tokens[0]->isa('MDOM::Token::Whitespace')) {
push @prefix, shift @tokens;
}
@tokens = (@prefix, _tokenize_command(join '', @tokens));
if ($tokens[-1]->isa('MDOM::Token::Continuation')) {
$saved_context = $context;
$context = COMMAND;
}
my $cmd = MDOM::Command->new;
$cmd->__add_elements(@tokens);
$rule->__add_elements($cmd);
$saved_context = RULE;
$context = RULE if $context == VOID;
return $rule;
}
elsif (@sep >= 2 && $sep[0] eq ':' and $sep[1] =~ /^::?$/) {
#### Found static pattern rule...
my $rule = MDOM::Rule::StaticPattern->new;
my @t = before { $_ eq ';' } @tokens;
$rule->__add_elements(@t);
splice @tokens, 0, scalar(@t);
if (@tokens) {
my @prefix = shift @tokens;
if ($tokens[0] && $tokens[0]->isa('MDOM::Token::Whitespace')) {
push @prefix, shift @tokens;
}
@tokens = (@prefix, _tokenize_command(join '', @tokens));
if ($tokens[-1]->isa('MDOM::Token::Continuation')) {
$saved_context = $context;
$context = COMMAND;
}
my $cmd = MDOM::Command->new;
$cmd->__add_elements(@tokens);
$rule->__add_elements($cmd);
}
$saved_context = RULE;
$context = RULE if $context == VOID;
return $rule;
}
elsif (@sep == 1 && $sep[0] =~ /^::?$/) {
#### Found simple rule without inlined command...
my $rule = MDOM::Rule::Simple->new;
$rule->__add_elements(@tokens);
$saved_context = RULE;
$context = RULE if $context == VOID;
return $rule;
}
elsif (@sep && $sep[0] =~ /(?x) ^ (?: = | := | \+= | \?= ) $/) {
my $assign = MDOM::Assignment->new;
### Assignment tokens: @tokens
$assign->__add_elements(@tokens);
$saved_context = VOID;
$context = VOID if $context == RULE;
return $assign;
}
elsif (all {
$_->isa('MDOM::Token::Comment') ||
$_->isa('MDOM::Token::Whitespace')
} @tokens) {
@tokens;
}
else {
#### Found unkown token sequence: @tokens
@tokens = _tokenize_command(join '', @tokens);
my $node = MDOM::Unknown->new;
$node->__add_elements(@tokens);
$node;
}
}
sub _dump_tokens {
my @tokens = map { $_->clone } @_;
warn "??? ", (join ' ',
map { s/\\/\\\\/g; s/\n/\\n/g; s/\t/\\t/g; "[$_]" } @tokens
), "\n";
}
sub _state_str {
$_rev_map{$saved_context}
}
sub _is_keyword {
any { $_[0] eq $_ } @keywords;
}
1;
__END__
=encoding utf-8
=head1 NAME
MDOM::Gmake - Represents a GNU makefile for Makefile::DOM
=head1 DESCRIPTION
Represents a GNU Makefile.
=head1 METHODS
=head2 C<extract_interp>
Extract interpolated make variables like C<$(foo)> and C<${bar}>.
=head1 AUTHOR
Yichun "agentzh" Zhang (章亦春) E<lt>agentzh@gmail.comE<gt>
=head1 COPYRIGHT
Copyright 2006-2014 by Yichun "agentzh" Zhang (章亦春).
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 SEE ALSO
L<MDOM::Document>, L<MDOM::Document::Gmake>, L<PPI>, L<Makefile::Parser::GmakeDB>, L<makesimple>.
|