json-format.pl: further fixes

- skip_ws() -> gettoken() as it doesn't always skip whitespace
 - add a newline after each top-level token
 - since we've become a streaming parser, GNU parallel may not apply,
   so remove the comment at the top of the file
This commit is contained in:
Přemysl Eric Janouch 2017-01-26 01:32:16 +01:00
parent cd4107b782
commit a38ad4d64d
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 6 additions and 9 deletions

View File

@ -1,6 +1,4 @@
#!/usr/bin/env perl #!/usr/bin/env perl
# To speed up processing of large files, GNU parallel can be used:
# $ parallel --pipe -k json-format.pl INPUT
use strict; use strict;
use warnings; use warnings;
use Term::ANSIColor; use Term::ANSIColor;
@ -75,7 +73,7 @@ sub nexttoken ($) {
return 'ERROR', $text; return 'ERROR', $text;
} }
sub skip_ws ($) { sub gettoken ($) {
my $json = shift; my $json = shift;
while (my ($token, $text) = nexttoken $json) { while (my ($token, $text) = nexttoken $json) {
next if !$keep_ws && $token eq 'WS'; next if !$keep_ws && $token eq 'WS';
@ -85,8 +83,7 @@ sub skip_ws ($) {
} }
sub printindent () { sub printindent () {
print "\n"; print "\n", ' ' x $indent;
print ' ' x $indent;
} }
sub do_value ($$$); sub do_value ($$$);
@ -94,7 +91,7 @@ sub do_object ($) {
my $json = shift; my $json = shift;
my $in_field_name = 1; my $in_field_name = 1;
my $first = 1; my $first = 1;
while (my ($token, $text) = skip_ws $json) { while (my ($token, $text) = gettoken $json) {
if ($token eq 'COLON') { if ($token eq 'COLON') {
$in_field_name = 0; $in_field_name = 0;
} elsif ($token eq 'COMMA') { } elsif ($token eq 'COMMA') {
@ -117,7 +114,7 @@ sub do_object ($) {
sub do_array ($) { sub do_array ($) {
my $json = shift; my $json = shift;
my $first = 1; my $first = 1;
while (my ($token, $text) = skip_ws $json) { while (my ($token, $text) = gettoken $json) {
if ($token eq 'RBRACKET') { if ($token eq 'RBRACKET') {
$indent--; $indent--;
printindent unless $keep_ws; printindent unless $keep_ws;
@ -151,7 +148,7 @@ sub do_value ($$$) {
} }
my @buffer; my @buffer;
while (my ($token, $text) = skip_ws \@buffer) { while (my ($token, $text) = gettoken \@buffer) {
do_value $token, $text, \@buffer; do_value $token, $text, \@buffer;
}
print "\n" unless $keep_ws; print "\n" unless $keep_ws;
}