Skip to content

Instantly share code, notes, and snippets.

@gerdr
Created October 4, 2013 19:55
Show Gist options
  • Save gerdr/6831810 to your computer and use it in GitHub Desktop.
Save gerdr/6831810 to your computer and use it in GitHub Desktop.
use v6;
sub pretty($perl, :$indent = ' ') is export {
my ($pc, $depth, $string, $esc) = '', 0, False, False;
join '', gather for $perl.comb -> $cc {
NEXT $pc = $cc;
unless $string {
given \($pc, $cc) {
when :('(', ')') {}
when :($, ')') {
take "\n", $indent x --$depth;
}
when :('(', $) {
take "\n", $indent x ++$depth
}
when :('[', ']') {}
when :($, ']') {
take "\n", $indent x --$depth;
}
when :('[', $) {
take "\n", $indent x ++$depth
}
when :('{', '}') {}
when :($, '}') {
take "\n", $indent x --$depth;
}
when :('{', $) {
take "\n", $indent x ++$depth
}
when :(',', ' ') {
take "\n", $indent x $depth;
next;
}
}
}
$esc = !$esc && $pc eq '\\';
$string ^^= $cc eq '"' unless $esc;
take $cc;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment