An implementation of the Universal Chess Interface in Rust.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.1 KiB

//! Parser contains the command parser for handling receiving commands from the GUI
use std::collections::HashMap;
use commands;
use either::Either;
pub use either::Left;
pub use either::Right;
/// Token represents a parsable token in the string sent via STDIN from the GUI
#[derive(Debug, Eq, PartialEq)]
pub enum Token {
UCI,
DEBUG,
ISREADY,
SETOPTION,
REGISTER,
UCINEWGAME,
POSITION,
GO,
STOP,
PONDERHIT,
QUIT,
}
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub enum CommandValue {
Int(i32),
Float(f64),
Text(String),
Boolean(bool),
}
pub struct Parser {}
/// TokenResult is a convenient way to package an entire command string. It is identified by it's associated
/// Token, and the arguments.
///
/// Example:
///
/// setoption name Hash value 32
/// --------- ---- ---- ----- --
/// Token arg val arg val
///
/// debug on
/// ----- --
/// Token val
///
///
/// The user is still responsible for knowing what to look for in the hashmap but this structure makes it far
/// easier to work with commands. Some commands don't have arguments, in that case the args will be None.
#[derive(Debug, PartialEq)]
pub struct TokenResult {
token: Token,
args: Either<Option<HashMap<&'static str, CommandValue>>, &'static str>,
}
impl TokenResult {
/// Intantiates a new TokenResult
pub fn new(token: Token, args: Either<Option<HashMap<&'static str, CommandValue>>, &'static str>>) -> TokenResult {
TokenResult { token, args }
}
}
impl Parser {
/// Instantiates a new Parser
pub fn new() -> Parser {
Parser{}
}
/// Parses a single string from the GUI and turns it into a neatly packaged TokenResult for processing
/// by the engine.
pub fn parse(&self, s: &str) -> Result<TokenResult, &'static str> {
// TODO: Remove the /n from the end
let tokens: Vec<&str> = s.split_whitespace().collect::<Vec<&str>>();
// TODO: Parse things based on the token, probably need a special function for each token based on a switch
// of tokens[0].
// remove this when it's working
Ok(TokenResult::new(Token::SETOPTION, Left(Some(HashMap::new()))))
}
}