tishlang_parser 2.36.0

Tish recursive descent parser
Documentation
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
//! Tish recursive descent parser.

mod parser;

use parser::Parser;

use tishlang_ast::Program;
use tishlang_lexer::Lexer;
pub use tishlang_lexer::LexerOptions;

/// Parse `source`, reading lexer options from the environment (e.g. `TISH_IGNORE_INDENT=1`
/// to ignore indentation syntax). Every backend funnels through here, so the env toggle
/// reaches run/build/dump-ast/fmt/lint/lsp uniformly.
pub fn parse(source: &str) -> Result<Program, String> {
    parse_with_options(source, LexerOptions::from_env())
}

/// Parse with explicit lexer options, bypassing the environment.
///
/// With `LexerOptions { ignore_indent: true }`, indentation is treated as ordinary
/// whitespace and blocks must be brace-delimited — useful for debugging how nested
/// blocks transpile, since fully brace-delimited code parses identically either way.
pub fn parse_with_options(source: &str, options: LexerOptions) -> Result<Program, String> {
    let lexer = Lexer::with_options(source, options);
    let tokens: Result<Vec<_>, _> = lexer.collect();
    let tokens = tokens?;
    let mut parser = Parser::new(&tokens);
    parser.parse_program()
}

#[cfg(test)]
mod tests {
    use super::*;
    use tishlang_ast::{CallArg, Expr, ObjectProp, Statement};

    /// #381: pathologically nested source must return a catchable `Err`, NOT overflow the native
    /// stack and abort the process. Exercises expression nesting (`((((…`), array nesting (`[[[[…`),
    /// and prefix-op nesting (`-!-!…`) — all funnel through the depth-guarded `parse_unary`.
    #[test]
    fn deeply_nested_source_errors_instead_of_aborting() {
        let n = 50_000;
        for (open, close) in [("(", ")"), ("[", "]")] {
            let src = format!("let x = {}1{}", open.repeat(n), close.repeat(n));
            let r = parse(&src);
            assert!(r.is_err(), "{n}× nested {open} should be a catchable Err, not an abort");
            assert!(
                r.unwrap_err().contains("nesting too deep"),
                "expected a depth-limit error"
            );
        }
        let prefix = format!("let x = {}1", "-".repeat(n));
        assert!(parse(&prefix).is_err(), "deep prefix-op chain should error, not abort");
    }

    /// The depth guard must not reject ordinary, modestly-nested code.
    #[test]
    fn normal_nesting_still_parses() {
        assert!(parse("let x = ((((1 + 2))))").is_ok());
        assert!(parse("let y = [[1, 2], [3, [4, [5]]]]").is_ok());
        assert!(parse("fn f() { if (a) { if (b) { return 1 } } }").is_ok());
    }

    /// #244: loose equality `==` / `!=` is rejected uniformly at parse (across every backend), with an
    /// actionable message pointing at the strict form. `===` / `!==` still parse.
    #[test]
    fn loose_equality_is_rejected_at_parse() {
        let eq = parse("let b = 1 == 1").unwrap_err();
        assert!(eq.contains("'=='") && eq.contains("'==='"), "got: {eq}");
        let ne = parse("let b = 1 != 2").unwrap_err();
        assert!(ne.contains("'!='") && ne.contains("'!=='"), "got: {ne}");
        // Rejected wherever an expression is parsed, not just at top level.
        assert!(parse("fn f(x) { if (x == 0) { return 1 } return 0 }").is_err());
        assert!(parse("let y = a != null && a.b").is_err());
        // Strict forms are unaffected.
        assert!(parse("let b = 1 === 1").is_ok());
        assert!(parse("let b = 1 !== 2").is_ok());
        assert!(parse("let y = a !== null && a.b").is_ok());
    }

