liberty-db 0.17.2

A fully defined liberty (std. cells in VLSI) data structure, efficient parser & formatter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use core::fmt::{self, Write};
use nom::{
  IResult, Parser as _,
  branch::alt,
  character::complete::char,
  combinator::{map, map_res, opt},
  multi::many1,
  sequence::{delimited, preceded},
};

use crate::{
  Ctx,
  ast::{
    CodeFormatter, Indentation, ParseScope,
    parser::{float_one, key, space, unquote},
  },
};

#[derive(Debug, Clone, PartialEq)]
#[derive(serde::Serialize, serde::Deserialize)]
pub enum FormulaExpr {
  Add(Box<FormulaExpr>, Box<FormulaExpr>),
  Sub(Box<FormulaExpr>, Box<FormulaExpr>),
  Mul(Box<FormulaExpr>, Box<FormulaExpr>),
  Div(Box<FormulaExpr>, Box<FormulaExpr>),
  Neg(Box<FormulaExpr>),
  Num(f64),
  Var(String),
}

#[derive(Debug, Clone, Default)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Formula {
  pub expr: FormulaExpr,
  pub value: Option<f64>,
}

impl Default for FormulaExpr {
  #[inline]
  fn default() -> Self {
    Self::Num(0.0)
  }
}

impl<C: 'static + Ctx> crate::ast::ParsingBuilder<C> for Formula {
  type Builder = FormulaExpr;
  #[inline]
  #[expect(clippy::renamed_function_params)]
  fn build(expr: Self::Builder, scope: &mut crate::ast::BuilderScope<C>) -> Self {
    let value = expr.eval(&expr, |k: &str| scope.voltage_map.get(k).copied());
    Self { expr, value }
  }
}
impl<C: 'static + Ctx> crate::ast::SimpleAttri<C> for Formula {
  #[inline]
  fn nom_parse<'a>(
    i: &'a str,
    scope: &mut ParseScope<'_>,
  ) -> crate::ast::SimpleParseRes<'a, FormulaExpr> {
    crate::ast::parser::simple_custom(
      i,
      &mut scope.loc.line_num,
      FormulaExpr::parse,
      unquote,
    )
  }
  #[inline]
  fn fmt_self<T: Write, I: Indentation>(
    &self,
    f: &mut CodeFormatter<'_, T, I>,
  ) -> fmt::Result {
    write!(f, "{}", self.expr)
  }
}

impl FormulaExpr {
  #[inline]
  pub fn parse(i: &str) -> IResult<&str, Self> {
    map_res(tokens, |tokens| parse_formula(&tokens)).parse_complete(i)
  }
  #[inline]
  #[expect(clippy::float_arithmetic, clippy::option_if_let_else)]
  pub fn eval<F: Fn(&str) -> Option<f64> + Copy>(
    &self,
    top: &Self,
    query_fn: F, // map: &HashMap<String, f64, S>,
  ) -> Option<f64> {
    match self {
      Self::Add(e1, e2) => {
        let f1 = e1.eval(top, query_fn)?;
        e2.eval(top, query_fn).map(|f2| f1 + f2)
      }
      Self::Sub(e1, e2) => {
        let f1 = e1.eval(top, query_fn)?;
        e2.eval(top, query_fn).map(|f2| f1 - f2)
      }
      Self::Mul(e1, e2) => {
        let f1 = e1.eval(top, query_fn)?;
        e2.eval(top, query_fn).map(|f2| f1 * f2)
      }
      Self::Div(e1, e2) => {
        let f1 = e1.eval(top, query_fn)?;
        e2.eval(top, query_fn).map(|f2| f1 / f2)
      }
      Self::Neg(e) => e.eval(top, query_fn).map(|f| -f),
      Self::Num(f) => Some(*f),
      Self::Var(k) => {
        if let Some(f) = query_fn(k) {
          Some(f)
        } else {
          crate::error!("Eval formula [{top}]: Can NOT find voltage {k}");
          None
        }
      }
    }
  }
  /// precedence
  const fn prec(&self) -> u8 {
    match self {
      Self::Add(..) | Self::Sub(..) => 1,
      Self::Mul(..) | Self::Div(..) => 2,
      Self::Neg(_) => 3,
      Self::Num(_) | Self::Var(_) => 4,
    }
  }
  /// recursively format,
  /// `parent_op`: parent node's op type,
  /// `is_right`: is right sub-tree
  fn fmt_with(
    &self,
    f: &mut fmt::Formatter<'_>,
    parent_op: ParentOp,
    is_right: bool,
  ) -> fmt::Result {
    let my_prec = self.prec();
    let par_prec = parent_op.prec();

    // need paren:
    // 1) my precedence < parent's precedence
    // 2) parent is Sub/Div && self is right sub-tree && my precedence == parent's precedence
    let need_paren = my_prec < par_prec
      || (is_right
        && matches!(parent_op, ParentOp::Sub | ParentOp::Div)
        && my_prec == par_prec);

    if need_paren {
      write!(f, "(")?;
    }

    match self {
      Self::Num(n) => write!(f, "{n}")?,
      Self::Var(s) => write!(f, "{s}")?,
      Self::Neg(expr) => {
        write!(f, "-")?;
        expr.fmt_with(f, ParentOp::Neg, false)?;
      }
      Self::Add(lhs, rhs) => {
        lhs.fmt_with(f, ParentOp::Add, false)?;
        write!(f, " + ")?;
        rhs.fmt_with(f, ParentOp::Add, true)?;
      }
      Self::Sub(lhs, rhs) => {
        lhs.fmt_with(f, ParentOp::Sub, false)?;
        write!(f, " - ")?;
        rhs.fmt_with(f, ParentOp::Sub, true)?;
      }
      Self::Mul(lhs, rhs) => {
        lhs.fmt_with(f, ParentOp::Mul, false)?;
        write!(f, " * ")?;
        rhs.fmt_with(f, ParentOp::Mul, true)?;
      }
      Self::Div(lhs, rhs) => {
        lhs.fmt_with(f, ParentOp::Div, false)?;
        write!(f, " / ")?;
        rhs.fmt_with(f, ParentOp::Div, true)?;
      }
    }

    if need_paren {
      write!(f, ")")?;
    }
    Ok(())
  }
}

