fix: fix lint issues

上级 96ab6a0e
...@@ -113,7 +113,6 @@ impl AbstractRule for BeginEndRule { ...@@ -113,7 +113,6 @@ impl AbstractRule for BeginEndRule {
} }
// todo: support for hasBackReferences // todo: support for hasBackReferences
cached_compiled_patterns cached_compiled_patterns.compile(grammar, allow_a, allow_g)
.compile(grammar, allow_a, allow_g)
} }
} }
...@@ -102,6 +102,6 @@ impl AbstractRule for BeginWhileRule { ...@@ -102,6 +102,6 @@ impl AbstractRule for BeginWhileRule {
return cached_compiled_patterns return cached_compiled_patterns
.compile(grammar, allow_a, allow_g) .compile(grammar, allow_a, allow_g)
.clone() .clone();
} }
} }
...@@ -72,6 +72,6 @@ impl AbstractRule for MatchRule { ...@@ -72,6 +72,6 @@ impl AbstractRule for MatchRule {
return cached_compiled_patterns return cached_compiled_patterns
.compile(grammar, allow_a, allow_g) .compile(grammar, allow_a, allow_g)
.clone() .clone();
} }
} }
...@@ -8,8 +8,8 @@ pub mod compiled_rule; ...@@ -8,8 +8,8 @@ pub mod compiled_rule;
pub mod empty_rule; pub mod empty_rule;
pub mod include_only_rule; pub mod include_only_rule;
pub mod match_rule; pub mod match_rule;
pub mod rule;
pub mod reg_exp_source; pub mod reg_exp_source;
pub mod rule;
pub use self::abstract_rule::AbstractRule; pub use self::abstract_rule::AbstractRule;
pub use self::begin_end_rule::BeginEndRule; pub use self::begin_end_rule::BeginEndRule;
...@@ -19,9 +19,9 @@ pub use self::compiled_rule::CompiledRule; ...@@ -19,9 +19,9 @@ pub use self::compiled_rule::CompiledRule;
pub use self::empty_rule::EmptyRule; pub use self::empty_rule::EmptyRule;
pub use self::include_only_rule::IncludeOnlyRule; pub use self::include_only_rule::IncludeOnlyRule;
pub use self::match_rule::MatchRule; pub use self::match_rule::MatchRule;
pub use self::rule::Rule;
pub use self::reg_exp_source::RegExpSource; pub use self::reg_exp_source::RegExpSource;
pub use self::reg_exp_source::RegExpSourceList; pub use self::reg_exp_source::RegExpSourceList;
pub use self::rule::Rule;
use crate::grammar::Grammar; use crate::grammar::Grammar;
use crate::inter::{IRawGrammar, IRawRepository}; use crate::inter::{IRawGrammar, IRawRepository};
...@@ -44,4 +44,3 @@ pub trait IGrammarRegistry { ...@@ -44,4 +44,3 @@ pub trait IGrammarRegistry {
} }
pub trait IRuleFactoryHelper: IGrammarRegistry + IRuleRegistry {} pub trait IRuleFactoryHelper: IGrammarRegistry + IRuleRegistry {}
use crate::rule::CompiledRule;
use crate::grammar::Grammar; use crate::grammar::Grammar;
use crate::rule::CompiledRule;
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
pub struct AnchorCache { pub struct AnchorCache {
...@@ -70,7 +70,7 @@ impl RegExpSourceList { ...@@ -70,7 +70,7 @@ impl RegExpSourceList {
.collect::<Vec<i32>>(); .collect::<Vec<i32>>();
let compiled_rule = CompiledRule::new(reg_exps, rules); let compiled_rule = CompiledRule::new(reg_exps, rules);
self._cached = Some(compiled_rule.clone()); self._cached = Some(compiled_rule.clone());
return compiled_rule return compiled_rule;
} }
} else { } else {
println!("// todo: cached {:?}", self._items); println!("// todo: cached {:?}", self._items);
......
pub mod scanner; pub mod scanner;
\ No newline at end of file
use onig::{Regex}; use onig::Regex;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct IOnigCaptureIndex { pub struct IOnigCaptureIndex {
pub start: usize, pub start: usize,
...@@ -23,17 +22,18 @@ pub struct Scanner { ...@@ -23,17 +22,18 @@ pub struct Scanner {
impl Scanner { impl Scanner {
pub fn new(patterns: Vec<String>) -> Self { pub fn new(patterns: Vec<String>) -> Self {
Scanner { Scanner { index: 0, patterns }
index: 0,
patterns,
}
} }
pub fn dispose(&mut self) { pub fn dispose(&mut self) {
self.index = 0 self.index = 0
} }
pub fn find_next_match_sync(&mut self, origin_str: String, start_position: i32) -> Option<IOnigMatch> { pub fn find_next_match_sync(
&mut self,
origin_str: String,
start_position: i32,
) -> Option<IOnigMatch> {
if self.index >= self.patterns.clone().len() { if self.index >= self.patterns.clone().len() {
self.index = 0; self.index = 0;
return None; return None;
...@@ -41,7 +41,7 @@ impl Scanner { ...@@ -41,7 +41,7 @@ impl Scanner {
let mut after_pos_str = String::from(""); let mut after_pos_str = String::from("");
let mut start_pos = start_position; let mut start_pos = start_position;
let mut string_vec = origin_str.graphemes(true).collect::<Vec<&str>>(); let string_vec = origin_str.graphemes(true).collect::<Vec<&str>>();
let mut has_utf8 = false; let mut has_utf8 = false;
if string_vec.len() != origin_str.len() { if string_vec.len() != origin_str.len() {
...@@ -83,7 +83,8 @@ impl Scanner { ...@@ -83,7 +83,8 @@ impl Scanner {
if has_utf8 { if has_utf8 {
let x1 = after_pos_str.split_at(end).0; let x1 = after_pos_str.split_at(end).0;
let utf8_end = before_vec.len() + x1.graphemes(true).collect::<Vec<&str>>().len() + 1; let utf8_end =
before_vec.len() + x1.graphemes(true).collect::<Vec<&str>>().len() + 1;
let utf8_start = utf8_end - length; let utf8_start = utf8_end - length;
capture = IOnigCaptureIndex { capture = IOnigCaptureIndex {
...@@ -112,10 +113,9 @@ impl Scanner { ...@@ -112,10 +113,9 @@ impl Scanner {
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::scanner::scanner::{Scanner, IOnigMatch}; use crate::scanner::scanner::Scanner;
#[test] #[test]
fn should_handle_simple_regex() { fn should_handle_simple_regex() {
...@@ -144,14 +144,35 @@ mod tests { ...@@ -144,14 +144,35 @@ mod tests {
assert_eq!(true, false); assert_eq!(true, false);
} }
let result = scanner.find_next_match_sync(String::from("xxaxxbxxc"), 0).unwrap(); let result = scanner
assert_eq!(serde_json::to_string(&result).unwrap(), String::from("{\"index\":0,\"capture_indices\":[{\"start\":2,\"end\":3,\"length\":1}]}")); .find_next_match_sync(String::from("xxaxxbxxc"), 0)
.unwrap();
let result2 = scanner.find_next_match_sync(String::from("xxaxxbxxc"), 4).unwrap(); assert_eq!(
assert_eq!(serde_json::to_string(&result2).unwrap(), String::from("{\"index\":1,\"capture_indices\":[{\"start\":5,\"end\":6,\"length\":1}]}")); serde_json::to_string(&result).unwrap(),
String::from(
let result3 = scanner.find_next_match_sync(String::from("xxaxxbxxc"), 7).unwrap(); "{\"index\":0,\"capture_indices\":[{\"start\":2,\"end\":3,\"length\":1}]}"
assert_eq!(serde_json::to_string(&result3).unwrap(), String::from("{\"index\":2,\"capture_indices\":[{\"start\":8,\"end\":9,\"length\":1}]}")); )
);
let result2 = scanner
.find_next_match_sync(String::from("xxaxxbxxc"), 4)
.unwrap();
assert_eq!(
serde_json::to_string(&result2).unwrap(),
String::from(
"{\"index\":1,\"capture_indices\":[{\"start\":5,\"end\":6,\"length\":1}]}"
)
);
let result3 = scanner
.find_next_match_sync(String::from("xxaxxbxxc"), 7)
.unwrap();
assert_eq!(
serde_json::to_string(&result3).unwrap(),
String::from(
"{\"index\":2,\"capture_indices\":[{\"start\":8,\"end\":9,\"length\":1}]}"
)
);
if let None = scanner.find_next_match_sync(String::from("xxaxxbxxc"), 9) { if let None = scanner.find_next_match_sync(String::from("xxaxxbxxc"), 9) {
assert_eq!(true, true); assert_eq!(true, true);
...@@ -165,35 +186,84 @@ mod tests { ...@@ -165,35 +186,84 @@ mod tests {
let regex = vec![String::from("1"), String::from("2")]; let regex = vec![String::from("1"), String::from("2")];
let mut scanner = Scanner::new(regex); let mut scanner = Scanner::new(regex);
let result = scanner.find_next_match_sync(String::from("ab…cde21"), 5).unwrap(); let result = scanner
assert_eq!(serde_json::to_string(&result).unwrap(), String::from("{\"index\":0,\"capture_indices\":[{\"start\":8,\"end\":9,\"length\":1}]}")); .find_next_match_sync(String::from("ab…cde21"), 5)
.unwrap();
assert_eq!(
serde_json::to_string(&result).unwrap(),
String::from(
"{\"index\":0,\"capture_indices\":[{\"start\":8,\"end\":9,\"length\":1}]}"
)
);
} }
#[test] #[test]
fn should_handle_unicode2() { fn should_handle_unicode2() {
let mut scanner2 = Scanner::new(vec![String::from("\"")]); let mut scanner2 = Scanner::new(vec![String::from("\"")]);
let result2 = scanner2.find_next_match_sync(String::from("{\"\": 1}"), 1).unwrap(); let result2 = scanner2
assert_eq!(serde_json::to_string(&result2).unwrap(), String::from("{\"index\":0,\"capture_indices\":[{\"start\":2,\"end\":3,\"length\":1}]}")); .find_next_match_sync(String::from("{\"\": 1}"), 1)
.unwrap();
assert_eq!(
serde_json::to_string(&result2).unwrap(),
String::from(
"{\"index\":0,\"capture_indices\":[{\"start\":2,\"end\":3,\"length\":1}]}"
)
);
} }
#[test] #[test]
fn should_handle_unicode3() { fn should_handle_unicode3() {
let regex = vec![String::from("Y"), String::from("X")]; let regex = vec![String::from("Y"), String::from("X")];
let mut scanner = Scanner::new(regex); let mut scanner = Scanner::new(regex);
let result = scanner.find_next_match_sync(String::from("a💻bYX"), 0).unwrap(); let result = scanner
assert_eq!(serde_json::to_string(&result).unwrap(), String::from("{\"index\":0,\"capture_indices\":[{\"start\":4,\"end\":5,\"length\":1}]}")); .find_next_match_sync(String::from("a💻bYX"), 0)
.unwrap();
let result1 = scanner.find_next_match_sync(String::from("a💻bYX"), 1).unwrap(); assert_eq!(
assert_eq!(serde_json::to_string(&result1).unwrap(), String::from("{\"index\":0,\"capture_indices\":[{\"start\":4,\"end\":5,\"length\":1}]}")); serde_json::to_string(&result).unwrap(),
String::from(
let result2 = scanner.find_next_match_sync(String::from("a💻bYX"), 2).unwrap(); "{\"index\":0,\"capture_indices\":[{\"start\":4,\"end\":5,\"length\":1}]}"
assert_eq!(serde_json::to_string(&result2).unwrap(), String::from("{\"index\":0,\"capture_indices\":[{\"start\":4,\"end\":5,\"length\":1}]}")); )
);
let result3 = scanner.find_next_match_sync(String::from("a💻bYX"), 3).unwrap();
assert_eq!(serde_json::to_string(&result3).unwrap(), String::from("{\"index\":0,\"capture_indices\":[{\"start\":4,\"end\":5,\"length\":1}]}")); let result1 = scanner
.find_next_match_sync(String::from("a💻bYX"), 1)
let result4 = scanner.find_next_match_sync(String::from("a💻bYX"), 4).unwrap(); .unwrap();
assert_eq!(serde_json::to_string(&result4).unwrap(), String::from("{\"index\":1,\"capture_indices\":[{\"start\":5,\"end\":6,\"length\":1}]}")); assert_eq!(
serde_json::to_string(&result1).unwrap(),
String::from(
"{\"index\":0,\"capture_indices\":[{\"start\":4,\"end\":5,\"length\":1}]}"
)
);
let result2 = scanner
.find_next_match_sync(String::from("a💻bYX"), 2)
.unwrap();
assert_eq!(
serde_json::to_string(&result2).unwrap(),
String::from(
"{\"index\":0,\"capture_indices\":[{\"start\":4,\"end\":5,\"length\":1}]}"
)
);
let result3 = scanner
.find_next_match_sync(String::from("a💻bYX"), 3)
.unwrap();
assert_eq!(
serde_json::to_string(&result3).unwrap(),
String::from(
"{\"index\":0,\"capture_indices\":[{\"start\":4,\"end\":5,\"length\":1}]}"
)
);
let result4 = scanner
.find_next_match_sync(String::from("a💻bYX"), 4)
.unwrap();
assert_eq!(
serde_json::to_string(&result4).unwrap(),
String::from(
"{\"index\":1,\"capture_indices\":[{\"start\":5,\"end\":6,\"length\":1}]}"
)
);
// let result5 = scanner.find_next_match_sync(String::from("a💻bYX"), 5).unwrap(); // let result5 = scanner.find_next_match_sync(String::from("a💻bYX"), 5).unwrap();
// assert_eq!(serde_json::to_string(&result5).unwrap(), String::from("{\"index\":1,\"capture_indices\":[{\"start\":4,\"end\":5,\"length\":1}]}")); // assert_eq!(serde_json::to_string(&result5).unwrap(), String::from("{\"index\":1,\"capture_indices\":[{\"start\":4,\"end\":5,\"length\":1}]}"));
...@@ -202,8 +272,15 @@ mod tests { ...@@ -202,8 +272,15 @@ mod tests {
#[test] #[test]
fn should_out_of_bounds() { fn should_out_of_bounds() {
let mut scanner = Scanner::new(vec![String::from("X")]); let mut scanner = Scanner::new(vec![String::from("X")]);
let result = scanner.find_next_match_sync(String::from("X💻X"), -10000).unwrap(); let result = scanner
assert_eq!(serde_json::to_string(&result).unwrap(), String::from("{\"index\":0,\"capture_indices\":[{\"start\":1,\"end\":2,\"length\":1}]}")); .find_next_match_sync(String::from("X💻X"), -10000)
.unwrap();
assert_eq!(
serde_json::to_string(&result).unwrap(),
String::from(
"{\"index\":0,\"capture_indices\":[{\"start\":1,\"end\":2,\"length\":1}]}"
)
);
let result2 = scanner.find_next_match_sync(String::from("X💻X"), 10000); let result2 = scanner.find_next_match_sync(String::from("X💻X"), 10000);
assert_eq!(format!("{:?}", result2), "None"); assert_eq!(format!("{:?}", result2), "None");
...@@ -215,7 +292,14 @@ mod tests { ...@@ -215,7 +292,14 @@ mod tests {
let result = scanner.find_next_match_sync(String::from("first-and-second"), 0); let result = scanner.find_next_match_sync(String::from("first-and-second"), 0);
assert_eq!(format!("{:?}", result), "None"); assert_eq!(format!("{:?}", result), "None");
let result2 = scanner.find_next_match_sync(String::from("first-and-second"), 5).unwrap(); let result2 = scanner
assert_eq!(serde_json::to_string(&result2).unwrap(), String::from("{\"index\":0,\"capture_indices\":[{\"start\":5,\"end\":9,\"length\":4}]}")); .find_next_match_sync(String::from("first-and-second"), 5)
.unwrap();
assert_eq!(
serde_json::to_string(&result2).unwrap(),
String::from(
"{\"index\":0,\"capture_indices\":[{\"start\":5,\"end\":9,\"length\":4}]}"
)
);
} }
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册