diff --git a/scie-grammar/src/grammar/grammar/mod.rs b/scie-grammar/src/grammar/grammar/mod.rs index e660f2067913092c9a7b0074f1a3c54fce2d2001..bf58db57cf5ecfdc11de8585e67e85b57801e824 100644 --- a/scie-grammar/src/grammar/grammar/mod.rs +++ b/scie-grammar/src/grammar/grammar/mod.rs @@ -5,8 +5,8 @@ use onig::*; use crate::grammar::line_tokens::{LineTokens, TokenTypeMatcher}; use crate::grammar::{ScopeListElement, StackElement}; use crate::inter::{IRawGrammar, IRawRepository, IRawRepositoryMap, IRawRule}; -use crate::rule::{AbstractRule, EmptyRule, IGrammarRegistry, IRuleFactoryHelper, IRuleRegistry}; use crate::rule::rule_factory::RuleFactory; +use crate::rule::{AbstractRule, EmptyRule, IGrammarRegistry, IRuleFactoryHelper, IRuleRegistry}; pub mod scope_list_element; pub mod scope_metadata; @@ -107,15 +107,17 @@ impl Grammar { if self.root_id.clone() == -1 { let mut repository = self.grammar.repository.clone().unwrap(); let based = repository.clone().map.self_s.unwrap(); - self.root_id = - RuleFactory::get_compiled_rule_id(based.clone(), self, &mut repository.clone(), String::from("")); + self.root_id = RuleFactory::get_compiled_rule_id( + based.clone(), + self, + &mut repository.clone(), + String::from(""), + ); } let mut is_first_line: bool = false; match prev_state.clone() { - None => { - is_first_line = true - } + None => is_first_line = true, Some(state) => { if state == StackElement::null() { is_first_line = true @@ -125,11 +127,24 @@ impl Grammar { if is_first_line { let scope_list = ScopeListElement::default(); - prev_state = Some(StackElement::new(None, self.root_id.clone(), -1, -1, false, None, scope_list.clone(), scope_list.clone())) + prev_state = Some(StackElement::new( + None, + self.root_id.clone(), + -1, + -1, + false, + None, + scope_list.clone(), + scope_list.clone(), + )) } let format_line_text = format!("{:?}\n", line_text); - let line_tokens = LineTokens::new(emit_binary_tokens, line_text, self._token_type_matchers.clone()); + let line_tokens = LineTokens::new( + emit_binary_tokens, + line_text, + self._token_type_matchers.clone(), + ); self.tokenize_string( format_line_text.parse().unwrap(), is_first_line, @@ -164,8 +179,13 @@ impl Grammar { ); } - - self.match_rule_or_injections(line_text, is_first_line, line_pos, prev_state, anchor_position); + self.match_rule_or_injections( + line_text, + is_first_line, + line_pos, + prev_state, + anchor_position, + ); } pub fn check_while_conditions( @@ -177,7 +197,9 @@ impl Grammar { line_tokens: LineTokens, ) { let mut anchor_position = -1; - if _stack.begin_rule_captured_eol { anchor_position = 0 } + if _stack.begin_rule_captured_eol { + anchor_position = 0 + } // let while_rules = vec![]; } @@ -189,7 +211,13 @@ impl Grammar { stack: StackElement, anchor_position: i32, ) { - self.match_rule(line_text, is_first_line, line_pos, stack.clone(), anchor_position); + self.match_rule( + line_text, + is_first_line, + line_pos, + stack.clone(), + anchor_position, + ); } pub fn match_rule( diff --git a/scie-grammar/src/grammar/grammar/stack_element.rs b/scie-grammar/src/grammar/grammar/stack_element.rs index 96c97e88c1145449aff5fc6075beaba654eab708..97722dab01b6ef86b5035d563b252f53bd880fbd 100644 --- a/scie-grammar/src/grammar/grammar/stack_element.rs +++ b/scie-grammar/src/grammar/grammar/stack_element.rs @@ -1,7 +1,7 @@ -use core::ptr; -use crate::grammar::{Grammar, ScopeListElement}; use crate::grammar::grammar_registry::GrammarRegistry; -use crate::rule::{IRuleRegistry, AbstractRule}; +use crate::grammar::{Grammar, ScopeListElement}; +use crate::rule::{AbstractRule, IRuleRegistry}; +use core::ptr; #[derive(Debug, Clone, PartialEq, Eq)] pub struct StackElement { @@ -27,8 +27,7 @@ impl StackElement { begin_rule_captured_eol: false, end_rule: None, name_scopes_list: Default::default(), - content_name_scopes_list: Default::default() - + content_name_scopes_list: Default::default(), } } @@ -38,7 +37,16 @@ impl StackElement { pub fn get_rule(&self, grammar: &mut Grammar) -> Box { grammar.get_rule(self.rule_id) } - pub fn new(parent: Option>, rule_id: i32, enter_pos: i32, anchor_pos: i32, begin_rule_captured_eol: bool, end_rule: Option, name_scopes_list: ScopeListElement, content_name_scopes_list: ScopeListElement) -> Self { + pub fn new( + parent: Option>, + rule_id: i32, + enter_pos: i32, + anchor_pos: i32, + begin_rule_captured_eol: bool, + end_rule: Option, + name_scopes_list: ScopeListElement, + content_name_scopes_list: ScopeListElement, + ) -> Self { StackElement { parent, // todo: this.depth = (this.parent ? this.parent.depth + 1 : 1); @@ -49,7 +57,7 @@ impl StackElement { begin_rule_captured_eol, end_rule, name_scopes_list, - content_name_scopes_list + content_name_scopes_list, } } } diff --git a/scie-grammar/src/grammar/line_tokens.rs b/scie-grammar/src/grammar/line_tokens.rs index fc7194d22c294a3bfc8a729fc88ee14dd45ae416..374c931e667b01561d97d5bca178dcfdbafa69e7 100644 --- a/scie-grammar/src/grammar/line_tokens.rs +++ b/scie-grammar/src/grammar/line_tokens.rs @@ -5,7 +5,7 @@ pub struct TokenTypeMatcher {} pub struct IToken { pub start_index: i32, pub end_index: i32, - pub scopes: Vec + pub scopes: Vec, } #[derive(Debug, Clone)] @@ -15,19 +15,22 @@ pub struct LineTokens { pub _tokens: Vec, pub _binary_tokens: Vec, pub _last_token_end_index: i32, - pub _token_type_overrides: Vec + pub _token_type_overrides: Vec, } impl LineTokens { - pub fn new(emit_binary_tokens: bool, _line_text: String, _token_type_overrides: Vec) -> Self { + pub fn new( + emit_binary_tokens: bool, + _line_text: String, + _token_type_overrides: Vec, + ) -> Self { LineTokens { emit_binary_tokens, _line_text, _tokens: vec![], _binary_tokens: vec![], _last_token_end_index: 0, - _token_type_overrides + _token_type_overrides, } } } - diff --git a/scie-grammar/src/grammar/mod.rs b/scie-grammar/src/grammar/mod.rs index 4d5af068c55bd944c1a6460915e41a048a270a3c..98a339b71c438e083a872905eed0104c5d426aed 100644 --- a/scie-grammar/src/grammar/mod.rs +++ b/scie-grammar/src/grammar/mod.rs @@ -1,9 +1,9 @@ pub mod grammar; pub mod grammar_reader; -pub mod line_tokens; pub mod grammar_registry; +pub mod line_tokens; -pub use self::grammar::Grammar; -pub use self::grammar::stack_element::StackElement; pub use self::grammar::scope_list_element::ScopeListElement; pub use self::grammar::scope_metadata::ScopeMetadata; +pub use self::grammar::stack_element::StackElement; +pub use self::grammar::Grammar; diff --git a/scie-grammar/src/inter/mod.rs b/scie-grammar/src/inter/mod.rs index ce459882fbb971ba22570ad9b75bbd35b7f0a7dc..02764fa2a610aca4943843d2f0f336c66388b920 100644 --- a/scie-grammar/src/inter/mod.rs +++ b/scie-grammar/src/inter/mod.rs @@ -20,7 +20,7 @@ impl ILocation { #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] pub struct ILocatable { - #[serde(flatten, skip_serializing_if="Option::is_none")] + #[serde(flatten, skip_serializing_if = "Option::is_none")] pub textmate_location: Option, } @@ -42,9 +42,9 @@ pub struct IRawCapturesMap { pub struct IRawRepositoryMap { #[serde(flatten)] pub name_map: HashMap>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub self_s: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub base_s: Option, } @@ -62,7 +62,7 @@ impl IRawRepositoryMap { pub struct IRawRepository { #[serde(flatten)] pub map: Box, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub location: Option, } @@ -79,19 +79,19 @@ impl IRawRepository { pub struct IRawCaptures { #[serde(flatten)] pub map: IRawCapturesMap, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub location: Option, } #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] pub struct IRawRule { pub id: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub location: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub include: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub name: Option, #[serde(alias = "contentName")] @@ -99,34 +99,34 @@ pub struct IRawRule { #[serde(alias = "match")] pub match_s: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub captures: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub begin: Option, - #[serde(alias = "beginCaptures", skip_serializing_if="Option::is_none")] + #[serde(alias = "beginCaptures", skip_serializing_if = "Option::is_none")] pub begin_captures: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub end: Option, - #[serde(alias = "endCaptures", skip_serializing_if="Option::is_none")] + #[serde(alias = "endCaptures", skip_serializing_if = "Option::is_none")] pub end_captures: Option>, - #[serde(alias = "while", skip_serializing_if="Option::is_none")] + #[serde(alias = "while", skip_serializing_if = "Option::is_none")] pub while_s: Option, - #[serde(alias = "whileCaptures", skip_serializing_if="Option::is_none")] + #[serde(alias = "whileCaptures", skip_serializing_if = "Option::is_none")] pub while_captures: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub patterns: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub repository: Option, - #[serde(alias = "applyEndPatternLast", skip_serializing_if="Option::is_none")] + #[serde(alias = "applyEndPatternLast", skip_serializing_if = "Option::is_none")] pub apply_end_pattern_last: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub information_for_contributors: Option>, } diff --git a/scie-grammar/src/lib.rs b/scie-grammar/src/lib.rs index 54a5bf751242b080fd01981aae128d6d1ed2cc2e..9f841b6d987383a253456aeb624392e6721855d2 100644 --- a/scie-grammar/src/lib.rs +++ b/scie-grammar/src/lib.rs @@ -8,5 +8,5 @@ extern crate erased_serde; pub mod grammar; pub mod inter; -pub mod rule; pub mod matcher; +pub mod rule; diff --git a/scie-grammar/src/matcher.rs b/scie-grammar/src/matcher.rs index 0388c6c9bbd07a1bf9124bf49137e73bfec43672..4dec49b6496ea27a3c024c23e47d42b4ce48e435 100644 --- a/scie-grammar/src/matcher.rs +++ b/scie-grammar/src/matcher.rs @@ -9,6 +9,4 @@ pub struct MatcherWithPriority { priority: MatchPriority, } -pub struct Matcher { - -} \ No newline at end of file +pub struct Matcher {} diff --git a/scie-grammar/src/rule/abstract_rule.rs b/scie-grammar/src/rule/abstract_rule.rs index dd27767e79757bc62a48518de523e659f6f8fa9b..e8f3fa313eafbc9f6c9d280c10b98ce0d02a9794 100644 --- a/scie-grammar/src/rule/abstract_rule.rs +++ b/scie-grammar/src/rule/abstract_rule.rs @@ -1,6 +1,6 @@ -use dyn_clone::{clone_trait_object, DynClone}; -use core::fmt; use crate::rule::{IRuleRegistry, RegExpSourceList}; +use core::fmt; +use dyn_clone::{clone_trait_object, DynClone}; pub trait AbstractRule: DynClone + erased_serde::Serialize { fn id(&self) -> i32; @@ -8,8 +8,21 @@ pub trait AbstractRule: DynClone + erased_serde::Serialize { fn has_missing_pattern(&self) -> bool { false } - fn collect_patterns_recursive(&self, grammar: Box, out: RegExpSourceList, is_first: bool) {} - fn compile(&self, grammar: Box, end_regex_source: Option, allow_a: bool, allow_g: bool) {} + fn collect_patterns_recursive( + &self, + grammar: Box, + out: RegExpSourceList, + is_first: bool, + ) { + } + fn compile( + &self, + grammar: Box, + end_regex_source: Option, + allow_a: bool, + allow_g: bool, + ) { + } } impl fmt::Debug for dyn AbstractRule { @@ -21,4 +34,3 @@ impl fmt::Debug for dyn AbstractRule { serialize_trait_object!(AbstractRule); clone_trait_object!(AbstractRule); - diff --git a/scie-grammar/src/rule/begin_end_rule.rs b/scie-grammar/src/rule/begin_end_rule.rs index 18f1ba78dfcd50e67d9f1444758351fd9b2b2f8c..48d553d201c338b32c72456a1e7e3f7d48459589 100644 --- a/scie-grammar/src/rule/begin_end_rule.rs +++ b/scie-grammar/src/rule/begin_end_rule.rs @@ -1,7 +1,7 @@ -use crate::rule::{RegExpSource, RegExpSourceList}; -use crate::rule::{Rule, AbstractRule}; use crate::inter::ILocation; use crate::rule::rule_factory::ICompilePatternsResult; +use crate::rule::{AbstractRule, Rule}; +use crate::rule::{RegExpSource, RegExpSourceList}; #[derive(Clone, Debug, Serialize)] pub struct BeginEndRule { @@ -54,7 +54,9 @@ impl BeginEndRule { } impl AbstractRule for BeginEndRule { - fn id(&self) -> i32 { self.rule.id } + fn id(&self) -> i32 { + self.rule.id + } fn type_of(&self) -> String { String::from(self.rule.clone()._type) } @@ -62,4 +64,3 @@ impl AbstractRule for BeginEndRule { self.has_missing_patterns } } - diff --git a/scie-grammar/src/rule/begin_while_rule.rs b/scie-grammar/src/rule/begin_while_rule.rs index 04253695935a795eb94bb9dd668c2821ba3911bd..f2d8efb786d1099165fe98946f6978b70d2bac1e 100644 --- a/scie-grammar/src/rule/begin_while_rule.rs +++ b/scie-grammar/src/rule/begin_while_rule.rs @@ -1,7 +1,7 @@ +use crate::inter::ILocation; +use crate::rule::rule_factory::ICompilePatternsResult; use crate::rule::{AbstractRule, Rule}; use crate::rule::{RegExpSource, RegExpSourceList}; -use crate::rule::rule_factory::ICompilePatternsResult; -use crate::inter::ILocation; #[derive(Clone, Debug, Serialize)] pub struct BeginWhileRule { @@ -54,7 +54,9 @@ impl BeginWhileRule { } impl AbstractRule for BeginWhileRule { - fn id(&self) -> i32 { self.rule.id } + fn id(&self) -> i32 { + self.rule.id + } fn type_of(&self) -> String { String::from(self.rule.clone()._type) } diff --git a/scie-grammar/src/rule/capture_rule.rs b/scie-grammar/src/rule/capture_rule.rs index 6723a6ef2b82ff696d355652ac88aca2540d46db..7264e37226e1d09c6868f64f7cfdd9ba96f096e4 100644 --- a/scie-grammar/src/rule/capture_rule.rs +++ b/scie-grammar/src/rule/capture_rule.rs @@ -1,5 +1,5 @@ -use crate::rule::{Rule, AbstractRule}; use crate::inter::ILocation; +use crate::rule::{AbstractRule, Rule}; #[derive(Clone, Debug, Serialize)] pub struct CaptureRule { @@ -20,7 +20,13 @@ impl CaptureRule { retokenize_captured_with_rule_id: 0, } } - pub fn new(location: Option, id: i32, name: Option, content_name: Option, retokenize_captured_with_rule_id: i32) -> Self { + pub fn new( + location: Option, + id: i32, + name: Option, + content_name: Option, + retokenize_captured_with_rule_id: i32, + ) -> Self { CaptureRule { rule: Rule { _type: String::from("CaptureRule"), @@ -35,6 +41,10 @@ impl CaptureRule { } impl AbstractRule for CaptureRule { - fn id(&self) -> i32 { self.rule.id } - fn type_of(&self) -> String { String::from(self.rule.clone()._type) } + fn id(&self) -> i32 { + self.rule.id + } + fn type_of(&self) -> String { + String::from(self.rule.clone()._type) + } } diff --git a/scie-grammar/src/rule/compiled_rule.rs b/scie-grammar/src/rule/compiled_rule.rs index 276f69f308b15f113ae139560959c0287be83299..d15d5c1610639b23d82dc054117cc5bf7ef8440d 100644 --- a/scie-grammar/src/rule/compiled_rule.rs +++ b/scie-grammar/src/rule/compiled_rule.rs @@ -1,10 +1,13 @@ pub struct CompiledRule { pub debug_reg_exps: Vec, - pub rules: Vec + pub rules: Vec, } impl CompiledRule { pub fn new(debug_reg_exps: Vec, rules: Vec) -> Self { - CompiledRule { debug_reg_exps, rules } + CompiledRule { + debug_reg_exps, + rules, + } } -} \ No newline at end of file +} diff --git a/scie-grammar/src/rule/empty_rule.rs b/scie-grammar/src/rule/empty_rule.rs index c58bec548c1f84b803e48e77afef9c3b2d559a6b..41b7b2a0fce2245b036d72d4311467f5461ba4d3 100644 --- a/scie-grammar/src/rule/empty_rule.rs +++ b/scie-grammar/src/rule/empty_rule.rs @@ -4,7 +4,9 @@ use crate::rule::AbstractRule; pub struct EmptyRule {} impl AbstractRule for EmptyRule { - fn id(&self) -> i32 { 0 } + fn id(&self) -> i32 { + 0 + } fn type_of(&self) -> String { String::from("EmptyRule") } diff --git a/scie-grammar/src/rule/include_only_rule.rs b/scie-grammar/src/rule/include_only_rule.rs index 2e2ed59b2f30e0f3413dc24be7100b14b6f0f7e8..9241500a612b0ad3a532d4f35dc8c7fdcfb189aa 100644 --- a/scie-grammar/src/rule/include_only_rule.rs +++ b/scie-grammar/src/rule/include_only_rule.rs @@ -1,6 +1,6 @@ -use crate::rule::rule_factory::ICompilePatternsResult; -use crate::rule::{Rule, AbstractRule}; use crate::inter::ILocation; +use crate::rule::rule_factory::ICompilePatternsResult; +use crate::rule::{AbstractRule, Rule}; #[derive(Clone, Debug, Serialize)] pub struct IncludeOnlyRule { @@ -33,7 +33,9 @@ impl IncludeOnlyRule { } impl AbstractRule for IncludeOnlyRule { - fn id(&self) -> i32 { self.rule.id } + fn id(&self) -> i32 { + self.rule.id + } fn type_of(&self) -> String { String::from(self.rule.clone()._type) } diff --git a/scie-grammar/src/rule/match_rule.rs b/scie-grammar/src/rule/match_rule.rs index 8538a24af1a35fa5d20939dfa229a2f81207d138..13d7855081088f3b95d4af61bc93710b1e0b298e 100644 --- a/scie-grammar/src/rule/match_rule.rs +++ b/scie-grammar/src/rule/match_rule.rs @@ -1,6 +1,6 @@ -use crate::rule::{AbstractRule, Rule}; use crate::inter::ILocation; use crate::rule::RegExpSource; +use crate::rule::{AbstractRule, Rule}; #[derive(Clone, Debug, Serialize)] pub struct MatchRule { @@ -32,7 +32,9 @@ impl MatchRule { } impl AbstractRule for MatchRule { - fn id(&self) -> i32 { self.rule.id } + fn id(&self) -> i32 { + self.rule.id + } fn type_of(&self) -> String { String::from(self.rule.clone()._type) } diff --git a/scie-grammar/src/rule/mod.rs b/scie-grammar/src/rule/mod.rs index ff040f6c113f8b042953da5b08838ff81d8c5cde..6cdd9e302126b811d5ceef31ca6869a5be07de06 100644 --- a/scie-grammar/src/rule/mod.rs +++ b/scie-grammar/src/rule/mod.rs @@ -1,25 +1,25 @@ -pub mod rule_factory; pub mod rule_convert; +pub mod rule_factory; pub mod abstract_rule; pub mod begin_end_rule; pub mod begin_while_rule; +pub mod capture_rule; pub mod compiled_rule; +pub mod empty_rule; pub mod include_only_rule; pub mod match_rule; -pub mod capture_rule; -pub mod empty_rule; pub mod rule; -pub use self::rule::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; +pub use self::compiled_rule::CompiledRule; pub use self::empty_rule::EmptyRule; +pub use self::include_only_rule::IncludeOnlyRule; +pub use self::match_rule::MatchRule; +pub use self::rule::Rule; use crate::inter::{ILocation, IRawGrammar, IRawRepository}; use crate::rule::rule_factory::ICompilePatternsResult; diff --git a/scie-grammar/src/rule/rule.rs b/scie-grammar/src/rule/rule.rs index f8c3c6c437f6246df5ec0a7e59bb88a43086cf90..ef05a1307dce47c6cc12b02da38034fbb95563a8 100644 --- a/scie-grammar/src/rule/rule.rs +++ b/scie-grammar/src/rule/rule.rs @@ -27,4 +27,4 @@ impl Rule { _content_name: content_name, } } -} \ No newline at end of file +} diff --git a/scie-grammar/src/rule/rule_convert.rs b/scie-grammar/src/rule/rule_convert.rs index 57f074e5aef0a9d6b97f3ccac7c2cd29772695b1..987864c7a535120fb7a3c2cf650c2edfce8524d1 100644 --- a/scie-grammar/src/rule/rule_convert.rs +++ b/scie-grammar/src/rule/rule_convert.rs @@ -1,5 +1,7 @@ +use crate::rule::{ + AbstractRule, BeginEndRule, BeginWhileRule, CaptureRule, IncludeOnlyRule, MatchRule, +}; use std::collections::BTreeMap as Map; -use crate::rule::{AbstractRule, MatchRule, IncludeOnlyRule, CaptureRule, BeginWhileRule, BeginEndRule}; #[derive(Serialize, Debug)] pub struct RuleList { @@ -47,15 +49,14 @@ fn abstract_rule_to_json(map: Map>) { println!("{:?}", j); } - #[cfg(test)] mod tests { use crate::grammar::Grammar; - use std::path::Path; - use std::fs::File; use crate::inter::IRawGrammar; - use std::io::Read; use crate::rule::rule_convert::abstract_rule_to_json; + use std::fs::File; + use std::io::Read; + use std::path::Path; #[test] fn should_build_text_grammar() { @@ -82,4 +83,4 @@ GitHub 漫游指南 grammar } -} \ No newline at end of file +} diff --git a/scie-grammar/src/rule/rule_factory.rs b/scie-grammar/src/rule/rule_factory.rs index 68943a40f6a3562805f436cc45d093258f42105e..691f9cac23d5ba6abaf590e1110a7018f6edbb1a 100644 --- a/scie-grammar/src/rule/rule_factory.rs +++ b/scie-grammar/src/rule/rule_factory.rs @@ -1,6 +1,9 @@ use crate::grammar::Grammar; -use crate::inter::{IRawCaptures, IRawRepository, IRawRule, ILocation}; -use crate::rule::{BeginEndRule, BeginWhileRule, CaptureRule, IRuleRegistry, IncludeOnlyRule, MatchRule, AbstractRule, Rule}; +use crate::inter::{ILocation, IRawCaptures, IRawRepository, IRawRule}; +use crate::rule::{ + AbstractRule, BeginEndRule, BeginWhileRule, CaptureRule, IRuleRegistry, IncludeOnlyRule, + MatchRule, Rule, +}; #[derive(Clone, Debug, Serialize)] pub struct ICompilePatternsResult { @@ -33,22 +36,34 @@ impl RuleFactory { } } - r.resize((maximum_capture_id + 1) as usize, Box::new(CaptureRule::empty())); + r.resize( + (maximum_capture_id + 1) as usize, + Box::new(CaptureRule::empty()), + ); for (id_str, desc) in capts.clone().map.capture_map { // todo: figure captureId === '$vscodeTextmateLocation' let numeric_capture_id: usize = id_str.parse().unwrap_or(0); let mut retokenize_captured_with_rule_id = 0; - let options_patterns = capts.map.capture_map - .get(&*numeric_capture_id.to_string()); + let options_patterns = capts.map.capture_map.get(&*numeric_capture_id.to_string()); if let Some(rule) = options_patterns { if let Some(patterns) = rule.clone().patterns { - retokenize_captured_with_rule_id = - RuleFactory::get_compiled_rule_id(desc.clone(), helper, repository, String::from("")); + retokenize_captured_with_rule_id = RuleFactory::get_compiled_rule_id( + desc.clone(), + helper, + repository, + String::from(""), + ); } } - r[numeric_capture_id] = RuleFactory::create_capture_rule(helper, desc.clone().location, desc.clone().name, desc.clone().content_name, retokenize_captured_with_rule_id); + r[numeric_capture_id] = RuleFactory::create_capture_rule( + helper, + desc.clone().location, + desc.clone().name, + desc.clone().content_name, + retokenize_captured_with_rule_id, + ); } // todo: remove first element, because it's filled & empty. }; @@ -56,14 +71,25 @@ impl RuleFactory { r } - pub fn create_capture_rule(helper: &mut Grammar, location: Option, name: Option, content_name: Option, retokenizeCapturedWithRuleId: i32) -> Box { + pub fn create_capture_rule( + helper: &mut Grammar, + location: Option, + name: Option, + content_name: Option, + retokenizeCapturedWithRuleId: i32, + ) -> Box { let id = helper.register_id(); - let rule = CaptureRule::new(location, id, name, content_name, retokenizeCapturedWithRuleId); + let rule = CaptureRule::new( + location, + id, + name, + content_name, + retokenizeCapturedWithRuleId, + ); helper.register_rule(Box::from(rule)); return helper.get_rule(id); } - pub fn compile_patterns( origin_patterns: Option>, helper: &mut Grammar, @@ -92,7 +118,8 @@ impl RuleFactory { ); } } else if include_s == "$base" || include_s == "$self" { - let local_included_rule = repository.map.name_map.get_mut(include_s.as_str()); + let local_included_rule = + repository.map.name_map.get_mut(include_s.as_str()); if let Some(mut rule) = local_included_rule.cloned() { pattern_id = RuleFactory::get_compiled_rule_id( *rule, @@ -123,15 +150,22 @@ impl RuleFactory { } } } else { - pattern_id = - RuleFactory::get_compiled_rule_id(pattern, helper, repository, String::from("")); + pattern_id = RuleFactory::get_compiled_rule_id( + pattern, + helper, + repository, + String::from(""), + ); } if pattern_id != -1 { let rule = helper.get_rule(pattern_id); let mut skip_rule = false; - if rule.type_of() == "IncludeOnlyRule" || rule.type_of() == "BeginEndRule" || rule.type_of() == "BeginWhileRule" { - if rule.has_missing_pattern() { + if rule.type_of() == "IncludeOnlyRule" + || rule.type_of() == "BeginEndRule" + || rule.type_of() == "BeginWhileRule" + { + if rule.has_missing_pattern() { skip_rule = true; } } @@ -151,12 +185,12 @@ impl RuleFactory { if 0 != r.len() { has_missing_patterns = true } - }, + } Some(patterns) => { if patterns.len() != r.len() { has_missing_patterns = true } - }, + } } let result = ICompilePatternsResult { @@ -182,16 +216,17 @@ impl RuleFactory { // by name. if desc_name != "" { if let Some(a) = repository.map.name_map.get(desc_name.as_str()).clone() { - repository.map.name_map.get_mut(desc_name.as_str()).unwrap().id = Some(id); + repository + .map + .name_map + .get_mut(desc_name.as_str()) + .unwrap() + .id = Some(id); } } if let Some(match_s) = desc.match_s { - let rule_factory = RuleFactory::compile_captures( - desc.captures, - helper, - repository, - ); + let rule_factory = RuleFactory::compile_captures(desc.captures, helper, repository); let match_rule = MatchRule::new( desc.location.clone(), id.clone(), @@ -206,7 +241,11 @@ impl RuleFactory { if let None = desc.begin { if let Some(repo) = desc.repository.clone() { - desc.repository.unwrap().map.name_map.extend(repository.clone().map.name_map); + desc.repository + .unwrap() + .map + .name_map + .extend(repository.clone().map.name_map); } let mut patterns = desc.patterns; @@ -218,8 +257,7 @@ impl RuleFactory { } } - let rule_factory = - RuleFactory::compile_patterns(patterns, helper, repository); + let rule_factory = RuleFactory::compile_patterns(patterns, helper, repository); let include_only_rule = IncludeOnlyRule::new( desc.location.clone(), id.clone(), @@ -247,11 +285,8 @@ impl RuleFactory { RuleFactory::compile_captures(begin_captures, helper, repository); let end_rule_factory = RuleFactory::compile_captures(end_captures, helper, repository); - let pattern_factory = RuleFactory::compile_patterns( - desc.patterns, - helper, - repository, - ); + let pattern_factory = + RuleFactory::compile_patterns(desc.patterns, helper, repository); let begin_while_rule = BeginWhileRule::new( desc.location, @@ -271,13 +306,8 @@ impl RuleFactory { let begin_rule_factory = RuleFactory::compile_captures(begin_captures, helper, repository); - let end_rule_factory = - RuleFactory::compile_captures(end_captures, helper, repository); - let pattern_factory = RuleFactory::compile_patterns( - desc.patterns, - helper, - repository, - ); + let end_rule_factory = RuleFactory::compile_captures(end_captures, helper, repository); + let pattern_factory = RuleFactory::compile_patterns(desc.patterns, helper, repository); // todo: register with compile patterns let begin_end_rule = BeginEndRule::new(