home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 355_02 / slk2.exe / SPP / RES.C < prev    next >
C/C++ Source or Header  |  1991-06-09  |  7KB  |  399 lines

  1. /*
  2.     Sherlock Preprocessor -- reserved word handling.
  3.  
  4.     Source:  res.c
  5.     Started: September 28, 1987; June 9, 1991.
  6.     Version: March 25, 1988; April 5, 1988
  7.         April 16, 1990 Fixed bug in is_reserved.
  8.  
  9.  
  10.     PUBLIC DOMAIN SOFTWARE
  11.  
  12.     Sherlock, including the SPP, SDEL and SDIF programs, was placed in
  13.     the public domain on June 15, 1991, by its author,
  14.  
  15.         Edward K. Ream
  16.         166 North Prospect Ave.
  17.         Madison, WI 53705.
  18.         (608) 257-0802
  19.  
  20.     Sherlock may be used for any commercial or non-commercial purpose.
  21.  
  22.  
  23.     DISCLAIMER OF WARRANTIES
  24.  
  25.     Edward K. Ream (Ream) specifically disclaims all warranties,
  26.     expressed or implied, with respect to this computer software,
  27.     including but not limited to implied warranties of merchantability
  28.     and fitness for a particular purpose.  In no event shall Ream be
  29.     liable for any loss of profit or any commercial damage, including
  30.     but not limited to special, incidental consequential or other damages.
  31.     
  32. */
  33.  
  34. #include "spp.h"
  35.  
  36. /*
  37.     Define the reserved word tables using macros.
  38. */
  39.  
  40. static struct entry_struct {
  41.     char *    e_string;
  42.     int    e_arg1;        /* en_token value. */
  43. };
  44.  
  45. static struct tab_struct {
  46.     struct entry_struct * t_entry;
  47.     short t_strlen;
  48.     short t_entries;
  49. };
  50.  
  51. #define table(name, n) struct tab_struct FAR name [n+1] = {
  52. #define sub_tab(name, str_len, n_entries) {name, str_len, n_entries}
  53. #define end_tab {0L, 0}}
  54.  
  55. #define header(label,n_entries) struct entry_struct FAR label [n_entries] = {
  56. #define key(string,val)    {string,val}
  57. #define end_h         }
  58.  
  59. /*
  60.     Reserved word tables (One entry per reserved word.)
  61.  
  62.     Each table is made up of zero or more subtables,
  63.     one subtable for each length.
  64.     Subtables are arranged in increasing length.
  65.     Within each subtable, entries are arranged alphabetically.
  66. */
  67.  
  68. /* ----- A TABLE ----- */
  69. #define a4_n 1
  70.     header(a4_tab, a4_n)
  71.     key("auto", K_AUTO)
  72.     end_h;
  73.  
  74.     table(a_tab, 1)
  75.     sub_tab(a4_tab, 4, a4_n),
  76.     end_tab;
  77.  
  78. /* ----- B TABLE ----- */
  79. #define b5_n 1
  80.     header(b5_tab, b5_n)
  81.     key("break", K_BREAK)
  82.     end_h;
  83.  
  84.     table(b_tab, 1)
  85.     sub_tab(b5_tab, 5, b5_n),
  86.     end_tab;
  87.  
  88. /* ----- C TABLE ------ */
  89. #define c4_n 2
  90.     header(c4_tab, c4_n)
  91.     key("case", K_CASE),
  92.     key("char", K_CHAR),
  93.     end_h;
  94.  
  95. #define c5_n 1
  96.     header(c5_tab, c5_n)
  97.     key("const", K_CONST)
  98.     end_h;
  99.  
  100. #define c8_n 1
  101.     header(c8_tab, c8_n)
  102.     key("continue", K_CONTINUE)
  103.     end_h;
  104.  
  105.     table(c_tab, 3)
  106.     sub_tab(c4_tab, 4, c4_n),
  107.     sub_tab(c5_tab, 5, c5_n),
  108.     sub_tab(c8_tab, 8, c8_n),
  109.     end_tab;
  110.  
  111. /* ----- D TABLE ----- */
  112. #define d2_n 1
  113.     header(d2_tab, d2_n)
  114.     key("do", K_DO),
  115.     end_h;
  116.  
  117. #define d6_n 1
  118.     header(d6_tab, d6_n)
  119.     key("double", K_DOUBLE)
  120.     end_h;
  121.  
  122. #define d7_n 1
  123.     header(d7_tab, d7_n)
  124.     key("default", K_DEFAULT)
  125.     end_h;
  126.  
  127.     table(d_tab, 3)
  128.     sub_tab(d2_tab, 2, d2_n),
  129.     sub_tab(d6_tab, 6, d6_n),
  130.     sub_tab(d7_tab, 7, d7_n),
  131.     end_tab;
  132.  
  133. /* ----- E TABLE ----- */
  134. #define e4_n 2
  135.     header(e4_tab, e4_n)
  136.     key("else", K_ELSE),
  137.     key("enum", K_ENUM),
  138.     end_h;
  139.  
  140. #define e5_n 1
  141.     header(e5_tab, e5_n)
  142.     key("entry", K_ENTRY)
  143.     end_h;
  144.  
  145. #define e6_n 1
  146.     header(e6_tab, e6_n)
  147.     key("extern", K_EXTERN)
  148.     end_h;
  149.  
  150.     table(e_tab, 3)
  151.     sub_tab(e4_tab, 4, e4_n),
  152.     sub_tab(e5_tab, 5, e5_n),
  153.     sub_tab(e6_tab, 6, e6_n),
  154.     end_tab;
  155.  
  156. /* ----- F TABLE ----- */
  157. #define f3_n 1
  158.     header(f3_tab, f3_n)
  159.     key("for", K_FOR)
  160.     end_h;
  161.  
  162. #define f5_n 1
  163.     header(f5_tab, f5_n)
  164.     key("float", K_FLOAT)
  165.     end_h;
  166.  
  167.     table(f_tab, 2)
  168.     sub_tab(f3_tab, 3, f3_n),
  169.     sub_tab(f5_tab, 5, f5_n),
  170.     end_tab;
  171.  
  172. /* ----- G TABLE ----- */
  173. #define g4_n 1
  174.     header(g4_tab, g4_n)
  175.     key("goto", K_GOTO)
  176.     end_h;
  177.  
  178.     table(g_tab, 1)
  179.     sub_tab(g4_tab, 4, g4_n),
  180.     end_tab;
  181.  
  182. /* ----- I TABLE ----- */
  183. #define i2_n 1
  184.     header(i2_tab, i2_n)
  185.     key("if", K_IF)
  186.     end_h;
  187.  
  188. #define i3_n 1
  189.     header(i3_tab, i3_n)
  190.     key("int", K_INT)
  191.     end_h;
  192.  
  193.     table(i_tab, 2)
  194.     sub_tab(i2_tab, 2, i2_n),
  195.     sub_tab(i3_tab, 3, i3_n),
  196.     end_tab;
  197.  
  198. /* ----- L TABLE ----- */
  199. #define l4_n 1
  200.     header(l4_tab, l4_n)
  201.     key("long", K_LONG)
  202.     end_h;
  203.  
  204.     table(l_tab, 1)
  205.     sub_tab(l4_tab, 4, l4_n),
  206.     end_tab;
  207.  
  208. /* ----- N TABLE ----- */
  209. #define n7_n 1
  210.     header(n7_tab, n7_n)
  211.     key("noalias", K_NOALIAS)
  212.     end_h;
  213.  
  214.     table(n_tab, 1)
  215.     sub_tab(n7_tab, 7, n7_n),
  216.     end_tab;
  217.  
  218. /* ----- R TABLE ----- */
  219. #define r6_n 1
  220.     header(r6_tab, r6_n)
  221.     key("return", K_RETURN)
  222.     end_h;
  223.  
  224. #define r8_n 1
  225.     header(r8_tab, r8_n)
  226.     key("register", K_REGISTER)
  227.     end_h;
  228.  
  229.     table(r_tab, 2)
  230.     sub_tab(r6_tab, 6, r6_n),
  231.     sub_tab(r8_tab, 8, r8_n),
  232.     end_tab;
  233.  
  234. /* ----- S TABLE ----- */
  235. #define s5_n 1
  236.     header(s5_tab, s5_n)
  237.     key("short", K_SHORT),
  238.     end_h;
  239.  
  240. #define s6_n 5
  241.     header(s6_tab, s6_n)
  242.     key("signed", K_SIGNED),
  243.     key("sizeof", K_SIZEOF),
  244.     key("static", K_STATIC),
  245.     key("struct", K_STRUCT),
  246.     key("switch", K_SWITCH)
  247.     end_h;
  248.  
  249.     table(s_tab, 2)
  250.     sub_tab(s5_tab, 5, s5_n),
  251.     sub_tab(s6_tab, 6, s6_n),
  252.     end_tab;
  253.  
  254. /* ----- T TABLE ----- */
  255. #define t7_n 1
  256.     header(t7_tab, t7_n)
  257.     key("typedef", K_TYPEDEF)
  258.     end_h;
  259.  
  260.     table(t_tab, 1)
  261.     sub_tab(t7_tab, 7, t7_n),
  262.     end_tab;
  263.  
  264. /* ----- U TABLE ----- */
  265. #define u5_n 1
  266.     header(u5_tab, u5_n)
  267.     key("union", K_UNION)
  268.     end_h;
  269.  
  270. #define u8_n 1
  271.     header(u8_tab, u8_n)
  272.     key("unsigned", K_UNSIGNED)
  273.     end_h;
  274.  
  275.     table(u_tab, 2)
  276.     sub_tab(u5_tab, 5, u5_n),
  277.     sub_tab(u8_tab, 8, u8_n),
  278.     end_tab;
  279.  
  280. /* ----- V TABLE ----- */
  281. #define v4_n 1
  282.     header(v4_tab, v4_n)
  283.     key("void", K_VOID)
  284.     end_h;
  285.  
  286. #define v8_n 1
  287.     header(v8_tab, v8_n)
  288.     key("volatile", K_VOLATILE)
  289.     end_h;
  290.  
  291.     table(v_tab, 2)
  292.     sub_tab(v4_tab, 4, v4_n),
  293.     sub_tab(v8_tab, 8, v8_n),
  294.     end_tab;
  295.  
  296. /* ----- W TABLE ----- */
  297. #define w5_n 1
  298.     header(w5_tab, w5_n)
  299.     key("while", K_WHILE)
  300.     end_h;
  301.  
  302.     table(w_tab, 1)
  303.     sub_tab(w5_tab, 5, w5_n),
  304.     end_tab;
  305.  
  306. /*
  307.     Indirect reserved word table
  308.     WARNING:  this used to be offset from 'A' instead of 'a'.
  309. */
  310. struct tab_struct * rwx_tab [] = {
  311.  
  312.     a_tab,
  313.     b_tab,
  314.     c_tab,
  315.     d_tab,
  316.     e_tab,
  317.     f_tab,
  318.     g_tab,
  319.     0L,    /* h */
  320.     i_tab,
  321.     0L,    /* j */
  322.     0L,    /* k */
  323.     l_tab,
  324.     0L,    /* m */
  325.     n_tab,
  326.     0L,    /* o */
  327.     0L,    /* p */
  328.     0L,    /* q */
  329.     r_tab,
  330.     s_tab,
  331.     t_tab,
  332.     u_tab,
  333.     v_tab,
  334.     w_tab,
  335.     0L,    /* x */
  336.     0L,    /* y */
  337.     0L    /* z */
  338. };
  339.  
  340. /*
  341.     See if name[] contains a reserved word.
  342.     Return the token or NULL_TOK if not found.
  343. */
  344. en_tokens
  345. is_reserved(register char *name, int length)
  346. {
  347.     char c;
  348.     register struct tab_struct   * p;
  349.     register struct entry_struct * e;
  350.     register int i, n;
  351.  
  352.     TRACEP("is_reserved", printf("name: %s, length %d\n", name, length));
  353.  
  354.     c = *name;
  355.     if (c < 'a' || c > 'z') {
  356.         goto not_found;
  357.     }
  358.  
  359.     /* Search for the proper subtable. */
  360.     for (p = rwx_tab [c - 'a']; ; p++) {
  361.  
  362.         TRACEP("is_reserved", printf("p = %lx\n", p));
  363.         /* 6/9/91 */
  364.         if (p == NULL || p -> t_entry == NULL) {
  365.             goto not_found;
  366.         }
  367.  
  368.         TRACEP("is_reserved",
  369.             printf("p -> t_strlen = %d\n", p -> t_strlen));
  370.  
  371.         if (p -> t_strlen > length) {
  372.             goto not_found;
  373.         }
  374.         else if (p -> t_strlen == length) {
  375.             break;
  376.         }
  377.     }
  378.  
  379.     /* A subtable with the right length has been found. */
  380.     n = p  -> t_entries;
  381.     for (i = 0, e = p  -> t_entry; i < n; e++, i++) {
  382.  
  383.         TRACEP("is_reserved",
  384.             printf("compare: %s, %s\n", name, e -> e_string));
  385.  
  386.         if (str_eq(name, e -> e_string)) {
  387.  
  388.             /* Found. */
  389.             token    = ((int) (e -> e_arg1)) & 0xff;
  390.             TRACEP("is_reserved", printf("returns %d\n", token));
  391.             return token;
  392.         }
  393.     }
  394.  
  395. not_found:
  396.     TRACEP("is_reserved_ret", printf("ISR: returns 0\n"));
  397.     return NULL_TOK;
  398. }
  399.