pub struct Parser<'text> {
pub nodes: Vec<Rc<Node>>,
pub tokens: Vec<Token<'text>>,
/* private fields */
}Expand description
Command parser
The lifetime parameter 'text refers to the lifetime of the
tokens passed into the parser. This is the same as the lifetime
of the text used to create the tokens.
When creating a Parser, you must give it an Rc<RootNode>.
RootNode instances should be created using a CommandTree.
use commands::parser::{Command, CommandTree, Parser};
let mut tree = CommandTree::new();
tree.command(Command::new("show"));
tree.command(Command::new("set"));
tree.command(Command::new("help"));
let mut parser = Parser::new(tree.finalize());The parser is constructed as a mutable object as most of
the methods on it will modify its state.
Fields§
§nodes: Vec<Rc<Node>>The nodes which have been accepted during parse or advance.
tokens: Vec<Token<'text>>The tokens which have been accepted during parse or advance.
Implementations§
Source§impl<'text> Parser<'text>
impl<'text> Parser<'text>
Sourcepub fn complete(&self, token: Option<Token<'text>>) -> Vec<Completion<'_>>
pub fn complete(&self, token: Option<Token<'text>>) -> Vec<Completion<'_>>
Given an optional token, get the possible valid completions for the current parser state.
Possible completions are successors of the current node which
are not hidden, are acceptable, and which match the token,
if one has been provided.
Nodes may customize the Complete trait to customize the
Completion and CompletionOptions which are generated
for that node.
Each valid successor node will have one Completion in the
result vector. Each Completion will have one or more
CompletionOption for each valid way that the value may be
entered.
use commands::parser::{Command, CommandTree, Parser};
use commands::tokenizer::{Token, tokenize};
let mut tree = CommandTree::new();
tree.command(Command::new("show"));
tree.command(Command::new("set"));
tree.command(Command::new("help"));
let mut parser = Parser::new(tree.finalize());
// Completing now should have 3 options, 1 for each command.
let comps = parser.complete(None);
assert_eq!(comps.len(), 3);
// But completing with a token for 'h' should have 1 option.
if let Ok(tokens) = tokenize("h") {
let comps = parser.complete(Some(tokens[0]));
assert_eq!(comps.len(), 1);
assert_eq!(comps[0].options.len(), 1);
assert_eq!(comps[0].options[0].option_string, "help");
} else {
panic!("Tokenize failed.");
}
// And completing for 's' should have 2 options.
if let Ok(tokens) = tokenize("s") {
let comps = parser.complete(Some(tokens[0]));
assert_eq!(comps.len(), 2);
} else {
panic!("Tokenize failed.");
}Sourcepub fn parse(
&mut self,
tokens: Vec<Token<'text>>,
) -> Result<(), ParseError<'text>>
pub fn parse( &mut self, tokens: Vec<Token<'text>>, ) -> Result<(), ParseError<'text>>
Parse a vector of tokens, advancing through the node hierarchy.
use commands::parser::{Command, CommandTree, Parser};
use commands::tokenizer::tokenize;
let mut tree = CommandTree::new();
tree.command(Command::new("show interface"));
let mut parser = Parser::new(tree.finalize());
if let Ok(tokens) = tokenize("show interface") {
parser.parse(tokens);
}Sourcepub fn advance(&mut self, token: Token<'text>) -> Result<(), ParseError<'text>>
pub fn advance(&mut self, token: Token<'text>) -> Result<(), ParseError<'text>>
Parse a single token, advancing through the node hierarchy.
Sourcepub fn execute(&self)
pub fn execute(&self)
Execute the command that has been accepted by the parser.
- XXX: This should be returning a Result probably.
Sourcepub fn verify(&self) -> Result<(), VerifyError>
pub fn verify(&self) -> Result<(), VerifyError>
Verify that the parser is in a valid state with respect to having accepted a command and all required parameters.