Skip to content

Commit 511e81b

Browse files
committed
refactor(policy): drop try_consume helper
1 parent e6a2ba6 commit 511e81b

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

src/policy/semantic.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,17 +387,9 @@ impl<'a> MathParser<'a> {
387387
}
388388
}
389389

390-
fn try_consume(&mut self, want: char) -> bool {
390+
fn expect(&mut self, want: char) -> Result<(), Error> {
391391
if self.peek() == Some(want) {
392392
self.next();
393-
true
394-
} else {
395-
false
396-
}
397-
}
398-
399-
fn expect(&mut self, want: char) -> Result<(), Error> {
400-
if self.try_consume(want) {
401393
Ok(())
402394
} else {
403395
Err(malformed_math(&format!("expected '{}'", want)))
@@ -468,6 +460,10 @@ impl<'a> MathParser<'a> {
468460
parser.expect('{')?;
469461
frames.push(Frame { subs: Vec::new(), kind: Kind::Thresh });
470462
}
463+
'∧' | '∨' | ',' => {
464+
return Err(malformed_math("missing sub-expression before operator"))
465+
}
466+
')' | '}' => return Err(malformed_math("empty group or threshold")),
471467
_ => cur = Some(Arc::new(parser.parse_terminal()?)),
472468
}
473469
continue;
@@ -549,7 +545,7 @@ impl<'a> MathParser<'a> {
549545
return Err(malformed_math("unclosed group or threshold"));
550546
}
551547
let root = cur.ok_or_else(|| malformed_math("empty input"))?;
552-
Ok(Arc::try_unwrap(root).unwrap_or_else(|arc| (*arc).clone()))
548+
Ok(Arc::try_unwrap(root).expect("root Arc is uniquely owned"))
553549
}
554550

555551
fn parse_terminal<Pk: FromStrKey>(&mut self) -> Result<Policy<Pk>, Error> {
@@ -558,15 +554,17 @@ impl<'a> MathParser<'a> {
558554
return Err(malformed_math("expected terminal name"));
559555
}
560556

561-
if !self.try_consume('(') {
557+
self.skip_ws();
558+
if self.peek() != Some('(') {
562559
return match name {
563560
"UNSATISFIABLE" => Ok(Policy::Unsatisfiable),
564561
"TRIVIAL" => Ok(Policy::Trivial),
565562
_ => Err(malformed_math("unknown nullary terminal")),
566563
};
567564
}
565+
self.next();
568566

569-
let arg = self.consume_while(|c| c != ')');
567+
let arg = self.consume_while(|c| c != ')').trim();
570568
self.expect(')')?;
571569

572570
Ok(match name {

0 commit comments

Comments
 (0)