grammar.rs 12.1 KB
Newer Older
1
use std::collections::BTreeMap as Map;
P
Phodal Huang 已提交
2

P
Phodal Huang 已提交
3
use crate::grammar::line_tokens::{LineTokens, TokenTypeMatcher};
4
use crate::grammar::{ScopeListElement, StackElement, MatchRuleResult};
P
Phodal Huang 已提交
5
use crate::inter::{IRawGrammar, IRawRepository, IRawRepositoryMap, IRawRule};
6
use crate::rule::rule_factory::RuleFactory;
P
Phodal Huang 已提交
7
use crate::rule::{AbstractRule, EmptyRule, IGrammarRegistry, IRuleFactoryHelper, IRuleRegistry};
P
Phodal Huang 已提交
8
use scie_scanner::scanner::scanner::IOnigMatch;
P
Phodal Huang 已提交
9

P
Phodal Huang 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22
pub struct IToken {
    pub start_index: i32,
    pub end_index: i32,
    pub scopes: Vec<String>,
}

pub struct ITokenizeLineResult {
    pub tokens: Vec<IToken>,
    pub rule_stack: Box<StackElement>,
}

pub struct ITokenizeLineResult2 {
    pub tokens: Vec<i32>,
P
Phodal Huang 已提交
23
    pub rule_stack: Box<StackElement>,
P
Phodal Huang 已提交
24 25 26 27 28
}

pub trait IGrammar {
    fn tokenize_line(line_text: String, prev_state: Option<StackElement>) -> ITokenizeLineResult;
    /**
P
Phodal Huang 已提交
29 30 31 32 33 34 35 36 37
     * Tokenize `lineText` using previous line state `prevState`.
     * The result contains the tokens in binary format, resolved with the following information:
     *  - language
     *  - token type (regex, string, comment, other)
     *  - font style
     *  - foreground color
     *  - background color
     * e.g. for getting the languageId: `(metadata & MetadataConsts.LANGUAGEID_MASK) >>> MetadataConsts.LANGUAGEID_OFFSET`
     */
P
Phodal Huang 已提交
38
    fn tokenize_line2(line_text: String, prev_state: Option<StackElement>) -> ITokenizeLineResult2;
P
Phodal Huang 已提交
39 40
}

P
Phodal Huang 已提交
41
pub trait Matcher {}
P
Phodal Huang 已提交
42

P
Phodal Huang 已提交
43
#[derive(Debug, Clone)]
P
Phodal Huang 已提交
44
pub struct Grammar {
45
    root_id: i32,
P
Phodal Huang 已提交
46
    grammar: IRawGrammar,
47
    pub last_rule_id: i32,
48
    pub rule_id2desc: Map<i32, Box<dyn AbstractRule>>,
P
Phodal Huang 已提交
49
    pub _token_type_matchers: Vec<TokenTypeMatcher>,
P
Phodal Huang 已提交
50 51
}

P
Phodal Huang 已提交
52
pub fn init_grammar(grammar: IRawGrammar, _base: Option<IRawRule>) -> IRawGrammar {
P
Phodal Huang 已提交
53 54 55
    let mut _grammar = grammar.clone();

    let mut new_based: IRawRule = IRawRule::new();
P
Phodal Huang 已提交
56 57 58
    if let Some(repo) = grammar.clone().repository {
        new_based.location = repo.clone().location;
    }
P
Phodal Huang 已提交
59 60
    new_based.patterns = Some(grammar.clone().patterns.clone());
    new_based.name = grammar.clone().name;
P
Phodal Huang 已提交
61 62 63 64

    let mut repository_map = IRawRepositoryMap::new();
    repository_map.base_s = Some(new_based.clone());
    repository_map.self_s = Some(new_based.clone());
P
Phodal Huang 已提交
65 66 67
    if let Some(repo) = grammar.clone().repository {
        repository_map.name_map = repo.clone().map.name_map.clone();
    }
P
Phodal Huang 已提交
68 69 70

    _grammar.repository = Some(IRawRepository {
        map: Box::new(repository_map.clone()),
71
        location: None,
P
Phodal Huang 已提交
72 73 74 75 76
    });

    _grammar
}

