Skip to content

Commit

Permalink
Merge pull request #4046 from masatake/extend---list-languages-option
Browse files Browse the repository at this point in the history
main: extend --list-languages option to list only parsers using packcc
  • Loading branch information
masatake authored Aug 8, 2024
2 parents fa2a82d + 3603d85 commit 5f06997
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/building-with-pegof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ jobs:

- run: ./ctags --list-features | grep pegof

- run: make units
- run: echo "Targets: $(./ctags --list-languages=_packcc | tr '\n' ',')"
- run: make units LANGUAGES="$(./ctags --list-languages=_packcc | tr '\n' ',')"
- run: make dist
# See EXTRA_DIST in Makefile.am.
# Currently we add .pego files.
16 changes: 14 additions & 2 deletions main/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -2166,9 +2166,21 @@ static void processListMapsOption (

static void processListLanguagesOption (
const char *const option CTAGS_ATTR_UNUSED,
const char *const parameter CTAGS_ATTR_UNUSED)
const char *const parameter)
{
printLanguageList ();
enum parserCategory category = PARSER_CATEGORY_NONE;

if (parameter)
{
if (strcmp(parameter, "_libxml") == 0)
category = PARSER_CATEGORY_LIBXML;
else if (strcmp(parameter, "_libyaml") == 0)
category = PARSER_CATEGORY_LIBYAML;
else if (strcmp(parameter, "_packcc") == 0)
category = PARSER_CATEGORY_PACKCC;
}

printLanguageList (category);
exit (0);
}

Expand Down
55 changes: 47 additions & 8 deletions main/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ typedef struct sParserObject {
is set here if this parser is OLDLANG.
LANG_IGNORE is set if no being pretended. */

enum parserCategory category; /* Used when --languages=_CATEGORY is specified. */

} parserObject;

/*
Expand Down Expand Up @@ -1971,7 +1973,8 @@ static void linkDependenciesAtInitializeParsing (parserDefinition *const parser)
}

/* Used in both builtin and optlib parsers. */
static void initializeParsingCommon (parserDefinition *def, bool is_builtin)
static void initializeParsingCommon (parserDefinition *def, bool is_builtin,
enum parserCategory category)
{
parserObject *parser;

Expand All @@ -1983,6 +1986,7 @@ static void initializeParsingCommon (parserDefinition *def, bool is_builtin)
def->id = LanguageCount++;
parser = LanguageTable + def->id;
parser->def = def;
parser->category = category;

hashTablePutItem (LanguageHTable, def->name, def);

Expand All @@ -2008,6 +2012,31 @@ static char *acceptableLangName(char *name)
return NULL;
}

static enum parserCategory getCategoryForParserFunc(parserDefinitionFunc* func)
{
/* Putting a NULL not to make a zero-sized array that some compilers don't support. */
parserDefinitionFunc* libxml_fa [] = { NULL, XML_PARSER_LIST };
parserDefinitionFunc* libyaml_fa[] = { NULL, YAML_PARSER_LIST };
parserDefinitionFunc* packcc_fa[] = { NULL, PEG_PARSER_LIST };

#define RETURN_IF_FOUND(c,C) \
do { \
for (size_t i = 0; i < ARRAY_SIZE(c##_fa); i++) \
{ \
if (c##_fa[i] == func) \
return PARSER_CATEGORY_##C; \
} \
} while (0)

RETURN_IF_FOUND(libxml, LIBXML);
RETURN_IF_FOUND(libyaml, LIBYAML);
RETURN_IF_FOUND(packcc, PACKCC);

#undef RETURN_IF_FOUND

return PARSER_CATEGORY_NONE;
}

extern void initializeParsing (void)
{
unsigned int builtInCount;
Expand All @@ -2032,6 +2061,7 @@ extern void initializeParsing (void)
verbose ("Installing parsers: ");
for (i = 0 ; i < builtInCount ; ++i)
{
enum parserCategory category = getCategoryForParserFunc(BuiltInParsers [i]);
parserDefinition* const def = (*BuiltInParsers [i]) ();
if (def != NULL)
{
Expand All @@ -2046,7 +2076,7 @@ extern void initializeParsing (void)
/* parser definition must define one and only one parsing routine */
Assert ((!!def->parser) + (!!def->parser2) == 1);

initializeParsingCommon (def, true);
initializeParsingCommon (def, true, category);
}
}
verbose ("\n");
Expand Down Expand Up @@ -2420,7 +2450,7 @@ extern void processLanguageDefineOption (
def->versionCurrent = data.versionCurrent;
def->versionAge = data.versionAge;

initializeParsingCommon (def, false);
initializeParsingCommon (def, false, PARSER_CATEGORY_NONE);
linkDependenciesAtInitializeParsing (def);

LanguageTable [def->id].currentPatterns = stringListNew ();
Expand Down Expand Up @@ -3872,17 +3902,26 @@ static void printLanguage (const langType language, parserDefinition** ltable)
printf ("%s%s\n", lang->name, isLanguageEnabled (lang->id) ? "" : " [disabled]");
}

extern void printLanguageList (void)
extern void printLanguageList (enum parserCategory category)
{
unsigned int i;
unsigned int n;
parserDefinition **ltable;

ltable = xMalloc (LanguageCount, parserDefinition*);
for (i = 0 ; i < LanguageCount ; ++i)
ltable[i] = LanguageTable[i].def;
qsort (ltable, LanguageCount, sizeof (parserDefinition*), compareParsersByName);
for (i = 0, n = 0 ; i < LanguageCount ; ++i)
{
if (category != PARSER_CATEGORY_NONE)
{
if (LanguageTable[i].category != category)
continue;
}
ltable[n] = LanguageTable[i].def;
++n;
}
qsort (ltable, n, sizeof (parserDefinition*), compareParsersByName);

for (i = 0 ; i < LanguageCount ; ++i)
for (i = 0 ; i < n ; ++i)
printLanguage (i, ltable);

eFree (ltable);
Expand Down
10 changes: 9 additions & 1 deletion main/parse_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ typedef enum {
LMAP_TABLE_OUTPUT = 1 << 2,
} langmapType;

enum parserCategory
{
PARSER_CATEGORY_NONE,
PARSER_CATEGORY_LIBXML,
PARSER_CATEGORY_LIBYAML,
PARSER_CATEGORY_PACKCC,
};

/*
* FUNCTION PROTOTYPES
*/
Expand Down Expand Up @@ -115,7 +123,7 @@ extern void printLanguageRoles (const langType language, const char* letters,
bool withListHeader, bool machinable, FILE *fp);
extern void printLanguageAliases (const langType language,
bool withListHeader, bool machinable, FILE *fp);
extern void printLanguageList (void);
extern void printLanguageList (enum parserCategory category);
extern void printLanguageParams (const langType language,
bool withListHeader, bool machinable, FILE *fp);
extern void printLanguageSubparsers (const langType language,
Expand Down

0 comments on commit 5f06997

Please sign in to comment.