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
//! Lite parsing converts a flat stream of tokens from the lexer to a syntax element structure that
//! can be parsed.

use crate::{Token, TokenContents};
use nu_protocol::{ast::RedirectionSource, ParseError, Span};
use std::mem;

#[derive(Debug, Clone, Copy)]
pub enum LiteRedirectionTarget {
    File {
        connector: Span,
        file: Span,
        append: bool,
    },
    Pipe {
        connector: Span,
    },
}

impl LiteRedirectionTarget {
    pub fn connector(&self) -> Span {
        match self {
            LiteRedirectionTarget::File { connector, .. }
            | LiteRedirectionTarget::Pipe { connector } => *connector,
        }
    }
}

#[derive(Debug, Clone)]
pub enum LiteRedirection {
    Single {
        source: RedirectionSource,
        target: LiteRedirectionTarget,
    },
    Separate {
        out: LiteRedirectionTarget,
        err: LiteRedirectionTarget,
    },
}

#[derive(Debug, Clone, Default)]
pub struct LiteCommand {
    pub pipe: Option<Span>,
    pub comments: Vec<Span>,
    pub parts: Vec<Span>,
    pub redirection: Option<LiteRedirection>,
}

impl LiteCommand {
    fn push(&mut self, span: Span) {
        self.parts.push(span);
    }

    fn try_add_redirection(
        &mut self,
        source: RedirectionSource,
        target: LiteRedirectionTarget,
    ) -> Result<(), ParseError> {
        let redirection = match (self.redirection.take(), source) {
            (None, source) => Ok(LiteRedirection::Single { source, target }),
            (
                Some(LiteRedirection::Single {
                    source: RedirectionSource::Stdout,
                    target: out,
                }),
                RedirectionSource::Stderr,
            ) => Ok(LiteRedirection::Separate { out, err: target }),
            (
                Some(LiteRedirection::Single {
                    source: RedirectionSource::Stderr,
                    target: err,
                }),
                RedirectionSource::Stdout,
            ) => Ok(LiteRedirection::Separate { out: target, err }),
            (
                Some(LiteRedirection::Single {
                    source,
                    target: first,
                }),
                _,
            ) => Err(ParseError::MultipleRedirections(
                source,
                first.connector(),
                target.connector(),
            )),
            (
                Some(LiteRedirection::Separate { out, .. }),
                RedirectionSource::Stdout | RedirectionSource::StdoutAndStderr,
            ) => Err(ParseError::MultipleRedirections(
                RedirectionSource::Stdout,
                out.connector(),
                target.connector(),
            )),
            (Some(LiteRedirection::Separate { err, .. }), RedirectionSource::Stderr) => {
                Err(ParseError::MultipleRedirections(
                    RedirectionSource::Stderr,
                    err.connector(),
                    target.connector(),
                ))
            }
        }?;

        self.redirection = Some(redirection);

        Ok(())
    }
}

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

impl LitePipeline {
    fn push(&mut self, element: &mut LiteCommand) {
        if !element.parts.is_empty() || element.redirection.is_some() {
            self.commands.push(mem::take(element));
        }
    }
}

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

impl LiteBlock {
    fn push(&mut self, pipeline: &mut LitePipeline) {
        if !pipeline.commands.is_empty() {
            self.block.push(mem::take(pipeline));
        }
    }
}

fn last_non_comment_token(tokens: &[Token], cur_idx: usize) -> Option<TokenContents> {
    let mut expect = TokenContents::Comment;
    for token in tokens.iter().take(cur_idx).rev() {
        // skip ([Comment]+ [Eol]) pair
        match (token.contents, expect) {
            (TokenContents::Comment, TokenContents::Comment)
            | (TokenContents::Comment, TokenContents::Eol) => expect = TokenContents::Eol,
            (TokenContents::Eol, TokenContents::Eol) => expect = TokenContents::Comment,
            (token, _) => return Some(token),
        }
    }
    None
}

