Source

daybodep / ddtokens.c

/*
    Daybo Logic Dependency tool
    Copyright (C) 2001-2006  David Duncan Ross Palmer, Daybo Logic.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


  Daybo Logic
  57 Alderwood Parc,
  Penryn,
  Kernow,
  United Kingdom

  TR10 8RL
*/

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef HDRSTOP
# pragma hdrstop
#endif /*HDRSTOP*/

#include "daybodep.h"
/*-------------------------------------------------------------------------*/
/*
  Table of known symbols
*/
static const char *KnownSymbols[] = {
  "#", /* Pre-processor */
  ",", /* Coma */
  "(", /* Parenthesis begin */
  ")", /* Parenthesis end */
  ";", /* Semi-colon */
  "{", /* Begin block of code or data */
  "}", /* End block of code or data */
  "-", /* Subtract */
  ".", /* Direct membership or floating point */
  "//", /* Begin single line C++ comment */
  "/", /* division */
  "%", /* Modulus */
  "->", /* Indirect membership */
  "<", /* Less than */
  "<=", /* Less than or equal to */
  ">=", /* More than or equal to */
  ">", /* More than */
  "--", /* Decrement */
  "++", /* Increment */
  "+", /* Addition */
  "-=", /* Decrement by */
  "+=", /* Increment by */
  "==", /* Equality */
  "=", /* Assignment */
  "&", /* Bitwise AND */
  "&&", /* Logical AND */
  "|", /* Bitwise OR */
  "||", /* Logical OR */
  "^", /* Bitwise XOR */
  "^^", /* Logical XOR */
  "~", /* Bitwise NOT or Destructor prefix */
  "*", /* Multiply */
  "*=", /* Multiply and assign */
  "&=", /* Bitwise AND and assign */
  "|=", /* Bitwise OR and assign */
  "^=", /* Bitwise XOR and assign */
  "/=", /* Divide and assign */
  "%=", /* Modulus and assign */
  "!", /* Logical NOT */
  "/*", /* Begin standard C comment */
  "*/", /* End standard C comment */
  "<<", /* Bit shift left */
  "<<=", /* Bit shift left and assign */
  ">>", /* Bit shift right */
  ">>=", /* Bit shift right and assign */
  "\\"  /* '\' is an escape sequence or line continuation character */
};

/*
  Table of known keywords
*/
static const char *KnownKeywords[] = {
  "asm", /* Begins inline assembly */
  "int", /* Basic integer */
  "char", /* Character type (size 1) */
  "signed", /* Signedness modifier */
  "unsigned", /* Signedness modifier */
  "short", /* Modifier */
  "long", /* Modifier */
  "float", /* Built-in data type */
  "double", /* Built-in data type */
  "bool", /* Keyword in C++, data type in C */
  "class", /* Keyword in C++ */
  "namespace", /* Reference to a C++ namespace */
  "using", /* Only used as "using namespace" to my knowledge */
  "virtual", /* C++ virtual function declaration */
  "friend",
  "mutable",
  "const", /* Constant */
  "struct", /* Structure */
  "static", /* Linkage modifier */
  "extern", /* Linkage modifier */
  "inline", /* Related to linkage modifiers, not always standard */
  "auto", /* Automatic scope (default, so never used) */
  "register", /* Use a processor register if possible */
  "break", /* Leave loops */
  "case",
  "switch",
  "continue", /* Restart loops */
  "for",
  "while",
  "do",
  "if",
  "cout",
  "cin",
  "else",
  "enum",
  "far", /* Type modifier */
  "near", /* Type modifier */
  "goto", /* Rarely used */
  "huge", /* Type modifier */
  "new", /* C++ only */
  "delete", /* C++ only */
  "operator", /* C++ only */
  "public", /* C++, for use in classes only */
  "private", /* C++, for use in classes only */
  "protected", /* C++, for use in classes only */
  "return", /* Function return */
  "sizeof",
  "this", /* Important C++ keyword */
  "typedef", /* Very important standard C keyword */
  "union",
  "void",
  "volatile"  /* Type modifier, warns compiler of dark forces */
};

/*
  Table of known pre-processor keywords
*/
static const char *KnownPreProcs[] = {
  "include",
  "elif",
  "else",
  "endif",
  "error",
  "if", /* covers complex expressions */
  "ifdef",
  "ifndef",
  "warning",
  "line",
  "pragma",
  "undef",
  "define"
};

static char *Words[64];
/*-------------------------------------------------------------------------*/
const char *GetTokenByIndex(
  const enum tokType TokenType,
  const unsigned int Index
)
{
  switch ( TokenType ) {
    case ( tokSymbol ) : {
      if ( Index < sizeof(KnownSymbols)/sizeof(KnownSymbols[0]) )
        return KnownSymbols[Index];
    }
    case ( tokKeyword ) : {
      if ( Index < sizeof(KnownKeywords)/sizeof(KnownKeywords[0]) )
        return KnownKeywords[Index];
    }
    case ( tokPreProc ) : {
      if ( Index < sizeof(KnownPreProcs)/sizeof(KnownPreProcs[0]) )
        return KnownPreProcs[Index];
    }
  }
  return NULL;
}
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
void WordListInit()
{
  unsigned int i;

  for ( i = 0U; i < sizeof(Words)/sizeof(Words[0]); i++ )
    Words[i] = NULL;
}
/*-------------------------------------------------------------------------*/
bool WordListAddString(
  const char *Str
)
{
  unsigned int si; /* Source index */
  unsigned int di; /* Destination index */
  unsigned int slen;
  char word[256] = { '\0' };
  char prevChar = '\0';

  if ( Str ) {
    slen = strlen(Str);
    if ( slen ) {
      for ( si = 0U; si < slen; si++ ) {
        if ( prevChar == ' ' || prevChar == '\0' ) {
          /* Reset word */
          for ( di = 0U; di < sizeof(word)/sizeof(word[0]); di++ )
            word[di] = '\0';
          di = 0U;
        }
        if ( Str[si] != ' ' )
          word[di] = Str[si];
        if ( ++di >= sizeof(word)/sizeof(word[0]) ) {
          fprintf(
            GetStandardError(),
            "%s: \"%s\"",
            "Word overflow",
            Str
          );
          return false;
        }
        prevChar = Str[si];
        if ( 
          (prevChar == ' ') ||
          (prevChar == '\0') ||
          (si == slen-1)
        ) {
          word[di] = '\0';
          WordListAddWord(word);
        }
      }
      return true;
    }
  }
  return false;
}
/*-------------------------------------------------------------------------*/
bool WordListAddWord(
  const char *Word
)
{
  if ( Word ) {
    unsigned int len = strlen(Word);
    if ( len ) {
      unsigned int i;

      for ( i = 0U; i < sizeof(Words)/sizeof(Words[0]); i++ ) {
        if ( !Words[i] ) {
          Words[i] = malloc(len+1);
          if ( Words[i] ) {
            (*Words[i]) = '\0';
            strcat(Words[i], Word);
            return true;
          }
        }
      }
    }
  }
  return false;
}
/*-------------------------------------------------------------------------*/
const char *WordListGetWord(
  unsigned int Index
)
{
  static const char *Blank = "";
  if ( Index < sizeof(Words)/sizeof(Words[0]) )
    return Words[Index];

  return Blank;
}
/*-------------------------------------------------------------------------*/
void WordListClear()
{
  unsigned int i;

  for ( i = 0U; i < sizeof(Words)/sizeof(Words[0]); i++ ) {
    if ( Words[i] ) {
      free(Words[i]);
      Words[i] = NULL;
    }
  }
}
/*-------------------------------------------------------------------------*/