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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use std::{iter::Peekable, slice::Iter};

use crate::{Token, TokenData, TokenType};

use super::{MacroError, PreprocessError, PreprocessResult};

#[derive(Debug, Clone)]
pub struct MacroArg {
    required: bool,
    default: Vec<Token>,
}

impl MacroArg {
    /// Create a new macro argument.
    pub fn new(required: bool, default: Vec<Token>) -> MacroArg {
        MacroArg { required, default }
    }

    /// Returns true of this argument is required.
    pub fn required(&self) -> bool {
        self.required
    }

    /// Returns the default value of this argument.
    pub fn default_owned(&self) -> Vec<Token> {
        self.default.to_owned()
    }
}

pub struct Macro {
    id: String,
    contents: Vec<Token>,
    args: Vec<MacroArg>,
    num_required_args: usize,
}

impl Macro {
    /// Creates a new macro
    pub fn new(
        id: &str,
        contents: Vec<Token>,
        args: Vec<MacroArg>,
        num_required_args: usize,
    ) -> Macro {
        Macro {
            id: id.to_owned(),
            contents,
            args,
            num_required_args,
        }
    }

    /// Parses a macro from the provided token iterator
    pub fn parse_macro(
        start_line: usize,
        token_iter: &mut Peekable<Iter<Token>>,
    ) -> PreprocessResult<Macro> {
        // Check to see if we have a token, and it is an identifier
        if token_iter.peek().is_some() && token_iter.peek().unwrap().tt() == TokenType::IDENTIFIER {
            let id = match token_iter.next().unwrap().data() {
                TokenData::STRING(s) => s,
                _ => unreachable!(),
            };
            let mut contents = Vec::new();
            let mut args = Vec::new();
            let mut clean_exit = false;
            let mut min_args = 0;
            let mut max_args = 0;

            // There needs to be an .endmacro directive, so no more tokens is an error
            if token_iter.peek().is_none() {
                return Err(PreprocessError::MacroParseError(
                    id.to_owned(),
                    start_line,
                    MacroError::IncompleteMacroDefinition.into(),
                )
                .into());
            }
            // If there are arguments to this macro
            else if token_iter.peek().unwrap().tt() != TokenType::NEWLINE {
                // Now we either have a number of arguments, or a range.

                // It has to be an int
                if token_iter.peek().unwrap().tt() != TokenType::INT {
                    return Err(PreprocessError::MacroParseError(
                        id.to_owned(),
                        start_line,
                        MacroError::InvalidNumberOfArguments(token_iter.peek().unwrap().as_str())
                            .into(),
                    )
                    .into());
                }
                min_args = match token_iter.next().unwrap().data() {
                    TokenData::INT(i) => *i,
                    _ => unreachable!(),
                };
                max_args = min_args;

                // The next token can either be a newline or a -
                if token_iter.peek().unwrap().tt() == TokenType::NEWLINE {
                    // If it is a newline that means that all arguments are required.
                    for _ in 0..min_args {
                        args.push(MacroArg::new(true, Vec::new()));
                    }
                }
                // If the next token was not a newline, it must be a minus, followed by a max numberof args
                else if token_iter.peek().unwrap().tt() == TokenType::MINUS {
                    let num_required_default_values;

                    // Consume the comma
                    token_iter.next();

                    // Next has to be an int
                    if token_iter.peek().unwrap().tt() != TokenType::INT {
                        return Err(PreprocessError::MacroParseError(
                            id.to_owned(),
                            start_line,
                            MacroError::ExpectedArgumentRange(token_iter.peek().unwrap().as_str())
                                .into(),
                        )
                        .into());
                    }
                    max_args = match token_iter.next().unwrap().data() {
                        TokenData::INT(i) => *i,
                        _ => unreachable!(),
                    };

                    // Test if the range makes sense...
                    if min_args >= max_args {
                        return Err(PreprocessError::MacroParseError(
                            id.to_owned(),
                            start_line,
                            MacroError::InvalidArgumentRange((min_args, max_args)).into(),
                        )
                        .into());
                    }

                    num_required_default_values = max_args - min_args;

                    // Populate all of the really required arguments
                    for _ in 0..min_args {
                        args.push(MacroArg::new(true, Vec::new()));
                    }

                    // If it is a range, now we have to deal with all of the default values
                    for _ in 0..num_required_default_values {
                        let mut argument_contents = Vec::new();

                        // We need to have all of the macro argument default values, if one is missing, there is a problem.
                        if token_iter.peek().is_none()
                            || token_iter.peek().unwrap().tt() == TokenType::NEWLINE
                        {
                            return Err(PreprocessError::MacroParseError(
                                id.to_owned(),
                                start_line,
                                MacroError::MissingDefaultArgumentValue.into(),
                            )
                            .into());
                        }

                        // Go until we run out or hit a comma or newline
                        while token_iter.peek().is_some()
                            && token_iter.peek().unwrap().tt() != TokenType::COMMA
                            && token_iter.peek().unwrap().tt() != TokenType::NEWLINE
                        {
                            let token = token_iter.next().unwrap();

                            argument_contents.push(token.clone());
                        }

                        // If it was a comma that stopped us, consume it
                        if token_iter.peek().unwrap().tt() == TokenType::COMMA {
                            token_iter.next();
                        }

                        // Now that we have the argument contents, add it to the list
                        args.push(MacroArg::new(false, argument_contents));
                    }

                    // There MUST be a newline here
                    if token_iter.peek().is_none()
                        || token_iter.peek().unwrap().tt() != TokenType::NEWLINE
                    {
                        let token_str = match token_iter.peek() {
                            Some(t) => t.as_str(),
                            None => String::new(),
                        };

                        return Err(PreprocessError::MacroParseError(
                            id.to_owned(),
                            start_line,
                            MacroError::TokenAfterMacroArguments(token_str).into(),
                        )
                        .into());
                    }
                }
                // If it isn't either, that is an error
                else {
                    let invalid_token = token_iter.peek().unwrap();

                    return Err(PreprocessError::MacroParseError(
                        id.to_owned(),
                        start_line,
                        MacroError::InvalidTokenInDeclaration(invalid_token.as_str()).into(),
                    )
                    .into());
                }
            }
            // If there are no arguments, we just move on, but consume the newline
            token_iter.next();

            // Now we can fill in the body of the macro
            while token_iter.peek().is_some() {
                // If the next token on the line is a directive, then test if it is the .endmacro directive
                if token_iter.peek().unwrap().tt() == TokenType::DIRECTIVE {
                    let directive = match token_iter.peek().unwrap().data() {
                        TokenData::STRING(s) => s,
                        _ => unreachable!(),
                    };

                    if *directive == "endmacro" {
                        // Consume the directive
                        token_iter.next();
                        // Set clean exit to true
                        clean_exit = true;
                        // Break from this loop
                        break;
                    }
                    // If it isn't the endmacro directive, then just treat it like any other token
                    else {
                        contents.push(token_iter.next().unwrap().clone());
                    }
                }
                // We should also test for argument placeholders
                else if token_iter.peek().unwrap().tt() == TokenType::AMPERSAND {
                    let argument_number;
                    let line;

                    // Consume it
                    token_iter.next();

                    // Now we MUST have an integer follow this
                    if token_iter.peek().is_none()
                        || token_iter.peek().unwrap().tt() != TokenType::INT
                    {
                        let token_str = match token_iter.peek() {
                            Some(t) => {
                                line = t.line();
                                t.as_str()
                            }
                            None => {
                                line = start_line;
                                String::new()
                            }
                        };

                        return Err(PreprocessError::MacroParseError(
                            id.to_owned(),
                            line,
                            MacroError::InvalidArgumentReference(token_str).into(),
                        )
                        .into());
                    } else {
                        line = token_iter.peek().unwrap().line();
                    }

                    // Get the number of the argument
                    argument_number = match token_iter.next().unwrap().data() {
                        TokenData::INT(i) => *i,
                        _ => unreachable!(),
                    };

                    // Make sure it isn't out of bounds
                    if argument_number > max_args {
                        return Err(PreprocessError::MacroParseError(
                            id.to_owned(),
                            line,
                            MacroError::ArgumentReferenceOutOfBounds(argument_number).into(),
                        )
                        .into());
                    }

                    // Now replace it with a placeholder
                    contents.push(Token::new(
                        TokenType::PLACEHOLDER,
                        TokenData::INT(argument_number),
                    ));
                }
                // If it isn't either, just push the token
                else {
                    contents.push(token_iter.next().unwrap().clone());
                }
            }

            // If this loop ended because we ran out of tokens... that is bad
            if !clean_exit {
                Err(PreprocessError::MacroParseError(
                    id.to_owned(),
                    start_line,
                    MacroError::EndedWithoutClosing.into(),
                )
                .into())
            } else {
                Ok(Macro::new(id, contents, args, min_args as usize))
            }
        }
        // If we don't that is an error because it is required
        else {
            Err(PreprocessError::MacroParseError(
                String::new(),
                start_line,
                MacroError::MissingIdentifier.into(),
            )
            .into())
        }
    }

    pub fn id(&self) -> String {
        self.id.to_owned()
    }

    pub fn contents_cloned(&self) -> Vec<Token> {
        self.contents.to_owned()
    }

    pub fn args_cloned(&self) -> Vec<MacroArg> {
        self.args.to_owned()
    }

    pub fn get_contents_iter(&self) -> Peekable<Iter<Token>> {
        self.contents.iter().peekable()
    }

    pub fn args(&self) -> &Vec<MacroArg> {
        &self.args
    }

    pub fn num_required_args(&self) -> usize {
        self.num_required_args
    }
}