synthphonia/parser/
config.rs

1use std::collections::{HashMap, BTreeMap};
2
3use itertools::Itertools;
4use pest::iterators::Pair;
5
6use crate::{expr::Expr, galloc::AllocForCharIter, parser::problem::new_custom_error_span, value::{ConstValue, Value}};
7
8use super::problem::{Error, FunSig, Rule};
9use derive_more::{From, DebugCustom};
10
11#[derive(From, Clone, PartialEq, Eq, Hash, Default)]
12/// A configuration fields of extended SyGuS-IF. Holding a collection of key-value pairs.
13pub struct Config(BTreeMap<String, ConstValue>);
14
15impl Config {
16    /// Creates a new instance of the type containing an empty BTreeMap. 
17    pub fn new() -> Self {
18        Config(BTreeMap::new())
19    }
20    /// Parses a `Pair` of a configuration from a parsed syntax tree and returns a `Config` object. 
21    pub fn parse(pair: Pair<'_, Rule>) -> Result<Self, Error> {
22        assert!(pair.as_rule() == Rule::config);
23        let span = pair.as_span();
24        let hash: Result<BTreeMap<String, ConstValue>, Error> = pair.into_inner().map(|x| {
25            let [sym, v] : [Pair<'_, Rule>; 2] = x.into_inner().collect_vec().try_into().map_err(|_| new_custom_error_span("Expecting [(key value),*]".into(), span))?;
26            match v.as_rule() {
27                Rule::value => Ok((sym.as_str().into(), ConstValue::parse(v)?)),
28                Rule::symbol => Ok((sym.as_str().into(), ConstValue::Str(v.as_str().chars().galloc_collect_str()))),
29                Rule::expr => Ok((sym.as_str().into(), ConstValue::Expr(Expr::parse(v, None).unwrap()))),
30                _ => panic!(),
31            }
32        }).collect();
33        Ok(hash?.into())
34    }
35    /// Provides a method to retrieve a string reference from the configuration for a given key. 
36    pub fn get_str(&self, name: &str) -> Option<&'static str> {
37        self.0.get(name).and_then(|x| x.as_str())
38    }
39    /// Retrieves an `i64` value from the configuration map based on the provided string name key. 
40    /// This function attempts to access the key within the internal map and converts the associated `ConstValue` to an `i64` if possible, returning `Some(i64)` when successful or `None` if the key does not exist or cannot be converted.
41    pub fn get_i64(&self, name: &str) -> Option<i64> {
42        self.0.get(name).and_then(|x| x.as_i64())
43    }
44    /// Provides a method to retrieve a `usize` value from the configuration. 
45    pub fn get_usize(&self, name: &str) -> Option<usize> {
46        self.0.get(name).and_then(|x| x.as_usize())
47    }
48    /// Retrieves an optional boolean value associated with a given key from the configuration. 
49    pub fn get_bool(&self, name: &str) -> Option<bool> {
50        self.0.get(name).and_then(|x| x.as_bool())
51    }
52    /// Provides functionality to retrieve a static reference to an `Expr` associated with a given name.  
53    pub fn get_expr(&self, name: &str) -> Option<&'static Expr> {
54        self.0.get(name).and_then(|x| x.as_expr())
55    }
56    /// Merges another instance into the current one by extending its internal map with the entries from the provided instance. 
57
58    pub fn merge(&mut self, other: Self) {
59        self.0.extend(other.0);
60    }
61}
62
63impl std::fmt::Debug for Config {
64    /// Formats the contents of the `Config` into a user-friendly string representation. 
65
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        for (k, v) in self.0.iter() {
68            write!(f, " #{}:{}", k, v)?
69        }
70        Ok(())
71    }
72}
73
74
75
76