/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

#ifndef _KEYTAB_H_
#define _KEYTAB_H_

#include "abs.h"

/* first, the correct sizes of the tables. The first one is the main
 * Keyword_Table that is used while parsing, the others are source lists
 * which are hashed into the Keyword_Table during initialization. The 
 * main table size is chosen to make it a perfect hash (no collisons) 
 * for the set of keywords. The size however, is predetermined and
 * compiled in (see keytab.c - the end of the file is a test routine
 * used to determine the correct table size) */

#define KEYTABSIZE               1163
#define ANSI_KEYTABSIZE          32
#define ELROS_KEYTABSIZE         42
#define PRIMITIVE_KEYTABSIZE     17
#define CONFIG_KEYTABSIZE        5


// these are the different kinds of keywords. 

typedef  
  enum _KeyKind {
    KEY_ANSI,       // ANSI C keyword
    KEY_ELROS,      // elros keyword (give, accept, etc...)
    KEY_PRIM,       // elros primitive (Integer, IA5String, etc...)
    KEY_CONFIG,     // config file (params.dat) keyword
    KEY_UNKNOWN     // used to initialize Keywords
    }
Key_Kind;


/* the next types are not for the Keyword_Table, but for the other
 * source lists... */

// the entries for primitives and elros keywords 

typedef 
  struct {
    char            *pw_name;
    int              pw_yylex;
    enum abstr_kind  pw_primitive;
  }
PrimKeytoken;

typedef 
  struct {
    char *kw_name;
    int   kw_yylex;
    int   lang_flg;   
  }
ElrosKeytoken;

// the entries for ansi_keyword_table and config_keyword_table.

typedef 
  struct _Keytoken {
    char  *name;
    int    token;
  }
Keytoken;

/*
 * the Keyword_Table hash table entry
 */

class Keyword {
 public:
  Key_Kind         kind;
  char            *name;
  int              token;
  int              lflag;   // used when kind is KEY_ELROS
  enum abstr_kind  akind;   // used when kind is KEY_PRIM 

  Keyword()
    {
      kind   = KEY_UNKNOWN;
      name   = NULL;
      token  = 0;
      akind  = AKIND_UNKNOWN;
    }

  ~Keyword()
    {
      /* Empty. CAUTION: do not attempt to free name, the way 
       * this class is used, name is always made to point to an
       * existing string! */
    }
};


/*
 *  the class fast_hash_table, what the Keyword_Table is.
 */


class fast_hash_table {

  int       size;
  int       hashval(char *);
  Keyword **contents;
  
 public:
  
  fast_hash_table  ( int s );
  ~fast_hash_table ( );

  int       add    ( Keyword *key );
  Keyword  *lookup ( char *key );
  void      show   ( );
};


/* globals pertaining to keywords. If this file is included from 
 * keytab.c, then we must skip these declarations since keytab.c 
 * actually defines them (which would clash with storage class 
 * extern) */

#ifndef _KEYTAB_C

extern  PrimKeytoken      prim_table[];
extern  ElrosKeytoken     keyword_table[];
extern  Keytoken          ansi_keyword_table[];
extern  Keytoken          config_keyword_table[];

extern  fast_hash_table  *Keyword_Table;

#endif


/* these functions are in epp.l */

int is_primitive   ( char * , enum abstr_kind * );
int is_keyword     ( char * );
int is_ansi_key    ( char * );
int is_config_key  ( char * );


#endif