impl fmt::Display for Formula {
  #[inline]
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    fmt::Display::fmt(&self.expr, f)
  }
}
impl fmt::Display for FormulaExpr {
  #[inline]
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    // no parent op for top level, either is not sub-tree
    self.fmt_with(f, ParentOp::None, false)
  }
}

/// To denote the relation between current node and parent node,
/// and what's op type of parent node
#[derive(Copy, Clone)]
enum ParentOp {
  None,
  Add,
  Sub,
  Mul,
  Div,
  Neg,
}

impl ParentOp {
  /// precedence
  const fn prec(self) -> u8 {
    match self {
      Self::None => 0,
      Self::Add | Self::Sub => 1,
      Self::Mul | Self::Div => 2,
      Self::Neg => 3,
    }
  }
}

/// `ExprErr`
#[derive(Clone, Copy, Debug, thiserror::Error, PartialEq, Eq)]
#[derive(serde::Serialize, serde::Deserialize)]
pub enum ExprErr {
  /// `SingleOp`
  #[error("rset tokens")]
  LeftToken,
  /// `Bracket`
  #[error("left-right Bracket mismatch")]
  Bracket,
  /// `NoIdea`
  #[error("something go wrong, code=1")]
  NoIdea1,
}

#[inline]
fn parse_formula(data: &[Token<'_>]) -> Result<FormulaExpr, ExprErr> {
  struct Parser<'a> {
    tokens: &'a [Token<'a>],
    pos: usize,
  }
  #[expect(clippy::arithmetic_side_effects, clippy::wildcard_enum_match_arm)]
  impl<'a> Parser<'a> {
    // entry: after parse, need no token left
    fn parse(&mut self) -> Result<FormulaExpr, ExprErr> {
      let expr = self.parse_add_sub()?;
      if self.pos != self.tokens.len() {
        return Err(ExprErr::LeftToken);
      }
      Ok(expr)
    }
    fn current(&self) -> Option<&Token<'a>> {
      self.tokens.get(self.pos)
    }

    // add/sub
    fn parse_add_sub(&mut self) -> Result<FormulaExpr, ExprErr> {
      let mut node = self.parse_mul_div()?;
      while let Some(tok) = self.current() {
        match tok {
          Token::Add => {
            self.pos += 1;
            let rhs = self.parse_mul_div()?;
            node = FormulaExpr::Add(Box::new(node), Box::new(rhs));
          }
          Token::Sub => {
            self.pos += 1;
            let rhs = self.parse_mul_div()?;
            node = FormulaExpr::Sub(Box::new(node), Box::new(rhs));
          }
          _ => break,
        }
      }
      Ok(node)
    }

    // mul/div
    fn parse_mul_div(&mut self) -> Result<FormulaExpr, ExprErr> {
      let mut node = self.parse_unary()?;
      while let Some(tok) = self.current() {
        match tok {
          Token::Mul => {
            self.pos += 1;
            let rhs = self.parse_unary()?;
            node = FormulaExpr::Mul(Box::new(node), Box::new(rhs));
          }
          Token::Div => {
            self.pos += 1;
            let rhs = self.parse_unary()?;
            node = FormulaExpr::Div(Box::new(node), Box::new(rhs));
          }
          _ => break,
        }
      }
      Ok(node)
    }

    // unary op, negative only for now
    fn parse_unary(&mut self) -> Result<FormulaExpr, ExprErr> {
      match self.current() {
        Some(Token::Sub) => {
          self.pos += 1;
          let expr = self.parse_unary()?;
          Ok(FormulaExpr::Neg(Box::new(expr)))
        }
        Some(Token::Add) => {
          self.pos += 1;
          let expr = self.parse_unary()?;
          Ok(expr)
        }
        _ => self.parse_primary(),
      }
    }

    // primary op: num/var/paren
    fn parse_primary(&mut self) -> Result<FormulaExpr, ExprErr> {
      let res = match self.current() {
        Some(Token::Num(n)) => Ok(FormulaExpr::Num(*n)),
        Some(Token::Var(name)) => Ok(FormulaExpr::Var((*name).to_owned())),
        // Recursively call `parse_formula` when meet paren
        Some(Token::Paren(inner)) => Ok(parse_formula(inner)?),
        _ => return Err(ExprErr::NoIdea1),
      };
      // move pos
      self.pos += 1;
      res
    }
  }
  let mut parser = Parser { tokens: data, pos: 0 };
  parser.parse()
}

