aboutsummaryrefslogtreecommitdiffstats
path: root/src/sectionizer.rs
blob: 55f231e3e722b8641c47d4a29a6b8dd6f0dc0b73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::tokenizer::{PlacedToken, Token, file_to_tokens};

use std::collections::HashSet;
use std::path::{Path, PathBuf};

pub struct Section {
    pub tokens: Vec<PlacedToken>,
    pub path: PathBuf,
}

impl Section {
    fn new(path: PathBuf, tokens: &[PlacedToken]) -> Self {
        Self {
            tokens: Vec::from(tokens),
            path,
        }
    }
}

pub fn sectionize(path: &Path) -> Vec<Section> {
    let mut read_files = HashSet::new();
    read_files.insert(path.to_path_buf());
    let tokens = file_to_tokens(path);
    let mut all_tokens = vec![(path.to_path_buf(), tokens)];
    let mut sections = Vec::new();

    let mut i = 0;
    while i < all_tokens.len() {
        let (path, tokens) = all_tokens[i].clone();
        i += 1;
        let mut last = 0;
        let mut curr = 0;
        while curr < tokens.len() {
            if match (tokens.get(curr + 0), tokens.get(curr + 1), tokens.get(curr + 2)) {
                (Some((Token::Newline, _)), ..)
                    => {
                        if curr == last {
                            last += 1;
                        }
                        false
                    },

                (Some((Token::Use, _)),
                 Some((Token::Identifier(use_file), _)),
                 Some((Token::Newline, _))) => {
                    let use_file: PathBuf = format!("{}.sy", use_file).into();
                    if !read_files.contains(&use_file) {
                        let use_file_tokens = file_to_tokens(&use_file);
                        read_files.insert(use_file.clone());
                        all_tokens.push((use_file, use_file_tokens))
                    }
                    true
                },

                (Some((Token::LeftBrace, _)), ..)
                    => {
                        let mut blocks = 0;
                        loop {
                            curr += 1;
                            match tokens.get(curr) {
                                Some((Token::LeftBrace, _)) => {
                                    blocks += 1;
                                }

                                Some((Token::RightBrace, _)) => {
                                    curr += 1;
                                    blocks -= 1;
                                    if blocks <= 0 {
                                        break;
                                    }
                                }

                                None => {
                                    break;
                                }

                                _ => {}
                            }
                        }
                        false
                    },

                (Some((Token::Identifier(_), _)),
                 Some((Token::ColonColon, _)),
                 Some(_))
                    => true,

                (Some((Token::Identifier(_), _)),
                 Some((Token::ColonEqual, _)),
                 Some(_))
                    => true,

                _ => false,
            } {
                sections.push(Section::new(path.clone(), &tokens[last..curr]));
                last = curr;
            }
            curr += 1;
        }
        sections.push(Section::new(path.clone(), &tokens[last..curr]));
    }
    sections
}