pub fn lite_parse(tokens: &[Token]) -> (LiteBlock, Option<ParseError>) {
    if tokens.is_empty() {
        return (LiteBlock::default(), None);
    }

    let mut block = LiteBlock::default();
    let mut pipeline = LitePipeline::default();
    let mut command = LiteCommand::default();

    let mut last_token = TokenContents::Eol;
    let mut file_redirection = None;
    let mut curr_comment: Option<Vec<Span>> = None;
    let mut error = None;

    for (idx, token) in tokens.iter().enumerate() {
        if let Some((source, append, span)) = file_redirection.take() {
            if command.parts.is_empty() {
                error = error.or(Some(ParseError::LabeledError(
                    "Redirection without command or expression".into(),
                    "there is nothing to redirect".into(),
                    span,
                )));

                command.push(span);

                match token.contents {
                    TokenContents::Comment => {
                        command.comments.push(token.span);
                        curr_comment = None;
                    }
                    TokenContents::Pipe
                    | TokenContents::ErrGreaterPipe
                    | TokenContents::OutErrGreaterPipe => {
                        pipeline.push(&mut command);
                        command.pipe = Some(token.span);
                    }
                    TokenContents::Semicolon => {
                        pipeline.push(&mut command);
                        block.push(&mut pipeline);
                    }
                    TokenContents::Eol => {
                        pipeline.push(&mut command);
                    }
                    _ => command.push(token.span),
                }
            } else {
                match &token.contents {
                    TokenContents::PipePipe => {
                        error = error.or(Some(ParseError::ShellOrOr(token.span)));
                        command.push(span);
                        command.push(token.span);
                    }
                    TokenContents::Item => {
                        let target = LiteRedirectionTarget::File {
                            connector: span,
                            file: token.span,
                            append,
                        };
                        if let Err(err) = command.try_add_redirection(source, target) {
                            error = error.or(Some(err));
                            command.push(span);
                            command.push(token.span)
                        }
                    }
                    TokenContents::OutGreaterThan
                    | TokenContents::OutGreaterGreaterThan
                    | TokenContents::ErrGreaterThan
                    | TokenContents::ErrGreaterGreaterThan
                    | TokenContents::OutErrGreaterThan
                    | TokenContents::OutErrGreaterGreaterThan => {
                        error =
                            error.or(Some(ParseError::Expected("redirection target", token.span)));
                        command.push(span);
                        command.push(token.span);
                    }
                    TokenContents::Pipe
                    | TokenContents::ErrGreaterPipe
                    | TokenContents::OutErrGreaterPipe => {
                        error =
                            error.or(Some(ParseError::Expected("redirection target", token.span)));
                        command.push(span);
                        pipeline.push(&mut command);
                        command.pipe = Some(token.span);
                    }
                    TokenContents::Eol => {
                        error =
                            error.or(Some(ParseError::Expected("redirection target", token.span)));
                        command.push(span);
                        pipeline.push(&mut command);
                    }
                    TokenContents::Semicolon => {
                        error =
                            error.or(Some(ParseError::Expected("redirection target", token.span)));
                        command.push(span);
                        pipeline.push(&mut command);
                        block.push(&mut pipeline);
                    }
                    TokenContents::Comment => {
                        error = error.or(Some(ParseError::Expected("redirection target", span)));
                        command.push(span);
                        command.comments.push(token.span);
                        curr_comment = None;
                    }
                }
            }
        } else {
            match &token.contents {
                TokenContents::PipePipe => {
                    error = error.or(Some(ParseError::ShellOrOr(token.span)));
                    command.push(token.span);
                }
                TokenContents::Item => {
                    // This is commented out to preserve old parser behavior,
                    // but we should probably error here.
                    //
                    // if element.redirection.is_some() {
                    //     error = error.or(Some(ParseError::LabeledError(
                    //         "Unexpected positional".into(),
                    //         "cannot add positional arguments after output redirection".into(),
                    //         token.span,
                    //     )));
                    // }
                    //
                    // For example, this is currently allowed: ^echo thing o> out.txt extra_arg

                    // If we have a comment, go ahead and attach it
                    if let Some(curr_comment) = curr_comment.take() {
                        command.comments = curr_comment;
                    }
                    command.push(token.span);
                }
                TokenContents::OutGreaterThan => {
                    file_redirection = Some((RedirectionSource::Stdout, false, token.span));
                }
                TokenContents::OutGreaterGreaterThan => {
                    file_redirection = Some((RedirectionSource::Stdout, true, token.span));
                }
                TokenContents::ErrGreaterThan => {
                    file_redirection = Some((RedirectionSource::Stderr, false, token.span));
                }
                TokenContents::ErrGreaterGreaterThan => {
                    file_redirection = Some((RedirectionSource::Stderr, true, token.span));
                }
                TokenContents::OutErrGreaterThan => {
                    file_redirection =
                        Some((RedirectionSource::StdoutAndStderr, false, token.span));
                }
                TokenContents::OutErrGreaterGreaterThan => {
                    file_redirection = Some((RedirectionSource::StdoutAndStderr, true, token.span));
                }
                TokenContents::ErrGreaterPipe => {
                    let target = LiteRedirectionTarget::Pipe {
                        connector: token.span,
                    };
                    if let Err(err) = command.try_add_redirection(RedirectionSource::Stderr, target)
                    {
                        error = error.or(Some(err));
                    }
                    pipeline.push(&mut command);
                    command.pipe = Some(token.span);
                }
                TokenContents::OutErrGreaterPipe => {
                    let target = LiteRedirectionTarget::Pipe {
                        connector: token.span,
                    };
                    if let Err(err) =
                        command.try_add_redirection(RedirectionSource::StdoutAndStderr, target)
                    {
                        error = error.or(Some(err));
                    }
                    pipeline.push(&mut command);
                    command.pipe = Some(token.span);
                }
                TokenContents::Pipe => {
                    pipeline.push(&mut command);
                    command.pipe = Some(token.span);
                }
                TokenContents::Eol => {
                    // Handle `[Command] [Pipe] ([Comment] | [Eol])+ [Command]`
                    //
                    // `[Eol]` branch checks if previous token is `[Pipe]` to construct pipeline
                    // and so `[Comment] | [Eol]` should be ignore to make it work
                    let actual_token = last_non_comment_token(tokens, idx);
                    if actual_token != Some(TokenContents::Pipe) {
                        pipeline.push(&mut command);
                        block.push(&mut pipeline);
                    }

                    if last_token == TokenContents::Eol {
                        // Clear out the comment as we're entering a new comment
                        curr_comment = None;
                    }
                }
                TokenContents::Semicolon => {
                    pipeline.push(&mut command);
                    block.push(&mut pipeline);
                }
                TokenContents::Comment => {
                    // Comment is beside something
                    if last_token != TokenContents::Eol {
                        command.comments.push(token.span);
                        curr_comment = None;
                    } else {
                        // Comment precedes something
                        if let Some(curr_comment) = &mut curr_comment {
                            curr_comment.push(token.span);
                        } else {
                            curr_comment = Some(vec![token.span]);
                        }
                    }
                }
            }
        }

        last_token = token.contents;
    }

    if let Some((_, _, span)) = file_redirection {
        command.push(span);
        error = error.or(Some(ParseError::Expected("redirection target", span)));
    }

    pipeline.push(&mut command);
    block.push(&mut pipeline);

    if last_non_comment_token(tokens, tokens.len()) == Some(TokenContents::Pipe) {
        (
            block,
            Some(ParseError::UnexpectedEof(
                "pipeline missing end".into(),
                tokens[tokens.len() - 1].span,
            )),
        )
    } else {
        (block, error)
    }
}