Skip to content

Commit

Permalink
RakuAST: add more choices for ::Doc::Block
Browse files Browse the repository at this point in the history
Rather than just being abbreviated or not, it can now also be marked
as a directive or as a "for".

- adds :for and :directive named arguments to .new
- adds .for and .directive methods
- updates .DEPARSE and .raku logic accordingly
  • Loading branch information
lizmat committed Jul 16, 2023
1 parent acd8cc4 commit 1c25d07
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
19 changes: 12 additions & 7 deletions src/Raku/ast/doc-block.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,29 @@ class RakuAST::Doc::Block
has str $.type; # the type (e.g. "doc", "head", "item", etc)
has int $.level; # the level (default "", or numeric 1..N)
has Hash $!config; # the config hash (e.g. :numbered, :allow<B>)
has Bool $.abbreviated; # bool: true if =item rather than =begin item
has List $!paragraphs; # the actual content
has int $.status; # 0 =begin, 1 =for, 2 =abbrev, 3 =directive
has int $!pod-index; # index in $=pod
has Mu $!resolved-config; # HLL-resolved config

method new(Str :$type!,
Int :$level,
Hash :$config,
List :$paragraphs,
Bool :$directive,
Bool :$for,
Bool :$abbreviated
) {
my $obj := nqp::create(self);
$obj.set-type($type);
$obj.set-level($level);
$obj.set-config($config);
$obj.set-abbreviated($abbreviated);
$obj.set-paragraphs($paragraphs);

my int $status :=
$directive ?? 3 !! $abbreviated ?? 2 !! $for ?? 1 !! 0;
nqp::bindattr_i($obj,RakuAST::Doc::Block,'$!status',$status);

if nqp::isconcrete($*LEGACY-POD-INDEX) {
nqp::bindattr_i($obj,RakuAST::Doc::Block,
'$!pod-index', $*LEGACY-POD-INDEX++);
Expand Down Expand Up @@ -113,11 +118,6 @@ class RakuAST::Doc::Block
}
method config() { self.IMPL-WRAP-MAP($!config) }

method set-abbreviated(Bool $value) {
nqp::bindattr(self, RakuAST::Doc::Block, '$!abbreviated',
$value ?? True !! False);
}

method set-paragraphs($paragraphs) {
nqp::bindattr(self, RakuAST::Doc::Block, '$!paragraphs',
$paragraphs
Expand All @@ -132,6 +132,11 @@ class RakuAST::Doc::Block
}
method paragraphs() { self.IMPL-WRAP-LIST($!paragraphs) }

method block() { $!status == 0 }
method for() { $!status == 1 }
method abbreviated() { $!status >= 2 }
method directive() { $!status == 3 }

method visit-children(Code $visitor) {
for $!paragraphs {
if nqp::istype($_,RakuAST::Doc::Block) {
Expand Down
16 changes: 15 additions & 1 deletion src/core.c/RakuAST/Deparse.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,14 @@ class RakuAST::Deparse {
elsif $type eq 'code' {
$paragraphs := self.deparse($_) with try $paragraphs.AST;
if $abbreviated {
self.hsyn('rakudoc-type', '=code') ~ "\n" ~ $paragraphs
'=' ~ self.hsyn('rakudoc-type', 'code') ~ "\n" ~ $paragraphs
}
elsif $ast.for {
'=for '
~ self.hsyn('rakudoc-type', $type)
~ $config
~ "\n"
~ self.hsyn('rakudoc-verbatim', $paragraphs.chomp)
}
else {
$type = ' ' ~ self.hsyn('rakudoc-type', $type);
Expand Down Expand Up @@ -783,6 +790,13 @@ class RakuAST::Deparse {
~ ($type eq 'table' ?? "\n" !! ' ')
~ $paragraphs
}
elsif $ast.for {
'=for '
~ self.hsyn('rakudoc-type', $type ~ $ast.level)
~ $config
~ "\n"
~ $paragraphs.chomp
}
else {
$type = ' ' ~ self.hsyn('rakudoc-type', $type ~ $ast.level);
self.hsyn('rakudoc-prefix', '=begin')
Expand Down
10 changes: 8 additions & 2 deletions src/core.c/RakuAST/Raku.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ augment class RakuAST::Node {

my $special := BEGIN nqp::hash(
'abbreviated', -> {
:abbreviated if self.abbreviated
:abbreviated if self.abbreviated && !self.directive
},
'adverbs', -> {
my $adverbs := nqp::decont(self.adverbs);
Expand Down Expand Up @@ -139,6 +139,9 @@ augment class RakuAST::Node {
my $config := nqp::decont(self.config);
:config($config.Hash) if $config
},
'directive', -> {
:directive if self.directive
},
'dwim-left', -> {
:dwim-left if self.dwim-left
},
Expand All @@ -149,6 +152,9 @@ augment class RakuAST::Node {
my $elsifs := nqp::decont(self.elsifs);
:$elsifs if $elsifs
},
'for', -> {
:for if self.for
},
'how', -> {
my $how := self.how;
as-class('how', $how.^name.subst("Perl6::"))
Expand Down Expand Up @@ -373,7 +379,7 @@ augment class RakuAST::Node {
#- Doc -------------------------------------------------------------------------

multi method raku(RakuAST::Doc::Block:D: --> Str:D) {
self!nameds: <type level abbreviated config paragraphs>
self!nameds: <type level directive for abbreviated config paragraphs>
}

multi method raku(RakuAST::Doc::Declarator:D: --> Str:D) {
Expand Down

0 comments on commit 1c25d07

Please sign in to comment.