#!/usr/bin/env perl6
use v6.d;

use Grammar::TokenProcessing;
use JSON::Fast;

my %*SUB-MAIN-OPTS = :named-anywhere;

#| Get tokens from token lines in grammar roles.
multi MAIN(
        Str $inputFileName where *.IO.f, #= Input file name.
        Str :$format = 'json'            #= Format of the output. One of 'hash', 'raku', or 'json'.
           ) {
    my $res = get-tokens-hash($inputFileName);
    given $format {
        when 'raku' { say $res.raku; }
        when 'hash' { .say for $res; }
        when 'json' { say to-json($res); }
    }
}

multi MAIN(
        *@args,                         #= Input file names.
        Str :$format = 'json',          #= Format of the output. One of 'hash', 'raku', or 'json'.
        Bool :$per-file = False         #= Should the tokens be printed out per file or not?
           ) {
    if [and] @args.map({ $_.IO.e }) {
        if $per-file {
            for @args -> $f {
                say '-' x 30;
                say $f;
                MAIN($f, :$format);
                say '-' x 30;
            }
        } else {

            my @res = @args>>.Str.map({ get-tokens-hash($_).Array });

            given $format {
                when 'raku' { say @res.raku; }
                when 'hash' { .say for @res; }
                when 'json' { say to-json(@res); }
            }
        }
    } else {
        note "Not all of the arguments are file names."
    }
}