Struct commands::parser::Parser [] [src]

pub struct Parser<'text> {
    pub nodes: Vec<Rc<Node>>,
    pub tokens: Vec<Token<'text>>,
    // some fields omitted
}

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

The nodes which have been accepted during parse or advance.

tokens

The tokens which have been accepted during parse or advance.

Methods

impl<'text> Parser<'text>

fn new(initial_node: Rc<Node>) -> Parser<'text>

Construct a parser with a root node.

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.");
}

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);
}

fn advance(&mut self, token: Token<'text>) -> Result<(), ParseError<'text>>

Parse a single token, advancing through the node hierarchy.

fn execute(&self)

Execute the command that has been accepted by the parser.

  • XXX: This should be returning a Result probably.

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.