/*
 *   Copyright (C) 1988-2006 Lucent Technologies Inc. and others.
 *   All Rights Reserved.
 */

/*	cscope - interactive C symbol cross-reference
 *
 *	keyword look-up routine for the SDL symbol scanner
 */

#ifdef MULTILINGUAL

#include "global.h"

/* This keyword table is also used for keyword text compression.  Keywords
 * with an index less than the numeric value of a space are replaced with the
 * control character corresponding to the index, so they cannot be moved
 * without changing the database file version and adding compatibility code
 * for old databases.
 */
struct	keystruct skeyword[] = {
	{ "#define",	' ',	MISC,	NULL },	/* must be table entry 0 for old databases */
	{ "#include",	' ',	MISC,	NULL },	/* must be table entry 1 */
	{ "ALTERNATIVE",' ',	FLOW,	NULL },
	{ "CALL",	' ',	FLOW,	NULL },
	{ "COMMENT",	' ',	FLOW,	NULL },
	{ "CREATE",	' ',	FLOW,	NULL },
	{ "DCL",	' ',	DECL,	NULL },
	{ "#define",	' ',	MISC,	NULL },	/* must be table entry 7 */
	{ "DECISION",	' ',	FLOW,	NULL },
	{ "\t",		'\0',	MISC,	NULL },	/* must be table entry 9 */
	{ "\n",		'\0',	MISC,	NULL },	/* must be table entry 10 */
	{ "define",	'\0',	MISC,	NULL },
	{ "dnl",	' ',	FLOW,	NULL },
	{ "ELSE",	' ',	FLOW,	NULL },
	{ "ENDALTERNATIVE",'\0',FLOW,	NULL },
	{ "ENDDECISION",'\0',	FLOW,	NULL },
	{ "ENDMODEL",	'\0',	FLOW,	NULL },
	{ "ENDPROCEDURE",'\0',	FLOW,	NULL },
	{ "ENDPROCESS",	'\0',	FLOW,	NULL },
	{ "ENDSTATE",	'\0',	FLOW,	NULL },
	{ "EXTERNAL",	' ',	FLOW,	NULL }, /* 20 */
	{ "FPAR",	' ',	DECL,	NULL },
	{ "include",	'\0',	MISC,	NULL },
	{ "INPUT",	' ',	FLOW,	NULL },
	{ "INTERNAL",	' ',	FLOW,	NULL },
	{ "JOIN",	' ',	FLOW,	NULL },
	{ "JOINMODEL",	' ',	FLOW,	NULL },
	{ "JOINSTATE",	' ',	FLOW,	NULL },
	{ "MACRO",	' ',	FLOW,	NULL },
	{ "MACROS",	' ',	FLOW,	NULL },
	{ "MODCODE",	' ',	MISC,	NULL }, /* 30 */
	{ "MODELDEF",	' ',	MISC,	NULL },
	
	/* these keywords are not compressed */
	{ "MODEL",	' ',	FLOW,	NULL },
	{ "NEXTSTATE",	' ',	FLOW,	NULL },
	{ "OUTPUT",	' ',	FLOW,	NULL },
	{ "PROCEDURE",	' ',	FLOW,	NULL },
	{ "PROCESS",	' ',	FLOW,	NULL },
	{ "PROVIDED",	' ',	FLOW,	NULL },
	{ "RESET",	' ',	MISC,	NULL },
	{ "RETURN",	'\0',	FLOW,	NULL },
	{ "SAVE",	' ',	FLOW,	NULL },
	{ "SET",	' ',	MISC,	NULL },
	{ "SIGNALS",	' ',	FLOW,	NULL },
	{ "START",	' ',	FLOW,	NULL },
	{ "STATE",	' ',	FLOW,	NULL },
	{ "STATES",	' ',	FLOW,	NULL },
	{ "STOP",	' ',	FLOW,	NULL },
	{ "TASK",	' ',	FLOW,	NULL },
	{ "TO",		' ',	FLOW,	NULL },
	{ "WITH",	' ',	FLOW,	NULL },
};
#define KEYWORDS	(sizeof(skeyword) / sizeof(struct keystruct))

#define HASHMOD	(KEYWORDS * 2 + 1)

static	struct	keystruct *hashtab[HASHMOD]; /* pointer table */

#if defined( __STDC__ ) || defined( __BORLANDC__ )
void	sinitsymtab(void);
struct	keystruct *slookup(char *ident);
#endif

/* put the keywords into the symbol table */

void
sinitsymtab()
{
	register int	i, j;
	register struct	keystruct *p;
	
	for (i = 1; i < KEYWORDS; ++i) {
		p = &skeyword[i];
		j = hash(p->text) % HASHMOD;
		p->next = hashtab[j];
		hashtab[j] = p;
	}
}

/* see if this identifier is a keyword */

struct	keystruct *
slookup(ident)
register char	*ident;
{
	register struct	keystruct *p;
	int	c;
	
	/* look up the identifier in the keyword table */
	for (p = hashtab[hash(ident) % HASHMOD]; p != NULL; p = p->next) {
		if (strequal(ident, p->text)) {
			if (compress == YES && (c = p - skeyword) < ' ') {
				ident[0] = c;	/* compress the keyword */
			}
			return(p);
		}
	}
	/* this is an identifier */
	return(NULL);
}

#endif
