Skip to content

Commit

Permalink
Implement Nix optlib parser
Browse files Browse the repository at this point in the history
  • Loading branch information
bsima committed Jun 20, 2024
1 parent 08e07dc commit 2838f80
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 0 deletions.
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
2 changes: 2 additions & 0 deletions Units/simple-nix.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name input.nix /^ name = "hello";$/;" a
hash input.nix /^ hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";$/;" a
9 changes: 9 additions & 0 deletions Units/simple-nix.d/input.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ stdenv, fetchgit }:

stdenv.mkDerivation {
name = "hello";
src = fetchgit {
url = "https://example.com";
hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
};
}
2 changes: 2 additions & 0 deletions docs/news/HEAD.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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-]{4,20})\\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.
#
#
--langdef=Nix
--map-Nix=+.nix

# Index packages when we see "name = <tag>" or "pname = <tag>"
--kinddef-Nix=p,package,package definition
--regex-Nix=/[p?]name\s*=\s*"(\w+)"/\1/p/

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

# Attrs definitions just have =, but only index if they have >=4 chars,
# otherwise we get way too many tags. 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-]{4,20})\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

0 comments on commit 2838f80

Please sign in to comment.