    /// #428: `async` in expression position parses as an async arrow (paren and single-param forms);
    /// non-async arrows keep `async_: false`.
    #[test]
    fn async_arrow_expressions_parse() {
        use tishlang_ast::Expr;
        let arrow_async = |src: &str| -> bool {
            let p = parse(src).expect("parse");
            let Some(Statement::VarDecl { init: Some(e), .. }) = p.statements.first() else {
                panic!("expected `let x = <arrow>`");
            };
            match e {
                Expr::ArrowFunction { async_, .. } => *async_,
                other => panic!("expected ArrowFunction, got {other:?}"),
            }
        };
        assert!(arrow_async("let f = async () => 1"), "async paren arrow");
        assert!(arrow_async("let g = async x => x + 1"), "async single-param arrow");
        assert!(arrow_async("let h = async (a, b) => a + b"), "async multi-param arrow");
        assert!(!arrow_async("let f = () => 1"), "plain arrow is not async");
        assert!(!arrow_async("let g = x => x"), "plain single-param arrow is not async");
        // `async` not followed by an arrow in expr position is an error, not a silent identifier.
        assert!(parse("let x = async 5").is_err());
    }

    #[test]
    fn test_async_fn_parse() {
        let program = parse("async fn foo() { }").expect("parse async fn");
        assert_eq!(program.statements.len(), 1);
        if let tishlang_ast::Statement::FunDecl { async_, name, .. } = &program.statements[0] {
            assert!(async_, "expected async function");
            assert_eq!(name.as_ref(), "foo");
        } else {
            panic!("expected FunDecl");
        }
    }

    // #158: a block's span — and the enclosing function's span — must end at the closing `}`, not
    // overrun onto the statement on the following line.
    #[test]
    fn block_and_fn_spans_stop_at_closing_brace() {
        // `}` is on line 3 (1-based); `let after` begins on line 4.
        let program = parse("fn f(a) {\n  let x = a\n}\nlet after = 1\n").expect("parse");
        let Statement::FunDecl { span, body, .. } = &program.statements[0] else {
            panic!("expected FunDecl");
        };
        assert_eq!(span.end.0, 3, "fn span must end on the `}}` line, not the next statement: {span:?}");
        assert_eq!(
            body.span().end.0,
            3,
            "body block span must end on the `}}` line: {:?}",
            body.span()
        );
    }

    #[test]
    fn test_object_literal_shorthand_single() {
        let program = parse("const o = { port }").expect("parse object shorthand");
        assert_eq!(program.statements.len(), 1);
        let stmt = &program.statements[0];
        let init = match stmt {
            Statement::VarDecl {
                init: Some(ref i), ..
            } => i,
            _ => panic!("expected VarDecl with init"),
        };
        let props = match init {
            Expr::Object { ref props, .. } => props,
            _ => panic!("expected Object expr"),
        };
        assert_eq!(props.len(), 1);
        match &props[0] {
            ObjectProp::KeyValue(k, v, _) => {
                assert_eq!(k.as_ref(), "port");
                if let Expr::Ident { ref name, .. } = v {
                    assert_eq!(name.as_ref(), "port");
                } else {
                    panic!("expected Ident value for shorthand");
                }
            }
            _ => panic!("expected KeyValue prop"),
        }
    }

