shelly-shell 0.2.0

A Rust based Unix style shell with a typed and structured language syntax.
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
501
502
503
504
505
506
507

use crate::language::{ ast::{ * },
                       data::value::Value,
                       tokenizer::{ TokenBuffer, TokenKind, TokenLiteral, TokenValue, StringFlag },
                       parser::{ base_utils::{ expect_token,
                                               match_one_of,
                                               Lookahead,
                                               try_expect_token,
                                               try_expect_one_of_tokens },
                       results::{ ParseResult, ParserError, ParserErrorKind } } };



fn math_operator(kind: TokenKind) -> Option<(AstMathOperator, u8)>
{
    match kind
    {
        TokenKind::Plus     => Some((AstMathOperator::Add,      1)),
        TokenKind::Minus    => Some((AstMathOperator::Subtract, 1)),
        TokenKind::Asterisk => Some((AstMathOperator::Multiply, 2)),
        TokenKind::Slash    => Some((AstMathOperator::Divide,   2)),
        TokenKind::Percent  => Some((AstMathOperator::Modulo,   2)),
        _                   => None
    }
}


/**
 * Parse one arithmetic operand. A mismatch consumes nothing. Parentheses accept
 * any value expression, including a command whose result is used as the operand.
 */
fn parse_math_primary(buffer: &mut TokenBuffer<'_, '_>) -> ParseResult<Option<AstExpression>>
{
    let mut lookahead = Lookahead::new(buffer);
    let Some(token) = lookahead.buffer.next()? else { return Ok(None); };

    let expression = match token.kind
        {
            TokenKind::Literal =>
                {
                    let mut string_flag = None;

                    let value = match token.value
                        {
                            TokenValue::Literal(TokenLiteral::Integer(value, _)) =>
                                {
                                    Value::Integer(value)
                                },

                            TokenValue::Literal(TokenLiteral::Float(value, text)) =>
                                {
                                    Value::Float(value, Some(text))
                                },

                            TokenValue::Literal(TokenLiteral::Boolean(value)) => Value::Boolean(value),

                            TokenValue::Literal(TokenLiteral::String(value, flag, escaped_dollars)) =>
                                {
                                    string_flag = Some(match flag
                                        {
                                            StringFlag::Interpolated =>
                                                AstStringFlag::Interpolated(escaped_dollars),

                                            StringFlag::NonInterpolated =>
                                                AstStringFlag::NonInterpolated,
                                        });

                                    Value::from_string(value)
                                },

                            _ => return Ok(None)
                        };

                    new_ast_literal(token.location, value, string_flag)
                },

            TokenKind::Minus =>
                {
                    // A standalone '-' remains a command argument. With an operand it is unary
                    // negation, expressed as subtraction so normal checked arithmetic applies.
                    let Some(operand) = parse_math_primary(&mut *lookahead.buffer)? else
                    {
                        return Ok(None);
                    };
                    AstExpression
                        {
                            location: token.location.clone(),
                            kind: AstExpressionKind::MathExpression(AstMathOperator::Subtract,
                                Box::new(new_ast_literal(token.location, Value::Integer(0), None)),
                                Box::new(operand)),
                            string_flag: None
                        }
                },

            TokenKind::Identifier =>
                {
                    let TokenValue::Identifier(name) = token.value else { return Ok(None); };

                    // Argument expansion is not a scalar arithmetic operand.
                    if try_expect_token(&mut *lookahead.buffer, TokenKind::Splat)?.is_some()
                    {
                        return Ok(None);
                    }

                    new_ast_variable(token.location, name)
                },

            TokenKind::ParenOpen =>
                {
                    let expression = parse_value_expression(&mut *lookahead.buffer)?
                        .ok_or_else(|| ParserError
                            {
                                location: Some(token.location.clone()),
                                kind: ParserErrorKind::ExpectedExpression
                            })?;
                    expect_token(&mut *lookahead.buffer, TokenKind::ParenClose)?;
                    AstExpression
                        {
                            location: token.location,
                            kind: AstExpressionKind::Grouped(Box::new(expression)),
                            string_flag: None
                        }
                },

            _ => return Ok(None)
        };

    lookahead.commit();
    Ok(Some(expression))
}


