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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! Discover scoping as a preprocessing step to reduce the need to carry around extra
//! state in either the `Lexer` or `Parser`.
use nom::Slice;
use slog;
use slog_scope;


use ::token::{NEWLINE_BYTES, NEWLINE, SPACE, Tk, Id, Tag};
use ::slice::{TkSlice};
use ::preprocessor::Preprocessor;


/// Limit taken from CPython. They use a fixed size array to cap the indent stack.
const INDENT_STACK_SIZE : usize         = 100;

const TK_BLOCK_START: Tk = Tk::const_(
    Id::BlockStart,
    NEWLINE_BYTES,
    Tag::Note(BlockScopePreprocessor::NAME)
);

const TK_BLOCK_END: Tk = Tk::const_(
    Id::BlockEnd,
    NEWLINE_BYTES,
    Tag::Note(BlockScopePreprocessor::NAME)
);

pub const TK_LINE_CONT: Tk  = Tk::const_(
    Id::LineContinuation,
    NEWLINE_BYTES,
    Tag::Note(BlockScopePreprocessor::NAME)
);


const BLOCK_START: &'static [Tk] = &[TK_BLOCK_START];
const BLOCK_END  : &'static [Tk] = &[TK_BLOCK_END];
const LINE_CONT  : &'static [Tk] = &[TK_LINE_CONT];


// TODO: {T91} move to rsnek_runtime::macros
macro_rules! strings_error_indent_mismatch {
    ($len:expr, $indent:expr) => {
        format!("Indent len={} is not a multiple of first indent len={}", $len, $indent);
    }
}

// TODO: {T91} move to rsnek_runtime::macros
macro_rules! strings_error_unexpected_indent {
    ($len:expr, $indent:expr) => {
        format!("Unexpected indent len={}, expected indent len={}", $len, $indent);
    }
}


// TODO: {T91} move to rsnek_runtime::macros
macro_rules! strings_error_indent_overflow {
    ($max:expr) => {
        format!("Number of INDENT is more than the max allowed {}", $max);
    }
}


/// Preprocessor to splice BlockStart and BlockEnd tokens based on indent deltas
/// into the slice of tokens so we do not have to keep the state in the main parser
/// logic.
#[derive(Debug, Clone, Serialize)]
pub struct BlockScopePreprocessor {
    #[serde(skip_serializing)]
    log: slog::Logger
}


impl BlockScopePreprocessor {
    pub const NAME: &'static str = "BlockScopePreprocessor";

    // TODO: {114} This is a prototype of using a Logger for this struct which is in accordance
    // with best practices for slog, even though we are grabbing the root logger which is not.
    // Consider this an experimentation of the worth of using slog.
    pub fn new() -> Self {
        BlockScopePreprocessor {
            log: slog_scope::logger().new(slog_o!())
        }
    }

    pub const fn name(&self) -> &str {
        BlockScopePreprocessor::NAME
    }

