nix-community / rnix-parser

A Nix parser written in Rust [maintainer=@oberblastmeister]
MIT License
364 stars 44 forks source link

Error handling for Select expressions #33

Open aaronjanse opened 3 years ago

aaronjanse commented 3 years ago

The following is a common scenario when editing Nix:

rec {
    foo.bar = 13;
    xyz = foo.; # trailing period was just typed
}

rnix-parser consumes the rest of the file as an error, rather than stopping at the semicolon:

`dump-ast` output
error: unexpected TOKEN_SEMICOLON at 38..41, wanted any of [TOKEN_IDENT]
error: unexpected end of file, wanted any of [TOKEN_IDENT]
error: unexpected end of file, wanted any of [TOKEN_SEMICOLON]
error: unexpected end of file
NODE_ROOT 0..41 {
  NODE_ATTR_SET 0..41 {
    TOKEN_REC("rec") 0..3
    TOKEN_WHITESPACE(" ") 3..4
    TOKEN_CURLY_B_OPEN("{") 4..5
    TOKEN_WHITESPACE("\n    ") 5..10
    NODE_KEY_VALUE 10..23 {
      NODE_KEY 10..17 {
        NODE_IDENT 10..13 {
          TOKEN_IDENT("foo") 10..13
        }
        TOKEN_DOT(".") 13..14
        NODE_IDENT 14..17 {
          TOKEN_IDENT("bar") 14..17
        }
      }
      TOKEN_WHITESPACE(" ") 17..18
      TOKEN_ASSIGN("=") 18..19
      TOKEN_WHITESPACE(" ") 19..20
      NODE_LITERAL 20..22 {
        TOKEN_INTEGER("13") 20..22
      }
      TOKEN_SEMICOLON(";") 22..23
    }
    TOKEN_WHITESPACE("\n    ") 23..28
    NODE_KEY_VALUE 28..41 {
      NODE_KEY 28..31 {
        NODE_IDENT 28..31 {
          TOKEN_IDENT("xyz") 28..31
        }
      }
      TOKEN_WHITESPACE(" ") 31..32
      TOKEN_ASSIGN("=") 32..33
      TOKEN_WHITESPACE(" ") 33..34
      NODE_SELECT 34..41 {
        NODE_IDENT 34..37 {
          TOKEN_IDENT("foo") 34..37
        }
        TOKEN_DOT(".") 37..38
        NODE_ERROR 38..41 {
          TOKEN_SEMICOLON(";") 38..39
          TOKEN_WHITESPACE("\n") 39..40
          TOKEN_CURLY_B_CLOSE("}") 40..41
        }
      }
    }
  }
}

It would be nice if this scenario were handled, since that would allow rnix-lsp to provide autocomplete here.

I'd be happy to create a PR if this (presumably minor) change to rnix-parser is wanted.

aaronjanse commented 3 years ago

I'd be happy to create a PR if this (presumably minor) change to rnix-parser is wanted.

Actually, I don't think I understand rowan well enough to make a PR short-term, but I'd be happy to learn if needed

EDIT: I've made a WIP pull request