    #[test]
    fn test_object_literal_string_key() {
        let program =
            parse(r#"const o = { "ai-a": 0, human: 1 }"#).expect("parse object with string key");
        assert_eq!(program.statements.len(), 1);
        let stmt = &program.statements[0];
        let init = match stmt {
            Statement::VarDecl {
                init: Some(ref i), ..
            } => i,
            _ => panic!("expected VarDecl with init"),
        };
        let props = match init {
            Expr::Object { ref props, .. } => props,
            _ => panic!("expected Object expr"),
        };
        assert_eq!(props.len(), 2);
        match &props[0] {
            ObjectProp::KeyValue(k, _, _) => assert_eq!(k.as_ref(), "ai-a"),
            _ => panic!("expected KeyValue prop"),
        }
        match &props[1] {
            ObjectProp::KeyValue(k, _, _) => assert_eq!(k.as_ref(), "human"),
            _ => panic!("expected KeyValue prop"),
        }
    }

    #[test]
    fn test_object_literal_shorthand_mixed() {
        let program = parse("const o = { port, x: 1 }").expect("parse mixed object");
        assert_eq!(program.statements.len(), 1);
        let stmt = &program.statements[0];
        let init = match stmt {
            Statement::VarDecl {
                init: Some(ref i), ..
            } => i,
            _ => panic!("expected VarDecl with init"),
        };
        let props = match init {
            Expr::Object { ref props, .. } => props,
            _ => panic!("expected Object expr"),
        };
        assert_eq!(props.len(), 2);
        match &props[0] {
            ObjectProp::KeyValue(k, v, _) => {
                assert_eq!(k.as_ref(), "port");
                if let Expr::Ident { ref name, .. } = v {
                    assert_eq!(name.as_ref(), "port");
                } else {
                    panic!("expected Ident value for shorthand");
                }
            }
            _ => panic!("expected KeyValue prop"),
        }
        match &props[1] {
            ObjectProp::KeyValue(k, v, _) => {
                assert_eq!(k.as_ref(), "x");
                if let Expr::Literal { .. } = v {
                    // x: 1
                } else {
                    panic!("expected Literal for x");
                }
            }
            _ => panic!("expected KeyValue prop"),
        }
    }

    fn unwrap_expr_stmt(program: &tishlang_ast::Program) -> &Expr {
        match program.statements.first() {
            Some(Statement::ExprStmt { expr, .. }) => expr,
            _ => panic!("expected expression statement"),
        }
    }

    #[test]
    fn new_expression_simple_call() {
        let program = parse("new Foo()").expect("parse");
        let e = unwrap_expr_stmt(&program);
        match e {
            Expr::New { callee, args, .. } => {
                assert!(
                    matches!(callee.as_ref(), Expr::Ident { name, .. } if name.as_ref() == "Foo")
                );
                assert!(args.is_empty());
            }
            _ => panic!("expected New, got {:?}", e),
        }
    }

    #[test]
    fn new_expression_with_args() {
        let program = parse("new Uint8Array(16)").expect("parse");
        let e = unwrap_expr_stmt(&program);
        match e {
            Expr::New { callee, args, .. } => {
                assert!(
                    matches!(callee.as_ref(), Expr::Ident { name, .. } if name.as_ref() == "Uint8Array")
                );
                assert_eq!(args.len(), 1);
                assert!(matches!(&args[0], CallArg::Expr(Expr::Literal { .. })));
            }
            _ => panic!("expected New"),
        }
    }

    #[test]
    fn new_expression_member_callee() {
        let program = parse("new ns.AudioContext()").expect("parse");
        let e = unwrap_expr_stmt(&program);
        match e {
            Expr::New { callee, args, .. } => {
                assert!(matches!(
                    callee.as_ref(),
                    Expr::Member { prop: tishlang_ast::MemberProp::Name { name, .. }, .. } if name.as_ref() == "AudioContext"
                ));
                assert!(args.is_empty());
            }
            _ => panic!("expected New"),
        }
    }

    #[test]
    fn new_expression_chained_new() {
        let program = parse("new new Date()").expect("parse");
        let e = unwrap_expr_stmt(&program);
        match e {
            Expr::New { callee, args, .. } => {
                assert!(args.is_empty());
                match callee.as_ref() {
                    Expr::New {
                        callee: inner,
                        args: inner_args,
                        ..
                    } => {
                        assert!(
                            matches!(inner.as_ref(), Expr::Ident { name, .. } if name.as_ref() == "Date")
                        );
                        assert!(inner_args.is_empty());
                    }
                    _ => panic!("expected nested New"),
                }
            }
            _ => panic!("expected New"),
        }
    }

    #[test]
    fn new_then_member_access() {
        let program = parse("new Foo().bar").expect("parse");
        let e = unwrap_expr_stmt(&program);
        match e {
            Expr::Member {
                object,
                prop: tishlang_ast::MemberProp::Name { name, .. },
                ..
            } => {
                assert_eq!(name.as_ref(), "bar");
                match object.as_ref() {
                    Expr::New { callee, args, .. } => {
                        assert!(
                            matches!(callee.as_ref(), Expr::Ident { name, .. } if name.as_ref() == "Foo")
                        );
                        assert!(args.is_empty());
                    }
                    _ => panic!("expected New object"),
                }
            }
            _ => panic!("expected Member"),
        }
    }

    #[test]
    fn new_with_spread_arg() {
        let program = parse("new Foo(...xs)").expect("parse");
        let e = unwrap_expr_stmt(&program);
        match e {
            Expr::New { args, .. } => {
                assert!(
                    matches!(&args[0], CallArg::Spread(Expr::Ident { name, .. }) if name.as_ref() == "xs")
                );
            }
            _ => panic!("expected New"),
        }
    }

    #[test]
    fn stdlib_builtins_d_tish_parses() {
        const SRC: &str = include_str!("../../../stdlib/builtins.d.tish");
        parse(SRC).expect("stdlib/builtins.d.tish should parse");
    }

    #[test]
    fn for_empty_head_parses() {
        let src = r#"fn f() {
  for (;;)
    const x = 1
}"#;
        let program = parse(src).expect("for (;;)");
        let body = match &program.statements[0] {
            Statement::FunDecl { body, .. } => body,
            _ => panic!("expected fn"),
        };
        let stmts = match body.as_ref() {
            Statement::Block { statements, .. } => statements,
            _ => panic!("expected block body"),
        };
        assert!(
            matches!(
                stmts.iter().find(|s| matches!(s, Statement::For { .. })),
                Some(Statement::For {
                    init: None,
                    cond: None,
                    update: None,
                    ..
                })
            ),
            "expected for (;;)"
        );
    }

