home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.7z / ftp.whtech.com / emulators / v9t9 / linux / sources / V9t9 / source / lexer.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-10-19  |  2.5 KB  |  166 lines

  1.  
  2. /*
  3.     Lexical analyzer.
  4. */
  5.  
  6. #include <ctype.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. #define __LEXER__
  12. #include "lexer.h"
  13.  
  14. FILE       *istr;
  15.  
  16. #define EOFch '\0'
  17. #define TOKLEN 1024
  18. char        linebuf[TOKLEN], *line;
  19. char        token[TOKLEN];
  20. char        lexerror[TOKLEN];
  21. int         len;
  22. int         pos;
  23. int         linenum;
  24.  
  25. void
  26. lexer_init(FILE * f)
  27. {
  28.     istr = f;
  29.     line = NULL;
  30.     linenum = 0;
  31.     lexer_readToken();
  32. }
  33.  
  34. static char *
  35. lexer_error(char *s, char *v)
  36. {
  37.     if (v == NULL)
  38.         v = tok;
  39.     sprintf(lexerror, "Line %d:  %s ('%s')\n", linenum, s, v);
  40.     return lexerror;
  41. }
  42.  
  43. void
  44. lexer_readALine()
  45. {
  46.     int         i;
  47.  
  48.     if (line = fgets(linebuf, 1024, istr)) {
  49.         pos = 0;
  50.         linenum++;
  51.     }
  52. }
  53.  
  54. char
  55. lexer_readChar()
  56. {
  57.     if (line == NULL || pos >= strlen(line)) {
  58.         lexer_readALine();
  59.         pos = 0;
  60.     }
  61.     if (line == NULL)
  62.         return EOFch;
  63.     return line[pos++];
  64.  
  65. }
  66.  
  67. void
  68. lexer_unreadChar()
  69. {
  70.     pos--;
  71. }
  72.  
  73. int
  74. lexer_isSpace(char c)
  75. {
  76.     return (c == ' ' || c == '\t');
  77. }
  78.  
  79. int
  80. lexer_isTokenChar(char c)
  81. {
  82.     return (c == ',' || c == ';' || c == '\n' || c == '\r' || c == '['
  83.             || c == ']' || c == '#' || c == '=');
  84. }
  85.  
  86. void
  87. lexer_readAToken()
  88. {
  89.     char        c;
  90.     int         string = 0;
  91.  
  92.     len = 0;
  93.  
  94.     do {
  95.         c = lexer_readChar();
  96.     } while (c != EOFch && lexer_isSpace(c));
  97.  
  98.     if (c != EOFch && c == '\"') {
  99.         string = 1;
  100.         c = lexer_readChar();
  101.     }
  102.  
  103.     while (c != EOFch && (string ? c != '\"' : !lexer_isSpace(c))) {
  104.         if (!string && lexer_isTokenChar(c)) {
  105.             if (len == 0)
  106.                 token[len++] = c;
  107.             else
  108.                 lexer_unreadChar();
  109.             token[len] = 0;
  110.             tok = token;
  111.             return;
  112.         } else {
  113.             token[len++] = c;
  114.             c = lexer_readChar();
  115.         }
  116.     }
  117.  
  118.     if (c == EOFch)
  119.         if (len == 0) {
  120.             token[0] = 0;
  121.             tok = token;
  122.             return;
  123.         } else if (len == 1 && lexer_isTokenChar(token[0])) {
  124.             token[len] = 0;
  125.             tok = token;
  126.             return;
  127.         } else;
  128.     else if (!string)
  129.         lexer_unreadChar();
  130.     tok = token;
  131.     token[len] = 0;
  132. }
  133.  
  134. void
  135. lexer_readToken()
  136. {
  137.     while (tok == NULL) {
  138.         lexer_readAToken();
  139.     }
  140. }
  141.  
  142. void
  143. lexer_consumeToken()
  144. {
  145.     if (tok == NULL) {
  146.         lexer_readToken();
  147.         if (tok == NULL)
  148.             logger(LOG_COMMANDS | LOG_FATAL,
  149.                    (lexer_error("Reached premature end-of-file", NULL));
  150.                    } else
  151.                    lexer_readToken();
  152.                    tok = NULL;
  153.                    lexer_readToken();
  154.                    }
  155.  
  156.                    int lexer_compareToken(char *t) {
  157.                    lexer_readToken();
  158.                    if (strcmp(t, tok) == 0) {
  159.                    lexer_consumeToken(); lexer_readToken(); return 1;}
  160.                    else
  161.                    return 0;}
  162.  
  163.                    void lexer_matchToken(char *t, char *s) {
  164.                    if (!lexer_compareToken(t))
  165.                    logger(LOG_FATAL | LOG_COMMANDS, (lexer_error(s, tok));}
  166.