Skip to content

Commit

Permalink
RakuAST: fix rules for determing implied code blocks
Browse files Browse the repository at this point in the history
Previously, it was assumed that the indentation level of the
=begin pod would be taken as a base to determine an implied code
block.  But it turns out it is indentation level of the last seen
doc block.

=begin pod
    =head1 Foo

    This is *not* an implied code block
=end pod

This moves the indentation of abbreviated doc blocks into its
contents so that there is no difference between:

    =head1 Foo

and

    =begin head1
    Foo

with regards to contents.  This also eliminates the :spaces argument
to the RakuAST::Doc::Block.from-paragraphs method, as this is no longer
needed.
  • Loading branch information
lizmat committed May 2, 2023
1 parent ae7a219 commit c55455d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/Raku/Actions.nqp
Expand Up @@ -2770,7 +2770,7 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
}

$SEEN{$/.from} := RakuAST::Doc::Block.from-paragraphs:
:spaces(~$<spaces>), :$type, :$level, :$config, :@paragraphs;
:$type, :$level, :$config, :@paragraphs;
}

method doc-block:sym<for>($/) {
Expand All @@ -2787,7 +2787,7 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
nqp::push(@paragraphs,~$lines);
}
$*SEEN{$/.from} := RakuAST::Doc::Block.from-paragraphs:
:spaces(~$<spaces>), :$type, :$level, :$config, :@paragraphs;
:$type, :$level, :$config, :@paragraphs;
}

method doc-block:sym<abbreviated>($/) {
Expand All @@ -2800,12 +2800,12 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
my $level := extract-level($/);

my @paragraphs := nqp::list(
($<header> ?? ~$<header> !! "") ~ ($<lines> ?? ~$<lines> !! "")
($<header> ?? $<spaces> ~ $<header> !! "")
~ ($<lines> ?? ~$<lines> !! "")
);

$*SEEN{$/.from} := RakuAST::Doc::Block.from-paragraphs:
:spaces(~$<spaces>), :$type, :$level, :$config, :abbreviated,
:@paragraphs;
:$type, :$level, :$config, :abbreviated, :@paragraphs;
}
}

Expand Down
39 changes: 29 additions & 10 deletions src/core.c/RakuAST/Fixups.pm6
Expand Up @@ -91,6 +91,13 @@ augment class RakuAST::Doc::Markup {

augment class RakuAST::Doc::Paragraph {

# conceptual leading whitespace of first element
method leading-whitespace() {
nqp::istype((my $first := self.atoms.head),Str)
?? $first.leading-whitespace
!! ""
}

# easy integer checks
my int32 $A = nqp::ord('A');
my int32 $Z = nqp::ord('Z');
Expand Down Expand Up @@ -198,10 +205,14 @@ augment class RakuAST::Doc::Paragraph {

augment class RakuAST::Doc::Block {

# conceptual leading whitespace of first element
method leading-whitespace() {
self.paragraphs.head.leading-whitespace;
}

# create block with type/paragraph introspection
method from-paragraphs(:$spaces, :$type, :@paragraphs, *%_) {
method from-paragraphs(:$type, :@paragraphs, *%_) {
my $block := self.new(:$type, |%_);
my $offset := $spaces.chars;

# these need verbatim stuff
if $type eq 'comment' {
Expand All @@ -210,12 +221,21 @@ augment class RakuAST::Doc::Block {

# these need to be handled as code
elsif $type eq 'code' | 'input' | 'output' {
my int $offset = @paragraphs.head.leading-whitespace.chars;
$block.add-paragraph(@paragraphs.map(*.substr($offset)).join)
}

# potentially need introspection
elsif $type eq 'pod' | 'doc' {

my str $last-leading-ws;
my int $offset;
my sub set-leading-ws($leading-ws) {
$last-leading-ws = $leading-ws // "";
$offset = nqp::chars($last-leading-ws);
}

set-leading-ws(@paragraphs.head.leading-whitespace);
for @paragraphs -> $paragraph {

# need further introspection
Expand Down Expand Up @@ -245,15 +265,12 @@ augment class RakuAST::Doc::Block {
@codes = ();
}

my str $last-leading-ws =
$paragraph.leading-whitespace.substr($offset);
for $paragraph.lines(:!chomp) {

if .is-whitespace {
if @codes {
my int $ws-offset = $last-leading-ws.chars;
@codes.push((
.chars >= $ws-offset ?? .substr($ws-offset) !! ""
.chars >= $offset ?? .substr($offset) !! ""
).chomp);
}
else {
Expand All @@ -263,7 +280,7 @@ augment class RakuAST::Doc::Block {

# not just whitespace
else {
my $line := (.starts-with($spaces)
my $line := (.starts-with($last-leading-ws)
?? .substr($offset)
!! .trim-leading
).chomp;
Expand All @@ -273,10 +290,11 @@ augment class RakuAST::Doc::Block {
if $leading-ws ne $last-leading-ws {
add-codes if @codes;
$last-leading-ws = $leading-ws;
@codes = $line.substr($leading-ws.chars);
$offset = nqp::chars($leading-ws);
@codes = $line.substr($offset);
}
else {
@codes.push: $line.substr($leading-ws.chars);
@codes.push: $line.substr($offset);
}
}
else {
Expand All @@ -291,6 +309,7 @@ augment class RakuAST::Doc::Block {

# already introspected
else {
set-leading-ws($paragraph.leading-whitespace);
$block.add-paragraph($paragraph);
}
}
Expand All @@ -300,7 +319,7 @@ augment class RakuAST::Doc::Block {
else {
$block.add-paragraph(
nqp::istype($_,Str)
?? RakuAST::Doc::Paragraph.from-string(.substr($offset))
?? RakuAST::Doc::Paragraph.from-string($_)
!! $_
) for @paragraphs;
}
Expand Down

0 comments on commit c55455d

Please sign in to comment.