/**
 * Extend a left operand using precedence climbing. The higher right-hand
 * threshold makes equal-precedence operators left-associative.
 */
fn parse_math_binary_tail(buffer: &mut TokenBuffer<'_, '_>,
                          mut left: AstExpression,
                          min_precedence: u8,
                          allow_glob_fallback: bool) -> ParseResult<AstExpression>
{
    let mut matched_operator = false;

    loop
    {
        // Keep the operator speculative until its right operand has parsed.
        // EOF, statement terminators, closing parentheses, **, and other
        // non-operators remain unread for the enclosing parser.
        let mut lookahead = Lookahead::new(buffer);
        let Some(token) = lookahead.buffer.next()? else { break; };

        if matches!(token.kind, TokenKind::LineBreak | TokenKind::StatementBreak | TokenKind::ParenClose)
        {
            break;
        }

        let Some((operator, precedence)) = math_operator(token.kind) else { break; };

        if precedence < min_precedence
        {
            break;
        }

        let right = match parse_math_primary(&mut *lookahead.buffer)?
            {
                Some(right) => right,
                None =>
                    {
                        if    allow_glob_fallback
                           && !matched_operator
                           && matches!(operator, AstMathOperator::Multiply)
                        {
                            break;
                        }

                        return Err(ParserError
                            {
                                location: Some(token.location),
                                kind: ParserErrorKind::ExpectedExpression
                            });
                    }
            };

        let right = parse_math_binary_tail(&mut *lookahead.buffer, right, precedence + 1, false)?;

        left = AstExpression
            {
                location: left.location.clone(),
                kind: AstExpressionKind::MathExpression(operator, Box::new(left), Box::new(right)),
                string_flag: None
            };

        lookahead.commit();
        matched_operator = true;
    }

    Ok(left)
}


fn parse_math_expression(buffer: &mut TokenBuffer<'_, '_>) -> ParseResult<Option<AstExpression>>
{
    let mut lookahead = Lookahead::new(buffer);
    let starts_with_group =
        {
            let peek = Lookahead::new(&mut *lookahead.buffer);
            matches!(peek.buffer.next()?, Some(token) if token.kind == TokenKind::ParenOpen)
        };

    let Some(left) = parse_math_primary(&mut *lookahead.buffer)? else { return Ok(None); };
    let expression = parse_math_binary_tail(&mut *lookahead.buffer, left, 0, !starts_with_group)?;

    // A lone number/variable belongs to the existing expression rules. Explicit
    // parentheses also accept a single operand, e.g. (42) or ($count).
    if starts_with_group || matches!(&expression.kind, AstExpressionKind::MathExpression(_, _, _))
    {
        lookahead.commit();
        return Ok(Some(expression));
    }

    Ok(None)
}


fn parse_variable_expression(buffer: &mut TokenBuffer<'_, '_>) -> ParseResult<Option<AstExpression>>
{
    let identifier = expect_token(buffer, TokenKind::Identifier)?;

    let identifier_value = match identifier.value
        {
            TokenValue::Identifier(identifier) => identifier,

            _ => return Err(ParserError
                {
                    location: Some(identifier.location.clone()),
                    kind: ParserErrorKind::ExpectedExpression,
                })
        };

    // Check for the splat operator, `...`, which may follow the variable.
    let splat_operator = try_expect_token(buffer, TokenKind::Splat)?;

    if let Some(_) = splat_operator
    {
        Ok(Some(new_ast_variable_splat(identifier.location.clone(), identifier_value)))
    }
    else
    {
        Ok(Some(new_ast_variable(identifier.location.clone(), identifier_value)))
    }
}


