nushell / nushell

A new type of shell
https://www.nushell.sh/
MIT License
31.78k stars 1.63k forks source link

`nu --ide-ast` is missing an opening '{' after `match` #12923

Closed maxim-uvarov closed 2 months ago

maxim-uvarov commented 4 months ago

Describe the bug

nu --ide-ast is missing an opening '{' after match

image

How to reproduce

"match 3 { 1 => { 'yes!' }, _ => { 'no!' } }" | save test_match.nu -f
nu --ide-ast test_match.nu | from json | first 5

Expected behavior

I expect nu --ide-ast to output all the symbols without skipping them

Screenshots

No response

Configuration

key value
version 0.93.1
major 0
minor 93
patch 1
branch main
commit_hash 8adf3406e5e6c19f86c82c1f51db93d47217d8ef
build_os macos-aarch64
build_target aarch64-apple-darwin
rust_version rustc 1.77.2 (25ef9e3d8 2024-04-09)
rust_channel 1.77.2-aarch64-apple-darwin
cargo_version cargo 1.77.2 (e52e36006 2024-03-26)
build_time 2024-05-17 13:40:01 +00:00
build_rust_channel release
allocator mimalloc
features default, sqlite, system-clipboard, trash, which
installed_plugins explore, image, inc, polars, regex

Additional context

No response

fdncred commented 4 months ago

I get this on Windows. Note, the gray boxes are just showing where there are spaces. image

fdncred commented 4 months ago

Seems like it's just like yours. I wonder why it's missing. It doesn't have the outer {} at all. I kind of wonder if this is by design or not because it doesn't have every character listed in the string.

My guess is that it's only reporting items that have a span and maybe some of the syntax doesn't have spans. I'm not really sure.

fdncred commented 4 months ago

A more verbose way of looking at the ast is

ast "match 3 { 1 => { 'yes!' }, _ => { 'no!' } }"

I wonder if it skips some characters too?

Edit: Seem to do the same thing.

I think it's happening somewhere in here. https://github.com/nushell/nushell/blob/1cdc39bc2a616fde4c2b58b45893653517725643/crates/nu-parser/src/flatten.rs#L220-L246

maxim-uvarov commented 4 months ago

@fdncred I don't know how to read it 🙈

ast "match 3 { 1 => { 'yes!' }, _ => { 'no!' } }" | get block

Block {
    signature: Signature {
        name: "",
        usage: "",
        extra_usage: "",
        search_terms: [],
        required_positional: [],
        optional_positional: [],
        rest_positional: None,
        named: [],
        input_output_types: [],
        allow_variants_without_examples: false,
        is_filter: false,
        creates_scope: false,
        allows_unknown_args: false,
        category: Default,
    },
    pipelines: [
        Pipeline {
            elements: [
                PipelineElement {
                    pipe: None,
                    expr: Expression {
                        expr: Call(
                            Call {
                                decl_id: 30,
                                head: Span {
                                    start: 1989120,
                                    end: 1989125,
                                },
                                arguments: [
                                    Positional(
                                        Expression {
                                            expr: Int(
                                                3,
                                            ),
                                            span: Span {
                                                start: 1989126,
                                                end: 1989127,
                                            },
                                            ty: Int,
                                            custom_completion: None,
                                        },
                                    ),
                                    Positional(
                                        Expression {
                                            expr: MatchBlock(
                                                [
                                                    (
                                                        MatchPattern {
                                                            pattern: Value(
                                                                Expression {
                                                                    expr: Int(
                                                                        1,
                                                                    ),
                                                                    span: Span {
                                                                        start: 1989130,
                                                                        end: 1989131,
                                                                    },
                                                                    ty: Int,
                                                                    custom_completion: None,
                                                                },
                                                            ),
                                                            guard: None,
                                                            span: Span {
                                                                start: 1989130,
                                                                end: 1989131,
                                                            },
                                                        },
                                                        Expression {
                                                            expr: Block(
                                                                5756,
                                                            ),
                                                            span: Span {
                                                                start: 1989135,
                                                                end: 1989145,
                                                            },
                                                            ty: Block,
                                                            custom_completion: None,
                                                        },
                                                    ),
                                                    (
                                                        MatchPattern {
                                                            pattern: IgnoreValue,
                                                            guard: None,
                                                            span: Span {
                                                                start: 1989147,
                                                                end: 1989148,
                                                            },
                                                        },
                                                        Expression {
                                                            expr: Block(
                                                                5757,
                                                            ),
                                                            span: Span {
                                                                start: 1989152,
                                                                end: 1989161,
                                                            },
                                                            ty: Block,
                                                            custom_completion: None,
                                                        },
                                                    ),
                                                ],
                                            ),
                                            span: Span {
                                                start: 1989128,
                                                end: 1989163,
                                            },
                                            ty: Any,
                                            custom_completion: None,
                                        },
                                    ),
                                ],
                                parser_info: {},
                            },
                        ),
                        span: Span {
                            start: 1989120,
                            end: 1989163,
                        },
                        ty: Any,
                        custom_completion: None,
                    },
                    redirection: None,
                },
            ],
        },
    ],
    captures: [],
    redirect_env: false,
    span: Some(
        Span {
            start: 1989120,
            end: 1989163,
        },
    ),
}
maxim-uvarov commented 4 months ago

Seems like it's just like yours.

Seems like my Mac has been acting weird on its own in the last few days :( I didn't change anything inside the nushell sources, just built from some commit from main.

fdncred commented 4 months ago

@fdncred I don't know how to read it 🙈

This is the debug details of the nushell ast with object names that are used in the source code itself.

I'm guessing all this is working as expected right now. I'm not positive why some things show up and others do not other than looking at the code I linked to and investigating further.

sholderbach commented 2 months ago

I would say this is down to how we implement the AST for match at the moment. It is not really a traditional shape_block but rather the special syntax form for all the following match arms.

Note how the => match arm syntax token also isn't part of the output.

The output may be suboptimal if you want to do work on the nu --ide-ast stuff, but there is no hard requirement how this has to look. We will probably draw somewhat different AST boundaries for https://github.com/nushell/new-nu-parser