    /// Determine the length of the first indent as the longest consecutive span of
    /// space tokens after a newline before a non space is reached where the the length
    /// of the span must be greater than 0.
    fn determine_indent<'a>(&self, tokens: &TkSlice<'a>) -> usize {

        let mut start: Option<usize> = None;
        let mut last: (usize, Tk<'a>) = (0, Tk::default());

        for (idx, tk) in tokens.iter().enumerate() {
            match (last.1.id(), tk.id(), start) {
                // A space preceded by a newline starts the search for the indent
                (Id::Newline, Id::Space, None) => start = Some(idx),
                // A newline preceded by a space but in the middle of a span means
                // a line of all whitespace. Reset search.
                (Id::Space, Id::Newline, Some(_)) => {
                    start = None;
                },
                // Search started and we have encountered the first non ws char
                // so send back the len of the search span as the known indent size.
                (Id::Space, id, Some(s)) if id != Id::Space && id != Id::Newline => {
                    return tokens.slice(s..idx).len();
                },
                _ => {}
            };

            last = (idx, tk.clone());
        }

        return 0
    }

    /// Given the length of the current span of whitespace, the size of the first discovered
    /// indent, and the current index in the indent stack.
    #[inline]
    fn balance_scopes<'b>(&self, span_len: usize, indent: usize,
                          stack_idx_start: usize, indent_stack: &mut [usize],
                          acc: &mut Vec<TkSlice<'b>>) -> Result<usize, String> {

        if span_len % indent != 0 {
            return Err(strings_error_indent_mismatch!(span_len, indent));
        }

        let mut stack_idx = stack_idx_start;

        match indent_stack[stack_idx] {
            // The next indent span must be the current + known indent size, or it
            // is an error case.
            curr if span_len == curr + indent => {
                if INDENT_STACK_SIZE <= stack_idx + 1 {
                    return Err(strings_error_indent_overflow!(INDENT_STACK_SIZE))
                }

                stack_idx += 1;
                indent_stack[stack_idx] = curr + indent;
                slog_trace!(self.log,
                "{}", self.name();
                "action" => "emit",
                "token" => "BlockStart",
                "stack_idx" => stack_idx);
                acc.push(TkSlice(&BLOCK_START));
            },
            // The de-indent case where we are going from a nested scope N back to the
            // N-1 scope.
            curr if span_len < curr => {
                'emit_block_end: while stack_idx != 0 {
                    stack_idx -= 1;

                    slog_trace!(self.log,
                    "{}", self.name();
                    "action" => "emit",
                    "token" => "BlockEnd",
                    "stack_idx" => stack_idx);
                    acc.push(TkSlice(&BLOCK_END));

                    if indent_stack[stack_idx] == span_len {
                        break 'emit_block_end;
                    }
                }
            },
            // Indent is the same as the current indent, so no changes
            curr if span_len == curr => {
                acc.push(TkSlice(&NEWLINE));
            }
            // Over indenting
            curr => {
                return Err(strings_error_unexpected_indent!(span_len, curr + indent));
            }
        }

        Ok(stack_idx)
    }
}


