| 222 | | my @gh = @{$ctx->grammar_history()}; |
|---|
| 223 | | my @th = @{$ctx->token_history()}; |
|---|
| 224 | | my @ath = @{$ctx->all_tokens()}; |
|---|
| 225 | | my $grammar = $gh[$#gh]; |
|---|
| 226 | | my @candidates; |
|---|
| 227 | | push(@th, "") if $action =~ /More input expected/; |
|---|
| 228 | | my $prefix = ' ' x length("@th[0 .. $#th - 1]") . (@th <= @ath ? ' ' : ''); |
|---|
| 229 | | |
|---|
| 230 | | $action = "$C{underline}@ath$C{nounderline}\n $prefix^ $action"; |
|---|
| 231 | | for my $key (keys(%{$gh[$#gh]})) { |
|---|
| 232 | | my $cmd = $key; |
|---|
| 233 | | my $type = ref($grammar->{$key}); |
|---|
| 234 | | |
|---|
| 235 | | next if $cmd =~ /^[A-Z]+$/; |
|---|
| 236 | | $cmd =~ s/^(\d+\))?(\$(\*)?)?//; |
|---|
| 237 | | if ($type eq 'HASH' and $grammar->{$key}->{HELP}) { |
|---|
| 238 | | $type = ref($grammar->{$key}->{HELP}); |
|---|
| 239 | | if ($type eq 'ARRAY') { |
|---|
| 240 | | $cmd = $grammar->{$key}->{HELP}->[0]; |
|---|
| 241 | | } elsif ($type eq 'CODE') { |
|---|
| 242 | | my %s = $grammar->{$key}->{HELP}->($ctx, @{$ctx->args()}); |
|---|
| 243 | | push(@candidates, keys(%s)); |
|---|
| 244 | | $cmd = undef; |
|---|
| | 222 | if (defined($ctx)) { |
|---|
| | 223 | my @gh = @{$ctx->grammar_history()}; |
|---|
| | 224 | my @th = @{$ctx->token_history()}; |
|---|
| | 225 | my @ath = @{$ctx->all_tokens()}; |
|---|
| | 226 | my $grammar = $gh[$#gh]; |
|---|
| | 227 | my @candidates; |
|---|
| | 228 | push(@th, "") if $action =~ /More input expected/; |
|---|
| | 229 | my $prefix = ' ' x length("@th[0 .. $#th - 1]") . (@th <= @ath ? ' ' : ''); |
|---|
| | 230 | |
|---|
| | 231 | $action = "$C{underline}@ath$C{nounderline}\n $prefix^ $action"; |
|---|
| | 232 | for my $key (keys(%{$gh[$#gh]})) { |
|---|
| | 233 | my $cmd = $key; |
|---|
| | 234 | my $type = ref($grammar->{$key}); |
|---|
| | 235 | |
|---|
| | 236 | next if $cmd =~ /^[A-Z]+$/; |
|---|
| | 237 | $cmd =~ s/^(\d+\))?(\$(\*)?)?//; |
|---|
| | 238 | if ($type eq 'HASH' and $grammar->{$key}->{HELP}) { |
|---|
| | 239 | $type = ref($grammar->{$key}->{HELP}); |
|---|
| | 240 | if ($type eq 'ARRAY') { |
|---|
| | 241 | $cmd = $grammar->{$key}->{HELP}->[0]; |
|---|
| | 242 | } elsif ($type eq 'CODE') { |
|---|
| | 243 | my %s = $grammar->{$key}->{HELP}->($ctx, @{$ctx->args()}); |
|---|
| | 244 | push(@candidates, keys(%s)); |
|---|
| | 245 | $cmd = undef; |
|---|
| | 246 | } |
|---|
| | 247 | } |
|---|
| | 248 | if (defined($cmd)) { |
|---|
| | 249 | push(@candidates, $cmd); |
|---|
| 250 | | } |
|---|
| 251 | | if (@candidates and $grammar->{ACTION}) { |
|---|
| 252 | | push(@candidates, "<eol>"); |
|---|
| 253 | | } |
|---|
| 254 | | @candidates = sort(@candidates); |
|---|
| 255 | | if (@candidates > 1) { |
|---|
| 256 | | $action .= "\n ${prefix}Candidates are " . join(", ", @candidates[0 .. $#candidates - 1]) . " and " . $candidates[-1] . "."; |
|---|
| 257 | | } elsif (@candidates == 1) { |
|---|
| 258 | | $action .= "\n ${prefix}Expecting @candidates."; |
|---|
| 259 | | } else { |
|---|
| 260 | | if ($grammar->{ACTION}) { |
|---|
| 261 | | $action .= "\n ${prefix}Expected end of command."; |
|---|
| 262 | | # } else { |
|---|
| 263 | | # $action .= "\n ${prefix}Can't find any candidates, probably a grammar error." |
|---|
| | 255 | @candidates = sort(@candidates); |
|---|
| | 256 | if (@candidates > 1) { |
|---|
| | 257 | $action .= "\n ${prefix}Candidates are " . join(", ", @candidates[0 .. $#candidates - 1]) . " and " . $candidates[-1] . "."; |
|---|
| | 258 | } elsif (@candidates == 1) { |
|---|
| | 259 | $action .= "\n ${prefix}Expecting @candidates."; |
|---|
| | 260 | } else { |
|---|
| | 261 | if ($grammar->{ACTION}) { |
|---|
| | 262 | $action .= "\n ${prefix}Expected end of command."; |
|---|
| | 263 | } |
|---|