refactor: extract begin end rule

上级 e2441010
use crate::rule::reg_exp_source::{RegExpSource, RegExpSourceList};
use crate::rule::{Rule, AbstractRule};
use crate::inter::ILocation;
use crate::rule::rule_factory::ICompilePatternsResult;
#[derive(Clone, Debug, Serialize)]
pub struct BeginEndRule {
#[serde(flatten)]
pub rule: Rule,
pub _begin: RegExpSource,
pub begin_captures: Vec<Box<dyn AbstractRule>>,
pub _end: RegExpSource,
// pub endHasBackReferences: Option<bool>,
pub end_captures: Vec<Box<dyn AbstractRule>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub apply_end_pattern_last: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub _cached_compiled_patterns: Option<RegExpSourceList>,
pub patterns: Vec<i32>,
pub has_missing_patterns: bool,
}
impl BeginEndRule {
pub fn new(
location: Option<ILocation>,
id: i32,
name: Option<String>,
content_name: Option<String>,
begin: String,
begin_captures: Vec<Box<dyn AbstractRule>>,
_end: String,
end_captures: Vec<Box<dyn AbstractRule>>,
apply_end_pattern_last: Option<bool>,
patterns: ICompilePatternsResult,
) -> BeginEndRule {
BeginEndRule {
rule: Rule {
_type: String::from("BeginEndRule"),
_location: location,
id,
_name: name,
_content_name: content_name,
},
_begin: RegExpSource::new(begin.clone(), id.clone()),
begin_captures,
_end: RegExpSource::new(_end.clone(), id.clone()),
end_captures,
apply_end_pattern_last,
has_missing_patterns: patterns.clone().has_missing_patterns,
patterns: patterns.patterns,
_cached_compiled_patterns: None,
}
}
}
impl AbstractRule for BeginEndRule {
fn id(&self) -> i32 { self.rule.id }
fn type_of(&self) -> String {
String::from(self.rule.clone()._type)
}
fn has_missing_pattern(&self) -> bool {
self.has_missing_patterns
}
}
pub mod rule_factory;
pub mod rule_convert;
pub mod reg_exp_source;
pub mod compiled_rule;
pub mod abstract_rule;
pub mod begin_end_rule;
pub mod compiled_rule;
pub use self::abstract_rule::AbstractRule;
pub use self::compiled_rule::CompiledRule;
// pub use self::begin_end_rule::AbstractRule;
pub use self::begin_end_rule::BeginEndRule;
use crate::inter::{ILocation, IRawGrammar, IRawRepository};
use reg_exp_source::{RegExpSource, RegExpSourceList};
......@@ -181,66 +182,6 @@ impl AbstractRule for MatchRule {
}
}
#[derive(Clone, Debug, Serialize)]
pub struct BeginEndRule {
#[serde(flatten)]
pub rule: Rule,
pub _begin: RegExpSource,
pub begin_captures: Vec<Box<dyn AbstractRule>>,
pub _end: RegExpSource,
// pub endHasBackReferences: Option<bool>,
pub end_captures: Vec<Box<dyn AbstractRule>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub apply_end_pattern_last: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub _cached_compiled_patterns: Option<RegExpSourceList>,
pub patterns: Vec<i32>,
pub has_missing_patterns: bool,
}
impl BeginEndRule {
pub fn new(
location: Option<ILocation>,
id: i32,
name: Option<String>,
content_name: Option<String>,
begin: String,
begin_captures: Vec<Box<dyn AbstractRule>>,
_end: String,
end_captures: Vec<Box<dyn AbstractRule>>,
apply_end_pattern_last: Option<bool>,
patterns: ICompilePatternsResult,
) -> BeginEndRule {
BeginEndRule {
rule: Rule {
_type: String::from("BeginEndRule"),
_location: location,
id,
_name: name,
_content_name: content_name,
},
_begin: RegExpSource::new(begin.clone(), id.clone()),
begin_captures,
_end: RegExpSource::new(_end.clone(), id.clone()),
end_captures,
apply_end_pattern_last,
has_missing_patterns: patterns.clone().has_missing_patterns,
patterns: patterns.patterns,
_cached_compiled_patterns: None,
}
}
}
impl AbstractRule for BeginEndRule {
fn id(&self) -> i32 { self.rule.id }
fn type_of(&self) -> String {
String::from(self.rule.clone()._type)
}
fn has_missing_pattern(&self) -> bool {
self.has_missing_patterns
}
}
#[derive(Clone, Debug, Serialize)]
pub struct CaptureRule {
pub rule: Rule,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册