Skip to content

Commit b4898dd

Browse files
committed
Cargo: new subparser based on TOML parser
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
1 parent f96779d commit b4898dd

File tree

11 files changed

+157
-0
lines changed

11 files changed

+157
-0
lines changed

Tmain/list-subparsers-all.d/stdout-expected.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Autoconf M4 base <> sub {bidirectional}
55
Automake Make base <= sub {dedicated}
66
Bats Sh base <= sub {dedicated}
77
BibLaTeX BibTeX base <> sub {bidirectional}
8+
Cargo TOML base <= sub {dedicated}
89
DBusIntrospect XML base <> sub {bidirectional}
910
FunctionParameters Perl base <> sub {bidirectional}
1011
GemSpec Ruby base <= sub {dedicated}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--languages=-TOML
2+
--map-Cargo=.cargo
3+
--sort=no
4+
--fields=+lK
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
myapp input.cargo /^name = "myapp"$/;" package language:Cargo
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packcc
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[package]
2+
name = "myapp"

docs/news/HEAD.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ New parsers
1818
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1919

2020
* TOML *peg/packcc*
21+
* Cargo *TOML based subparser*
2122

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

main/parsers_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
ClojureParser, \
7878
CMakeParser, \
7979
CParser, \
80+
CargoParser, \
8081
CppParser, \
8182
CPreProParser, \
8283
CssParser, \

parsers/cargo.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright (c) 2024, Masatake YAMATO
3+
*
4+
* This source code is released for free distribution under the terms of the
5+
* GNU General Public License version 2 or (at your option) any later version.
6+
*
7+
* This module contains functions for generating tags for Cargo.toml file.
8+
*
9+
* - ref. https://doc.rust-lang.org/cargo/index.html
10+
*/
11+
12+
/*
13+
* INCLUDE FILES
14+
*/
15+
#include "general.h" /* must always come first */
16+
17+
#include "x-toml.h"
18+
#include "entry.h"
19+
#include "kind.h"
20+
#include "read.h"
21+
#include "parse.h"
22+
#include "subparser.h"
23+
24+
#include <string.h>
25+
26+
/*
27+
* DATA DECLARATIONS
28+
*/
29+
30+
typedef enum {
31+
K_PACKAGE,
32+
#if 0
33+
K_CRATE,
34+
#endif
35+
} CargoKind;
36+
37+
static kindDefinition CargoKinds[] = {
38+
{true, 'p', "package", "packages"},
39+
#if 0
40+
{true, 'c', "crate", "crates"},
41+
#endif
42+
};
43+
44+
struct sCargoSubparser {
45+
tomlSubparser toml;
46+
};
47+
48+
/*
49+
* FUNCTION DEFINITIONS
50+
*/
51+
static void valueCallback (tomlSubparser *sub, const char *value, long offset, int corkIndex)
52+
{
53+
if (value[0] == '\0' || (value[0] == '"' && value[1] == '"'))
54+
return;
55+
56+
tagEntryInfo *e = getEntryInCorkQueue (corkIndex);
57+
if (e && strcmp(e->name, "name") == 0
58+
&& e->extensionFields.scopeIndex != CORK_NIL)
59+
{
60+
tagEntryInfo *parent = getEntryInCorkQueue(e->extensionFields.scopeIndex);
61+
if (parent && strcmp(parent->name, "package") == 0
62+
&& parent->extensionFields.scopeIndex == CORK_NIL)
63+
{
64+
tagEntryInfo pe;
65+
unsigned long lineNumber = getInputLineNumberForFileOffset(offset);
66+
MIOPos filePosition = getInputFilePositionForLine (lineNumber);
67+
68+
/* Dropping double quote chars. */
69+
vString *package = vStringNewInit (*value == '"'? value + 1: value);
70+
if (*value == '"')
71+
vStringChop (package);
72+
73+
initTagEntry (&pe, vStringValue (package), K_PACKAGE);
74+
pe.lineNumber = lineNumber;
75+
pe.filePosition = filePosition;
76+
77+
makeTagEntry (&pe);
78+
79+
vStringDelete (package);
80+
}
81+
}
82+
}
83+
84+
#if 0
85+
static void makeTagEntryCallback(subparser *s, const tagEntryInfo *tag, int corkIndex)
86+
{
87+
tagEntryInfo *e = getEntryInCorkQueue (corkIndex);
88+
if (e && e->extensionFields.scopeIndex != CORK_NIL && e->kindIndex == TOML_K_QKEY)
89+
{
90+
tagEntryInfo *pe = getEntryInCorkQueue (e->extensionFields.scopeIndex);
91+
if (pe && pe->extensionFields.scopeIndex == CORK_NIL)
92+
{
93+
if (strcmp(pe->name, "dependencies") == 0)
94+
;
95+
tagEntryInfo ce;
96+
97+
initTagEntry (&ce, e->name, K_CRATE);
98+
ce.lineNumber = e->lineNumber;
99+
ce.filePosition = e->filePosition;
100+
101+
makeTagEntry (&ce);
102+
}
103+
}
104+
}
105+
#endif
106+
107+
static void findCargoTags (void)
108+
{
109+
scheduleRunningBaseparser (0);
110+
}
111+
112+
extern parserDefinition* CargoParser (void)
113+
{
114+
static const char *const patterns [] = { "Cargo.toml", NULL };
115+
116+
static struct sCargoSubparser cargoSubparser = {
117+
.toml = {
118+
.subparser = {
119+
.direction = SUBPARSER_SUB_RUNS_BASE,
120+
#if 0
121+
.makeTagEntryNotify = makeTagEntryCallback,
122+
#endif
123+
},
124+
.valueNotify = valueCallback,
125+
},
126+
};
127+
static parserDependency dependencies [] = {
128+
[0] = { DEPTYPE_SUBPARSER, "TOML", &cargoSubparser },
129+
};
130+
131+
parserDefinition* const def = parserNew ("Cargo");
132+
133+
def->dependencies = dependencies;
134+
def->dependencyCount = ARRAY_SIZE(dependencies);
135+
def->kindTable = CargoKinds;
136+
def->kindCount = ARRAY_SIZE (CargoKinds);
137+
def->patterns = patterns;
138+
def->parser = findCargoTags;
139+
def->useCork = CORK_QUEUE;
140+
return def;
141+
}

source.mak

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ PARSER_SRCS = \
422422
parsers/tcloo.c \
423423
parsers/tex.c \
424424
parsers/tex-beamer.c \
425+
parsers/cargo.c \
425426
parsers/ttcn.c \
426427
parsers/txt2tags.c \
427428
parsers/typescript.c \

win32/ctags_vs2013.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@
274274
<ClCompile Include="..\parsers\biblatex.c" />
275275
<ClCompile Include="..\parsers\bibtex.c" />
276276
<ClCompile Include="..\parsers\c-based.c" />
277+
<ClCompile Include="..\parsers\cargo.c" />
277278
<ClCompile Include="..\parsers\clojure.c" />
278279
<ClCompile Include="..\parsers\cobol.c" />
279280
<ClCompile Include="..\parsers\cpreprocessor.c" />

0 commit comments

Comments
 (0)