From c02c07e92b239d4c2eb4be2f3fc968777a3d7e19 Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Sun, 16 Jul 2023 15:02:47 +0200 Subject: [PATCH] RakuAST: refactor margin handling for =alias --- src/Raku/Actions.nqp | 32 ++++++++++++++++---------------- src/Raku/Grammar.nqp | 27 ++++++++++++++------------- src/core.c/RakuAST/Deparse.pm6 | 7 +++++-- t/12-rakuast/doc-block.rakutest | 7 ++++--- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/Raku/Actions.nqp b/src/Raku/Actions.nqp index 4679978441c..44a01507687 100644 --- a/src/Raku/Actions.nqp +++ b/src/Raku/Actions.nqp @@ -2839,6 +2839,22 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions { $*CU.replace-finish-content(~$); } + method doc-block:sym($/) { + if $*FROM-SEEN{$/.from}++ { + return; + } + + my @paragraphs := nqp::list(~$); + if $ -> @lines { + for @lines { + nqp::push(@paragraphs,~$_); + } + } + + $*SEEN{$/.from} := RakuAST::Doc::Block.from-alias: + :margin(~$), :lemma(~$), :@paragraphs + } + method doc-block:sym($/) { if $*FROM-SEEN{$/.from}++ { return; @@ -2904,22 +2920,6 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions { :$type, :$level, :$config, :@paragraphs; } - method doc-block:sym($/) { - if $*FROM-SEEN{$/.from}++ { - return; - } - - my @paragraphs := nqp::list(~$); - if $ -> @lines { - for @lines { - nqp::push(@paragraphs,~$_); - } - } - - $*SEEN{$/.from} := RakuAST::Doc::Block.from-alias: - :lemma(~$), :@paragraphs - } - method doc-block:sym($/) { if $*FROM-SEEN{$/.from}++ { return; diff --git a/src/Raku/Grammar.nqp b/src/Raku/Grammar.nqp index cdf85523cbe..9422794c156 100644 --- a/src/Raku/Grammar.nqp +++ b/src/Raku/Grammar.nqp @@ -3617,6 +3617,20 @@ if $*COMPILING_CORE_SETTING { ^^ \h* '=finish' $ = .* } + # handle =alias + token doc-block:sym { + + # save any leading whitespace from start of line + ^^ $=[ \h* ] + + # fetch lemma as first line + '=alias' \h+ $=<.doc-identifier> \h+ $=\N+ + + [\n $ '=' \h+ $=\N+]* + + \n? + } + # handle =begin on verbatim blocks token doc-block:sym { @@ -3726,19 +3740,6 @@ if $*COMPILING_CORE_SETTING { $=[[^^ $ \h* [ <-[=\n]> | '=' ** 2..* ] \N* \n? ]* \n*] } - token doc-block:sym { - - # save any leading whitespace from start of line - ^^ $=[ \h* ] - - # fetch lemma as first line - '=alias' \h+ $=<.doc-identifier> \h+ $=\N+ - - [\n $ '=' \h+ $=\N+]* - - \n? - } - token doc-block:sym { # save any leading whitespace from start of line diff --git a/src/core.c/RakuAST/Deparse.pm6 b/src/core.c/RakuAST/Deparse.pm6 index 9d1ebee2f98..78f622704bc 100644 --- a/src/core.c/RakuAST/Deparse.pm6 +++ b/src/core.c/RakuAST/Deparse.pm6 @@ -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 diff --git a/t/12-rakuast/doc-block.rakutest b/t/12-rakuast/doc-block.rakutest index b335085044e..1c36cce6b45 100644 --- a/t/12-rakuast/doc-block.rakutest +++ b/t/12-rakuast/doc-block.rakutest @@ -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 => ( @@ -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 }