P
Phodal Huang 已提交
77
impl Grammar {
P
Phodal Huang 已提交
78
    pub fn new(grammar: IRawGrammar) -> Grammar {
P
Phodal Huang 已提交
79
        let _grammar = init_grammar(grammar.clone(), None);
P
Phodal Huang 已提交
80
        Grammar {
81
            last_rule_id: 0,
P
Phodal Huang 已提交
82
            grammar: _grammar,
P
Phodal Huang 已提交
83
            root_id: -1,
84
            rule_id2desc: Map::new(),
P
Phodal Huang 已提交
85
            _token_type_matchers: vec![],
P
Phodal Huang 已提交
86 87 88
        }
    }

P
Phodal Huang 已提交
89
    fn tokenize(
90
        &mut self,
P
Phodal Huang 已提交
91
        line_text: String,
92
        prev_state: Option<StackElement>,
P
Phodal Huang 已提交
93 94
        emit_binary_tokens: bool,
    ) {
95 96
        if self.root_id.clone() == -1 {
            let mut repository = self.grammar.repository.clone().unwrap();
P
Phodal Huang 已提交
97
            let based = repository.clone().map.self_s.unwrap();
P
Phodal Huang 已提交
98 99 100 101 102 103
            self.root_id = RuleFactory::get_compiled_rule_id(
                based.clone(),
                self,
                &mut repository.clone(),
                String::from(""),
            );
104
        }
P
Phodal Huang 已提交
105

P
Phodal Huang 已提交
106
        let mut is_first_line: bool = false;
107 108 109

        let mut current_state = StackElement::null();

P
Phodal Huang 已提交
110
        match prev_state.clone() {
P
Phodal Huang 已提交
111
            None => is_first_line = true,
112 113 114 115
            Some(state) => {
                if state == StackElement::null() {
                    is_first_line = true
                }
116 117

                current_state = state;
P
Phodal Huang 已提交
118
            }
119
        }
P
Phodal Huang 已提交
120

P
Phodal Huang 已提交
121
        if is_first_line {
P
Phodal Huang 已提交
122 123 124 125 126 127 128 129 130 131 132 133
            // let scope_list = ScopeListElement::default();
            let _root_scope_name = self.get_rule(self.root_id.clone())
                .get_name(None, None);
            let mut root_scope_name = String::from("unknown");
            if let Some(name) = _root_scope_name {
                root_scope_name = name
            }

            let scope_list = ScopeListElement::new(
                None,
                root_scope_name
            );
134
            let mut state = StackElement::new(
P
Phodal Huang 已提交
135 136 137 138 139 140 141 142
                None,
                self.root_id.clone(),
                -1,
                -1,
                false,
                None,
                scope_list.clone(),
                scope_list.clone(),
143 144 145
            );

            current_state = state;
P
Phodal Huang 已提交
146 147
        } else {
            is_first_line = false;
P
Phodal Huang 已提交
148 149
        }

P
Phodal Huang 已提交
150
        let format_line_text = format!("{:?}\n", line_text);
P
Phodal Huang 已提交
151 152 153 154 155
        let line_tokens = LineTokens::new(
            emit_binary_tokens,
            line_text,
            self._token_type_matchers.clone(),
        );
P
Phodal Huang 已提交
156 157 158 159
        self.tokenize_string(
            format_line_text.parse().unwrap(),
            is_first_line,
            0,
160
            &mut current_state,
P
Phodal Huang 已提交
161 162
            line_tokens,
            true,
163
        );
P
Phodal Huang 已提交
164 165
    }

P
Phodal Huang 已提交
166 167 168
    pub fn tokenize_string(
        &mut self,
        line_text: String,
169 170 171
        origin_is_first: bool,
        origin_line_pos: i32,
        prev_state: &mut StackElement,
P
Phodal Huang 已提交
172
        line_tokens: LineTokens,
P
Phodal Huang 已提交
173
        check_while_conditions: bool,
174
    ) -> Option<StackElement> {
P
Phodal Huang 已提交
175
        let _line_length = line_text.len();
176
        let mut _stop = false;
P
Phodal Huang 已提交
177
        let mut anchor_position = -1;
P
Phodal Huang 已提交
178

179

P
Phodal Huang 已提交
180
        if check_while_conditions {
P
Phodal Huang 已提交
181 182 183
            // todo: add realy logic
            self.check_while_conditions(
                line_text.clone(),
184 185
                origin_is_first.clone(),
                origin_line_pos.clone(),
P
Phodal Huang 已提交
186 187 188
                prev_state.clone(),
                line_tokens.clone(),
            );
P
Phodal Huang 已提交
189 190
        }

191 192 193 194

        let mut line_pos = origin_line_pos.clone();
        let mut is_first_line = origin_is_first.clone();
        while !_stop {
P
Phodal Huang 已提交
195
            let r = self.match_rule(line_text.clone(), is_first_line, line_pos, prev_state, anchor_position);
196 197
            if let None = r {
                _stop = true;
P
Phodal Huang 已提交
198
                return None;
199 200
            }

P
Phodal Huang 已提交
201 202 203 204 205 206 207 208 209 210
            let capture_result = r.unwrap();
            let capture_indices = capture_result.capture_indices;
            let matched_rule_id = capture_result.matched_rule_id;
            if matched_rule_id == -1 {
                println!("todo: matched the `end` for this rule => pop it");
            } else {
                let rule = self.get_rule(matched_rule_id);
                line_tokens.produce(prev_state, capture_indices[0].start as i32)
            }

211 212 213 214 215 216
            if capture_indices[0].end > line_pos as usize {
                line_pos = capture_indices[0].end as i32;
                is_first_line = false;
            }
        }
        Some(prev_state.clone())
P
Phodal Huang 已提交
217 218
    }

P
Phodal Huang 已提交
219 220 221 222 223
    pub fn check_while_conditions(
        &mut self,
        line_text: String,
        is_first_line: bool,
        line_pos: i32,
P
Phodal Huang 已提交
224
        _stack: StackElement,
P
Phodal Huang 已提交
225 226 227
        line_tokens: LineTokens,
    ) {
        let mut anchor_position = -1;
P
Phodal Huang 已提交
228 229 230
        if _stack.begin_rule_captured_eol {
            anchor_position = 0
        }
P
Phodal Huang 已提交
231 232
        // let while_rules = vec![];
    }
P
Phodal Huang 已提交
233

P
Phodal Huang 已提交
234 235 236 237 238
    pub fn match_rule_or_injections(
        &mut self,
        line_text: String,
        is_first_line: bool,
        line_pos: i32,
P
Phodal Huang 已提交
239
        stack: &mut StackElement,
P
Phodal Huang 已提交
240
        anchor_position: i32,
P
Phodal Huang 已提交
241
    ) {
242
        let match_result = self.match_rule(
P
Phodal Huang 已提交
243 244 245
            line_text,
            is_first_line,
            line_pos,
P
Phodal Huang 已提交
246
            stack,
P
Phodal Huang 已提交
247 248
            anchor_position,
        );
249 250 251 252
        if let Some(result) = match_result {} else {
            // None
        };
        // todo: get injections logic
P
Phodal Huang 已提交
253 254 255 256 257 258 259
    }

