home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / envsof20 / source / syntax / language.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-01  |  5.4 KB  |  260 lines

  1. /*
  2.  * language.c -- Programing language specific functions
  3.  *
  4.  * Copyright 1999 Thomas Aglassinger and others, see file "forum.txt"
  5.  */
  6.  
  7. #include "defs.h"
  8.  
  9. #include "debug.h"
  10. #include "language.h"
  11. #include "keyword.h"
  12.  
  13. #include <stdlib.h>
  14.  
  15. Prototype void setup_char_array(void);
  16. Prototype int fstrncmp(const char *s1, const char *s2, ULONG n);
  17. Prototype int is_reserved_word(const char *word, ULONG word_start, ULONG word_end, struct keyword_info *info);
  18. Prototype int is_reserved_name(const char *name, ULONG name_start, ULONG name_end, struct keyword_info *info);
  19. Prototype int get_word_type(const char *text, ULONG word_start, ULONG word_end, struct keyword_info *info);
  20. Prototype char *get_word_type_text(int word_type);
  21.  
  22. /// "Eiffel words"
  23.  
  24. static const char normal_char[] =
  25. "abcdefghijklmnopqrstuvwxyz"
  26. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  27. "_01234567890";
  28. static const char alpha_char[] =
  29. "abcdefghijklmnopqrstuvwxyz"
  30. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  31. "_";
  32.  
  33. static const char numeric_char[] = "._0123456789";
  34. static const char lower_char[] = "abcdefghijklmnopqrstuvwxyz";
  35. static const char upper_char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  36.  
  37. char is_normal[256] =
  38. {0};
  39.  
  40. char is_alpha[256] =
  41. {0};
  42.  
  43. char is_numeric[256] =
  44. {0};
  45.  
  46. char is_upper[256] =
  47. {0};
  48.  
  49. char is_lower[256] =
  50. {0};
  51.  
  52. void setup_char_array(void)
  53. {
  54.    int i;
  55.  
  56.    /* alphabetical identifiers */
  57.    i = 0;
  58.    while (alpha_char[i])
  59.    {
  60.       is_alpha[alpha_char[i]] = 1;
  61.       i += 1;
  62.    }
  63.  
  64.    /* normal identifiers */
  65.    i = 0;
  66.    while (normal_char[i])
  67.    {
  68.       is_normal[normal_char[i]] = 1;
  69.       i += 1;
  70.    }
  71.  
  72.    /* numbers */
  73.    i = 0;
  74.    while (numeric_char[i])
  75.    {
  76.       is_numeric[numeric_char[i]] = 1;
  77.       i += 1;
  78.    }
  79.  
  80.    /* upper case */
  81.    i = 0;
  82.    while (upper_char[i])
  83.    {
  84.       is_upper[upper_char[i]] = 1;
  85.       i += 1;
  86.    }
  87.  
  88.    /* lower case */
  89.    i = 0;
  90.    while (lower_char[i])
  91.    {
  92.       is_lower[lower_char[i]] = 1;
  93.       i += 1;
  94.    }
  95. }
  96.  
  97.  
  98. int fstrncmp(const char *s1, const char *s2, ULONG n)
  99. {
  100.    ULONG c = 0;
  101.    char c1, c2;
  102.  
  103.    while (*s2 && (*s1 == *s2) && (c < n))
  104.    {
  105.       s1++;
  106.       s2++;
  107.       c++;
  108.    }
  109.  
  110.    c2 = *s2;
  111.    if (c == n) {
  112.       c1 = 0;
  113.    } else {
  114.       c1 = *s1;
  115.    }
  116.  
  117.    if (c1 < c2)
  118.       return -1;
  119.    else if (c1 > c2)
  120.       return 1;
  121.    else
  122.       return 0;
  123. }
  124.  
  125. static char *fsearch(const char *text, ULONG word_start, ULONG word_end,
  126.               const char *base[], const ULONG maximum_count)
  127. {
  128.    const char *word = &text[word_start];
  129.    ULONG word_length = word_end - word_start;
  130.    int a = 0;
  131.    int b = maximum_count;
  132.    int c;
  133.    int d;
  134.  
  135.    DD(bug("  fsearch(\"%s\",%ld) in %d\n", word, word_length, maximum_count));
  136.    for (;;)
  137.    {
  138.       c = (a + b) / 2;
  139.       //DD(bug("  %2d [%2d,%2d]: %s\n", c, a, b, reserved_word[c]));
  140.       if ((d = fstrncmp(word, base[c], word_length)) == 0)
  141.          return base[c];
  142.       if (c == a)
  143.          break;
  144.       if (d < 0)
  145.          b = c;
  146.       else
  147.          a = c;
  148.    }
  149.    return NULL;
  150. }
  151.  
  152. int is_reserved_word(const char *word, ULONG word_start, ULONG word_end, struct keyword_info *info)
  153. {
  154.    return ((is_lower[word[word_start]]) && (fsearch(word, word_start, word_end, info->word, info->word_count) != NULL));
  155. }
  156.  
  157. int is_reserved_name(const char *name, ULONG name_start, ULONG name_end, struct keyword_info *info)
  158. {
  159.    return ((is_upper[name[name_start]]) && (fsearch(name, name_start, name_end, info->word, info->word_count) != NULL));
  160. }
  161.  
  162. int get_word_type(const char *text, ULONG word_start, ULONG word_end, struct keyword_info *info)
  163. {
  164.    int word_type = SYNTAX_TEXT;
  165.    ULONG word_index = word_start;
  166.  
  167.    if (is_lower[text[word_start]])
  168.    {
  169.       // first letter lower case
  170.       if (is_reserved_word(text, word_start, word_end, info))
  171.       {
  172.          word_type = SYNTAX_KEYWORD;
  173.       }
  174.       else
  175.       {
  176.          word_type = SYNTAX_TEXT;
  177.          while (word_index < word_end)
  178.          {
  179.             if (is_upper[text[word_index]])
  180.             {
  181.                word_type = SYNTAX_BAD;
  182.             }
  183.             word_index += 1;
  184.          }
  185.       }
  186.    }
  187.    else
  188.    {
  189.       // first letter upper case
  190.       if (is_reserved_name(text, word_start, word_end, info))
  191.       {
  192.          word_type = SYNTAX_RESERVED_WORD;
  193.       }
  194.       else
  195.       {
  196.  
  197.          int lower_count = 0;
  198.          int upper_count = 0;
  199.          word_type = SYNTAX_TYPE;
  200.          word_index += 1;
  201.          while (word_index < word_end)
  202.          {
  203.             if (is_upper[text[word_index]])
  204.             {
  205.                upper_count += 1;
  206.             }
  207.             if (is_lower[text[word_index]])
  208.             {
  209.                lower_count += 1;
  210.             }
  211.             word_index += 1;
  212.          }
  213.          if (lower_count > 0)
  214.          {
  215.             if (upper_count == 0)
  216.             {
  217.                word_type = SYNTAX_CONSTANT;
  218.             }
  219.             else
  220.             {
  221.                word_type = SYNTAX_BAD;
  222.             }
  223.          }
  224.          else
  225.          {
  226.             word_type = SYNTAX_TYPE;
  227.          }
  228.       }
  229.    }
  230.  
  231.    return word_type;
  232. }
  233.  
  234. char *get_word_type_text(int word_type)
  235. {
  236.    char *type_text = "normal";
  237.  
  238.    switch (word_type)
  239.    {
  240.       case SYNTAX_KEYWORD:
  241.          type_text = "keyword";
  242.          break;
  243.       case SYNTAX_RESERVED_WORD:
  244.          type_text = "reserved";
  245.          break;
  246.       case SYNTAX_CONSTANT:
  247.          type_text = "constant";
  248.          break;
  249.       case SYNTAX_TEXT:
  250.          type_text = "variable";
  251.          break;
  252.       case SYNTAX_TYPE:
  253.          type_text = "class";
  254.          break;
  255.    }
  256.  
  257.    return type_text;
  258. }
  259.  
  260.