clm-kernel 0.0.24

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Low-level byte scanning helpers for `guard_term_parser` (`INV-627`).

use crate::layer0::guard_grammar::{
    CHAR_DIGIT_0, CHAR_DIGIT_9, CHAR_LOWER_A, CHAR_LOWER_Z, CHAR_UPPER_A, CHAR_UPPER_Z, SEP_PIPE,
};
use crate::layer0::limits::{SPEC_ASCII_SPACE, SPEC_ESCAPE_SEQ_CHARS};
use crate::layer0::term::Term;
use crate::layer0::vocabulary::{
    GUARD_BACKSLASH, GUARD_CMP_EQ, GUARD_CMP_GE, GUARD_CMP_GT, GUARD_CMP_LE, GUARD_CMP_LT,
    GUARD_CMP_NE, GUARD_DQUOTE, GUARD_LPAREN, GUARD_OP_AND, GUARD_OP_OR, GUARD_RPAREN,
    GUARD_SQUOTE, GUARD_UNDERSCORE,
};

pub(crate) const CMP_OPS: &[&str] = &[
    GUARD_CMP_EQ, GUARD_CMP_NE, GUARD_CMP_LE, GUARD_CMP_GE, GUARD_CMP_LT, GUARD_CMP_GT,
];

pub(crate) fn is_ident_start_byte(b: u8) -> bool {
    (b >= CHAR_LOWER_A.as_bytes()[0] && b <= CHAR_LOWER_Z.as_bytes()[0])
        || (b >= CHAR_UPPER_A.as_bytes()[0] && b <= CHAR_UPPER_Z.as_bytes()[0])
        || b == GUARD_UNDERSCORE.as_bytes()[0]
}

pub(crate) fn is_ident_byte(b: u8) -> bool {
    is_ident_start_byte(b) || (b >= CHAR_DIGIT_0.as_bytes()[0] && b <= CHAR_DIGIT_9.as_bytes()[0])
}

pub(crate) fn skip_ws(bytes: &[u8], mut pos: usize, end: usize) -> usize {
    while pos < end && bytes[pos] <= SPEC_ASCII_SPACE as u8 {
        pos += 1;
    }
    pos
}

pub(crate) fn find_matching(bytes: &[u8], open_pos: usize, end: usize, open_b: u8, close_b: u8) -> Option<usize> {
    let (mut depth, mut quote, mut i) = (0usize, 0u8, open_pos);
    let bs = GUARD_BACKSLASH.as_bytes()[0];
    let sq = GUARD_SQUOTE.as_bytes()[0];
    let dq = GUARD_DQUOTE.as_bytes()[0];
    while i < end {
        let c = bytes[i];
        if quote != 0 {
            if c == bs {
                i += SPEC_ESCAPE_SEQ_CHARS as usize;
                continue;
            }
            if c == quote {
                quote = 0;
            }
            i += 1;
            continue;
        }
        if c == sq || c == dq {
            quote = c;
        } else if c == open_b {
            depth += 1;
        } else if c == close_b {
            depth -= 1;
            if depth == 0 {
                return Some(i);
            }
        }
        i += 1;
    }
    None
}

pub(crate) fn match_word(bytes: &[u8], pos: usize, end: usize, word: &[u8]) -> Option<usize> {
    let n = word.len();
    if pos + n > end || (pos > 0 && is_ident_byte(bytes[pos - 1])) || &bytes[pos..pos + n] != word {
        return None;
    }
    let after = pos + n;
    if after < end && is_ident_byte(bytes[after]) { None } else { Some(after) }
}

pub(crate) fn scan_segment_end(src: &str, start: usize, end: usize, reject_cmp: bool) -> Option<usize> {
    let bytes = src.as_bytes();
    let (mut depth, mut quote, mut i) = (0usize, 0u8, start);
    let (bs, sq, dq) = (GUARD_BACKSLASH.as_bytes()[0], GUARD_SQUOTE.as_bytes()[0], GUARD_DQUOTE.as_bytes()[0]);
    let (lp, rp) = (GUARD_LPAREN.as_bytes()[0], GUARD_RPAREN.as_bytes()[0]);
    while i < end {
        let c = bytes[i];
        if quote != 0 {
            if c == bs {
                i += SPEC_ESCAPE_SEQ_CHARS as usize;
                if i > end { return None; }
                continue;
            }
            if c == quote { quote = 0; }
            i += 1;
            continue;
        }
        if c == sq || c == dq { quote = c; }
        else if c == lp { depth += 1; }
        else if c == rp {
            if depth == 0 { break; }
            depth -= 1;
        } else if depth == 0 {
            if src[i..end].starts_with(GUARD_OP_OR) || src[i..end].starts_with(GUARD_OP_AND) { break; }
            if reject_cmp && CMP_OPS.iter().any(|op| src[i..end].starts_with(op)) { return None; }
        }
        i += 1;
    }
    if quote != 0 || depth != 0 { None } else { Some(i) }
}

pub(crate) fn prof_has(profs: &str, profile: &str) -> bool {
    profs.split(SEP_PIPE).any(|p| p == profile)
}

pub(crate) fn mk_prim(name: &str, args: Vec<Term>) -> Term {
    Term::Prim(name.to_string(), args)
}