Skip to content

Commit 201fc0c

Browse files
committed
ParseXS: refactor: add Node::global_cpp_line
(This commit is part of a series which will extend the AST parse tree from just representing individual XSUBs to representing the whole XS file.) This node stores a single "#foo BAR" C preprocessor line which appears within the XS part of an XS file, and which is in global (file) scope (as opposed to being within code in an XSUB or BOOT).
1 parent 1c98325 commit 201fc0c

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS.pm

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,20 +438,17 @@ sub process_file {
438438

439439
PARAGRAPH:
440440
while ($self->fetch_para()) {
441+
441442
# Process and emit any initial C-preprocessor lines and blank
442443
# lines. Also, keep track of #if/#else/#endif nesting, updating:
443444
# $self->{XS_parse_stack}
444445
# $self->{XS_parse_stack_top_if_idx}
445446
# $self->{bootcode_early}
446447
# $self->{bootcode_later}
447-
448448
while (@{ $self->{line} } && $self->{line}->[0] !~ /^[^\#]/) {
449-
my $ln = shift(@{ $self->{line} });
450-
print $ln, "\n";
451-
next unless $ln =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/;
452-
my $statement = $+;
453-
# update global tracking of #if/#else etc
454-
$self->analyze_preprocessor_statement($statement);
449+
my $node = ExtUtils::ParseXS::Node::global_cpp_line->new();
450+
$node->parse($self);
451+
$node->as_code($self);
455452
}
456453

457454
next PARAGRAPH unless @{ $self->{line} };

dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS/Node.pm

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,45 @@ sub as_code {
685685
}
686686

687687

688+
# ======================================================================
689+
690+
package ExtUtils::ParseXS::Node::global_cpp_line;
691+
692+
# AST node representing a single C-preprocessor line in file (global)
693+
# scope. (A "single" line can actually include embedded "\\\n"'s from line
694+
# continuations).
695+
696+
BEGIN { $build_subclass->(
697+
'cpp_line', # the text of the "# foo" CPP line
698+
)};
699+
700+
sub parse {
701+
my __PACKAGE__ $self = shift;
702+
my ExtUtils::ParseXS $pxs = shift;
703+
704+
$self->SUPER::parse($pxs); # set file/line_no
705+
706+
my $ln = $self->{cpp_line} = shift(@{$pxs->{line}});
707+
708+
# Update global tracking of *conditional* CPP directives;s
709+
# i.e. #if/#else etc
710+
711+
return 1 unless $ln =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/;
712+
my $directive = $+; # one of "if", "elsif", "else", "endif"
713+
$pxs->analyze_preprocessor_statement($directive);
714+
715+
1;
716+
}
717+
718+
719+
sub as_code {
720+
my __PACKAGE__ $self = shift;
721+
my ExtUtils::ParseXS $pxs = shift;
722+
723+
print $self->{cpp_line}, "\n";
724+
}
725+
726+
688727
# ======================================================================
689728

690729
package ExtUtils::ParseXS::Node::xsub;

0 commit comments

Comments
 (0)