aboutsummaryrefslogtreecommitdiffstats
path: root/src/sectionizer.rs
blob: b66f388927047e522e101f2d06a171186bd38fbc (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::error::{Error, ErrorKind};
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,
    pub faulty: bool,
}

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

pub fn sectionize(path: &Path) -> Result<Vec<Section>, Vec<Error>> {
    let mut read_files = HashSet::new();
    read_files.insert(path.to_path_buf());
    let tokens = file_to_tokens(path).map_err(|_| vec![
                 Error::new_nowhere(ErrorKind::FileNotFound(path.to_path_buf()), None)
    ])?;
    let mut all_tokens = vec![(path.to_path_buf(), tokens)];
    let mut sections = Vec::new();
    let mut errors = 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, line))) => {
                    let use_file: PathBuf = format!("{}.sy", use_file).into();
                    if !read_files.contains(&use_file) {
                        read_files.insert(use_file.clone());
                        match file_to_tokens(&use_file) {
                            Ok(tokens) => {
                                all_tokens.push((use_file, tokens))
                            }
                            Err(_) => {
                                errors.push(Error {
                                    kind: ErrorKind::FileNotFound(use_file),
                                    file: path.to_path_buf(),
                                    line: *line,
                                    message: None,
                                });
                            }
                        }
                    }
                    true
                },

                (Some((Token::LeftBrace, _)), ..)
                    => {
                        let mut blocks = 1;
                        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]));
    }
    if errors.is_empty() {
        Ok(sections)
    } else {
        Err(errors)
    }
}