Skip to content

Commit 8dfde48

Browse files
committed
Implement Nix optlib parser
1 parent 08e07dc commit 8dfde48

File tree

10 files changed

+145
-0
lines changed

10 files changed

+145
-0
lines changed

Units/simple-nix.d/args.ctags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--sort=no

Units/simple-nix.d/expected.tags

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
inc input.nix /^ inc = x: x + 1;$/;" f
2+
multilineFunc input.nix /^ multilineFunc =$/;" a
3+
attrset input.nix /^ attrset = {$/;" a
4+
multiline_attrset input.nix /^ multiline_attrset =$/;" a
5+
name input.nix /^ name = "hello";$/;" a
6+
hash input.nix /^ hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";$/;" a

Units/simple-nix.d/input.nix

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{ stdenv, fetchgit }:
2+
3+
let
4+
inc = x: x + 1;
5+
multilineFunc =
6+
a:
7+
b:
8+
c:
9+
a + b + c;
10+
attrset = {
11+
foo = "bar";
12+
};
13+
multiline_attrset =
14+
{ bar = "foo"; };
15+
in stdenv.mkDerivation {
16+
name = "hello";
17+
src = fetchgit {
18+
url = "https://example.com";
19+
hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
20+
};
21+
}

docs/news/HEAD.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Parser related changes
1414
New parsers
1515
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1616

17+
* Nix *optlib* by Ben Sima
18+
1719
Changes about parser specific kinds, roles, fields, and extras
1820
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1921

main/parsers_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
MesonOptionsParser, \
134134
MooseParser, \
135135
MyrddinParser, \
136+
NixParser, \
136137
NsisParser, \
137138
ObjcParser, \
138139
OcamlParser, \

optlib/nix.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Generated by ./misc/optlib2c from optlib/nix.ctags, Don't edit this manually.
3+
*/
4+
#include "general.h"
5+
#include "parse.h"
6+
#include "routines.h"
7+
#include "field.h"
8+
#include "xtag.h"
9+
10+
11+
static void initializeNixParser (const langType language CTAGS_ATTR_UNUSED)
12+
{
13+
}
14+
15+
extern parserDefinition* NixParser (void)
16+
{
17+
static const char *const extensions [] = {
18+
"nix",
19+
NULL
20+
};
21+
22+
static const char *const aliases [] = {
23+
NULL
24+
};
25+
26+
static const char *const patterns [] = {
27+
NULL
28+
};
29+
30+
static kindDefinition NixKindTable [] = {
31+
{
32+
true, 'p', "package", "package definition",
33+
},
34+
{
35+
true, 'f', "function", "function definition",
36+
},
37+
{
38+
true, 'a', "attr", "attribute definition",
39+
},
40+
};
41+
static tagRegexTable NixTagRegexTable [] = {
42+
{"[p?]name\\s*=\\s*\"(\\w+)\"", "\\1",
43+
"p", NULL, NULL, false},
44+
{"(\\S+)\\s*=\\s+\\w+:", "\\1",
45+
"f", NULL, NULL, false},
46+
{"\\s+([a-zA-Z_0-9-]{4,20})\\s*=", "\\1",
47+
"a", NULL, NULL, false},
48+
};
49+
50+
51+
parserDefinition* const def = parserNew ("Nix");
52+
53+
def->versionCurrent= 0;
54+
def->versionAge = 0;
55+
def->enabled = true;
56+
def->extensions = extensions;
57+
def->patterns = patterns;
58+
def->aliases = aliases;
59+
def->method = METHOD_NOT_CRAFTED|METHOD_REGEX;
60+
def->kindTable = NixKindTable;
61+
def->kindCount = ARRAY_SIZE(NixKindTable);
62+
def->tagRegexTable = NixTagRegexTable;
63+
def->tagRegexCount = ARRAY_SIZE(NixTagRegexTable);
64+
def->initialize = initializeNixParser;
65+
66+
return def;
67+
}

optlib/nix.ctags

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#
2+
# Copyright (c) 2024, Ben Sima
3+
#
4+
# Author: Ben Sima <[email protected]>
5+
#
6+
# This program is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU General Public License
8+
# as published by the Free Software Foundation; either version 2
9+
# of the License, or (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program; if not, write to the Free Software
18+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19+
# USA.
20+
#
21+
# References:
22+
#
23+
# - https://nix.dev/manual/nix/latest/language/
24+
#
25+
--langdef=Nix
26+
--map-Nix=+.nix
27+
28+
# Index packages when we see "name = <tag>" or "pname = <tag>"
29+
--kinddef-Nix=p,package,package definition
30+
--regex-Nix=/[p?]name\s*=\s*"(\w+)"/\1/p/
31+
32+
# Functions have args, so look for a : right of the =
33+
# (This should probably be a multiline regex.)
34+
--kinddef-Nix=f,function,function definition
35+
--regex-Nix=/(\S+)\s*=\s+\w+:/\1/f/
36+
37+
# Attrs definitions just have =, but only index if they have >=4 chars,
38+
# otherwise we get way too many tags. This will also index attr definitions
39+
# inside nix build files, which is not the most useful when writing nix code,
40+
# but can be useful when writing packages I guess.
41+
--kinddef-Nix=a,attr,attribute definition
42+
--regex-Nix=/\s+([a-zA-Z_0-9-]{4,20})\s*=/\1/a/

source.mak

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ OPTLIB2C_INPUT = \
241241
optlib/man.ctags \
242242
optlib/meson.ctags \
243243
optlib/mesonOptions.ctags \
244+
optlib/nix.ctags \
244245
optlib/org.ctags \
245246
optlib/passwd.ctags \
246247
optlib/pkgConfig.ctags \

win32/ctags_vs2013.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
<ClCompile Include="..\optlib\man.c" />
246246
<ClCompile Include="..\optlib\meson.c" />
247247
<ClCompile Include="..\optlib\mesonOptions.c" />
248+
<ClCompile Include="..\optlib\nix.c" />
248249
<ClCompile Include="..\optlib\org.c" />
249250
<ClCompile Include="..\optlib\passwd.c" />
250251
<ClCompile Include="..\optlib\pkgConfig.c" />

win32/ctags_vs2013.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@
258258
<ClCompile Include="..\optlib\mesonOptions.c">
259259
<Filter>Source Files\optlib</Filter>
260260
</ClCompile>
261+
<ClCompile Include="..\optlib\nix.c">
262+
<Filter>Source Files\optlib</Filter>
263+
</ClCompile>
261264
<ClCompile Include="..\optlib\org.c">
262265
<Filter>Source Files\optlib</Filter>
263266
</ClCompile>

0 commit comments

Comments
 (0)