feat: add collect pattern for match

上级 c001f911
...@@ -9,7 +9,7 @@ pub trait AbstractRule: DynClone + erased_serde::Serialize { ...@@ -9,7 +9,7 @@ pub trait AbstractRule: DynClone + erased_serde::Serialize {
fn has_missing_pattern(&self) -> bool { fn has_missing_pattern(&self) -> bool {
false false
} }
fn collect_patterns_recursive(&mut self, grammar: &mut Grammar, out: Option<RegExpSourceList>, is_first: bool) { fn collect_patterns_recursive(&mut self, grammar: &mut Grammar, out: &mut RegExpSourceList, is_first: bool) {
} }
fn compile( fn compile(
&mut self, &mut self,
......
...@@ -46,18 +46,19 @@ impl AbstractRule for IncludeOnlyRule { ...@@ -46,18 +46,19 @@ impl AbstractRule for IncludeOnlyRule {
self.has_missing_patterns self.has_missing_patterns
} }
fn collect_patterns_recursive(&mut self, grammar: &mut Grammar, out: Option<RegExpSourceList>, is_first: bool) { fn collect_patterns_recursive(&mut self, grammar: &mut Grammar, out: &mut RegExpSourceList, is_first: bool) {
for x in self.patterns.clone() { for x in self.patterns.clone() {
let mut rule = grammar.get_rule(x); let mut rule = grammar.get_rule(x);
rule.collect_patterns_recursive(grammar, out.clone(), is_first); rule.collect_patterns_recursive(grammar, out, is_first);
} }
} }
fn compile(&mut self, grammar: &mut Grammar, end_regex_source: Option<String>, allow_a: bool, allow_g: bool) { fn compile(&mut self, grammar: &mut Grammar, end_regex_source: Option<String>, allow_a: bool, allow_g: bool) {
if let None = self._cached_compiled_patterns { if let None = self._cached_compiled_patterns {
self._cached_compiled_patterns = Some(RegExpSourceList::new()); let mut cached_compiled_patterns = RegExpSourceList::new();
self.collect_patterns_recursive(grammar, self._cached_compiled_patterns.clone(), true); self.collect_patterns_recursive(grammar, &mut cached_compiled_patterns, true);
self._cached_compiled_patterns = Some(cached_compiled_patterns);
} }
return self._cached_compiled_patterns.as_ref().unwrap().compile(grammar, allow_a, allow_g); return self._cached_compiled_patterns.as_ref().unwrap().compile(grammar, allow_a, allow_g);
......
use crate::inter::ILocation; use crate::inter::ILocation;
use crate::rule::RegExpSource; use crate::rule::{RegExpSource, RegExpSourceList};
use crate::rule::{AbstractRule, Rule}; use crate::rule::{AbstractRule, Rule};
use crate::grammar::Grammar;
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
pub struct MatchRule { pub struct MatchRule {
...@@ -38,4 +39,7 @@ impl AbstractRule for MatchRule { ...@@ -38,4 +39,7 @@ impl AbstractRule for MatchRule {
fn type_of(&self) -> String { fn type_of(&self) -> String {
String::from(self.rule.clone()._type) String::from(self.rule.clone()._type)
} }
fn collect_patterns_recursive(&mut self, grammar: &mut Grammar, out: &mut RegExpSourceList, is_first: bool) {
out.push(self._match.clone());
}
} }
...@@ -59,7 +59,7 @@ impl Default for AnchorCache { ...@@ -59,7 +59,7 @@ impl Default for AnchorCache {
A0_G0: None, A0_G0: None,
A0_G1: None, A0_G1: None,
A1_G0: None, A1_G0: None,
A1_G1: None A1_G1: None,
} }
} }
} }
...@@ -68,7 +68,8 @@ impl Default for AnchorCache { ...@@ -68,7 +68,8 @@ impl Default for AnchorCache {
pub struct RegExpSourceList { pub struct RegExpSourceList {
pub _has_anchors: bool, pub _has_anchors: bool,
pub _cached: Option<CompiledRule>, pub _cached: Option<CompiledRule>,
pub _anchor_cache: AnchorCache pub _anchor_cache: AnchorCache,
pub _items: Vec<RegExpSource>
} }
impl RegExpSourceList { impl RegExpSourceList {
...@@ -76,19 +77,26 @@ impl RegExpSourceList { ...@@ -76,19 +77,26 @@ impl RegExpSourceList {
RegExpSourceList { RegExpSourceList {
_has_anchors: false, _has_anchors: false,
_cached: None, _cached: None,
_anchor_cache: Default::default() _anchor_cache: Default::default(),
_items: vec![]
} }
} }
pub fn compile(&self, grammar: &mut Grammar, allow_a: bool, allow_g: bool) { pub fn push(&mut self, item: RegExpSource) {
self._items.push(item.clone());
if item.has_anchor {
self._has_anchors = true;
}
} }
pub fn compile(&self, grammar: &mut Grammar, allow_a: bool, allow_g: bool) {}
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
pub struct RegExpSource { pub struct RegExpSource {
pub source: String, pub source: String,
pub rule_id: i32, pub rule_id: i32,
pub has_anchor: bool
} }
impl RegExpSource { impl RegExpSource {
...@@ -96,6 +104,7 @@ impl RegExpSource { ...@@ -96,6 +104,7 @@ impl RegExpSource {
RegExpSource { RegExpSource {
source: reg_exp_source, source: reg_exp_source,
rule_id, rule_id,
has_anchor: false
} }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册