fn parse_symbol_expression(buffer: &mut TokenBuffer<'_, '_>) -> ParseResult<Option<AstExpression>>
{
    let symbol = expect_token(buffer, TokenKind::Symbol)?;

    let symbol_value = match symbol.value
        {
            TokenValue::Symbol(symbol) => symbol,

            _ => return Err(ParserError
                {
                    location: Some(symbol.location.clone()),
                    kind: ParserErrorKind::ExpectedExpression,
                })
        };

    // Does the symbol have any $s in it?
    let string_flag = if symbol_value.contains('$')
        {
            Some(AstStringFlag::Interpolated(Vec::new()))
        }
        else
        {
            Some(AstStringFlag::NonInterpolated)
        };

    Ok(Some(new_ast_symbol(symbol.location.clone(), symbol_value, string_flag)))
}


fn parse_literal_expression(buffer: &mut TokenBuffer<'_, '_>) -> ParseResult<Option<AstExpression>>
{
    let literal = expect_token(buffer, TokenKind::Literal)?;

    let literal_value = match literal.value
        {
            TokenValue::Literal(literal) => literal,

            _ => return Err(ParserError
                {
                    location: Some(literal.location.clone()),
                    kind: ParserErrorKind::ExpectedExpression,
                })
        };

    let mut string_flag = None;

    let literal_value = match literal_value
        {
            TokenLiteral::Integer(value, _) => Value::Integer(value),
            TokenLiteral::Float(value, text) => Value::Float(value, Some(text)),
            TokenLiteral::Boolean(value) => Value::Boolean(value),
            TokenLiteral::String(value, flag, escaped_dollars) =>
                {
                    string_flag = match flag
                        {
                            StringFlag::Interpolated => Some(AstStringFlag::Interpolated(escaped_dollars)),
                            StringFlag::NonInterpolated => Some(AstStringFlag::NonInterpolated),
                        };
                    Value::from_string(value)
                }
        };

    Ok(Some(new_ast_literal(literal.location.clone(), literal_value, string_flag)))
}


fn parse_lonely_glob_expression(buffer: &mut TokenBuffer<'_, '_>) -> ParseResult<Option<AstExpression>>
{
    let glob_operator = try_expect_token(buffer, TokenKind::Glob)?;

    if let Some(glob_operator) = glob_operator
    {
        let location = glob_operator.location.clone();
        return Ok(Some(new_ast_symbol(location, "*".to_string(), None)));
    }

    let star = expect_token(buffer, TokenKind::Asterisk)?;

    Ok(Some(new_ast_symbol(star.location.clone(), "*".to_string(), None)))
}


fn parse_operator_to_symbol(buffer: &mut TokenBuffer<'_, '_>) -> ParseResult<Option<AstExpression>>
{
    let allowed_operators = [ TokenKind::Let,
                              TokenKind::Export,
                              TokenKind::Alias,
                              TokenKind::Sub,
                              TokenKind::If,
                              TokenKind::Else,
                              TokenKind::While,
                              TokenKind::Loop,
                              TokenKind::Match,
                              TokenKind::Return,
                              TokenKind::Function,
                              TokenKind::Struct,
                              TokenKind::Enum,
                              TokenKind::Import,
                              TokenKind::TypeDelimiter,
                              TokenKind::Assign,
                              TokenKind::Minus,
                              TokenKind::Plus,
                              TokenKind::Slash,
                              TokenKind::Percent,
                              TokenKind::Scope,
                              TokenKind::ErrorSource ];

    // Attempt to match one of the allowed operators
    if let Some(operator) = try_expect_one_of_tokens(buffer, &allowed_operators)?
    {
        let location = operator.location.clone();

        return Ok(Some(new_ast_symbol(location, operator.token_value_text(), None)));
    }

    Ok(None)
}