    pub fn match_rule(
        &mut self,
        line_text: String,
        is_first_line: bool,
        line_pos: i32,
P
Phodal Huang 已提交
260
        stack: &mut StackElement,
P
Phodal Huang 已提交
261
        anchor_position: i32,
262
    ) -> Option<MatchRuleResult> {
263
        let mut rule = stack.get_rule(self);
P
Phodal Huang 已提交
264
        let mut rule_scanner = rule.compile(
P
Phodal Huang 已提交
265
            self,
P
Phodal Huang 已提交
266
            stack.end_rule.clone(),
P
Phodal Huang 已提交
267 268 269
            is_first_line,
            line_pos == anchor_position,
        );
P
Phodal Huang 已提交
270 271 272
        // rule_scanner.scanner
        let r = rule_scanner.scanner.find_next_match_sync(line_text, line_pos);
        if let Some(result) = r {
273 274
            let match_rule_result = MatchRuleResult {
                capture_indices: result.capture_indices,
275
                matched_rule_id: rule_scanner.rules[result.index],
276 277 278 279
            };

            println!("{:?}", match_rule_result.clone());
            Some(match_rule_result)
P
Phodal Huang 已提交
280 281 282
        } else {
            None
        }
P
Phodal Huang 已提交
283
    }
P
Phodal Huang 已提交
284

285
    pub fn tokenize_line(&mut self, line_text: String, prev_state: Option<StackElement>) {
P
Phodal Huang 已提交
286 287 288
        self.tokenize(line_text, prev_state, false)
    }

P
Phodal Huang 已提交
289 290
    pub fn tokenize_line2(&self, line_text: String, prev_state: Option<StackElement>) {}
}
P
Phodal Huang 已提交
291 292 293 294

impl IRuleFactoryHelper for Grammar {}

impl IGrammarRegistry for Grammar {
P
Phodal Huang 已提交
295 296 297 298 299
    fn get_external_grammar(
        &self,
        scope_name: String,
        repository: IRawRepository,
    ) -> Option<IRawGrammar> {
P
Phodal Huang 已提交
300 301 302 303 304
        None
    }
}

