Lexer, Parser, Abstract Syntax Tree and Concrete Syntax Tree for the Nix Expressions Language.
NixEl is a Rust library that turns Nix code into a correct, typed data-structured.
It's based on the original lexer and parser of Nix, using the same algorithms and methods, which means guaranteed correctness. Additionally, it's been tested against every file in Nixpkgs, and also continuously on our infrastructure.
It also provides a command line application
called $ nixel
which calls the library
and pretty-prints the results.
For example, given some code:
nix
let
greeting = "Hello World!";
in
greeting
NixEL can generate an Abstract Syntax Tree for you:
rust
LetIn {
bindings: [
KeyValue(
AttributePath {
attributes: [
Raw {
content: "greeting",
position: (2, 3),
},
],
},
String {
parts: [
Raw {
content: "Hello World!",
position: (2, 15),
},
],
},
),
],
target: Variable {
identifier: "greeting",
position: (4, 3),
},
}
Or perform Lexical Analysis:
rust
LET "let" (1, 1)
ID "greeting" (2, 3)
= "=" (2, 12)
" "\"" (2, 14)
STR "Hello World!" (2, 15)
" "\"" (2, 27)
; ";" (2, 28)
IN "in" (3, 1)
ID "greeting" (4, 3)
And produce a Parse Tree:
rust
Γ := rules "expr"
expr := rules "expr_function"
expr_function := rules "LET" "binds" "IN" "expr_function"
LET := lexemes "LET"
LET "let" (1, 1)
binds := rules "binds" "attrpath" "=" "expr" ";"
binds := rules
attrpath := rules "attr"
attr := rules "ID"
ID := lexemes "ID"
ID "greeting" (2, 3)
= := lexemes "="
= "=" (2, 12)
expr := rules "expr_function"
expr_function := rules "expr_if"
expr_if := rules "expr_op"
expr_op := rules "expr_app"
expr_app := rules "expr_select"
expr_select := rules "expr_simple"
expr_simple := rules "\"" "string_parts" "\""
" := lexemes "\""
" "\"" (2, 14)
string_parts := rules "STR"
STR := lexemes "STR"
STR "Hello World!" (2, 15)
" := lexemes "\""
" "\"" (2, 27)
; := lexemes ";"
; ";" (2, 28)
IN := lexemes "IN"
IN "in" (3, 1)
expr_function := rules "expr_if"
expr_if := rules "expr_op"
expr_op := rules "expr_app"
expr_app := rules "expr_select"
expr_select := rules "expr_simple"
expr_simple := rules "ID"
ID := lexemes "ID"
ID "greeting" (4, 3)
This library is based on Santiago and can be used to implement tools like Code Formatters, documentation generators, and even alternative evaluators of the Nix expression language.
You can checkout more examples in the tests folder.
We hope you find NixEL useful!
And don’t forget to give us a star ⭐