impl<'a> Preprocessor<'a> for BlockScopePreprocessor {
    type In = TkSlice<'a>;
    type Out = Box<[Tk<'a>]>;
    type Error = String;

    fn transform<'b>(&self, tokens: TkSlice<'b>) -> Result<Box<[Tk<'b>]>, String> {
        let indent = self.determine_indent(&tokens);

        if indent == 0 {
            slog_trace!(self.log,
                "{}", self.name();
                "action" => "skip",
                "reason" => "indent==0");
            return Ok(tokens.tokens().to_owned().into_boxed_slice());
        }

        let mut acc: Vec<TkSlice<'b>> = Vec::new();

        let mut stack_idx = 0;
        let mut indent_stack: [usize; INDENT_STACK_SIZE] = [0; INDENT_STACK_SIZE];

        let mut start: Option<usize> = None;
        let mut end: Option<usize> = None;
        let mut is_continuation = false;
        let mut seen_non_ws = false;

        let mut last_tk: (usize, Tk<'b>) = (0, Tk::default());

        for (idx, tk) in tokens.iter().enumerate() {
            match (last_tk.1.id(), tk.id(), is_continuation, seen_non_ws, start) {
                // Just seen a newline on the last pass and it is not a \ escaped
                // continuation so start collapsing the whitespace
                (Id::Newline, Id::Space, false, false, None) => {
                    if let Some(e) = end {
                        acc.push(tokens.slice(e..idx - 1))
                    } else {
                        acc.push(tokens.slice(..idx - 1));
                    }

                    start = Some(idx);
                    end = None;
                },
                // Continue to consume spaces
                (Id::Space, Id::Space, false, false, _) => {},
                // Found the first non ws char
                (Id::Space, id, false, false, Some(s)) if id != Id::Space => {
                    // TODO: {T92} Formalize preprocessing to allow for injection of expression start
                    // and end
                    let span = tokens.slice(s..idx);

                    seen_non_ws = true;
                    is_continuation = false;
                    start = None;
                    end = Some(idx);

                    match self.balance_scopes(span.len(), indent, stack_idx,
                                              &mut indent_stack, &mut acc) {
                        Ok(new_stack_idx) => stack_idx = new_stack_idx,
                        Err(string) => return Err(string)
                    };

                    acc.push(span);
                },
                // A top level scope cannot close its scopes until it finds the next one
                (Id::Newline, id, false, false, _) if id != Id::Space && id != Id::Newline => {
                    // TODO: {T92} Formalize preprocessing to allow for injection of expression start
                    // and end
                    if let Some(e) = end {
                        acc.push(tokens.slice(e..idx - 1))
                    } else {
                        acc.push(tokens.slice(..idx - 1));
                    }

                    seen_non_ws = true;
                    start = None;
                    end = Some(idx);

                    match self.balance_scopes(0, indent, stack_idx,
                                              &mut indent_stack, &mut acc) {
                        Ok(new_stack_idx) => stack_idx = new_stack_idx,
                        Err(string) => return Err(string)
                    };
                },
                // Continuation start case. It does not matter how many backslashes happen
                // before the newline as long as the last backslash is immediately followed by
                // a newline.
                (_, Id::Backslash, _, _, _) => {
                    // TODO: {T92} Formalize preprocessing to allow for injection of expression start
                    // and end
                    // TODO: {T93} Handle backslash continuations... rewrite the
                    //   newline masked as a Id::Space??
                    is_continuation = true;
                    match (start, end) {
                        (Some(s), None) => {
                            acc.push(tokens.slice(s..idx - 1));
                            //start = Some(idx);
                        },
                        (None, Some(e)) => {
                            acc.push(tokens.slice(e..idx - 1));
                            end = Some(idx);
                        },

                        error => {
                            slog_error!(self.log,
                                "{}", self.name();
                                "error" => "LineContinuation"
                            );
                            return Err(format!(
                                "{} encountered unhandled case {:?}", self.name(), error))
                        }
                    }

                    slog_trace!(self.log,
                        "{}", self.name();
                        "action" => "rewrite",
                        "Id::Backslash" => format!("{}", Id::Space),
                        "stack_idx" => stack_idx
                    );

                    acc.push(TkSlice(&SPACE));
                    start = Some(idx);
                },
                // Continuation start case.
                (Id::Backslash, Id::Newline, true, _, Some(s)) => {
                    acc.push(tokens.slice(s..idx - 1));

                    slog_trace!(self.log,
                        "{}", self.name();
                        "action" => "emit",
                        "token" => "LineContinuation",
                        "stack_idx" => stack_idx
                    );

                    acc.push(TkSlice(&LINE_CONT));
                    start = Some(idx + 1);
                },
                // TODO: {T92} Formalize preprocessing to allow for injection of expression start
                // and end
                // Newline after a Backslash Continuation, toggle the continuation to
                // false but keep seen_non_ws == true.
                (_, Id::Newline, true, _, _) => {
                    is_continuation = false;
                },
                // Normal newline
                (_, Id::Newline, false, _, _) => {
                    seen_non_ws = false;
                },
                ///
                ///

                _ => {}
            };

            last_tk = (idx, tk.clone());
        }

        // Put the rest of the tokens in the acc if the loops did not end on a
        // scope boundary.
        match (start, end) {
            (Some(i), _) |
            (_, Some(i)) => acc.push(tokens.slice(i..)),
            _ => unreachable!()
        };

        // There was not another block indented scope between the last
        // start and the end of the file. So emit the closing block ends until
        // all open block starts are balanced with block ends.
        'emit_trailing_end_scopes: while stack_idx != 0 {
            stack_idx -= 1;
            acc.push(TkSlice(&BLOCK_END));
        }

        let scoped_tokens = acc.iter()
            .flat_map(TkSlice::iter)
            .map(Tk::clone)
            .collect::<Vec<Tk<'b>>>();

        Ok(scoped_tokens.into_boxed_slice())
    }

}