summaryrefslogtreecommitdiff
path: root/src/stages/syntactic.rs
blob: 960a4edd1e1a41c4dbed31f34f79b4484e3748eb (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use crate::*;

use std::path::PathBuf;


pub fn parse_syntactic<P: Into<PathBuf>>(source_code: &str, path: Option<P>) -> Result<Vec<Tracked<SyntacticToken>>, Vec<Tracked<SyntacticError>>> {
    parse_syntactic_from_tokeniser(Tokeniser::new(source_code, path))
}

fn parse_syntactic_from_tokeniser(mut t: Tokeniser) -> Result<Vec<Tracked<SyntacticToken>>, Vec<Tracked<SyntacticError>>> {
    t.add_delimiters(&['@','&','%',';','{','}','(',')','[',']','#','~','"','\'']);
    t.add_terminators(&[':']);
    let mut tokens = Vec::new();
    let mut errors = Vec::new();
    let mut label_name = String::new();

    macro_rules! err {
        ($error:expr) => {{
            err!($error, t.get_source());
        }};
        ($error:expr, $source:expr) => {{
            errors.push(Tracked::from($error, $source));
            continue;
        }};
    }

    macro_rules! is_any {
        ($close:expr) => {
            |t: &mut Tokeniser| { t.eat_char() == Some($close) }
        };
    }

    loop {
        t.eat_whitespace();
        t.mark_start();
        let Some(c) = t.eat_char() else { break };
        let token = match c {
            '"' => {
                let source = t.get_source();
                match t.track_until(is_any!('"')) {
                    Some(string) => {
                        let mut bytes = string.into_bytes();
                        bytes.push(0x00);
                        SyntacticToken::String(bytes)
                    }
                    None => err!(SyntacticError::UnterminatedNullString, source),
                }
            }
            '\'' => {
                let source = t.get_source();
                match t.track_until(is_any!('\'')) {
                    Some(string) => SyntacticToken::String(string.into_bytes()),
                    None => err!(SyntacticError::UnterminatedRawString, source),
                }
            }
            '(' => {
                let source = t.get_source();
                if let Some(string) = t.track_until(is_any!(')')) {
                    // Check if the comment fills the entire line.
                    if t.start.position.column == 0 && t.end_of_line() {
                        if let Some(path) = string.strip_prefix(": ") {
                            t.embedded_path = Some(PathBuf::from(path.trim()));
                            t.embedded_first_line = t.start.position.line + 1;
                            continue;
                        }
                    }
                    SyntacticToken::Comment(string)
                } else {
                    err!(SyntacticError::UnterminatedComment, source)
                }
            }
            '%' => {
                let name = t.eat_token();
                let source = t.get_source();
                t.mark_child();
                if let Some(_) = t.track_until(is_any!(';')) {
                    let child = t.tokenise_child_span();
                    match parse_body_from_tokeniser(child) {
                        Ok(body) => {
                            let name = Tracked::from(name, source);
                            let definition = SyntacticMacroDefinition { name, body };
                            SyntacticToken::MacroDefinition(definition)
                        }
                        Err(mut err) => {
                            errors.append(&mut err);
                            continue;
                        }
                    }
                } else {
                    err!(SyntacticError::UnterminatedMacroDefinition, source);
                }
            }
            '{' => SyntacticToken::BlockOpen,
            '}' => SyntacticToken::BlockClose,
            '[' => continue,
            ']' => continue,

            ')' => err!(SyntacticError::UnmatchedCommentTerminator),
            ';' => err!(SyntacticError::UnmatchedMacroTerminator),

            '@' => {
                label_name = t.eat_token();
                SyntacticToken::LabelDefinition(label_name.clone())
            }
            '&' => {
                let name = t.eat_token();
                SyntacticToken::LabelDefinition(format!("{label_name}/{name}"))
            }
            '~' => {
                let name = t.eat_token();
                SyntacticToken::Invocation(format!("{label_name}/{name}"))
            }
            '#' => {
                let token = t.eat_token();
                match token.parse::<Value>() {
                    Ok(value) => SyntacticToken::Padding(value),
                    Err(_) => err!(SyntacticError::InvalidPaddingValue),
                }
            },
            c => {
                let token = format!("{c}{}", t.eat_token());
                if let Ok(value) = token.parse::<Value>() {
                    SyntacticToken::RawValue(value)
                } else if let Ok(instruction) = token.parse::<Instruction>() {
                    SyntacticToken::Instruction(instruction)
                } else {
                   SyntacticToken::Invocation(token)
                }
            }
        };

        t.mark_end();
        let source = t.get_source();
        tokens.push(Tracked::from(token, source));
    }

    // Check that every block open matches a block close.
    let mut stack = Vec::new();
    for token in &tokens {
        match &token.value {
            SyntacticToken::BlockOpen => stack.push(token.source.clone()),
            SyntacticToken::BlockClose => if let None = stack.pop() {
                let error = SyntacticError::UnmatchedBlockTerminator;
                errors.push(Tracked::from(error, token.source.clone()));
            }
            _ => (),
        }
    }
    for source in stack {
        let error = SyntacticError::UnterminatedBlock;
        errors.push(Tracked::from(error, source));
    }

    match errors.is_empty() {
        true => Ok(tokens),
        false => Err(errors),
    }
}


fn parse_body_from_tokeniser(t: Tokeniser) -> Result<Vec<Tracked<SyntacticToken>>, Vec<Tracked<SyntacticError>>> {
    let mut tokens = Vec::new();
    let mut errors = Vec::new();

    for token in parse_syntactic_from_tokeniser(t)? {
        match token.value {
            SyntacticToken::LabelDefinition(_) => {
                let error = SyntacticError::LabelDefinitionInMacroDefinition;
                errors.push(Tracked::from(error, token.source));
                continue;
            }
            SyntacticToken::MacroDefinition(_) => {
                let error = SyntacticError::MacroDefinitionInMacroDefinition;
                errors.push(Tracked::from(error, token.source));
                continue;
            }
            _ => tokens.push(token),
        };
    }

    match errors.is_empty() {
        true => Ok(tokens),
        false => Err(errors),
    }
}