    #[test]
    fn brace_function_body_does_not_nest_block_around_first_let() {
        let src = "fn h() {\n  let a = 1\n  let b = 2\n}\n";
        let program = parse(src).expect("parse");
        let body = match &program.statements[0] {
            Statement::FunDecl { body, .. } => body,
            _ => panic!("expected fn"),
        };
        let stmts = match body.as_ref() {
            Statement::Block { statements, .. } => statements,
            _ => panic!("expected block body"),
        };
        assert_eq!(
            stmts.len(),
            2,
            "expected two top-level lets in fn body, not Block(let) + let — got {stmts:?}"
        );
        assert!(matches!(stmts[0], Statement::VarDecl { .. }));
        assert!(matches!(stmts[1], Statement::VarDecl { .. }));
    }

    #[test]
    fn member_access_allows_type_property_name() {
        let src = "fn f() {\n  const label = 0\n  label.type = \"button\"\n}\n";
        parse(src).expect("label.type should parse: `type` is a keyword but valid after `.`");
    }

    #[test]
    fn brace_block_stmt_then_const_then_if_are_siblings() {
        let src = "fn g() {\n  f()\n  const x = 1\n  if (x) {\n    f()\n  }\n}\n";
        let program = parse(src).expect("parse");
        let body = match &program.statements[0] {
            Statement::FunDecl { body, .. } => body,
            _ => panic!("expected fn"),
        };
        let stmts = match body.as_ref() {
            Statement::Block { statements, .. } => statements,
            _ => panic!("expected block body"),
        };
        assert_eq!(
            stmts.len(),
            3,
            "expected expr; const; if as siblings — got {stmts:?}"
        );
        assert!(matches!(stmts[0], Statement::ExprStmt { .. }));
        assert!(matches!(stmts[1], Statement::VarDecl { .. }));
        assert!(matches!(stmts[2], Statement::If { .. }));
    }

    #[test]
    fn ignore_indent_parses_brace_blocks_identically() {
        // Fully brace-delimited code: braces are authoritative, indentation is decoration.
        // Ignoring indentation must therefore produce an identical AST.
        let src = "fn f() {\n  let a = 1\n  if (a) {\n    let b = 2\n    g(b)\n  }\n}\n";
        let normal = parse(src).expect("parse (indentation significant)");
        let ignored = parse_with_options(src, LexerOptions { ignore_indent: true })
            .expect("parse (indentation ignored)");
        assert_eq!(
            format!("{normal:#?}"),
            format!("{ignored:#?}"),
            "brace-delimited code must parse identically with indentation ignored"
        );
    }

    #[test]
    fn ignore_indent_drops_indentation_induced_block() {
        // A leading-indented line makes the lexer open an indent level, so the parser wraps
        // `a()` in a `Block` — the kind of stray, indentation-driven nesting that can give
        // transpiled JS the wrong lexical scope. Ignoring indentation removes that wrapper.
        let src = "  a()\nb()\n";

        let normal = parse(src).expect("parse normal");
        assert!(
            matches!(normal.statements.first(), Some(Statement::Block { .. })),
            "indentation should wrap a() in a Block, got: {:?}",
            normal.statements
        );

        let ignored = parse_with_options(src, LexerOptions { ignore_indent: true })
            .expect("parse ignored");
        assert!(
            ignored
                .statements
                .iter()
                .all(|s| matches!(s, Statement::ExprStmt { .. })),
            "with indentation ignored, both calls are flat expression statements, got: {:?}",
            ignored.statements
        );
    }
}