refactor: extract more rules

上级 f1980b93
use crate::rule::{AbstractRule, Rule};
use crate::rule::reg_exp_source::{RegExpSource, RegExpSourceList};
use crate::rule::rule_factory::ICompilePatternsResult;
use crate::inter::ILocation;
#[derive(Clone, Debug, Serialize)]
pub struct BeginWhileRule {
pub rule: Rule,
pub _begin: RegExpSource,
pub begin_captures: Vec<Box<dyn AbstractRule>>,
pub _while: Option<String>,
pub while_captures: Vec<Box<dyn AbstractRule>>,
pub apply_end_pattern_last: bool,
pub patterns: ICompilePatternsResult,
pub has_missing_patterns: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub cached_compiled_patterns: Option<RegExpSourceList>,
}
impl BeginWhileRule {
pub fn new(
location: Option<ILocation>,
id: i32,
name: Option<String>,
content_name: Option<String>,
_begin: Option<String>,
begin_captures: Vec<Box<dyn AbstractRule>>,
_while: Option<String>,
while_captures: Vec<Box<dyn AbstractRule>>,
patterns: ICompilePatternsResult,
) -> BeginWhileRule {
BeginWhileRule {
rule: Rule {
_type: String::from("BeginWhileRule"),
_location: location,
id,
_name: name,
_content_name: content_name,
},
_begin: RegExpSource::new(_begin.unwrap().clone(), id.clone()),
begin_captures,
_while,
while_captures,
apply_end_pattern_last: false,
has_missing_patterns: patterns.clone().has_missing_patterns,
patterns,
cached_compiled_patterns: None,
}
}
}
impl AbstractRule for BeginWhileRule {
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
}
}
use crate::rule::{Rule, AbstractRule};
use crate::inter::ILocation;
#[derive(Clone, Debug, Serialize)]
pub struct CaptureRule {
pub rule: Rule,
pub retokenize_captured_with_rule_id: i32,
}
impl CaptureRule {
pub fn empty() -> Self {
CaptureRule {
rule: Rule {
_type: "".to_string(),
_location: None,
id: 0,
_name: None,
_content_name: None,
},
retokenize_captured_with_rule_id: 0,
}
}
pub fn new(location: Option<ILocation>, id: i32, name: Option<String>, content_name: Option<String>, retokenize_captured_with_rule_id: i32) -> Self {
CaptureRule {
rule: Rule {
_type: String::from("CaptureRule"),
_location: location,
id,
_name: name,
_content_name: content_name,
},
retokenize_captured_with_rule_id,
}
}
}
impl AbstractRule for CaptureRule {
fn id(&self) -> i32 { self.rule.id }
fn type_of(&self) -> String { String::from(self.rule.clone()._type) }
}
use crate::rule::rule_factory::ICompilePatternsResult;
use crate::rule::{Rule, AbstractRule};
use crate::inter::ILocation;
#[derive(Clone, Debug, Serialize)]
pub struct IncludeOnlyRule {
#[serde(flatten)]
pub rule: Rule,
pub patterns: Vec<i32>,
pub has_missing_patterns: bool,
}
impl IncludeOnlyRule {
pub fn new(
location: Option<ILocation>,
id: i32,
name: Option<String>,
content_name: Option<String>,
captures: ICompilePatternsResult,
) -> Self {
IncludeOnlyRule {
rule: Rule {
_type: String::from("IncludeOnlyRule"),
_location: location,
id,
_name: name,
_content_name: content_name,
},
patterns: captures.patterns,
has_missing_patterns: captures.has_missing_patterns,
}
}
}
impl AbstractRule for IncludeOnlyRule {
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
}
}
use crate::rule::{AbstractRule, Rule};
use crate::inter::ILocation;
use crate::rule::reg_exp_source::RegExpSource;
#[derive(Clone, Debug, Serialize)]
pub struct MatchRule {
pub rule: Rule,
pub _match: RegExpSource,
pub captures: Vec<Box<dyn AbstractRule>>,
}
impl MatchRule {
pub fn new(
location: Option<ILocation>,
id: i32,
name: Option<String>,
_match: String,
captures: Vec<Box<dyn AbstractRule>>,
) -> Self {
MatchRule {
rule: Rule {
_type: String::from("MatchRule"),
_location: location,
id,
_name: name,
_content_name: None,
},
_match: RegExpSource::new(_match, id),
captures,
}
}
}
impl AbstractRule for MatchRule {
fn id(&self) -> i32 { self.rule.id }
fn type_of(&self) -> String {
String::from(self.rule.clone()._type)
}
}
......@@ -4,11 +4,19 @@ pub mod reg_exp_source;
pub mod abstract_rule;
pub mod begin_end_rule;
pub mod begin_while_rule;
pub mod compiled_rule;
pub mod include_only_rule;
pub mod match_rule;
pub mod capture_rule;
pub use self::abstract_rule::AbstractRule;
pub use self::compiled_rule::CompiledRule;
pub use self::begin_end_rule::BeginEndRule;
pub use self::begin_while_rule::BeginWhileRule;
pub use self::include_only_rule::IncludeOnlyRule;
pub use self::match_rule::MatchRule;
pub use self::capture_rule::CaptureRule;
use crate::inter::{ILocation, IRawGrammar, IRawRepository};
use reg_exp_source::{RegExpSource, RegExpSourceList};
......@@ -45,181 +53,6 @@ impl Rule {
}
}
#[derive(Clone, Debug, Serialize)]
pub struct IncludeOnlyRule {
#[serde(flatten)]
pub rule: Rule,
pub patterns: Vec<i32>,
pub has_missing_patterns: bool,
}
impl IncludeOnlyRule {
pub fn new(
location: Option<ILocation>,
id: i32,
name: Option<String>,
content_name: Option<String>,
captures: ICompilePatternsResult,
) -> Self {
IncludeOnlyRule {
rule: Rule {
_type: String::from("IncludeOnlyRule"),
_location: location,
id,
_name: name,
_content_name: content_name,
},
patterns: captures.patterns,
has_missing_patterns: captures.has_missing_patterns,
}
}
}
impl AbstractRule for IncludeOnlyRule {
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 BeginWhileRule {
pub rule: Rule,
pub _begin: RegExpSource,
pub begin_captures: Vec<Box<dyn AbstractRule>>,
pub _while: Option<String>,
pub while_captures: Vec<Box<dyn AbstractRule>>,
pub apply_end_pattern_last: bool,
pub patterns: ICompilePatternsResult,
pub has_missing_patterns: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub cached_compiled_patterns: Option<RegExpSourceList>,
}
impl BeginWhileRule {
pub fn new(
location: Option<ILocation>,
id: i32,
name: Option<String>,
content_name: Option<String>,
_begin: Option<String>,
begin_captures: Vec<Box<dyn AbstractRule>>,
_while: Option<String>,
while_captures: Vec<Box<dyn AbstractRule>>,
patterns: ICompilePatternsResult,
) -> BeginWhileRule {
BeginWhileRule {
rule: Rule {
_type: String::from("BeginWhileRule"),
_location: location,
id,
_name: name,
_content_name: content_name,
},
_begin: RegExpSource::new(_begin.unwrap().clone(), id.clone()),
begin_captures,
_while,
while_captures,
apply_end_pattern_last: false,
has_missing_patterns: patterns.clone().has_missing_patterns,
patterns,
cached_compiled_patterns: None,
}
}
}
impl AbstractRule for BeginWhileRule {
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 MatchRule {
pub rule: Rule,
pub _match: RegExpSource,
pub captures: Vec<Box<dyn AbstractRule>>,
}
impl MatchRule {
pub fn new(
location: Option<ILocation>,
id: i32,
name: Option<String>,
_match: String,
captures: Vec<Box<dyn AbstractRule>>,
) -> Self {
MatchRule {
rule: Rule {
_type: String::from("MatchRule"),
_location: location,
id,
_name: name,
_content_name: None,
},
_match: RegExpSource::new(_match, id),
captures,
}
}
}
impl AbstractRule for MatchRule {
fn id(&self) -> i32 { self.rule.id }
fn type_of(&self) -> String {
String::from(self.rule.clone()._type)
}
}
#[derive(Clone, Debug, Serialize)]
pub struct CaptureRule {
pub rule: Rule,
pub retokenize_captured_with_rule_id: i32,
}
impl CaptureRule {
pub fn empty() -> Self {
CaptureRule {
rule: Rule {
_type: "".to_string(),
_location: None,
id: 0,
_name: None,
_content_name: None,
},
retokenize_captured_with_rule_id: 0,
}
}
pub fn new(location: Option<ILocation>, id: i32, name: Option<String>, content_name: Option<String>, retokenize_captured_with_rule_id: i32) -> Self {
CaptureRule {
rule: Rule {
_type: String::from("CaptureRule"),
_location: location,
id,
_name: name,
_content_name: content_name,
},
retokenize_captured_with_rule_id,
}
}
}
impl AbstractRule for CaptureRule {
fn id(&self) -> i32 { self.rule.id }
fn type_of(&self) -> String { String::from(self.rule.clone()._type) }
}
#[derive(Clone, Debug, Serialize)]
pub struct NoneRule {}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册