Skip to content

Commit

Permalink
RakuAST: refactor margin handling for =alias
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Jul 16, 2023
1 parent a8a0416 commit c02c07e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
32 changes: 16 additions & 16 deletions src/Raku/Actions.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -2839,6 +2839,22 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
$*CU.replace-finish-content(~$<finish>);
}

method doc-block:sym<alias>($/) {
if $*FROM-SEEN{$/.from}++ {
return;
}

my @paragraphs := nqp::list(~$<first>);
if $<line> -> @lines {
for @lines {
nqp::push(@paragraphs,~$_);
}
}

$*SEEN{$/.from} := RakuAST::Doc::Block.from-alias:
:margin(~$<margin>), :lemma(~$<lemma>), :@paragraphs
}

method doc-block:sym<verbatim>($/) {
if $*FROM-SEEN{$/.from}++ {
return;
Expand Down Expand Up @@ -2904,22 +2920,6 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
:$type, :$level, :$config, :@paragraphs;
}

method doc-block:sym<alias>($/) {
if $*FROM-SEEN{$/.from}++ {
return;
}

my @paragraphs := nqp::list(~$<first>);
if $<line> -> @lines {
for @lines {
nqp::push(@paragraphs,~$_);
}
}

$*SEEN{$/.from} := RakuAST::Doc::Block.from-alias:
:lemma(~$<lemma>), :@paragraphs
}

method doc-block:sym<column-row>($/) {
if $*FROM-SEEN{$/.from}++ {
return;
Expand Down
27 changes: 14 additions & 13 deletions src/Raku/Grammar.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -3617,6 +3617,20 @@ if $*COMPILING_CORE_SETTING {
^^ \h* '=finish' <doc-newline> $<finish> = .*
}
# handle =alias
token doc-block:sym<alias> {
# save any leading whitespace from start of line
^^ $<margin>=[ \h* ]
# fetch lemma as first line
'=alias' \h+ $<lemma>=<.doc-identifier> \h+ $<first>=\N+
[\n $<margin> '=' \h+ $<line>=\N+]*
\n?
}
# handle =begin on verbatim blocks
token doc-block:sym<verbatim> {
Expand Down Expand Up @@ -3726,19 +3740,6 @@ if $*COMPILING_CORE_SETTING {
$<lines>=[[^^ $<spaces> \h* [ <-[=\n]> | '=' ** 2..* ] \N* \n? ]* \n*]
}
token doc-block:sym<alias> {
# save any leading whitespace from start of line
^^ $<spaces>=[ \h* ]
# fetch lemma as first line
'=alias' \h+ $<lemma>=<.doc-identifier> \h+ $<first>=\N+
[\n $<spaces> '=' \h+ $<line>=\N+]*
\n?
}
token doc-block:sym<column-row> {
# save any leading whitespace from start of line
Expand Down
7 changes: 5 additions & 2 deletions src/core.c/RakuAST/Deparse.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -709,12 +709,15 @@ class RakuAST::Deparse {

# special handling for alias directive
if $type eq 'alias' {
my str $margin = $ast.margin;
my ($lemma, $paragraph) = $ast.paragraphs;
$paragraph = self.deparse($paragraph)
unless nqp::istype($paragraph,Str);

return self.hsyn('rakudoc-type', '=alias')
~ " $lemma $paragraph.subst("\n", "\n= ", :global)\n"
return
$margin
~ self.hsyn('rakudoc-type', '=alias')
~ " $lemma $paragraph.subst("\n", "\n$margin= ", :global)\n"
}

# handle any config
Expand Down
7 changes: 4 additions & 3 deletions t/12-rakuast/doc-block.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,10 @@ CODE
}

subtest 'abbreviated alias in a statement list' => {
# =alias FOO bar␤42␤
# =alias FOO bar␤ = 137␤42␤
ast RakuAST::StatementList.new(
RakuAST::Doc::Block.new(
margin => ' ',
type => 'alias',
abbreviated => True,
paragraphs => (
Expand All @@ -189,8 +190,8 @@ subtest 'abbreviated alias in a statement list' => {
is-deeply $ast.EVAL, 42, 'do we get the final result';
for 'Str', $deparsed, 'Raku', $raku.EVAL.DEPARSE -> $type, $it {
is-deeply $it, q:to/CODE/, "$type: deparse";
=alias FOO bar
= 137
=alias FOO bar
= 137
42
CODE
}
Expand Down

0 comments on commit c02c07e

Please sign in to comment.