home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MODEM / UWPC201.ZIP / UW-SRC.ZIP / TERMCC.L < prev    next >
Encoding:
Text File  |  1991-07-25  |  7.3 KB  |  258 lines

  1. /*-------------------------------------------------------------------------
  2.  
  3.   TERMCC.L - Lexical analysis declarations for the Termcap Compiler.
  4.  
  5.     This file is part of the Termcap Compiler source code.
  6.     Copyright (C) 1990-1991  Rhys Weatherley
  7.  
  8.     This program is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation; either version 1, or (at your option)
  11.     any later version.
  12.  
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.   Revision History:
  23.   ================
  24.  
  25.    Version  DD/MM/YY  By  Description
  26.    -------  --------  --  --------------------------------------
  27.      1.0    23/03/91  RW  Original Version of TERMCC.L
  28.      1.1    25/01/91  RW  Add more instructions to support ANSI
  29.      1.2    25/07/91  RW  Added the "client" instruction
  30.  
  31.   You may contact the author by:
  32.   =============================
  33.  
  34.    e-mail: rhys@cs.uq.oz.au
  35.      mail: Rhys Weatherley
  36.        5 Horizon Drive
  37.        Jamboree Heights
  38.        Queensland 4074
  39.        Australia
  40.  
  41. -------------------------------------------------------------------------*/
  42.  
  43. /* YYSTYPE is the semantic type to use in parsing */
  44.  
  45. %{
  46.  
  47. #include <stdio.h>
  48. #include <ctype.h>
  49. #include "y_tab.h"
  50. #include "symbols.h"
  51.  
  52. #define    DEF_CHAR_CONST    '\0'    /* Default character constant on error */
  53.  
  54. extern    YYSTYPE    yylval;    /* Semantic value of current lexical symbol */
  55. int    lexline=1;    /* Current line in the input file */
  56.  
  57. %}
  58.  
  59. %%
  60. \/\/.*            /* Comment - ignore it (C++ comments) */
  61. send            return (CMD_SEND);
  62. send52            return (CMD_SEND52);
  63. cr            return (CMD_CR);
  64. lf            return (CMD_LF);
  65. bs            return (CMD_BS);
  66. tab            return (CMD_TAB);
  67. bell            return (CMD_BELL);
  68. bswrap            return (CMD_BSWRAP);
  69. move            return (CMD_MOVE);
  70. clear            return (CMD_CLEAR);
  71. clreol            return (CMD_CLREOL);
  72. clreos            return (CMD_CLREOS);
  73. clrsol            return (CMD_CLRSOL);
  74. clrsos            return (CMD_CLRSOS);
  75. insline            return (CMD_INSLINE);
  76. delline            return (CMD_DELLINE);
  77. inschar            return (CMD_INSCHAR);
  78. delchar            return (CMD_DELCHAR);
  79. setattr            return (CMD_SETATTR);
  80. setscrl            return (CMD_SETSCRL);
  81. getch            return (CMD_GETCH);
  82. setx            return (CMD_SETX);
  83. sety            return (CMD_SETY);
  84. load            return (CMD_LOAD);
  85. add            return (CMD_ADD);
  86. sub            return (CMD_SUB);
  87. cmp            return (CMD_CMP);
  88. switch            return (CMD_SWITCH);
  89. endsw            return (CMD_ENDSW);
  90. je            return (CMD_JE);
  91. jne            return (CMD_JNE);
  92. ja            return (CMD_JA);
  93. jae            return (CMD_JAE);
  94. jb            return (CMD_JB);
  95. jbe            return (CMD_JBE);
  96. jmp            return (CMD_JMP);
  97. jsr            return (CMD_JSR);
  98. ret            return (CMD_RET);
  99. remote            return (CMD_REMOTE);
  100. escape            return (CMD_ESCAPE);
  101. savexy            return (CMD_SAVEXY);
  102. restxy            return (CMD_RESTXY);
  103. getxy            return (CMD_GETXY);
  104. getx            return (CMD_GETX);
  105. gety            return (CMD_GETY);
  106. scrlup            return (CMD_SCRLUP);
  107. scrldn            return (CMD_SCRLDN);
  108. width            return (VAL_WIDTH);
  109. height            return (VAL_HEIGHT);
  110. set            return (CMD_SET);
  111. reset            return (CMD_RESET);
  112. test            return (CMD_TEST);
  113. name            return (CMD_NAME);
  114. key            return (CMD_KEY);
  115. endkeys            return (CMD_ENDKEYS);
  116. resarr            return (CMD_RESARR);
  117. getarg            return (CMD_GETARG);
  118. geta            return (CMD_GETA);
  119. dec            return (CMD_DEC);
  120. shift            return (CMD_SHIFT);
  121. setc            return (CMD_SETC);
  122. saveattr        return (CMD_SAVEATTR);
  123. restattr        return (CMD_RESTATTR);
  124. insblank        return (CMD_INSBLANK);
  125. client            return (CMD_CLIENT);
  126. [a-z_A-Z][a-z_A-Z0-9]*    { if ((yylval = addident (yytext)) == -1)
  127.                 parsefatal ("Out of symbol table space");
  128.                else
  129.                 return (IDENT);
  130.             }
  131. \"[^\"\n]*[\"\n]    { if (!lexstring ('"'))
  132.                 parserr ("String is too small");
  133.                else if ((yylval = addident (yytext)) == -1)
  134.                 parsefatal ("Out of symbol table space");
  135.                else
  136.                 return (STRING);
  137.             }
  138. '[^'\n]*['\n]        yylval = lexchar () & 255; return (NUMBER);
  139. 0[0-7]+            yylval = lexnoct (); return (NUMBER);
  140. 0x[0-9A-Fa-f]+        yylval = lexnhex (); return (NUMBER);
  141. [0-9]+            yylval = atoi (yytext); return (NUMBER);
  142. ,            return (COMMA);
  143. ;            return (SEMI);
  144. :            return (COLON);
  145. [ \t\r]            /* Ignore white space */
  146. \n            ++lexline;    /* Update the line number */
  147. .            parserr ("Illegal character");
  148. %%
  149.  
  150. /* Initialise the lexical analysis for a file that is not stdin */
  151. void    initlex (file)
  152. FILE    *file;
  153. {
  154.   lexline = 1;        /* Send the line counter back to the start */
  155.   yyrestart (file);    /* Restart the lexical analyser */
  156. } /* initlex */
  157.  
  158. /* Translate an escape character code into an actual code */
  159. static    int    lexesc (ch)
  160. char    ch;
  161. {
  162.   switch (ch)
  163.     {
  164.       case 'n': return ('\n');
  165.       case 'r': return ('\r');
  166.       case 'b': return ('\b');
  167.       case 't': return ('\t');
  168.       case '0': return ('\0');
  169.       default:  return (ch);
  170.     } /* switch */
  171. } /* lexesc */
  172.  
  173. /* Translate an octal character code into character */
  174. static    int    lexoct (posn)
  175. int    posn;
  176. {
  177.   return (((yytext[posn] - '0') << 6) |
  178.           ((yytext[posn + 1] - '0') << 3) |
  179.        (yytext[posn + 2] - '0'));
  180. } /* lexoct */
  181.  
  182. /* Analyse the string in 'yytext' to squash it */
  183. /* Returns the length of the resultant string. */
  184. static    int    lexstring (term)
  185. char    term;
  186. {
  187.   int posn=1;        /* start at 1 to skip initial '"' */
  188.   int squash=0;        /* Where to squash string into */
  189.   char ch;
  190.   while ((ch = yytext[posn]) != term && ch != '\n')
  191.     {
  192.       if (ch == '\\')    /* Escaped character? */
  193.         {
  194.       if (isdigit (yytext[posn + 1]))
  195.         {
  196.           if (isdigit (yytext[posn + 2]) &&
  197.               isdigit (yytext[posn + 3]))
  198.         {
  199.           yytext[squash++] = lexoct (posn + 1);
  200.           posn += 3;
  201.         } /* then */
  202.            else
  203.             yytext[squash++] = lexesc (yytext[++posn]);
  204.         } /* then */
  205.        else
  206.         yytext[squash++] = lexesc (yytext[++posn]);
  207.     } /* then */
  208.        else
  209.         yytext[squash++] = ch;
  210.       ++posn;
  211.     } /* while */
  212.   if (ch != term)
  213.     parserr ("Unterminated string constant");
  214.   yytext[squash] = '\0';
  215.   return (squash);    /* Return the length of the string */
  216. } /* lexstring */
  217.  
  218. /* Analyse the '-delimited string in yytext to get a character */
  219. static    int    lexchar ()
  220. {
  221.   if (lexstring ('\'') != 1)
  222.     {
  223.       parserr ("Character constant too long");
  224.       return (DEF_CHAR_CONST);    /* Default character constant */
  225.     }
  226.    else
  227.     return (yytext[0]);        /* Return the character at string start */
  228. }
  229.  
  230. /* Convert an octal number into decimal and return it */
  231. static    int    lexnoct ()
  232. {
  233.   int number,index;
  234.   number = 0;
  235.   index = 0;
  236.   while (yytext[index])
  237.     number = (number * 8) + (yytext[index++] - '0');
  238.   return (number);
  239. }
  240.  
  241. /* Convert a hexadecimal number (starting with "0x") */
  242. /* into decimal and return it.                 */
  243. static    int    lexnhex ()
  244. {
  245.   int number,index;
  246.   number = 0;
  247.   index = 2;
  248.   while (yytext[index])
  249.     {
  250.       number *= 16;
  251.       if (isdigit (yytext[index]))
  252.         number += yytext[index++] - '0';
  253.        else
  254.         number += ((yytext[index++] - 'A') & 0x0F) + 10;
  255.     }
  256.   return (number);
  257. }
  258.