clm_rust_core/layer1/
guard_parser.rs1use crate::layer0::guard_grammar::{
4 DIAG_NEGATED_GUARD_PASSED, IDX_ARG_2, PRIM_AND, PRIM_NOT, PRIM_OR, PROFILE_GUARD, PROFILE_RULE, VERDICT_CHAIN, VERDICT_NOT,
5};
6pub use crate::layer0::guard_term_eval::compile_cache_key as compile_guard_cache_key;
7pub use crate::layer0::guard_term_parser::compile_guard_term as compile_guard_to_term;
8use crate::layer0::term::Term;
9use crate::layer0::vocabulary::{
10 GUARD_OP_NOT, GUARD_OUTCOME_FAIL, GUARD_OUTCOME_PASS, GUARD_OUTCOME_UNMATCHED,
11};
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum GuardVerdict {
15 Pass,
16 Fail(String),
17 Unmatched(String),
18}
19
20impl GuardVerdict {
21 pub fn outcome(&self) -> &'static str {
22 if matches!(self, GuardVerdict::Pass) {
23 GUARD_OUTCOME_PASS
24 } else if matches!(self, GuardVerdict::Fail(_)) {
25 GUARD_OUTCOME_FAIL
26 } else {
27 GUARD_OUTCOME_UNMATCHED
28 }
29 }
30
31 pub fn is_pass(&self) -> bool {
32 matches!(self, GuardVerdict::Pass)
33 }
34}
35
36#[derive(Debug, Clone, PartialEq, Eq)]
37pub enum GuardAst {
38 Or(Box<GuardAst>, Box<GuardAst>),
39 And(Box<GuardAst>, Box<GuardAst>),
40 Not(Box<GuardAst>),
41 Atom(String),
42}
43
44fn term_to_guard_ast(term: &Term) -> GuardAst {
45 if let Term::Prim(name, args) = term {
46 if name == PRIM_OR && args.len() >= IDX_ARG_2 {
47 return GuardAst::Or(Box::new(term_to_guard_ast(&args[0])), Box::new(term_to_guard_ast(&args[1])));
48 }
49 if name == PRIM_AND && args.len() >= IDX_ARG_2 {
50 return GuardAst::And(Box::new(term_to_guard_ast(&args[0])), Box::new(term_to_guard_ast(&args[1])));
51 }
52 if name == PRIM_NOT && !args.is_empty() {
53 return GuardAst::Not(Box::new(term_to_guard_ast(&args[0])));
54 }
55 }
56 GuardAst::Atom(crate::layer0::term::render(term))
57}
58
59pub fn parse_guard_expression(source: &str, allow_word_ops: bool) -> Result<GuardAst, String> {
60 let profile = if allow_word_ops { PROFILE_RULE } else { PROFILE_GUARD };
61 compile_guard_to_term(source, profile)
62 .map(|t| term_to_guard_ast(&t))
63 .map_err(|_| source.to_string())
64}
65
66fn chain_rank(outcome: &str) -> usize {
67 VERDICT_CHAIN.iter().position(|&v| v == outcome).unwrap_or(1)
68}
69
70pub fn eval_kleene_not(verdict: GuardVerdict, label: &str) -> GuardVerdict {
71 let target = VERDICT_NOT
72 .iter()
73 .find(|&&(k, _)| k == verdict.outcome())
74 .map(|&(_, v)| v)
75 .unwrap_or(GUARD_OUTCOME_UNMATCHED);
76 if target == GUARD_OUTCOME_FAIL {
77 GuardVerdict::Fail([DIAG_NEGATED_GUARD_PASSED, label].concat())
78 } else if target == GUARD_OUTCOME_PASS {
79 GuardVerdict::Pass
80 } else {
81 verdict
82 }
83}
84
85pub fn eval_kleene_and<F: FnOnce() -> GuardVerdict>(left: GuardVerdict, eval_right: F) -> GuardVerdict {
86 if left.outcome() == GUARD_OUTCOME_FAIL {
87 return left;
88 }
89 let right = eval_right();
90 let target = VERDICT_CHAIN[chain_rank(left.outcome()).min(chain_rank(right.outcome()))];
91 if left.outcome() == target { left } else { right }
92}
93
94pub fn eval_kleene_or<F: FnOnce() -> GuardVerdict>(left: GuardVerdict, eval_right: F) -> GuardVerdict {
95 if left.outcome() == GUARD_OUTCOME_PASS {
96 return left;
97 }
98 let right = eval_right();
99 let target = VERDICT_CHAIN[chain_rank(left.outcome()).max(chain_rank(right.outcome()))];
100 if left.outcome() == target { left } else { right }
101}
102
103pub fn evaluate_guard_ast<F: Fn(&str) -> GuardVerdict>(ast: &GuardAst, eval_atom: &F) -> GuardVerdict {
104 match ast {
105 GuardAst::Atom(text) => eval_atom(text),
106 GuardAst::Not(inner) => {
107 let label = match inner.as_ref() {
108 GuardAst::Atom(t) => t.as_str(),
109 _ => GUARD_OP_NOT,
110 };
111 eval_kleene_not(evaluate_guard_ast(inner, eval_atom), label)
112 }
113 GuardAst::And(l, r) => eval_kleene_and(evaluate_guard_ast(l, eval_atom), || evaluate_guard_ast(r, eval_atom)),
114 GuardAst::Or(l, r) => eval_kleene_or(evaluate_guard_ast(l, eval_atom), || evaluate_guard_ast(r, eval_atom)),
115 }
116}