From 1d4604b2a2fca19ef6d2917bf9b089e7e130bb08 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sat, 10 Aug 2024 04:32:08 +0900 Subject: [PATCH] Cargo: new subparser based on TOML parser Signed-off-by: Masatake YAMATO --- .../list-subparsers-all.d/stdout-expected.txt | 1 + Units/parser-cargo.r/simple.d/args.ctags | 4 + Units/parser-cargo.r/simple.d/expected.tags | 1 + Units/parser-cargo.r/simple.d/features | 1 + Units/parser-cargo.r/simple.d/input.cargo | 2 + docs/news/HEAD.rst | 1 + main/parsers_p.h | 1 + parsers/cargo.c | 141 ++++++++++++++++++ source.mak | 1 + win32/ctags_vs2013.vcxproj | 1 + win32/ctags_vs2013.vcxproj.filters | 3 + 11 files changed, 157 insertions(+) create mode 100644 Units/parser-cargo.r/simple.d/args.ctags create mode 100644 Units/parser-cargo.r/simple.d/expected.tags create mode 100644 Units/parser-cargo.r/simple.d/features create mode 100644 Units/parser-cargo.r/simple.d/input.cargo create mode 100644 parsers/cargo.c diff --git a/Tmain/list-subparsers-all.d/stdout-expected.txt b/Tmain/list-subparsers-all.d/stdout-expected.txt index 7fbb3fcb20..19d66aed86 100644 --- a/Tmain/list-subparsers-all.d/stdout-expected.txt +++ b/Tmain/list-subparsers-all.d/stdout-expected.txt @@ -5,6 +5,7 @@ Autoconf M4 base <> sub {bidirectional} Automake Make base <= sub {dedicated} Bats Sh base <= sub {dedicated} BibLaTeX BibTeX base <> sub {bidirectional} +Cargo TOML base <= sub {dedicated} DBusIntrospect XML base <> sub {bidirectional} FunctionParameters Perl base <> sub {bidirectional} GemSpec Ruby base <= sub {dedicated} 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..4861429793 --- /dev/null +++ b/Units/parser-cargo.r/simple.d/features @@ -0,0 +1 @@ +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 \ diff --git a/win32/ctags_vs2013.vcxproj b/win32/ctags_vs2013.vcxproj index ad6944940c..81f0b89745 100644 --- a/win32/ctags_vs2013.vcxproj +++ b/win32/ctags_vs2013.vcxproj @@ -274,6 +274,7 @@ + diff --git a/win32/ctags_vs2013.vcxproj.filters b/win32/ctags_vs2013.vcxproj.filters index b059e75042..f974feffcf 100644 --- a/win32/ctags_vs2013.vcxproj.filters +++ b/win32/ctags_vs2013.vcxproj.filters @@ -345,6 +345,9 @@ Source Files\parsers + + Source Files\parsers + Source Files\parsers