Struct Parser

Source
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>

Source

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

Construct a parser with a root node.

Source

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

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

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

Parse a single token, advancing through the node hierarchy.

Source

pub fn execute(&self)

Execute the command that has been accepted by the parser.

  • XXX: This should be returning a Result probably.
Source

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.

Auto Trait Implementations§

§

impl<'text> Freeze for Parser<'text>

§

impl<'text> RefUnwindSafe for Parser<'text>

§

impl<'text> !Send for Parser<'text>

§

impl<'text> !Sync for Parser<'text>

§

impl<'text> Unpin for Parser<'text>

§

impl<'text> UnwindSafe for Parser<'text>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.