Add keywordTable to parserDefinition and use a common way to initialize keywords

This commit is contained in:
Jiří Techet 2016-07-29 22:50:11 +02:00
parent 6bc5797987
commit ae8dc545ce
10 changed files with 64 additions and 102 deletions

View File

@ -19,6 +19,7 @@
#include "mio.h"
#include "entry.h"
#include "keyword.h"
#include "main.h"
#define OPTION_WRITE
#include "options.h"
@ -293,12 +294,24 @@ extern void enableLanguage (const langType language, const boolean state)
LanguageTable [language]->enabled = state;
}
static void initializeParserOne (langType lang)
{
parserDefinition *const parser = LanguageTable [lang];
installKeywordTable (lang);
if ((parser->initialize != NULL) && (parser->initialized == FALSE))
{
parser->initialize (lang);
parser->initialized = TRUE;
}
}
static void initializeParsers (void)
{
unsigned int i;
for (i = 0 ; i < LanguageCount ; ++i)
if (LanguageTable [i]->initialize != NULL)
(LanguageTable [i]->initialize) ((langType) i);
int i;
for (i = 0; i < LanguageCount; i++)
initializeParserOne(i);
}
extern void initializeParsing (void)
@ -654,4 +667,22 @@ extern boolean parseFile (const char *const fileName)
return tagFileResized;
}
extern void installKeywordTable (const langType language)
{
parserDefinition* lang;
unsigned int i;
Assert (0 <= language && language < (int) LanguageCount);
lang = LanguageTable [language];
if ((lang->keywordTable != NULL) && (lang->keywordInstalled == FALSE))
{
for (i = 0; i < lang->keywordCount; ++i)
addKeyword (lang->keywordTable [i].name,
language,
lang->keywordTable [i].id);
lang->keywordInstalled = TRUE;
}
}
/* vi:set tabstop=4 shiftwidth=4 nowrap: */

View File

@ -65,6 +65,11 @@ typedef struct {
boolean enabled; /* currently enabled? */
stringList* currentPatterns; /* current list of file name patterns */
stringList* currentExtensions; /* current list of extensions */
const keywordTable *keywordTable;
unsigned int keywordCount;
unsigned int initialized:1; /* initialize() is called or not */
unsigned int keywordInstalled:1; /* keywordTable is installed or not. */
} parserDefinition;
typedef parserDefinition* (parserDefinitionFunc) (void);
@ -114,6 +119,8 @@ extern boolean processKindOption (const char *const option, const char *const pa
extern void printKindOptions (void);
extern boolean parseFile (const char *const fileName);
extern void installKeywordTable (const langType language);
/* Regex interface */
#ifdef HAVE_REGEX
extern void findRegexTags (void);

View File

@ -110,17 +110,6 @@ static const opKind OpKinds [] = {
/*
* FUNCTION DEFINITIONS
*/
static void buildAsmKeywordHash (void)
{
const size_t count = sizeof (AsmKeywords) / sizeof (AsmKeywords [0]);
size_t i;
for (i = 0 ; i < count ; ++i)
{
const keywordTable* const p = AsmKeywords + i;
addKeyword (p->name, Lang_asm, (int) p->id);
}
}
static opKeyword analyzeOperator (const vString *const op)
{
vString *keyword = vStringNew ();
@ -353,7 +342,6 @@ static void findAsmTags (void)
static void initialize (const langType language)
{
Lang_asm = language;
buildAsmKeywordHash ();
}
extern parserDefinition* AsmParser (void)
@ -375,6 +363,8 @@ extern parserDefinition* AsmParser (void)
def->patterns = patterns;
def->parser = findAsmTags;
def->initialize = initialize;
def->keywordTable = AsmKeywords;
def->keywordCount = ARRAY_SIZE (AsmKeywords);
return def;
}

View File

@ -416,18 +416,6 @@ static boolean insideInterface (void)
return result;
}
static void buildFortranKeywordHash (const langType language)
{
const size_t count =
sizeof (FortranKeywordTable) / sizeof (FortranKeywordTable [0]);
size_t i;
for (i = 0 ; i < count ; ++i)
{
const keywordTable* const p = &FortranKeywordTable [i];
addKeyword (p->name, language, (int) p->id);
}
}
/*
* Tag generation functions
*/
@ -2331,13 +2319,11 @@ static boolean findFortranTags (const unsigned int passCount)
static void initializeFortran (const langType language)
{
Lang_fortran = language;
buildFortranKeywordHash (language);
}
static void initializeF77 (const langType language)
{
Lang_f77 = language;
buildFortranKeywordHash (language);
}
extern parserDefinition* FortranParser (void)
@ -2355,6 +2341,8 @@ extern parserDefinition* FortranParser (void)
def->extensions = extensions;
def->parser2 = findFortranTags;
def->initialize = initializeFortran;
def->keywordTable = FortranKeywordTable;
def->keywordCount = ARRAY_SIZE (FortranKeywordTable);
return def;
}
@ -2373,6 +2361,8 @@ extern parserDefinition* F77Parser (void)
def->extensions = extensions;
def->parser2 = findFortranTags;
def->initialize = initializeF77;
def->keywordTable = FortranKeywordTable;
def->keywordCount = ARRAY_SIZE (FortranKeywordTable);
return def;
}
/* vi:set tabstop=4 shiftwidth=4: */

