gtp_parser_generator/
lib.rs

1#![recursion_limit = "1024"]
2
3#[macro_use]
4extern crate error_chain;
5extern crate pom;
6
7#[macro_use]
8mod macros;
9
10pub mod lexer;
11
12use pom::DataInput;
13
14mod errors {
15    // Create the Error, ErrorKind, ResultExt, and Result types
16    error_chain! {
17
18    errors {
19        NoCommand {}
20        NotEnoughArguments {}
21    }
22
23    }
24
25}
26
27use errors::*;
28
29#[derive(Debug, PartialEq)]
30pub struct Command {
31    name: String,
32    id: Option<u8>,
33    args: Vec<String>,
34}
35
36pub fn try_parse(input: &[u8]) -> Result<Command> {
37    let input = DataInput::new(input);
38    let parse = lexer::command().parse(&mut input.clone());
39    let (id, mut parse) = parse.unwrap();
40
41    let rest = parse.split_off(1);
42    let ref first = parse[0];
43    Ok(Command { name: first.clone(), id: id, args: rest })
44}
45
46#[test]
47fn parse_quit() {
48    let quit = try_parse(b"quit");
49    assert_eq!(quit.unwrap(), Command{ name: "quit".to_string(), id: None, args: Vec::new() });
50}