Skip to content

Commit

Permalink
Json option to dump ast
Browse files Browse the repository at this point in the history
  • Loading branch information
tuqqu committed Aug 29, 2022
1 parent eaf80fd commit af06f00
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bin/go-parser
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function print_help(): void
FLAGS:
-d, --var-dump Dump AST using var_dump() or dump() if it is available
--node-dumper Dump AST using NodeDumper
--json Dump AST as json
--decl Parse single declaration (no package clause)
-h, --help Print help message
Expand Down Expand Up @@ -88,6 +89,11 @@ function parse_argv(array $argv): array
case '--var-dump':
$dumper = function_exists('dump') ? dump(...) : var_dump(...);
break;
case '--json':
$dumper = static function (AstNode $node): void {
fwrite(STDOUT, json_encode($node, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
};
break;
default:
if (str_starts_with($arg, '-')) {
exit_with_error(sprintf('unrecognised option "%s"', $arg));
Expand Down Expand Up @@ -141,5 +147,6 @@ function main(array $argv): never
$errs = true;
}
}

exit((int) $errs);
}

0 comments on commit af06f00

Please sign in to comment.