rule.rs 2.3 KB
Newer Older
P
Phodal Huang 已提交
1
use crate::inter::{IRawRepository, IRawGrammar, ILocation, IRawRule};
2 3 4
use dyn_clone::{clone_trait_object, DynClone};
use std::borrow::Borrow;
use crate::grammar::grammar::Grammar;
5

P
Phodal Huang 已提交
6
pub struct RuleFactory {}
7

8
fn create_rule(id: i32) -> Box<dyn AbstractRule> {
P
Phodal Huang 已提交
9 10 11
    let rule = BeginEndRule {
        rule: Rule {
            location: ILocation::new(),
12
            id: id,
P
Phodal Huang 已提交
13 14 15 16 17 18 19 20
            name: None,
            content_name: None
        }
    };

    Box::from(rule)
}

21
impl RuleFactory {
22
    pub fn get_compiled_rule_id(desc: IRawRule, helper: &mut Grammar, repository: IRawRepository) -> i32 {
P
Phodal Huang 已提交
23 24 25 26 27 28 29 30 31
        match desc.id {
            None => {
                helper.register_rule(create_rule);
            },
            Some(_) => {},
        }

        desc.id.unwrap()
    }
32

33 34 35 36
    pub fn create_capture_rule() {}
}


37
#[derive(Clone, Debug)]
38 39 40 41 42 43 44 45 46 47
pub struct Rule {
    pub location: ILocation,
    pub id: i32,
    pub name: Option<String>,
    pub content_name: Option<String>,
}

impl Rule {
    pub fn new(location: ILocation, id: i32, name: Option<String>, content_name: Option<String>) -> Self {
        Rule { location, id, name, content_name }
48
    }
49
}
50

51
pub trait AbstractRule: DynClone {}
52

53 54 55
clone_trait_object!(AbstractRule);

#[derive(Clone, Debug)]
56 57
pub struct IncludeOnlyRule {
    pub rule: Rule
P
Phodal Huang 已提交
58
}
P
Phodal Huang 已提交
59

60 61
impl AbstractRule for IncludeOnlyRule {}

62
#[derive(Clone, Debug)]
63 64 65 66 67 68
pub struct BeginWhileRule {
    pub rule: Rule
}

impl AbstractRule for BeginWhileRule {}

69
#[derive(Clone, Debug)]
70 71 72 73 74 75
pub struct MatchRule {
    pub rule: Rule
}

impl AbstractRule for MatchRule {}

76
#[derive(Clone, Debug)]
77 78 79 80 81 82
pub struct BeginEndRule {
    pub rule: Rule
}

impl AbstractRule for BeginEndRule {}

83
#[derive(Clone, Debug)]
84 85 86 87 88 89 90
pub struct CaptureRule {
    pub rule: Rule
}

impl AbstractRule for CaptureRule {}


P
Phodal Huang 已提交
91 92 93
// todo: trait with types
// https://users.rust-lang.org/t/impl-trait-with-generic-function-for-generic-struct/27083/2
pub trait IRuleRegistry {
P
Phodal Huang 已提交
94 95 96
    // type Output;
    // fn method(&self) -> Self::Output;

97
    fn get_rule(&self, pattern_id: i32) -> Rule;
98
    fn register_rule(&mut self, c: fn(id: i32) -> Box<dyn AbstractRule>) -> Box<dyn AbstractRule>;
P
Phodal Huang 已提交
99 100 101 102
}

pub trait IGrammarRegistry {
    fn get_external_grammar(&self, scope_name: String, repository: IRawRepository) -> Option<IRawGrammar>;
P
Phodal Huang 已提交
103 104 105
}

pub trait IRuleFactoryHelper: IGrammarRegistry + IRuleRegistry {}