NAME
App::Dochazka::CLI::Parser - Parser module
PACKAGE VARIABLES AND EXPORTS
generate_semantic_tree
-
Populate the
$semantic_tree
package variable. lookup_command
parse
-
Parse the command string entered by the user.
possible_words
-
Given a state expressed as a stack of tokens, return list of possible tokens.
process_command
-
Given a command string, process it (by parsing, calling the handler, etc.) and return the result.
$semantic_tree
-
The semantic tree is a traversable tree representation of the CLI commands, i.e. the keys of the dispatch map
$App::Dochazka::CLI::CommandMap::dispatch_map
FUNCTIONS
generate_semantic_tree
Generate the semantic context tree.
The set of keys of %$dispatch_map
contains all the possible commands, each expressed as a sequence of tokens. The semantic context tree is generated from the dispatch map keys. Each node of the tree represents a possible token and the set of nodes at depth n of the tree is the set of possible tokens in the nth position within the command.
Taking, for example, a dispatch map consisting of the following two commands:
ABE BABEL CAROL
DALE EARL JENSEN PARLOR
DALE TWIT
The semantic tree would be:
(root)
|
+-----------+
| |
ABE DALE
| |
| +------+
| | |
BABEL EARL TWIT
| |
CAROL JENSEN
|
PARLOR
The point of this exercise is to facilitate command completion. If two a single token ABE has been entered by the user and <TAB> is pressed, finding out that BABEL is the only possible command in this position is a simple matter of traversing the above semantic tree. (And this is exactly what is done by the possible_words
routine in App::Dochazka::CLI::Parser.)
This routine takes an optional argument which, if provided, is assumed to be a reference to a dispatch map. In the absence of this argument, the $dispatch_map
package variable (initialized above) is used.
For this and more examples, see t/parser/semantic_tree.t
.
look_up_command
Given a normalized command string such as "GET BUGREPORT", look it up in the dispatch map and return the corresponding coderef, or nothing if the lookup fails.
parse
Parse command string entered by the user. Takes the command string, and returns:
$nc
-
The normalized command - suitable for lookup via
look_up_command
$ts
-
The token stack - a reference to the list of normalized tokens
$th
-
The token hash - a hash where the keys are the normalized tokens and the values are the raw values extracted from the command string. Whatever is left after command string parsing completes will be placed in the '_REMAINDER' key.
possible_words
Given a token stack, return the list of possible tokens.
process_command
Given a command entered by the user, process it and return the result.