View File

@ -133,15 +133,7 @@ static boolean isIdentChar (const int c)
static void initialize (const langType language)
{
size_t i;
const size_t count =
sizeof (GoKeywordTable) / sizeof (GoKeywordTable[0]);
Lang_go = language;
for (i = 0; i < count; ++i)
{
const keywordTable *const p = &GoKeywordTable[i];
addKeyword (p->name, language, (int) p->id);
}
}
static tokenInfo *newToken (void)
@ -837,5 +829,7 @@ extern parserDefinition *GoParser (void)
def->extensions = extensions;
def->parser = findGoTags;
def->initialize = initialize;
def->keywordTable = GoKeywordTable;
def->keywordCount = ARRAY_SIZE (GoKeywordTable);
return def;
}

View File

@ -180,18 +180,6 @@ static boolean isIdentChar (const int c)
c == '@' || c == '_' || c == '#');
}
static void buildJsKeywordHash (void)
{
const size_t count = sizeof (JsKeywordTable) /
sizeof (JsKeywordTable [0]);
size_t i;
for (i = 0 ; i < count ; ++i)
{
const keywordTable* const p = &JsKeywordTable [i];
addKeyword (p->name, Lang_js, (int) p->id);
}
}
static tokenInfo *newToken (void)
{
tokenInfo *const token = xMalloc (1, tokenInfo);
@ -1888,9 +1876,8 @@ static void parseJsFile (tokenInfo *const token)
static void initialize (const langType language)
{
Assert (sizeof (JsKinds) / sizeof (JsKinds [0]) == JSTAG_COUNT);
Assert (ARRAY_SIZE (JsKinds) == JSTAG_COUNT);
Lang_js = language;
buildJsKeywordHash ();
}
static void findJsTags (void)
@ -1923,6 +1910,8 @@ extern parserDefinition* JavaScriptParser (void)
def->kindCount = ARRAY_SIZE (JsKinds);
def->parser = findJsTags;
def->initialize = initialize;
def->keywordTable = JsKeywordTable;
def->keywordCount = ARRAY_SIZE (JsKeywordTable);
return def;
}

View File

@ -144,18 +144,6 @@ typedef struct _lexingState {
const unsigned char *cp; /* position in stream */
} lexingState;
static void initKeywordHash (void)
{
const size_t count = sizeof (objcKeywordTable) / sizeof (keywordTable);
size_t i;
for (i = 0; i < count; ++i)
{
addKeyword (objcKeywordTable[i].name, Lang_ObjectiveC,
(int) objcKeywordTable[i].id);
}
}
/*//////////////////////////////////////////////////////////////////////
//// Lexing */
static boolean isNum (char c)
@ -1133,8 +1121,6 @@ static void findObjcTags (void)
static void objcInitialize (const langType language)
{
Lang_ObjectiveC = language;
initKeywordHash ();
}
extern parserDefinition *ObjcParser (void)
@ -1146,6 +1132,7 @@ extern parserDefinition *ObjcParser (void)
def->extensions = extensions;
def->parser = findObjcTags;
def->initialize = objcInitialize;
def->keywordTable = objcKeywordTable;
def->keywordCount = ARRAY_SIZE (objcKeywordTable);
return def;
}

