Skip to content

Commit

Permalink
add fancy_regex dep for supporting look-ahead and look-behind; close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaza committed Feb 4, 2024
1 parent eee766c commit 49e9ac2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ itertools = "0.10.3"
pdb = "0.8.0"
petgraph = "0.6.2"
regex = "1.5"
fancy-regex = { git = "https://github.com/mnaza/fancy-regex.git" }
serde = { version = "1", features = ["derive"] }
smda = { git = "https://github.com/marirs/smda-rs.git" }
thiserror = "1"
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub enum Error {
#[error("{0}")]
RegexError(#[from] regex::Error),
#[error("{0}")]
FancyRegexError(#[from] fancy_regex::Error),
#[error("{0}")]
FromHexError(#[from] hex::FromHexError),
#[error("{0}")]
YamlError(#[from] yaml_rust::ScanError),
Expand Down Expand Up @@ -43,6 +45,8 @@ pub enum Error {
InvalidScope(u32, String),
#[error("invalid static scope: {0}")]
InvalidStaticScope(u32),
#[error("{0}")]
UndefinedComType(String),
#[error("invalid dynamic scope: {0}")]
InvalidDynamicScope(u32),
#[error("{0}")]
Expand Down
42 changes: 34 additions & 8 deletions src/rules/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ use crate::{rules::Value, Error, Result};

use super::{Scope, Scopes};

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub enum ComType {
Class,
Interface,
}

impl TryInto<ComType> for &str {
type Error = Error;

fn try_into(self) -> std::result::Result<ComType, Self::Error> {
match self {
"class" => Ok(ComType::Class),
"interface" => Ok(ComType::Interface),
_ => Err(Error::UndefinedComType(self.to_string())),
}
}
}

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub enum FeatureAccess {
Read,
Expand Down Expand Up @@ -40,6 +58,7 @@ pub enum RuleFeatureType {
Class,
OperandNumber(usize),
OperandOffset(usize),
ComType(ComType),
}

pub trait FeatureT {
Expand Down Expand Up @@ -198,6 +217,10 @@ impl Feature {
RuleFeatureType::OperandOffset(a) => Ok(Feature::OperandOffset(
OperandOffsetFeature::new(&a, &value.get_int()? as &i128, description)?,
)),
RuleFeatureType::ComType(_ct) => {
//TODO
unimplemented!()
}
}
}

Expand Down Expand Up @@ -1135,7 +1158,7 @@ impl SubstringFeature {
pub struct RegexFeature {
value: String,
_description: String,
re: regex::bytes::Regex,
re: fancy_regex::Regex,
scopes: HashSet<Scope>,
}

Expand All @@ -1145,15 +1168,15 @@ impl RegexFeature {
if value.ends_with("/i") {
rre = r"(?-u)(?s)(?i)".to_string() + &value["/".len()..value.len() - "/i".len()];
}
rre = rre.replace("\\/", "/");
rre = rre.replace("\\\"", "\"");
rre = rre.replace("\\'", "'");
rre = rre.replace("\\%", "%");
let rr = match regex::bytes::Regex::new(&rre) {
// rre = rre.replace("\\/", "/");
// rre = rre.replace("\\\"", "\"");
// rre = rre.replace("\\'", "'");
// rre = rre.replace("\\%", "%");
let rr = match fancy_regex::Regex::new(&rre) {
Ok(s) => s,
Err(e) => {
println!("{:?}", e);
return Err(Error::RegexError(e));
return Err(Error::FancyRegexError(e));
}
};
Ok(RegexFeature {
Expand All @@ -1180,8 +1203,11 @@ impl RegexFeature {
let mut ll = vec![];
for (feature, locations) in features {
if let Feature::String(s) = feature {
if self.re.find(s.value.as_bytes()).is_some() {
if let Ok(Some(_)) = self.re.find(s.value.as_bytes()) {
// eprintln!("true {}\t{}", self.re.as_str(), s.value);
ll.extend(locations);
} else {
// eprintln!("false {}\t{}", self.re.as_str(), s.value);
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ use statement::{
use std::collections::HashMap;
use yaml_rust::{yaml::Hash, Yaml, YamlLoader};

use self::features::ComType;

const MAX_BYTES_FEATURE_SIZE: usize = 0x100;

fn translate_com_features(_name: &str, _com_type: &ComType) -> Vec<StatementElement> {
//TODO
vec![]
}

#[derive(Debug)]
pub enum Value {
Str(String),
Expand Down Expand Up @@ -867,6 +874,31 @@ impl Rule {
))
}
}
} else if kkey.starts_with("com/") {
let com_type_name = &kkey["com/".len()..];
let com_type: ComType = com_type_name.try_into()?;
let val = match &d[key] {
Yaml::String(s) => s.clone(),
Yaml::Integer(i) => i.to_string(),
_ => return Err(Error::InvalidRule(line!(), format!("{:?}", d[key]))),
};
let description = match &d.get(&Yaml::String("description".to_string())) {
Some(Yaml::String(s)) => Some(s.clone()),
_ => None,
};
let (value, description) = Rule::parse_description(
&val,
&RuleFeatureType::ComType(com_type.clone()),
&description,
)?;
let d = match description {
Some(s) => s,
_ => "".to_string(),
};
let ff = translate_com_features(&value.get_str()?, &com_type);
return Ok(StatementElement::Statement(Box::new(Statement::Or(
OrStatement::new(ff, &d)?,
))));
} else {
let feature_type = Rule::parse_feature_type(kkey)?;
let val = match &d[key] {
Expand Down

0 comments on commit 49e9ac2

Please sign in to comment.