Skip to content

Commit

Permalink
Mark start_states and target_state as deprecated.
Browse files Browse the repository at this point in the history
  • Loading branch information
ltratt committed Dec 21, 2024
1 parent 78406d0 commit 382eba9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
4 changes: 2 additions & 2 deletions lrlex/src/lib/ctbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ pub fn lexerdef() -> {lexerdef_type} {{
Some(ref n) => format!("Some({}.to_string())", quote!(#n)),
None => "None".to_owned(),
};
let target_state = match &r.target_state {
let target_state = match &r.target_state() {
Some((id, op)) => format!("Some(({}, ::lrlex::StartStateOperation::{:?}))", id, op),
None => "None".to_owned(),
};
Expand All @@ -501,7 +501,7 @@ pub fn lexerdef() -> {lexerdef_type} {{
r.name_span().end()
);
let regex = &r.re_str;
let start_states = r.start_states.as_slice();
let start_states = r.start_states();
write!(
outs,
"
Expand Down
8 changes: 6 additions & 2 deletions lrlex/src/lib/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ pub struct Rule<StorageT> {
pub(super) re_str: String,
re: Regex,
/// Id(s) of permitted start conditions for the lexer to match this rule.
#[deprecated(note = "Use the start_states() function")]
pub start_states: Vec<usize>,
/// If Some(_), successful matching of this rule will cause the current stack of start
/// conditions in the lexer to be updated with the enclosed value, using the designated
/// operation.
/// If None, successful matching causes no change to the current start condition.
#[deprecated(note = "Use the target_state() function")]
pub target_state: Option<(usize, StartStateOperation)>,
}

Expand Down Expand Up @@ -158,11 +160,13 @@ impl<StorageT: PrimInt> Rule<StorageT> {

/// Return the IDs of the permitted start conditions for the lexer to match this rule.
pub fn start_states(&self) -> &[usize] {
#[allow(deprecated)]
self.start_states.as_slice()
}

/// Return the IDs of the permitted start conditions for the lexer to match this rule.
pub fn target_state(&self) -> Option<(usize, StartStateOperation)> {
#[allow(deprecated)]
self.target_state.clone()
}
}
Expand Down Expand Up @@ -412,7 +416,7 @@ where
Some((_, s)) => s,
};
for (ridx, r) in self.iter_rules().enumerate() {
if !Self::state_matches(current_state, &r.start_states) {
if !Self::state_matches(current_state, r.start_states()) {
continue;
}
if let Some(m) = r.re.find(&s[old_i..]) {
Expand All @@ -438,7 +442,7 @@ where
}
}
}
if let Some((target_state_id, op)) = &r.target_state {
if let Some((target_state_id, op)) = &r.target_state() {
let state = match self.get_start_state_by_id(*target_state_id) {
None => {
// TODO: I can see an argument for lexing state to be either `None` or `Some(target_state_id)` here
Expand Down
66 changes: 33 additions & 33 deletions lrlex/src/lib/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,9 @@ mod test {
let intrule = ast.get_rule(0).unwrap();
assert_eq!("known", intrule.name().unwrap());
assert_eq!(".", intrule.re_str);
assert!(intrule.target_state.is_none());
assert_eq!(1, intrule.start_states.len());
assert_eq!(1, *intrule.start_states.first().unwrap());
assert!(intrule.target_state().is_none());
assert_eq!(1, intrule.start_states().len());
assert_eq!(1, *intrule.start_states().first().unwrap());
}

#[test]
Expand Down Expand Up @@ -1066,9 +1066,9 @@ mod test {
assert_eq!(".", intrule.re_str);
assert_eq!(
(1, StartStateOperation::ReplaceStack),
*intrule.target_state.as_ref().unwrap()
*intrule.target_state().as_ref().unwrap()
);
assert_eq!(0, intrule.start_states.len());
assert_eq!(0, intrule.start_states().len());
}

#[test]
Expand Down Expand Up @@ -1420,130 +1420,130 @@ a\[\]a 'aboxa'
let mut rule = ast.get_rule_by_name("OPEN_BRACE").unwrap();
assert_eq!("OPEN_BRACE", rule.name().unwrap());
assert_eq!(r"\{", rule.re_str);
assert_eq!(1, rule.start_states.len());
assert_eq!(1, rule.start_states().len());
assert_eq!(
"brace",
rule.start_states
rule.start_states()
.iter()
.map(|s| states.get(s).unwrap())
.next()
.unwrap()
);
assert!(rule.target_state.is_some());
assert!(rule.target_state().is_some());
assert_eq!(
"brace",
rule.target_state
rule.target_state()
.as_ref()
.map(|(s, _)| states.get(s).unwrap())
.unwrap()
);
assert_eq!(
StartStateOperation::Push,
*rule.target_state.as_ref().map(|(_, s)| s).unwrap()
*rule.target_state().as_ref().map(|(_, s)| s).unwrap()
);
rule = ast.get_rule_by_name("CLOSE_BRACE").unwrap();
assert_eq!("CLOSE_BRACE", rule.name().unwrap());
assert_eq!(r"\}", rule.re_str);
assert_eq!(1, rule.start_states.len());
assert_eq!(1, rule.start_states().len());
assert_eq!(
"brace",
rule.start_states
rule.start_states()
.iter()
.map(|s| states.get(s).unwrap())
.next()
.unwrap()
);
assert!(rule.target_state.is_some());
assert!(rule.target_state().is_some());
assert_eq!(
"brace",
rule.target_state
rule.target_state()
.as_ref()
.map(|(s, _)| states.get(s).unwrap())
.unwrap()
);
assert_eq!(
StartStateOperation::Pop,
*rule.target_state.as_ref().map(|(_, s)| s).unwrap()
*rule.target_state().as_ref().map(|(_, s)| s).unwrap()
);
rule = ast.get_rule_by_name("OPEN_BRACKET").unwrap();
assert_eq!("OPEN_BRACKET", rule.name().unwrap());
assert_eq!(r"\[", rule.re_str);
assert_eq!(1, rule.start_states.len());
assert_eq!(1, rule.start_states().len());
assert_eq!(
"bracket",
rule.start_states
rule.start_states()
.iter()
.map(|s| states.get(s).unwrap())
.next()
.unwrap()
);
assert!(rule.target_state.is_some());
assert!(rule.target_state().is_some());
assert_eq!(
"bracket",
rule.target_state
rule.target_state()
.as_ref()
.map(|(s, _)| states.get(s).unwrap())
.unwrap()
);
assert_eq!(
StartStateOperation::Push,
*rule.target_state.as_ref().map(|(_, s)| s).unwrap()
*rule.target_state().as_ref().map(|(_, s)| s).unwrap()
);
rule = ast.get_rule_by_name("CLOSE_BRACKET").unwrap();
assert_eq!("CLOSE_BRACKET", rule.name().unwrap());
assert_eq!(r"\]", rule.re_str);
assert_eq!(1, rule.start_states.len());
assert_eq!(1, rule.start_states().len());
assert_eq!(
"bracket",
rule.start_states
rule.start_states()
.iter()
.map(|s| states.get(s).unwrap())
.next()
.unwrap()
);
assert!(rule.target_state.is_some());
assert!(rule.target_state().is_some());
assert_eq!(
"bracket",
rule.target_state
rule.target_state()
.as_ref()
.map(|(s, _)| states.get(s).unwrap())
.unwrap()
);
assert_eq!(
StartStateOperation::Pop,
*rule.target_state.as_ref().map(|(_, s)| s).unwrap()
*rule.target_state().as_ref().map(|(_, s)| s).unwrap()
);
rule = ast.get_rule_by_name("OPEN_FIRST_BRACE").unwrap();
assert_eq!("OPEN_FIRST_BRACE", rule.name().unwrap());
assert_eq!(r"\{", rule.re_str);
assert_eq!(0, rule.start_states.len());
assert!(rule.target_state.is_some());
assert_eq!(0, rule.start_states().len());
assert!(rule.target_state().is_some());
assert_eq!(
"brace",
rule.target_state
rule.target_state()
.as_ref()
.map(|(s, _)| states.get(s).unwrap())
.unwrap()
);
assert_eq!(
StartStateOperation::ReplaceStack,
*rule.target_state.as_ref().map(|(_, s)| s).unwrap()
*rule.target_state().as_ref().map(|(_, s)| s).unwrap()
);
rule = ast.get_rule_by_name("OPEN_FIRST_BRACKET").unwrap();
assert_eq!("OPEN_FIRST_BRACKET", rule.name().unwrap());
assert_eq!(r"\[", rule.re_str);
assert_eq!(0, rule.start_states.len());
assert!(rule.target_state.is_some());
assert_eq!(0, rule.start_states().len());
assert!(rule.target_state().is_some());
assert_eq!(
"bracket",
rule.target_state
rule.target_state()
.as_ref()
.map(|(s, _)| states.get(s).unwrap())
.unwrap()
);
assert_eq!(
StartStateOperation::ReplaceStack,
*rule.target_state.as_ref().map(|(_, s)| s).unwrap()
*rule.target_state().as_ref().map(|(_, s)| s).unwrap()
);
}

Expand Down

0 comments on commit 382eba9

Please sign in to comment.