From 9bad2b5892a80eb78b67a93044a6c92173099c2e Mon Sep 17 00:00:00 2001 From: Glenn Rice Date: Sat, 14 Sep 2024 05:13:17 -0500 Subject: [PATCH] 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. --- macros/core/PGML.pl | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/macros/core/PGML.pl b/macros/core/PGML.pl index 1f82ace88..bb90e389f 100644 --- a/macros/core/PGML.pl +++ b/macros/core/PGML.pl @@ -966,6 +966,21 @@ sub new { bless { type => $type, %$fields }, $class; } +sub stringifyHash { + my ($self, $hash) = @_; + return '{ ' . join( + ', ', + map { + "$_ => " + . ( + ref($hash->{$_}) eq 'HASH' + ? $self->stringifyHash($hash->{$_}) + : ("'" . $self->quote($hash->{$_}) . "'")) + } PGML::Sort(keys %$hash) + ) . ' }'; + +} + sub show { my $self = shift; my $indent = shift || ""; @@ -974,7 +989,9 @@ sub show { next if $id eq "stack"; if (ref($self->{$id}) eq 'ARRAY') { push(@strings, - $indent . $id . ": [" . join(',', map { "'" . $self->quote($_) . "'" } @{ $self->{$id} }) . "]"); + $indent . $id . ": [" . join(', ', map { "'" . $self->quote($_) . "'" } @{ $self->{$id} }) . "]"); + } elsif (ref($self->{$id}) eq 'HASH') { + push(@strings, $indent . $id . ": " . $self->stringifyHash($self->{$id})); } else { push(@strings, $indent . $id . ": '" . $self->quote($self->{$id}) . "'"); } @@ -1227,7 +1244,7 @@ sub show { my $self = shift; my $indent = shift; my @strings = ($self->SUPER::show($indent)); - push(@strings, $indent . "stack: ['" . join("','", map { $self->quote($_) } @{ $self->{stack} }) . "']"); + push(@strings, $indent . "stack: ['" . join("', '", map { $self->quote($_) } @{ $self->{stack} }) . "']"); return join("\n", @strings); }