pub fn parse_expression(buffer: &mut TokenBuffer<'_, '_>) -> ParseResult<Option<AstExpression>>
{
    // An executable reference currently stores only its name. Use a literal so that value
    // parsing does not turn the name into a call or apply symbol expansion.
    if let Some(escape) = try_expect_token(buffer, TokenKind::ExecEscape)?
    {
        let executable = parse_exec_expression(buffer)?.ok_or_else(|| ParserError
            {
                location: Some(escape.location.clone()),
                kind: ParserErrorKind::ExpectedExpression
            })?;

        if matches!(executable.kind, AstExpressionKind::Variable(_))
        {
            return Ok(Some(AstExpression
                {
                    location: escape.location,
                    kind: AstExpressionKind::ExecutableReference(Box::new(executable)),
                    string_flag: None
                }));
        }

        return Ok(Some(new_ast_literal(escape.location,
                                       Value::from_executable_string(executable.resolve_as_text()?),
                                       None)));
    }

    // The math rule itself decides when to fall back. Once it reports malformed
    // arithmetic, do not retry it as separate literal/variable/glob arguments.
    if let Some(expression) = parse_math_expression(buffer)?
    {
        return Ok(Some(expression));
    }

    match_one_of(buffer, &[parse_variable_expression,
                           parse_symbol_expression,
                           parse_literal_expression,
                           parse_lonely_glob_expression,
                           parse_operator_to_symbol])
}


/**
 * Parse a value with the command-call rules shared by assignments, returns, and parentheses.
 */
pub fn parse_value_expression(buffer: &mut TokenBuffer<'_, '_>) -> ParseResult<Option<AstExpression>>
{
    let Some(expression) = parse_expression(buffer)? else { return Ok(None); };

    if matches!(&expression.kind, AstExpressionKind::Symbol(_) | AstExpressionKind::Variable(_))
    {
        let arguments = parse_command_arguments(buffer)?;
        let location = expression.location.clone();

        if !arguments.is_empty()
        {
            return Ok(Some(AstExpression
                {
                    location: location.clone(),
                    kind: AstExpressionKind::Execute(Box::new(AstExecuteStatement
                        {
                            location,
                            executable_name: expression.resolve_as_text()?,
                            expand_path: matches!(&expression.kind,
                                AstExpressionKind::Symbol(symbol) if symbol.name.starts_with('~')),
                            arguments
                        })),
                    string_flag: None
                }));
        }

        // Globs remain collection values. Only a lone command word is ambiguous.
        if matches!(&expression.kind, AstExpressionKind::Symbol(symbol) if !symbol.is_glob())
        {
            return Ok(Some(AstExpression
                {
                    location,
                    kind: AstExpressionKind::TryExecute(Box::new(expression)),
                    string_flag: None
                }));
        }
    }

    Ok(Some(expression))
}


pub fn parse_command_arguments(buffer: &mut TokenBuffer<'_, '_>) -> ParseResult<Vec<AstExpression>>
{
    let mut parameter_expressions = Vec::new();

    loop
    {
        let next =
            {
                let mut lookahead = Lookahead::new(buffer);
                let Some(token) = lookahead.buffer.next()? else { break; };

                match token.kind
                {
                    TokenKind::LineBreak | TokenKind::StatementBreak | TokenKind::BlockClose
                    | TokenKind::ParenClose => break,

                    TokenKind::LineContinue =>
                        {
                            expect_token(lookahead.buffer, TokenKind::LineBreak)?;
                            lookahead.commit();
                            continue;
                        },

                    _ => token
                }
            };

        // The lookahead rewound this token so the full expression can consume it.
        let expression = parse_expression(buffer)?.ok_or_else(|| ParserError
            {
                location: Some(next.location),
                kind: ParserErrorKind::ExpectedExpression
            })?;

        parameter_expressions.push(expression);
    }

    Ok(parameter_expressions)
}


pub fn parse_exec_expression(buffer: &mut TokenBuffer<'_, '_>) -> ParseResult<Option<AstExpression>>
{
    match_one_of(buffer, &[parse_variable_expression,
                           parse_symbol_expression,
                           parse_literal_expression,
                           parse_operator_to_symbol])
}