diff --git a/Units/parser-cargo.r/simple.d/args.ctags b/Units/parser-cargo.r/simple.d/args.ctags new file mode 100644 index 0000000000..cdc99b0d13 --- /dev/null +++ b/Units/parser-cargo.r/simple.d/args.ctags @@ -0,0 +1,4 @@ +--languages=-TOML +--map-Cargo=.cargo +--sort=no +--fields=+lK diff --git a/Units/parser-cargo.r/simple.d/expected.tags b/Units/parser-cargo.r/simple.d/expected.tags new file mode 100644 index 0000000000..5f9ce4397b --- /dev/null +++ b/Units/parser-cargo.r/simple.d/expected.tags @@ -0,0 +1 @@ +myapp input.cargo /^name = "myapp"$/;" package language:Cargo diff --git a/Units/parser-cargo.r/simple.d/features b/Units/parser-cargo.r/simple.d/features new file mode 100644 index 0000000000..7a1d0bd69a --- /dev/null +++ b/Units/parser-cargo.r/simple.d/features @@ -0,0 +1,2 @@ +packcc + diff --git a/Units/parser-cargo.r/simple.d/input.cargo b/Units/parser-cargo.r/simple.d/input.cargo new file mode 100644 index 0000000000..9e7a3b6375 --- /dev/null +++ b/Units/parser-cargo.r/simple.d/input.cargo @@ -0,0 +1,2 @@ +[package] +name = "myapp" diff --git a/docs/news/HEAD.rst b/docs/news/HEAD.rst index e0528f5bd2..35b8034358 100644 --- a/docs/news/HEAD.rst +++ b/docs/news/HEAD.rst @@ -18,6 +18,7 @@ New parsers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * TOML *peg/packcc* +* Cargo *TOML based subparser* Changes about parser specific kinds, roles, fields, and extras ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/main/parsers_p.h b/main/parsers_p.h index d5fb592ce7..18365ca44e 100644 --- a/main/parsers_p.h +++ b/main/parsers_p.h @@ -77,6 +77,7 @@ ClojureParser, \ CMakeParser, \ CParser, \ + CargoParser, \ CppParser, \ CPreProParser, \ CssParser, \ diff --git a/parsers/cargo.c b/parsers/cargo.c new file mode 100644 index 0000000000..f4546178fe --- /dev/null +++ b/parsers/cargo.c @@ -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 + +/* +* 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; +} diff --git a/source.mak b/source.mak index 74b78284cf..60cf964120 100644 --- a/source.mak +++ b/source.mak @@ -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 \