nix-community / rnix-parser

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

Support paths with dynamic interpolation from Nix 2.4+ #52

Closed Ma27 closed 3 years ago

Ma27 commented 3 years ago

:warning: Warning: not ready to be merged. This is my first deep-dive and I'm a little afraid of regressions, so I'd first like to use this with rnix-lsp locally to test it a bit :)

Summary & Motivation

Since Nix 2.4 it's possible to perform string interpolations within paths:

nix-repl> let a = "rnix"; in /home/ma27/Projects/${a}-parser
/home/ma27/Projects/rnix-parser

Please note that the following expressions are not supported (by both Nix and this patch):

The newly introduced token NODE_PATH_WITH_INTERPOL is only used for these interpolated paths. Literal paths are still TOKEN_PATH inside NODE_LITERAL for backwards compatibility.

Backwards-incompatible changes

none

Further context

Closes #44


cc @nerdypepper

Ma27 commented 3 years ago

The changes can be tested via https://github.com/Ma27/rnix-lsp/tree/experimental.

Ma27 commented 3 years ago

Seems to be working for me.

cc @nerdypepper @aaronjanse for a review.

oppiliappan commented 3 years ago

Amazing, thanks! I'll take a look over the weekend.

aaronjanse commented 3 years ago

Thank you @Ma27!! I'll do some review & local testing over the next few days. I'm excited to have support for this syntax

ncfavier commented 2 years ago

This is not completely in sync with the syntax supported by Nix, for example ./${"a"}.b gives

error: unexpected TOKEN_PATH at 9..10, wanted any of [TOKEN_IDENT]
error: unexpected end of file, wanted any of [TOKEN_IDENT]
NODE_ROOT 0..10 {
  NODE_SELECT 0..10 {
    NODE_PATH_WITH_INTERPOL 0..8 {
      TOKEN_PATH("./") 0..2
      NODE_STRING_INTERPOL 2..8 {
        TOKEN_INTERPOL_START("${") 2..4
        NODE_STRING 4..7 {
          TOKEN_STRING_START("\"") 4..5
          TOKEN_STRING_CONTENT("a") 5..6
          TOKEN_STRING_END("\"") 6..7
        }
        TOKEN_INTERPOL_END("}") 7..8
      }
    }
    TOKEN_DOT(".") 8..9
    NODE_ERROR 9..10 {
      TOKEN_PATH("b") 9..10
    }
  }
}

whereas this syntax is supported by Nix since 2.4 (https://github.com/NixOS/nix/pull/5066).

Ma27 commented 2 years ago

Yes, I realized that this is a problem ~a week ago, I started working on a fix, but my time is slightly constrained unfortunately.

Ma27 commented 2 years ago

Opened a ticket to keep track of it: https://github.com/nix-community/rnix-parser/issues/85 :)

ncfavier commented 2 years ago

Fixed in https://github.com/nix-community/rnix-parser/pull/86