Skip to main content

parse

Function parse 

pub fn parse(spec: &str) -> Result<Distribution>
Expand description

Parses a DSL spec into a Distribution.

See the module documentation for the full grammar. The parse is total: it never panics on any input and always returns either a validated distribution or a descriptive error.

use atune_core::space::{dsl, Distribution};

assert_eq!(dsl::parse("loguniform(1e-5, 1e-2)")?, Distribution::try_new_with_log_float(1e-5, 1e-2)?);
assert_eq!(dsl::parse("int(2, 512, log)")?, Distribution::try_new_with_log_int(2, 512)?);
assert_eq!(dsl::parse("int(0, 10, step=2)")?, Distribution::try_new_with_stepped_int(0, 10, 2)?);
assert_eq!(dsl::parse("choice(adam, sgd)")?, Distribution::try_new_with_categorical_labels(["adam", "sgd"])?);
assert_eq!(dsl::parse("bool")?, Distribution::new_with_boolean());
assert_eq!(dsl::parse("42")?, Distribution::try_new_with_int(42, 42)?);            // fixed int
assert_eq!(dsl::parse("0.001")?, Distribution::try_new_with_float(0.001, 0.001)?); // fixed float
dsl::parse("loguniform(-1, 1)").unwrap_err();

§Errors

Error::InvalidSpace with a message naming what was expected and where, for empty input, an unknown function, the wrong argument count, a malformed or non-finite numeric literal, unbalanced parentheses, or any distribution the underlying constructors reject (low > high, a non-positive loguniform bound, a duplicate categorical label, …). Parses a DSL spec into a validated distribution.

The full grammar and public error contract are documented on the crate::space::dsl::parse re-export.

§Errors

Returns Error::InvalidSpace if the specification is empty, malformed, or describes an invalid distribution.