#[derive(Debug)]
enum Token<'s> {
  Add,
  Sub,
  Mul,
  Div,
  Num(f64),
  Var(&'s str),
  Paren(Vec<Token<'s>>),
}

fn tokens(i: &str) -> IResult<&str, Vec<Token<'_>>> {
  many1(preceded(
    opt(space),
    alt((
      map(char('+'), |_| Token::Add),
      map(char('-'), |_| Token::Sub),
      map(char('*'), |_| Token::Mul),
      map(char('/'), |_| Token::Div),
      map(float_one, Token::Num),
      map(key, Token::Var),
      open_paren,
    )),
  ))
  .parse_complete(i)
}

fn open_paren(i: &str) -> IResult<&str, Token<'_>> {
  delimited(char('('), map(tokens, Token::Paren), char(')')).parse_complete(i)
}

#[cfg(test)]
mod test {
  use super::*;
  #[test]
  fn tokenize() {
    tokens("-0.5 ;");
    tokens("VDD + 0.5 ;");
    let (_, t) = tokens("0.3*(-A1+0.1) + 0.5 ;").unwrap();
    let expr = parse_formula(&t).unwrap();
    println!("{expr}");
    let (_, t) = tokens("-+A ;").unwrap();
    parse_formula(&t);
    let (_, t) = tokens("A+ ;").unwrap();
    parse_formula(&t);
  }
  #[test]
  fn parse_fmt_self_check() {
    for (should_success, s) in [
      (true, "-0.5 ;"),
      (true, "VDD + 0.5 ;"),
      (true, "(A+B)*(C+D);"),
      (true, "0.3*(-A1+0.1) + 0.5 ;"),
      (true, "-+A ;"),
      (false, "A+ ;"),
      (false, "- ;"),
      (false, "(A"),
    ] {
      println!("----");
      println!("origin:   {s}");
      let parse_res = FormulaExpr::parse(s);
      if should_success {
        if let Ok((_, expr)) = parse_res {
          println!("parsed:   {expr}");
          let fmt_s = expr.to_string();
          let reparse_res = FormulaExpr::parse(&fmt_s);
          if let Ok((_, reparse_expr)) = reparse_res {
            println!("reparsed: {reparse_expr}");
            assert_eq!(expr, reparse_expr);
          } else {
            println!("{expr:?}");
            println!("{reparse_res:?}");
            panic!("not equal");
          }
        } else {
          println!("{parse_res:?}");
          panic!("It should success");
        }
      } else if let Err(e) = parse_res {
        println!("{e}");
      } else {
        panic!("It should go wrong");
      }
    }
  }
}