grammar.rs 20.0 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};
P
Phodal Huang 已提交
4 5
use crate::grammar::local_stack_element::LocalStackElement;
use crate::grammar::{MatchRuleResult, ScopeListElement, StackElement};
P
Phodal Huang 已提交
6
use crate::inter::{IRawGrammar, IRawRepository, IRawRepositoryMap, IRawRule};
P
Phodal Huang 已提交
7
use crate::rule::abstract_rule::RuleEnum;
P
Phodal Huang 已提交
8
use crate::rule::rule_factory::RuleFactory;
P
Phodal Huang 已提交
9
use crate::rule::{AbstractRule, EmptyRule, IGrammarRegistry, IRuleFactoryHelper, IRuleRegistry};
P
Phodal Huang 已提交
10
use core::cmp;
P
Phodal Huang 已提交
11
use scie_scanner::scanner::scanner::IOnigCaptureIndex;
P
Phodal Huang 已提交
12

P
Phodal Huang 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25
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 已提交
26
    pub rule_stack: Box<StackElement>,
P
Phodal Huang 已提交
27 28 29 30 31
}

pub trait IGrammar {
    fn tokenize_line(line_text: String, prev_state: Option<StackElement>) -> ITokenizeLineResult;
    /**
P
Phodal Huang 已提交
32 33 34 35 36 37 38 39 40
     * 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 已提交
41
    fn tokenize_line2(line_text: String, prev_state: Option<StackElement>) -> ITokenizeLineResult2;
P
Phodal Huang 已提交
42 43
}

P
Phodal Huang 已提交
44
pub trait Matcher {}
P
Phodal Huang 已提交
45

P
Phodal Huang 已提交
46 47 48 49 50 51 52
#[derive(Debug, Clone)]
pub struct TokenizeResult {
    line_length: usize,
    line_tokens: Box<LineTokens>,
    rule_stack: Box<Option<StackElement>>
}

P
Phodal Huang 已提交
53
#[derive(Debug, Clone)]
P
Phodal Huang 已提交
54
pub struct Grammar {
55
    root_id: i32,
P
Phodal Huang 已提交
56
    grammar: IRawGrammar,
57
    pub last_rule_id: i32,
58
    pub rule_id2desc: Map<i32, Box<dyn AbstractRule>>,
P
Phodal Huang 已提交
59
    pub _token_type_matchers: Vec<TokenTypeMatcher>,
P
Phodal Huang 已提交
60 61
}

P
Phodal Huang 已提交
62
pub fn init_grammar(grammar: IRawGrammar, _base: Option<IRawRule>) -> IRawGrammar {
P
Phodal Huang 已提交
63 64 65
    let mut _grammar = grammar.clone();

    let mut new_based: IRawRule = IRawRule::new();
P
Phodal Huang 已提交
66 67 68
    if let Some(repo) = grammar.clone().repository {
        new_based.location = repo.clone().location;
    }
P
Phodal Huang 已提交
69 70
    new_based.patterns = Some(grammar.clone().patterns.clone());
    new_based.name = grammar.clone().name;
P
Phodal Huang 已提交
71 72 73 74

    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 已提交
75 76 77
    if let Some(repo) = grammar.clone().repository {
        repository_map.name_map = repo.clone().map.name_map.clone();
    }
P
Phodal Huang 已提交
78 79 80

    _grammar.repository = Some(IRawRepository {
        map: Box::new(repository_map.clone()),
81
        location: None,
P
Phodal Huang 已提交
82 83 84 85 86
    });

    _grammar
}

P
Phodal Huang 已提交
87
impl Grammar {
P
Phodal Huang 已提交
88
    pub fn new(grammar: IRawGrammar) -> Grammar {
P
Phodal Huang 已提交
89
        let _grammar = init_grammar(grammar.clone(), None);
P
Phodal Huang 已提交
90
        Grammar {
91
            last_rule_id: 0,
P
Phodal Huang 已提交
92
            grammar: _grammar,
P
Phodal Huang 已提交
93
            root_id: -1,
94
            rule_id2desc: Map::new(),
P
Phodal Huang 已提交
95
            _token_type_matchers: vec![],
P
Phodal Huang 已提交
96 97 98
        }
    }

P
Phodal Huang 已提交
99
    fn tokenize(
100
        &mut self,
P
Phodal Huang 已提交
101
        line_text: String,
102
        prev_state: Option<StackElement>,
P
Phodal Huang 已提交
103
        emit_binary_tokens: bool,
P
Phodal Huang 已提交
104
    ) -> TokenizeResult {
105 106
        if self.root_id.clone() == -1 {
            let mut repository = self.grammar.repository.clone().unwrap();
P
Phodal Huang 已提交
107
            let based = repository.clone().map.self_s.unwrap();
P
Phodal Huang 已提交
108 109 110 111 112 113
            self.root_id = RuleFactory::get_compiled_rule_id(
                based.clone(),
                self,
                &mut repository.clone(),
                String::from(""),
            );
114
        }
P
Phodal Huang 已提交
115

P
Phodal Huang 已提交
116
        let mut is_first_line: bool = false;
117 118 119

        let mut current_state = StackElement::null();

P
Phodal Huang 已提交
120
        match prev_state.clone() {
P
Phodal Huang 已提交
121
            None => is_first_line = true,
122 123 124 125
            Some(state) => {
                if state == StackElement::null() {
                    is_first_line = true
                }
126 127

                current_state = state;
P
Phodal Huang 已提交
128
            }
129
        }
P
Phodal Huang 已提交
130

P
Phodal Huang 已提交
131
        if is_first_line {
P
Phodal Huang 已提交
132
            // let scope_list = ScopeListElement::default();
P
Phodal Huang 已提交
133
            let _root_scope_name = self.get_rule(self.root_id.clone()).get_name(None, None);
P
Phodal Huang 已提交
134 135 136 137 138
            let mut root_scope_name = String::from("unknown");
            if let Some(name) = _root_scope_name {
                root_scope_name = name
            }

P
Phodal Huang 已提交
139
            let scope_list = ScopeListElement::new(None, root_scope_name);
140
            let state = StackElement::new(
P
Phodal Huang 已提交
141 142 143 144 145 146 147 148
                None,
                self.root_id.clone(),
                -1,
                -1,
                false,
                None,
                scope_list.clone(),
                scope_list.clone(),
149 150 151
            );

            current_state = state;
P
Phodal Huang 已提交
152 153
        } else {
            is_first_line = false;
P
Phodal Huang 已提交
154 155
        }

156
        let format_line_text = line_text.clone() + "\n";
P
Phodal Huang 已提交
157
        let mut line_tokens = LineTokens::new(
P
Phodal Huang 已提交
158 159 160 161
            emit_binary_tokens,
            line_text,
            self._token_type_matchers.clone(),
        );
P
Phodal Huang 已提交
162 163
        let next_state = self.tokenize_string(
            format_line_text.clone(),
P
Phodal Huang 已提交
164 165
            is_first_line,
            0,
P
Phodal Huang 已提交
166
            current_state,
167
            &mut line_tokens,
P
Phodal Huang 已提交
168
            true,
169
        );
P
Phodal Huang 已提交
170 171 172 173 174 175

        TokenizeResult {
            line_length: format_line_text.clone().len(),
            line_tokens: Box::new(line_tokens),
            rule_stack: Box::new(next_state)
        }
P
Phodal Huang 已提交
176 177
    }

P
Phodal Huang 已提交
178 179 180
    pub fn tokenize_string(
        &mut self,
        line_text: String,
181 182
        origin_is_first: bool,
        origin_line_pos: i32,
P
Phodal Huang 已提交
183
        mut stack: StackElement,
184
        mut line_tokens: &mut LineTokens,
P
Phodal Huang 已提交
185
        check_while_conditions: bool,
186
    ) -> Option<StackElement> {
P
Phodal Huang 已提交
187
        let _line_length = line_text.len();
188
        let mut _stop = false;
189
        let mut anchor_position = -1;
P
Phodal Huang 已提交
190 191

        if check_while_conditions {
192
            // todo: add really logic
P
Phodal Huang 已提交
193 194
            self.check_while_conditions(
                line_text.clone(),
195 196
                origin_is_first.clone(),
                origin_line_pos.clone(),
197
                stack.clone(),
P
Phodal Huang 已提交
198 199
                line_tokens.clone(),
            );
P
Phodal Huang 已提交
200 201
        }

202 203
        let mut line_pos = origin_line_pos.clone();
        let mut is_first_line = origin_is_first.clone();
204
        while !_stop {
P
Phodal Huang 已提交
205 206 207 208
            let r = self.match_rule(
                line_text.clone(),
                is_first_line,
                line_pos,
P
Phodal Huang 已提交
209
                &mut stack,
P
Phodal Huang 已提交
210 211
                anchor_position,
            );
212
            if let None = r {
P
Phodal Huang 已提交
213
                line_tokens.produce(&mut stack, _line_length as i32);
214
                _stop = true;
P
Phodal Huang 已提交
215
                return Some(stack.clone());
216 217
            }

P
Phodal Huang 已提交
218 219 220 221 222
            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");
223
                _stop = true;
P
Phodal Huang 已提交
224
                return Some(stack.clone());
P
Phodal Huang 已提交
225 226
            } else {
                let rule = self.get_rule(matched_rule_id);
P
Phodal Huang 已提交
227
                line_tokens.produce(&mut stack, capture_indices[0].start as i32);
P
Phodal Huang 已提交
228
                // let before_push = stack.clone();
P
Phodal Huang 已提交
229 230 231 232 233
                let scope_name =
                    rule.get_name(Some(line_text.clone()), Some(capture_indices.clone()));
                let name_scopes_list = stack
                    .content_name_scopes_list
                    .push(self, scope_name);
P
Phodal Huang 已提交
234 235 236 237
                let mut begin_rule_capture_eol = false;
                if capture_indices[0].end == _line_length {
                    begin_rule_capture_eol = true;
                }
P
Phodal Huang 已提交
238
                let new_stack = stack.push(
P
Phodal Huang 已提交
239 240 241 242 243 244
                    matched_rule_id,
                    line_pos,
                    anchor_position,
                    begin_rule_capture_eol,
                    None,
                    name_scopes_list.clone(),
P
Phodal Huang 已提交
245
                    name_scopes_list.clone(),
P
Phodal Huang 已提交
246
                );
P
Phodal Huang 已提交
247
                stack = new_stack;
P
Phodal Huang 已提交
248

P
Phodal Huang 已提交
249 250
                match rule.get_rule_instance() {
                    RuleEnum::BeginEndRule(begin_rule) => {
251
                        let push_rule = begin_rule.clone();
252
                        Grammar::handle_captures(
P
Phodal Huang 已提交
253 254 255
                            self,
                            line_text.clone(),
                            is_first_line,
P
Phodal Huang 已提交
256
                            &mut stack,
257
                            line_tokens,
P
Phodal Huang 已提交
258 259 260
                            begin_rule.begin_captures,
                            capture_indices.clone(),
                        );
P
Phodal Huang 已提交
261

P
Phodal Huang 已提交
262
                        line_tokens.produce(&mut stack, capture_indices[0].end.clone() as i32);
263
                        anchor_position = capture_indices[0].end.clone() as i32;
P
Phodal Huang 已提交
264 265
                        let content_name = push_rule
                            .get_name(Some(line_text.clone()), Some(capture_indices.clone()));
266 267 268 269 270 271
                        let content_name_scopes_list = name_scopes_list.push(self, content_name);
                        // todo: not used
                        // let temp_stack = &mut stack.set_content_name_scopes_list(content_name_scopes_list);
                        // if push_rule.endHasBackReferences {
                        //
                        // }
272

273 274 275
                        // if (!hasAdvanced && beforePush.hasSameRuleAs(stack)) {
                        // _stop = true;
                        // return None;
276 277 278
                    }
                    RuleEnum::BeginWhileRule(while_rule) => {
                        _stop = true;
P
Phodal Huang 已提交
279
                        return Some(stack.clone());
280 281 282
                    }
                    _ => {
                        _stop = true;
P
Phodal Huang 已提交
283
                        return Some(stack.clone());
P
Phodal Huang 已提交
284 285
                    }
                }
P
Phodal Huang 已提交
286
            }
287 288 289 290 291

            if capture_indices[0].end > line_pos as usize {
                line_pos = capture_indices[0].end as i32;
                is_first_line = false;
            }
292
        }
293
        Some(stack.clone())
P
Phodal Huang 已提交
294 295
    }

P
Phodal Huang 已提交
296 297 298 299 300
    pub fn handle_captures(
        grammar: &mut Grammar,
        line_text: String,
        is_first_line: bool,
        stack: &mut StackElement,
301
        mut line_tokens: &mut LineTokens,
P
Phodal Huang 已提交
302 303
        captures: Vec<Box<dyn AbstractRule>>,
        capture_indices: Vec<IOnigCaptureIndex>,
P
Phodal Huang 已提交
304
    ) -> Option<LineTokens> {
P
Phodal Huang 已提交
305 306
        let captures_len = captures.clone().len();
        if captures_len == 0 {
P
Phodal Huang 已提交
307
            return None;
P
Phodal Huang 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        }

        let len = cmp::min(captures_len, capture_indices.len());
        let mut local_stack: Vec<LocalStackElement> = vec![];
        let max_end = capture_indices[0].end;
        for i in 0..len {
            let capture_rule = captures[i].clone();
            // if let None = capture_rule {
            //     continue
            // }

            let capture_index = capture_indices[i].clone();
            if capture_index.length == 0 {
                continue;
            }

            if capture_index.start > max_end {
                continue;
            }

P
Phodal Huang 已提交
328 329 330
            while local_stack.len() > 0
                && local_stack[local_stack.len() - 1].end_pos <= capture_index.start as i32
            {
P
Phodal Huang 已提交
331 332 333
                let mut local_stack_element = local_stack[local_stack.len() - 1].clone();
                line_tokens.produce_from_scopes(
                    &mut local_stack_element.scopes,
P
Phodal Huang 已提交
334
                    local_stack_element.end_pos,
P
Phodal Huang 已提交
335 336 337
                );
                local_stack.pop();
            }
338 339 340 341 342 343 344 345 346 347

            if local_stack.len() > 0 {
                let mut local_stack_element = local_stack[local_stack.len() - 1].clone();
                line_tokens.produce_from_scopes(
                    &mut local_stack_element.scopes,
                    local_stack_element.end_pos,
                );
            } else {
                line_tokens.produce(stack, capture_index.start as i32);
            }
348

349 350 351
            match capture_rule.get_rule_instance() {
                RuleEnum::CaptureRule(capture) => {
                    if capture.retokenize_captured_with_rule_id != 0 {
P
Phodal Huang 已提交
352 353 354 355 356 357 358 359
                        let scope_name = capture
                            .get_name(Some(line_text.clone()), Some(capture_indices.clone()));
                        let name_scopes_list =
                            stack.content_name_scopes_list.push(grammar, scope_name);
                        let content_name = capture.get_content_name(
                            Some(line_text.clone()),
                            Some(capture_indices.clone()),
                        );
360
                        let content_name_scopes_list = name_scopes_list.push(grammar, content_name);
361

P
Phodal Huang 已提交
362 363 364 365 366 367 368 369
                        let mut stack_clone = stack.clone().push(
                            capture.retokenize_captured_with_rule_id,
                            capture_index.start.clone() as i32,
                            -1,
                            false,
                            None,
                            name_scopes_list,
                            content_name_scopes_list,
370 371
                        );

372 373 374 375 376
                        let sub_text = line_text.split_at(capture_index.end).0;
                        let mut sub_is_first_line = false;
                        if is_first_line && capture_index.start == 0 {
                            sub_is_first_line = true;
                        }
P
Phodal Huang 已提交
377 378 379 380 381
                        Grammar::tokenize_string(
                            grammar,
                            String::from(sub_text),
                            sub_is_first_line,
                            capture_index.start as i32,
P
Phodal Huang 已提交
382
                            stack_clone,
383
                            line_tokens,
P
Phodal Huang 已提交
384
                            false,
385
                        );
P
Phodal Huang 已提交
386
                        continue;
387 388 389 390
                    }
                }
                _ => {}
            }
P
Phodal Huang 已提交
391

P
Phodal Huang 已提交
392 393
            let capture_scope_name =
                capture_rule.get_name(Some(line_text.clone()), Some(capture_indices.clone()));
P
Phodal Huang 已提交
394 395 396 397 398 399
            if let Some(name) = capture_scope_name.clone() {
                let mut base = stack.clone().content_name_scopes_list;
                if local_stack.len() > 0 {
                    base = local_stack[local_stack.len() - 1].clone().scopes;
                }
                let capture_rule_scopes_list = base.push(grammar, capture_scope_name.clone());
P
Phodal Huang 已提交
400 401 402 403
                local_stack.push(LocalStackElement::new(
                    capture_rule_scopes_list,
                    capture_index.end as i32,
                ));
P
Phodal Huang 已提交
404 405 406 407
            }
        }

        while local_stack.len() > 0 {
P
Phodal Huang 已提交
408 409
            let mut last_stack = local_stack[local_stack.len() - 1].clone();
            line_tokens.produce_from_scopes(&mut last_stack.scopes, last_stack.end_pos);
P
Phodal Huang 已提交
410
            local_stack.pop();
P
Phodal Huang 已提交
411
        }
P
Phodal Huang 已提交
412 413

        return Some(line_tokens.to_owned());
P
Phodal Huang 已提交
414
    }
P
Phodal Huang 已提交
415

P
Phodal Huang 已提交
416 417 418 419 420
    pub fn check_while_conditions(
        &mut self,
        line_text: String,
        is_first_line: bool,
        line_pos: i32,
P
Phodal Huang 已提交
421
        _stack: StackElement,
P
Phodal Huang 已提交
422 423 424
        line_tokens: LineTokens,
    ) {
        let mut anchor_position = -1;
P
Phodal Huang 已提交
425 426 427
        if _stack.begin_rule_captured_eol {
            anchor_position = 0
        }
P
Phodal Huang 已提交
428 429
        // let while_rules = vec![];
    }
P
Phodal Huang 已提交
430

P
Phodal Huang 已提交
431 432 433 434 435
    pub fn match_rule_or_injections(
        &mut self,
        line_text: String,
        is_first_line: bool,
        line_pos: i32,
P
Phodal Huang 已提交
436
        stack: &mut StackElement,
P
Phodal Huang 已提交
437
        anchor_position: i32,
P
Phodal Huang 已提交
438
    ) {
P
Phodal Huang 已提交
439 440
        let match_result =
            self.match_rule(line_text, is_first_line, line_pos, stack, anchor_position);
P
Phodal Huang 已提交
441
        if let Some(result) = match_result {} else {
442 443 444
            // None
        };
        // todo: get injections logic
P
Phodal Huang 已提交
445 446 447 448 449 450 451
    }

    pub fn match_rule(
        &mut self,
        line_text: String,
        is_first_line: bool,
        line_pos: i32,
P
Phodal Huang 已提交
452
        stack: &mut StackElement,
P
Phodal Huang 已提交
453
        anchor_position: i32,
454
    ) -> Option<MatchRuleResult> {
455
        let mut rule = stack.get_rule(self);
P
Phodal Huang 已提交
456
        let mut rule_scanner = rule.compile(
P
Phodal Huang 已提交
457
            self,
P
Phodal Huang 已提交
458
            stack.end_rule.clone(),
P
Phodal Huang 已提交
459 460 461
            is_first_line,
            line_pos == anchor_position,
        );
P
Phodal Huang 已提交
462 463 464
        let r = rule_scanner
            .scanner
            .find_next_match_sync(line_text, line_pos);
P
Phodal Huang 已提交
465
        if let Some(result) = r {
466 467
            let match_rule_result = MatchRuleResult {
                capture_indices: result.capture_indices,
468
                matched_rule_id: rule_scanner.rules[result.index],
469 470 471 472
            };

            println!("{:?}", match_rule_result.clone());
            Some(match_rule_result)
P
Phodal Huang 已提交
473 474 475
        } else {
            None
        }
P
Phodal Huang 已提交
476
    }
P
Phodal Huang 已提交
477

P
Phodal Huang 已提交
478
    pub fn tokenize_line(&mut self, line_text: String, prev_state: Option<StackElement>) -> TokenizeResult {
P
Phodal Huang 已提交
479 480 481
        self.tokenize(line_text, prev_state, false)
    }

P
Phodal Huang 已提交
482 483
    pub fn tokenize_line2(&self, line_text: String, prev_state: Option<StackElement>) {}
}
P
Phodal Huang 已提交
484 485 486 487

impl IRuleFactoryHelper for Grammar {}

impl IGrammarRegistry for Grammar {
P
Phodal Huang 已提交
488 489 490 491 492
    fn get_external_grammar(
        &self,
        scope_name: String,
        repository: IRawRepository,
    ) -> Option<IRawGrammar> {
P
Phodal Huang 已提交
493 494 495 496 497
        None
    }
}

impl IRuleRegistry for Grammar {
P
Phodal Huang 已提交
498 499
    fn register_id(&mut self) -> i32 {
        self.last_rule_id = self.last_rule_id + 1;
P
Phodal Huang 已提交
500
        self.last_rule_id.clone()
P
Phodal Huang 已提交
501 502
    }

P
Phodal Huang 已提交
503 504 505
    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 已提交
506
        }
P
Phodal Huang 已提交
507
        Box::from(EmptyRule {})
P
Phodal Huang 已提交
508
    }
P
Phodal Huang 已提交
509

P
Phodal Huang 已提交
510
    fn register_rule(&mut self, result: Box<dyn AbstractRule>) -> Box<dyn AbstractRule> {
P
Phodal Huang 已提交
511
        self.rule_id2desc
P
Phodal Huang 已提交
512
            .insert(result.id().clone(), result.clone());
513
        result
P
Phodal Huang 已提交
514
    }
P
Phodal Huang 已提交
515 516 517 518
}

#[cfg(test)]
mod tests {
P
Phodal Huang 已提交
519
    use std::fs::File;
520
    use std::io::{Read, Write};
P
Phodal Huang 已提交
521
    use std::path::Path;
P
Phodal Huang 已提交
522

P
Phodal Huang 已提交
523
    use crate::grammar::Grammar;
P
Phodal Huang 已提交
524
    use crate::inter::IRawGrammar;
525
    use crate::rule::IRuleRegistry;
P
Phodal Huang 已提交
526

P
Phodal Huang 已提交
527
    #[test]
P
Phodal Huang 已提交
528
    fn should_build_json_code() {
529 530 531 532 533 534 535 536
        let code = "
#include <stdio.h>
int main() {
printf(\"Hello, World!\");
return 0;
}
";
        let grammar = to_grammar("test-cases/first-mate/fixtures/c.json", code);
537
        // assert_eq!(grammar.rule_id2desc.len(), 162);
538
        // debug_output(&grammar, String::from("program.json"));
539 540
    }

P
Phodal Huang 已提交
541 542 543
    #[test]
    fn should_build_text_grammar() {
        let code = "
P
Phodal Huang 已提交
544
GitHub 漫游指南
P
Phodal Huang 已提交
545 546
";
        let grammar = to_grammar("test-cases/first-mate/fixtures/text.json", code);
547
        assert_eq!(grammar.rule_id2desc.len(), 8);
548 549 550
    }

    fn debug_output(grammar: &Grammar, path: String) {
P
Phodal Huang 已提交
551
        let j = serde_json::to_string(&grammar.rule_id2desc).unwrap();
552
        let mut file = File::create(path).unwrap();
P
Phodal Huang 已提交
553
        match file.write_all(j.as_bytes()) {
P
Phodal Huang 已提交
554 555
            Ok(_) => {}
            Err(_) => {}
P
Phodal Huang 已提交
556
        };
P
Phodal Huang 已提交
557 558
    }

559 560 561 562
    #[test]
    fn should_build_json_grammar() {
        let code = "{}";
        let grammar = to_grammar("test-cases/first-mate/fixtures/json.json", code);
563 564 565 566 567 568 569 570 571
        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);
572 573 574
        debug_output(&grammar, String::from("program.json"));
    }

P
Phodal Huang 已提交
575 576
    #[test]
    fn should_build_makefile_grammar() {
577 578 579 580 581 582 583 584 585 586
        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 已提交
587
";
588
        let mut grammar = to_grammar("test-cases/first-mate/fixtures/makefile.json", code);
P
Phodal Huang 已提交
589
        assert_eq!(grammar.rule_id2desc.len(), 64);
590
        assert_eq!(grammar.get_rule(1).patterns_length(), 4);
P
Phodal Huang 已提交
591 592 593
        debug_output(&grammar, String::from("program.json"));
    }

594 595
    fn to_grammar(grammar_path: &str, code: &str) -> Grammar {
        let path = Path::new(grammar_path);
P
Phodal Huang 已提交
596 597 598 599 600 601
        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 已提交
602
        let mut grammar = Grammar::new(g);
603
        let c_code = String::from(code);
P
Phodal Huang 已提交
604
        for line in c_code.lines() {
P
Phodal Huang 已提交
605 606
            let result = grammar.tokenize_line(String::from(line), None);
            println!("{:?}", result);
P
Phodal Huang 已提交
607
        }
608
        grammar
P
Phodal Huang 已提交
609 610
    }
}