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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use std::iter::Peekable;
use std::str::CharIndices;

use nu_source::{Span, Spanned, SpannedItem};

use crate::errors::{ParseError, ParseResult};

type Input<'t> = Peekable<CharIndices<'t>>;

#[derive(Debug, Clone)]
pub struct LiteCommand {
    pub name: Spanned<String>,
    pub args: Vec<Spanned<String>>,
}

impl LiteCommand {
    fn new(name: Spanned<String>) -> LiteCommand {
        LiteCommand { name, args: vec![] }
    }

    pub(crate) fn span(&self) -> Span {
        let start = self.name.span.start();
        let end = if let Some(x) = self.args.last() {
            x.span.end()
        } else {
            self.name.span.end()
        };

        Span::new(start, end)
    }
}

#[derive(Debug, Clone)]
pub struct LitePipeline {
    pub commands: Vec<LiteCommand>,
}

impl LitePipeline {
    pub(crate) fn span(&self) -> Span {
        let start = if !self.commands.is_empty() {
            self.commands[0].name.span.start()
        } else {
            0
        };

        if let Some((last, _)) = self.commands[..].split_last() {
            Span::new(start, last.span().end())
        } else {
            Span::new(start, 0)
        }
    }
}

#[derive(Debug, Clone)]
pub struct LiteBlock {
    pub block: Vec<LitePipeline>,
}

impl LiteBlock {
    pub(crate) fn span(&self) -> Span {
        let start = if !self.block.is_empty() {
            self.block[0].span().start()
        } else {
            0
        };

        if let Some((last, _)) = self.block[..].split_last() {
            Span::new(start, last.span().end())
        } else {
            Span::new(start, 0)
        }
    }
}

impl From<Spanned<String>> for LiteCommand {
    fn from(v: Spanned<String>) -> LiteCommand {
        LiteCommand::new(v)
    }
}

fn skip_whitespace(src: &mut Input) {
    while let Some((_, x)) = src.peek() {
        if x.is_whitespace() {
            let _ = src.next();
        } else {
            break;
        }
    }
}

#[derive(Clone, Copy)]
enum BlockKind {
    Paren,
    CurlyBracket,
    SquareBracket,
}

impl From<BlockKind> for char {
    fn from(bk: BlockKind) -> char {
        match bk {
            BlockKind::Paren => ')',
            BlockKind::SquareBracket => ']',
            BlockKind::CurlyBracket => '}',
        }
    }
}

fn bare(src: &mut Input, span_offset: usize) -> ParseResult<Spanned<String>> {
    skip_whitespace(src);

    let mut bare = String::new();
    let start_offset = if let Some((pos, _)) = src.peek() {
        *pos
    } else {
        0
    };

    let mut inside_quote: Option<char> = None;
    let mut block_level: Vec<BlockKind> = vec![];

    while let Some((_, c)) = src.peek() {
        let c = *c;
        if inside_quote.is_some() {
            if Some(c) == inside_quote {
                inside_quote = None;
            }
        } else if c == '\'' || c == '"' || c == '`' {
            inside_quote = Some(c);
        } else if c == '[' {
            block_level.push(BlockKind::SquareBracket);
        } else if c == ']' {
            if let Some(BlockKind::SquareBracket) = block_level.last() {
                let _ = block_level.pop();
            }
        } else if c == '{' {
            block_level.push(BlockKind::CurlyBracket);
        } else if c == '}' {
            if let Some(BlockKind::CurlyBracket) = block_level.last() {
                let _ = block_level.pop();
            }
        } else if c == '(' {
            block_level.push(BlockKind::Paren);
        } else if c == ')' {
            if let Some(BlockKind::Paren) = block_level.last() {
                let _ = block_level.pop();
            }
        } else if block_level.is_empty() && (c.is_whitespace() || c == '|' || c == ';') {
            break;
        }
        bare.push(c);
        let _ = src.next();
    }

    let span = Span::new(
        start_offset + span_offset,
        start_offset + span_offset + bare.len(),
    );

    if let Some(block) = block_level.last() {
        let delim: char = (*block).into();
        let cause = nu_errors::ParseError::unexpected_eof(delim.to_string(), span);

        while let Some(bk) = block_level.pop() {
            bare.push(bk.into());
        }

        return Err(ParseError {
            cause,
            partial: Some(bare.spanned(span)),
        });
    }

    if let Some(delimiter) = inside_quote {
        // The non-lite parse trims quotes on both sides, so we add the expected quote so that
        // anyone wanting to consume this partial parse (e.g., completions) will be able to get
        // correct information from the non-lite parse.
        bare.push(delimiter);

        return Err(ParseError {
            cause: nu_errors::ParseError::unexpected_eof(delimiter.to_string(), span),
            partial: Some(bare.spanned(span)),
        });
    }

    if bare.is_empty() {
        return Err(ParseError {
            cause: nu_errors::ParseError::unexpected_eof("command", span),
            partial: Some(bare.spanned(span)),
        });
    }

    Ok(bare.spanned(span))
}

