Skip to content

Commit c6ceb4b

Browse files
committed
chore: initial commit
0 parents  commit c6ceb4b

File tree

6 files changed

+1518
-0
lines changed

6 files changed

+1518
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
3+
src/main.rs

Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "uci-parser"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Danny Hammer <[email protected]>"]
6+
license = "MPL-2.0"
7+
description = "Universal Chess Interface parser"
8+
repository = "https://github.com/dannyhammer/uci-parser"
9+
homepage = "https://github.com/dannyhammer/uci-parser"
10+
keywords = ["chess", "chess parser", "universal chess interface", "uci", "parser", "uci parser"]
11+
12+
[dependencies]
13+
nom = "7.1.3"
14+
15+
[features]
16+
parse-go-perft = []
17+
parse-bench = []
18+
validate-promotion-moves = []
19+
clamp-negatives = []
20+
err-on-unused-input = []

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# A Universal Chess Interface (UCI) Parser
2+
3+
This crate contains types and functions for communicating with chess engines and chess GUIs through the [Universal Chess Interface (UCI)](https://backscattering.de/chess/uci/) protocol.
4+
5+
## Overview
6+
7+
The primary function of this crate is to provide well-typed representations for every message/command described in the UCI protocol, along with an easy-to-use API for converting between these well-typed representations and strings.
8+
9+
## Crate Features
10+
11+
How edge cases should be handled is a delicate subject and the correct answer depends on the needs of your engine.
12+
Rather than enforce my own opinion on handling those edge cases, I've marked them as crate features.
13+
14+
- `go-perft`: Adds support for parsing `perft <depth>` as an argument to the `go` command.
15+
16+
- This is not part of the UCI protocol, but is [common among engines](https://github.com/official-stockfish/Stockfish/blob/d6043970bd156b1d2ab6cb51e8d5cb0c6a40797c/tests/perft.sh#L17).
17+
18+
- `bench`: Adds support for parsing the string `bench` into `UciCommand::Bench`.
19+
20+
- This is not part of the UCI protocol, but is [common among engines](https://official-stockfish.github.io/docs/stockfish-wiki/UCI-&-Commands.html#bench) and very useful for engine development.
21+
- The arguments to `bench` are the same as the arguments to `go`, since both commands involve running searches.
22+
23+
- `validate-promotion-moves`: Restricts the grammar when parsing moves in UCI notation to ensure that promotions are valid:
24+
25+
- By default, moves are parsed with the grammar `[a-h][1-8][a-h][1-8][qnrb]`, which will parse `e2e4p` successfully, even though it doesn't "make sense" in-game. With this feature enabled, the grammar restricts to: `[a-h][1-8][a-h][1-8] | [a-h]7[a-h]8[qnrb] | [a-h]2[a-h]1[qnrb]`. This means that only moves from the penultimate ranks to the final ranks will be parsed as promotions, and the only valid pieces for a promotion are a Queen, Knight, Rook, or Bishop.
26+
27+
- `clamp-negatives`: Clamps negative numbers to `0` when parsing.
28+
29+
- By default, all numbers are assumed to be positive, and the parser will fail on strings like `go wtime -80`. This is normally not a problem,
30+
- All numeric values within the UCI protocol _should_ be positive. That said, there _have_ been instances where some GUIs send negative move times, which could mean any variety of things. If this feature is enabled, _all_ numeric values are clamped to _at least_ 0. That is, if a GUI sent `go movetime -42`, this crate will parse that as a `Duration` of `0` milliseconds. It is up to your engine to determine how to respond to these situations.
31+
32+
- `err-on-unused-input`: Causes the parser to fail if the input text was not fully consumed during parsing.
33+
- As per the [protocol](https://backscattering.de/chess/uci/#unknown), unknown tokens encountered _before_ a command are ignored (`joho debug on` parses to `debug on`). Unknown tokens encountered while parsing a specific command will generate errors (`debug joho on` fails). Unknown tokens _after_ a command are, by default, ignored (`debug on joho` parses to `debug on`). If this feature is enabled, the parser will fail if _all_ tokens were not consumed during parsing (`debug on joho` will fail).

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
#![doc = include_str!("../README.md")]
8+
9+
/// Utilities related to parsing UCI messages.
10+
mod parser;
11+
12+
/// Types representing the various GUI-to-Engine and Engine-to-GUI messages that can be sent.
13+
mod messages;
14+
15+
pub use messages::*;
16+
pub use parser::*;

0 commit comments

Comments
 (0)