Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace spec indices search with regex #166

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions src/patch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,22 +569,37 @@ fn make_cpu(cmod: &Hash) -> Result<CpuBuilder> {
}

/// Find left and right indices of enumeration token in specification string
fn spec_ind(spec: &str) -> (usize, usize) {
///
/// # Examples
///
/// ```ignore
/// let cases = [
/// ("RELOAD?", (6, 0)),
/// ("TMR[1-57]_MUX", (3, 4)),
/// ("DT[1-3]?", (2, 0)),
/// ("GPIO[ABCDE]", (4, 0)),
/// ("CSPT[1][7-9],CSPT[2][0-5]", (4, 0)),
/// ];
/// for (spec, (li, ri)) in cases {
/// assert_eq!(spec_ind(spec), Some((li, ri)));
/// }
/// ```
///
fn spec_ind(spec: &str) -> Option<(usize, usize)> {
use once_cell::sync::Lazy;
use regex::Regex;
let spec = spec.split(',').next().unwrap_or(spec);
let li = spec
.bytes()
.position(|b| b == b'*')
.or_else(|| spec.bytes().position(|b| b == b'?'))
.or_else(|| spec.bytes().position(|b| b == b'['))
.unwrap();
let ri = spec
.bytes()
.rev()
.position(|b| b == b'*')
.or_else(|| spec.bytes().rev().position(|b| b == b'?'))
.or_else(|| spec.bytes().rev().position(|b| b == b']'))
.unwrap();
(li, ri)
static RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^\w*((?:[\?*]|\[\d+(?:-\d+)?\]|\[[a-zA-Z]+(?:-[a-zA-Z]+)?\])+)\w*$").unwrap()
});
let Some(caps) = RE.captures(spec) else {
return None;
};
let spec = caps.get(0).unwrap();
let token = caps.get(1).unwrap();
let li = token.start();
let ri = spec.end() - token.end();
Some((li, ri))
}

fn check_offsets(offsets: &[u32], dim_increment: u32) -> bool {
Expand Down
26 changes: 21 additions & 5 deletions src/patch/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,11 @@ fn collect_in_array(
return Err(anyhow!("{path}: registers {rspec} not found"));
}
registers.sort_by_key(|r| r.address_offset);
let (li, ri) = spec_ind(rspec);
let Some((li, ri)) = spec_ind(rspec) else {
return Err(anyhow!(
"`{rspec}` contains no tokens or contains more than one token"
));
};
let dim = registers.len();
let dim_index = if rmod.contains_key(&"_start_from_zero".to_yaml()) {
(0..dim).map(|v| v.to_string()).collect::<Vec<_>>()
Expand Down Expand Up @@ -1241,10 +1245,18 @@ fn collect_in_cluster(
.iter()
.map(|r| {
let match_rspec = matchsubspec(&r.name, rspec).unwrap();
let (li, ri) = spec_ind(match_rspec);
r.name[li..r.name.len() - ri].to_string()
let Some((li, ri)) = spec_ind(match_rspec) else {
return Err(anyhow!(
"`{match_rspec}` contains no tokens or contains more than one token"
));
};
Ok(r.name[li..r.name.len() - ri].to_string())
})
.collect::<Vec<_>>();
.collect::<Result<Vec<_>, _>>();
let new_dim_index = match new_dim_index {
Ok(v) => v,
Err(e) => return Err(e),
};
if first {
dim = registers.len();
dim_index = new_dim_index;
Expand Down Expand Up @@ -1316,7 +1328,11 @@ fn collect_in_cluster(
reg.name = if let Some(name) = rmod.get_str("name")? {
name.into()
} else {
let (li, ri) = spec_ind(&rspec);
let Some((li, ri)) = spec_ind(&rspec) else {
return Err(anyhow!(
"`{rspec}` contains no tokens or contains more than one token"
));
};
format!("{}{}", &rspec[..li], &rspec[rspec.len() - ri..])
};
if let Some(desc) = rmod.get_str("description")? {
Expand Down
6 changes: 5 additions & 1 deletion src/patch/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,11 @@ impl RegisterExt for Register {
return Err(anyhow!("{}: fields {fspec} not found", self.name));
}
fields.sort_by_key(|f| f.bit_range.offset);
let (li, ri) = spec_ind(fspec);
let Some((li, ri)) = spec_ind(fspec) else {
return Err(anyhow!(
"`{fspec}` contains no tokens or contains more than one token"
));
};
let dim = fields.len();
let dim_index = if fmod.contains_key(&"_start_from_zero".to_yaml()) {
(0..dim).map(|v| v.to_string()).collect::<Vec<_>>()
Expand Down
Loading