fn command(src: &mut Input, span_offset: usize) -> ParseResult<LiteCommand> {
    let mut cmd = match bare(src, span_offset) {
        Ok(v) => LiteCommand::new(v),
        Err(e) => {
            return Err(ParseError {
                cause: e.cause,
                partial: e.partial.map(LiteCommand::new),
            });
        }
    };

    loop {
        skip_whitespace(src);

        if let Some((_, c)) = src.peek() {
            // The first character tells us a lot about each argument
            match c {
                ';' => {
                    // this is the end of the command and the end of the pipeline
                    break;
                }
                '|' => {
                    let _ = src.next();
                    if let Some((pos, next_c)) = src.peek() {
                        if *next_c == '|' {
                            // this isn't actually a pipeline but a comparison
                            let span = Span::new(pos - 1 + span_offset, pos + 1 + span_offset);
                            cmd.args.push("||".to_string().spanned(span));
                            let _ = src.next();
                        } else {
                            // this is the end of this command
                            break;
                        }
                    } else {
                        // this is the end of this command
                        break;
                    }
                }
                _ => {
                    // basic argument
                    match bare(src, span_offset) {
                        Ok(v) => {
                            cmd.args.push(v);
                        }

                        Err(e) => {
                            if let Some(v) = e.partial {
                                cmd.args.push(v);
                            }

                            return Err(ParseError {
                                cause: e.cause,
                                partial: Some(cmd),
                            });
                        }
                    }
                }
            }
        } else {
            break;
        }
    }

    Ok(cmd)
}

fn pipeline(src: &mut Input, span_offset: usize) -> ParseResult<LiteBlock> {
    let mut block = vec![];
    let mut commands = vec![];

    skip_whitespace(src);

    while src.peek().is_some() {
        // If there is content there, let's parse it
        let cmd = match command(src, span_offset) {
            Ok(v) => v,
            Err(e) => {
                if let Some(partial) = e.partial {
                    commands.push(partial);
                    block.push(LitePipeline { commands });
                }

                return Err(ParseError {
                    cause: e.cause,
                    partial: Some(LiteBlock { block }),
                });
            }
        };

        commands.push(cmd);
        skip_whitespace(src);

        if let Some((_, ';')) = src.peek() {
            let _ = src.next();

            if !commands.is_empty() {
                block.push(LitePipeline { commands });
                commands = vec![];
            }
        }
    }

    if !commands.is_empty() {
        block.push(LitePipeline { commands });
    }

    Ok(LiteBlock { block })
}

pub fn lite_parse(src: &str, span_offset: usize) -> ParseResult<LiteBlock> {
    pipeline(&mut src.char_indices().peekable(), span_offset)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn span(left: usize, right: usize) -> Span {
        Span::new(left, right)
    }

    mod bare {
        use super::*;

        #[test]
        fn simple_1() {
            let input = "foo bar baz";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0).unwrap();

            assert_eq!(result.span, span(0, 3));
        }

        #[test]
        fn simple_2() {
            let input = "'foo bar' baz";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0).unwrap();

            assert_eq!(result.span, span(0, 9));
        }

        #[test]
        fn simple_3() {
            let input = "'foo\" bar' baz";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0).unwrap();

            assert_eq!(result.span, span(0, 10));
        }

        #[test]
        fn simple_4() {
            let input = "[foo bar] baz";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0).unwrap();

            assert_eq!(result.span, span(0, 9));
        }

        #[test]
        fn simple_5() {
            let input = "'foo 'bar baz";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0).unwrap();

            assert_eq!(result.span, span(0, 9));
        }

        #[test]
        fn simple_6() {
            let input = "''foo baz";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0).unwrap();

            assert_eq!(result.span, span(0, 5));
        }

        #[test]
        fn simple_7() {
            let input = "'' foo";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0).unwrap();

            assert_eq!(result.span, span(0, 2));
        }

        #[test]
        fn simple_8() {
            let input = " '' foo";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0).unwrap();

            assert_eq!(result.span, span(1, 3));
        }

        #[test]
        fn simple_9() {
            let input = " 'foo' foo";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0).unwrap();

            assert_eq!(result.span, span(1, 6));
        }

        #[test]
        fn simple_10() {
            let input = "[foo, bar]";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0).unwrap();

            assert_eq!(result.span, span(0, 10));
        }

        #[test]
        fn ignore_future() {
            let input = "foo 'bar";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0).unwrap();

            assert_eq!(result.span, span(0, 3));
        }

        #[test]
        fn invalid_1() {
            let input = "'foo bar";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0);

            assert_eq!(result.is_ok(), false);
        }

        #[test]
        fn invalid_2() {
            let input = "'bar";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0);

            assert_eq!(result.is_ok(), false);
        }

        #[test]
        fn invalid_4() {
            let input = " 'bar";

            let input = &mut input.char_indices().peekable();
            let result = bare(input, 0);

            assert_eq!(result.is_ok(), false);
        }
    }

    mod lite_parse {
        use super::*;

        #[test]
        fn pipeline() {
            let result = lite_parse("cmd1 | cmd2 ; deploy", 0).unwrap();
            assert_eq!(result.span(), span(0, 20));
            assert_eq!(result.block[0].span(), span(0, 11));
            assert_eq!(result.block[1].span(), span(14, 20));
        }

        #[test]
        fn simple_1() {
            let result = lite_parse("foo", 0).unwrap();
            assert_eq!(result.block.len(), 1);
            assert_eq!(result.block[0].commands.len(), 1);
            assert_eq!(result.block[0].commands[0].name.span, span(0, 3));
        }

        #[test]
        fn simple_offset() {
            let result = lite_parse("foo", 10).unwrap();
            assert_eq!(result.block.len(), 1);
            assert_eq!(result.block[0].commands.len(), 1);
            assert_eq!(result.block[0].commands[0].name.span, span(10, 13));
        }

        #[test]
        fn incomplete_result() {
            let result = lite_parse("my_command \"foo' --test", 10).unwrap_err();
            assert!(matches!(result.cause.reason(), nu_errors::ParseErrorReason::Eof { .. }));

            let result = result.partial.unwrap();
            assert_eq!(result.block.len(), 1);
            assert_eq!(result.block[0].commands.len(), 1);
            assert_eq!(result.block[0].commands[0].name.item, "my_command");
            assert_eq!(result.block[0].commands[0].args.len(), 1);
            assert_eq!(result.block[0].commands[0].args[0].item, "\"foo' --test\"");
        }
    }
}