Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nix language parser #4020

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Units/simple-nix.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--sort=no
19 changes: 19 additions & 0 deletions Units/simple-nix.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
inc input.nix /^ inc = x: x + 1;$/;" f
inc input.nix /^ inc = x: x + 1;$/;" a
multilineFunc input.nix /^ multilineFunc =$/;" a
attrset input.nix /^ attrset = {$/;" a
foo input.nix /^ foo = "bar";$/;" a
multiline_attrset input.nix /^ multiline_attrset =$/;" a
bar input.nix /^ { bar = "foo"; };$/;" a
hello input.nix /^ hello = stdenv.mkDerivation {$/;" a
hello input.nix /^ name = "hello";$/;" p
name input.nix /^ name = "hello";$/;" a
src input.nix /^ src = fetchgit {$/;" a
url input.nix /^ url = "https:\/\/example.com";$/;" a
hash input.nix /^ hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";$/;" a
goodbye input.nix /^ goodbye = buildPythonPackge {$/;" a
goodbye input.nix /^ pname = "goodbye";$/;" p
pname input.nix /^ pname = "goodbye";$/;" a
src input.nix /^ src = fetchPyPi {$/;" a
url input.nix /^ url = "https:\/\/example.com";$/;" a
hash input.nix /^ hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";$/;" a
30 changes: 30 additions & 0 deletions Units/simple-nix.d/input.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{ stdenv, fetchgit, lib }:

let
inc = x: x + 1;
multilineFunc =
a:
b:
c:
a + b + c;
attrset = {
foo = "bar";
};
multiline_attrset =
{ bar = "foo"; };
in {
hello = stdenv.mkDerivation {
name = "hello";
src = fetchgit {
url = "https://example.com";
hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
};
};
goodbye = buildPythonPackge {
pname = "goodbye";
src = fetchgit {
url = "https://example.com";
hash = lib.fakeSha256;
};
};
}
bsima marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions docs/news/HEAD.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Parser related changes
New parsers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* Nix *optlib* by Ben Sima

Changes about parser specific kinds, roles, fields, and extras
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions main/parsers_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
MesonOptionsParser, \
MooseParser, \
MyrddinParser, \
NixParser, \
NsisParser, \
ObjcParser, \
OcamlParser, \
Expand Down
67 changes: 67 additions & 0 deletions optlib/nix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Generated by ./misc/optlib2c from optlib/nix.ctags, Don't edit this manually.
*/
#include "general.h"
#include "parse.h"
#include "routines.h"
#include "field.h"
#include "xtag.h"


static void initializeNixParser (const langType language CTAGS_ATTR_UNUSED)
{
}

extern parserDefinition* NixParser (void)
{
static const char *const extensions [] = {
"nix",
NULL
};

static const char *const aliases [] = {
NULL
};

static const char *const patterns [] = {
NULL
};

static kindDefinition NixKindTable [] = {
{
true, 'p', "package", "package definition",
},
{
true, 'f', "function", "function definition",
},
{
true, 'a', "attr", "attribute definition",
},
};
static tagRegexTable NixTagRegexTable [] = {
{"p?name\\s*=\\s*\"(\\w+)\"", "\\1",
"p", NULL, NULL, false},
{"(\\S+)\\s*=\\s+\\w+:", "\\1",
"f", NULL, NULL, false},
{"\\s+([a-zA-Z_0-9-]+)\\s*=", "\\1",
"a", NULL, NULL, false},
};


parserDefinition* const def = parserNew ("Nix");

def->versionCurrent= 0;
def->versionAge = 0;
def->enabled = true;
def->extensions = extensions;
def->patterns = patterns;
def->aliases = aliases;
def->method = METHOD_NOT_CRAFTED|METHOD_REGEX;
def->kindTable = NixKindTable;
def->kindCount = ARRAY_SIZE(NixKindTable);
def->tagRegexTable = NixTagRegexTable;
def->tagRegexCount = ARRAY_SIZE(NixTagRegexTable);
def->initialize = initializeNixParser;

return def;
}
40 changes: 40 additions & 0 deletions optlib/nix.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright (c) 2024, Ben Sima
#
# Author: Ben Sima <ben@bsima.me>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
bsima marked this conversation as resolved.
Show resolved Hide resolved
# References:
#
# - https://nix.dev/manual/nix/latest/language/
#
--langdef=Nix
--map-Nix=+.nix

# Index packages when we see "name = <tag>" or "pname = <tag>"
--kinddef-Nix=p,package,package definition
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as reading the test case, package is not tested at all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doh, good catch, i fixed the regex here

--regex-Nix=/p?name\s*=\s*"(\w+)"/\1/p/

# Functions have args, so look for a : right of the =
--kinddef-Nix=f,function,function definition
--regex-Nix=/(\S+)\s*=\s+\w+:/\1/f/

# Attrs definitions just have =. This will also index attr definitions inside
# nix build files, which is not the most useful when writing nix code, but can
# be useful when writing packages I guess.
--kinddef-Nix=a,attr,attribute definition
--regex-Nix=/\s+([a-zA-Z_0-9-]+)\s*=/\1/a/
1 change: 1 addition & 0 deletions source.mak
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ OPTLIB2C_INPUT = \
optlib/man.ctags \
optlib/meson.ctags \
optlib/mesonOptions.ctags \
optlib/nix.ctags \
optlib/org.ctags \
optlib/passwd.ctags \
optlib/pkgConfig.ctags \
Expand Down
1 change: 1 addition & 0 deletions win32/ctags_vs2013.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
<ClCompile Include="..\optlib\man.c" />
<ClCompile Include="..\optlib\meson.c" />
<ClCompile Include="..\optlib\mesonOptions.c" />
<ClCompile Include="..\optlib\nix.c" />
<ClCompile Include="..\optlib\org.c" />
<ClCompile Include="..\optlib\passwd.c" />
<ClCompile Include="..\optlib\pkgConfig.c" />
Expand Down
3 changes: 3 additions & 0 deletions win32/ctags_vs2013.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@
<ClCompile Include="..\optlib\mesonOptions.c">
<Filter>Source Files\optlib</Filter>
</ClCompile>
<ClCompile Include="..\optlib\nix.c">
<Filter>Source Files\optlib</Filter>
</ClCompile>
<ClCompile Include="..\optlib\org.c">
<Filter>Source Files\optlib</Filter>
</ClCompile>
Expand Down