View File

@ -238,17 +238,6 @@ static struct {
static vString *CurrentNamespace;
static void buildPhpKeywordHash (const langType language)
{
const size_t count = sizeof (PhpKeywordTable) / sizeof (PhpKeywordTable[0]);
size_t i;
for (i = 0; i < count ; i++)
{
const keywordTable* const p = &PhpKeywordTable[i];
addKeyword (p->name, language, (int) p->id);
}
}
static const char *accessToString (const accessType access)
{
static const char *const names[COUNT_ACCESS] = {
@ -1477,13 +1466,11 @@ static void findZephirTags (void)
static void initializePhpParser (const langType language)
{
Lang_php = language;
buildPhpKeywordHash (language);
}
static void initializeZephirParser (const langType language)
{
Lang_zephir = language;
buildPhpKeywordHash (language);
}
extern parserDefinition* PhpParser (void)
@ -1495,6 +1482,8 @@ extern parserDefinition* PhpParser (void)
def->extensions = extensions;
def->parser = findPhpTags;
def->initialize = initializePhpParser;
def->keywordTable = PhpKeywordTable;
def->keywordCount = ARRAY_SIZE (PhpKeywordTable);
return def;
}
@ -1507,6 +1496,8 @@ extern parserDefinition* ZephirParser (void)
def->extensions = extensions;
def->parser = findZephirTags;
def->initialize = initializeZephirParser;
def->keywordTable = PhpKeywordTable;
def->keywordCount = ARRAY_SIZE (PhpKeywordTable);
return def;
}

View File

@ -396,18 +396,6 @@ static boolean isMatchedEnd(tokenInfo *const token, int nest_lvl)
return terminated;
}
static void buildSqlKeywordHash (void)
{
const size_t count = sizeof (SqlKeywordTable) /
sizeof (SqlKeywordTable [0]);
size_t i;
for (i = 0 ; i < count ; ++i)
{
const keywordTable* const p = &SqlKeywordTable [i];
addKeyword (p->name, Lang_sql, (int) p->id);
}
}
static tokenInfo *newToken (void)
{
tokenInfo *const token = xMalloc (1, tokenInfo);
@ -2335,9 +2323,8 @@ static void parseSqlFile (tokenInfo *const token)
static void initialize (const langType language)
{
Assert (sizeof (SqlKinds) / sizeof (SqlKinds [0]) == SQLTAG_COUNT);
Assert (ARRAY_SIZE (SqlKinds) == SQLTAG_COUNT);
Lang_sql = language;
buildSqlKeywordHash ();
}
static void findSqlTags (void)
@ -2360,6 +2347,8 @@ extern parserDefinition* SqlParser (void)
def->extensions = extensions;
def->parser = findSqlTags;
def->initialize = initialize;
def->keywordTable = SqlKeywordTable;
def->keywordCount = ARRAY_SIZE (SqlKeywordTable);
return def;
}

View File

@ -106,15 +106,7 @@ static keywordTable VhdlKeywordTable [] = {
static void initialize (const langType language)
{
size_t i;
const size_t count = sizeof (VhdlKeywordTable) /
sizeof (VhdlKeywordTable [0]);
Lang_vhdl = language;
for (i = 0 ; i < count ; ++i)
{
const keywordTable* const p = &VhdlKeywordTable [i];
addKeyword (p->name, language, (int) p->id);
}
}
static void vUngetc (int c)
@ -292,6 +284,8 @@ extern parserDefinition* VhdlParser (void)
def->extensions = extensions;
def->parser = findVhdlTags;
def->initialize = initialize;
def->keywordTable = VhdlKeywordTable;
def->keywordCount = ARRAY_SIZE (VhdlKeywordTable);
return def;
}