Skip to content

Commit d3250bb

Browse files
committed
Improvements to the debugging show method of PGML.
This was something that was useful for me while I was working on writing the CodeMirror 6 PGML parser. It just formats the things in the PGML stack that are hashes in a nicer way, actually showing the contents instead of something like `HASH(0x5d3919403d78)` as it previously did. This does not affect normal PGML usage in problems at all.
1 parent ed18e79 commit d3250bb

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

macros/core/PGML.pl

+19-2
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,21 @@ sub new {
966966
bless { type => $type, %$fields }, $class;
967967
}
968968

969+
sub stringifyHash {
970+
my ($self, $hash) = @_;
971+
return '{ ' . join(
972+
', ',
973+
map {
974+
"$_ => "
975+
. (
976+
ref($hash->{$_}) eq 'HASH'
977+
? $self->stringifyHash($hash->{$_})
978+
: ("'" . $self->quote($hash->{$_}) . "'"))
979+
} PGML::Sort(keys %$hash)
980+
) . ' }';
981+
982+
}
983+
969984
sub show {
970985
my $self = shift;
971986
my $indent = shift || "";
@@ -974,7 +989,9 @@ sub show {
974989
next if $id eq "stack";
975990
if (ref($self->{$id}) eq 'ARRAY') {
976991
push(@strings,
977-
$indent . $id . ": [" . join(',', map { "'" . $self->quote($_) . "'" } @{ $self->{$id} }) . "]");
992+
$indent . $id . ": [" . join(', ', map { "'" . $self->quote($_) . "'" } @{ $self->{$id} }) . "]");
993+
} elsif (ref($self->{$id}) eq 'HASH') {
994+
push(@strings, $indent . $id . ": " . $self->stringifyHash($self->{$id}));
978995
} else {
979996
push(@strings, $indent . $id . ": '" . $self->quote($self->{$id}) . "'");
980997
}
@@ -1227,7 +1244,7 @@ sub show {
12271244
my $self = shift;
12281245
my $indent = shift;
12291246
my @strings = ($self->SUPER::show($indent));
1230-
push(@strings, $indent . "stack: ['" . join("','", map { $self->quote($_) } @{ $self->{stack} }) . "']");
1247+
push(@strings, $indent . "stack: ['" . join("', '", map { $self->quote($_) } @{ $self->{stack} }) . "']");
12311248
return join("\n", @strings);
12321249
}
12331250

0 commit comments

Comments
 (0)