impl IRuleRegistry for Grammar {
P
Phodal Huang 已提交
305 306
    fn register_id(&mut self) -> i32 {
        self.last_rule_id = self.last_rule_id + 1;
P
Phodal Huang 已提交
307
        self.last_rule_id.clone()
P
Phodal Huang 已提交
308 309
    }

P
Phodal Huang 已提交
310 311 312
    fn get_rule(&mut self, pattern_id: i32) -> Box<dyn AbstractRule> {
        if let Some(rule) = self.rule_id2desc.get_mut(&pattern_id) {
            return rule.to_owned();
P
Phodal Huang 已提交
313
        }
P
Phodal Huang 已提交
314
        Box::from(EmptyRule {})
P
Phodal Huang 已提交
315
    }
P
Phodal Huang 已提交
316

P
Phodal Huang 已提交
317
    fn register_rule(&mut self, result: Box<dyn AbstractRule>) -> Box<dyn AbstractRule> {
P
Phodal Huang 已提交
318
        self.rule_id2desc
P
Phodal Huang 已提交
319
            .insert(result.id().clone(), result.clone());
320
        result
P
Phodal Huang 已提交
321
    }
P
Phodal Huang 已提交
322 323 324 325
}

#[cfg(test)]
mod tests {
P
Phodal Huang 已提交
326
    use std::fs::File;
327
    use std::io::{Read, Write};
P
Phodal Huang 已提交
328
    use std::path::Path;
P
Phodal Huang 已提交
329

P
Phodal Huang 已提交
330
    use crate::grammar::Grammar;
P
Phodal Huang 已提交
331
    use crate::inter::IRawGrammar;
332
    use crate::rule::IRuleRegistry;
P
Phodal Huang 已提交
333

P
Phodal Huang 已提交
334
    #[test]
P
Phodal Huang 已提交
335
    fn should_build_json_code() {
336 337 338 339 340 341 342 343
        let code = "
#include <stdio.h>
int main() {
printf(\"Hello, World!\");
return 0;
}
";
        let grammar = to_grammar("test-cases/first-mate/fixtures/c.json", code);
344
        // assert_eq!(grammar.rule_id2desc.len(), 162);
345
        // debug_output(&grammar, String::from("program.json"));
346 347
    }

P
Phodal Huang 已提交
348 349 350
    #[test]
    fn should_build_text_grammar() {
        let code = "
P
Phodal Huang 已提交
351
GitHub 漫游指南
P
Phodal Huang 已提交
352 353
";
        let grammar = to_grammar("test-cases/first-mate/fixtures/text.json", code);
354
        assert_eq!(grammar.rule_id2desc.len(), 8);
355 356 357
    }

    fn debug_output(grammar: &Grammar, path: String) {
P
Phodal Huang 已提交
358
        let j = serde_json::to_string(&grammar.rule_id2desc).unwrap();
359
        let mut file = File::create(path).unwrap();
P
Phodal Huang 已提交
360
        match file.write_all(j.as_bytes()) {
P
Phodal Huang 已提交
361 362
            Ok(_) => {}
            Err(_) => {}
P
Phodal Huang 已提交
363
        };
P
Phodal Huang 已提交
364 365
    }

366 367 368 369
    #[test]
    fn should_build_json_grammar() {
        let code = "{}";
        let grammar = to_grammar("test-cases/first-mate/fixtures/json.json", code);
370 371 372 373 374 375 376 377 378
        assert_eq!(grammar.rule_id2desc.len(), 22);
        debug_output(&grammar, String::from("program.json"));
    }

    #[test]
    fn should_build_html_grammar() {
        let code = "{}";
        let grammar = to_grammar("test-cases/first-mate/fixtures/html.json", code);
        assert_eq!(grammar.rule_id2desc.len(), 67);
379 380 381
        debug_output(&grammar, String::from("program.json"));
    }

P
Phodal Huang 已提交
382 383
    #[test]
    fn should_build_makefile_grammar() {
384 385 386 387 388 389 390 391 392 393
        let code = "CC=gcc
CFLAGS=-I.
DEPS = hellomake.h
OBJ = hellomake.o hellofunc.o

%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS)
P
Phodal Huang 已提交
394
";
395
        let mut grammar = to_grammar("test-cases/first-mate/fixtures/makefile.json", code);
P
Phodal Huang 已提交
396
        assert_eq!(grammar.rule_id2desc.len(), 64);
397
        assert_eq!(grammar.get_rule(1).patterns_length(), 4);
P
Phodal Huang 已提交
398 399 400
        debug_output(&grammar, String::from("program.json"));
    }

401 402
    fn to_grammar(grammar_path: &str, code: &str) -> Grammar {
        let path = Path::new(grammar_path);
P
Phodal Huang 已提交
403 404 405 406 407 408
        let mut file = File::open(path).unwrap();
        let mut data = String::new();
        file.read_to_string(&mut data).unwrap();

        let g: IRawGrammar = serde_json::from_str(&data).unwrap();

P
Phodal Huang 已提交
409
        let mut grammar = Grammar::new(g);
410
        let c_code = String::from(code);
P
Phodal Huang 已提交
411 412 413
        for line in c_code.lines() {
            grammar.tokenize_line(String::from(line), None)
        }
414
        grammar
P
Phodal Huang 已提交
415 416
    }
}