Skip to content

Commit

Permalink
Cargo: new subparser based on TOML parser
Browse files Browse the repository at this point in the history
Signed-off-by: Masatake YAMATO <[email protected]>
  • Loading branch information
masatake committed Aug 9, 2024
1 parent de7bfe7 commit 3a5b8b6
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Units/parser-cargo.r/simple.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--languages=-TOML
--map-Cargo=.cargo
--sort=no
--fields=+lK
1 change: 1 addition & 0 deletions Units/parser-cargo.r/simple.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
myapp input.cargo /^name = "myapp"$/;" package language:Cargo
2 changes: 2 additions & 0 deletions Units/parser-cargo.r/simple.d/features
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packcc

2 changes: 2 additions & 0 deletions Units/parser-cargo.r/simple.d/input.cargo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[package]
name = "myapp"
1 change: 1 addition & 0 deletions docs/news/HEAD.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ New parsers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* TOML *peg/packcc*
* Cargo *TOML based subparser*

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 @@ -77,6 +77,7 @@
ClojureParser, \
CMakeParser, \
CParser, \
CargoParser, \
CppParser, \
CPreProParser, \
CssParser, \
Expand Down
141 changes: 141 additions & 0 deletions parsers/cargo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (c) 2024, Masatake YAMATO
*
* This source code is released for free distribution under the terms of the
* GNU General Public License version 2 or (at your option) any later version.
*
* This module contains functions for generating tags for Cargo.toml file.
*
* - ref. https://doc.rust-lang.org/cargo/index.html
*/

/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */

#include "x-toml.h"
#include "entry.h"
#include "kind.h"
#include "read.h"
#include "parse.h"
#include "subparser.h"

#include <string.h>

/*
* DATA DECLARATIONS
*/

typedef enum {
K_PACKAGE,
#if 0
K_CRATE,
#endif
} CargoKind;

static kindDefinition CargoKinds[] = {
{true, 'p', "package", "packages"},
#if 0
{true, 'c', "crate", "crates"},
#endif
};

struct sCargoSubparser {
tomlSubparser toml;
};

/*
* FUNCTION DEFINITIONS
*/
static void valueCallback (tomlSubparser *sub, const char *value, long offset, int corkIndex)
{
if (value[0] == '\0' || (value[0] == '"' && value[1] == '"'))
return;

tagEntryInfo *e = getEntryInCorkQueue (corkIndex);
if (e && strcmp(e->name, "name") == 0
&& e->extensionFields.scopeIndex != CORK_NIL)
{
tagEntryInfo *parent = getEntryInCorkQueue(e->extensionFields.scopeIndex);
if (parent && strcmp(parent->name, "package") == 0
&& parent->extensionFields.scopeIndex == CORK_NIL)
{
tagEntryInfo pe;
unsigned long lineNumber = getInputLineNumberForFileOffset(offset);
MIOPos filePosition = getInputFilePositionForLine (lineNumber);

/* Dropping double quote chars. */
vString *package = vStringNewInit (*value == '"'? value + 1: value);
if (*value == '"')
vStringChop (package);

initTagEntry (&pe, vStringValue (package), K_PACKAGE);
pe.lineNumber = lineNumber;
pe.filePosition = filePosition;

makeTagEntry (&pe);

vStringDelete (package);
}
}
}

#if 0
static void makeTagEntryCallback(subparser *s, const tagEntryInfo *tag, int corkIndex)
{
tagEntryInfo *e = getEntryInCorkQueue (corkIndex);
if (e && e->extensionFields.scopeIndex != CORK_NIL && e->kindIndex == TOML_K_QKEY)
{
tagEntryInfo *pe = getEntryInCorkQueue (e->extensionFields.scopeIndex);
if (pe && pe->extensionFields.scopeIndex == CORK_NIL)
{
if (strcmp(pe->name, "dependencies") == 0)
;
tagEntryInfo ce;

initTagEntry (&ce, e->name, K_CRATE);
ce.lineNumber = e->lineNumber;
ce.filePosition = e->filePosition;

makeTagEntry (&ce);
}
}
}
#endif

static void findCargoTags (void)
{
scheduleRunningBaseparser (0);
}

extern parserDefinition* CargoParser (void)
{
static const char *const patterns [] = { "Cargo.toml", NULL };

static struct sCargoSubparser cargoSubparser = {
.toml = {
.subparser = {
.direction = SUBPARSER_SUB_RUNS_BASE,
#if 0
.makeTagEntryNotify = makeTagEntryCallback,
#endif
},
.valueNotify = valueCallback,
},
};
static parserDependency dependencies [] = {
[0] = { DEPTYPE_SUBPARSER, "TOML", &cargoSubparser },
};

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

def->dependencies = dependencies;
def->dependencyCount = ARRAY_SIZE(dependencies);
def->kindTable = CargoKinds;
def->kindCount = ARRAY_SIZE (CargoKinds);
def->patterns = patterns;
def->parser = findCargoTags;
def->useCork = CORK_QUEUE;
return def;
}
1 change: 1 addition & 0 deletions source.mak
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ PARSER_SRCS = \
parsers/tcloo.c \
parsers/tex.c \
parsers/tex-beamer.c \
parsers/cargo.c \
parsers/ttcn.c \
parsers/txt2tags.c \
parsers/typescript.c \
Expand Down

0 comments on commit 3a5b8b6

Please sign in to comment.