home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume18 / notation / part01 / lexer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-13  |  2.1 KB  |  118 lines

  1. #include <stdio.h>
  2.  
  3. #include "chesstype.h"
  4. #include "notation.h"
  5. #include "lexer.h"
  6.  
  7. /*token */
  8. static char curtok[MAXTOKLEN];
  9.  
  10. extern FILE * infile ;
  11.  
  12. /* commentary level 
  13.  act as a boolean too */
  14. static int comment = FALSE;
  15.  
  16.  
  17. static char nextchar()
  18. {
  19.   if (!feof(infile))
  20.     return(getc(infile));
  21.   else
  22.     return('\0');
  23. }
  24.  
  25.  
  26.  
  27. /* this function returns the next token:
  28.    Definition of a token:
  29.    a number [0-9]*. ie 1. ,  35. 
  30.    a string terminated by a blank or rc or "." or tab
  31.    commentary enclosed in 90 or [] are skipped
  32.    Each move must not have a blank inside ie exc6 legal,
  33.    but not e x c6
  34.    */
  35. int nexttoken()
  36. {
  37.   int i =0;
  38.   char c;
  39.   
  40.   /*(void) fprintf(stdout, "nexttoken\n");*/
  41.   curtok[0]= '\0' ;
  42.   c = nextchar();
  43.   while ((c == ' ') || (c == '\n') || (c == '\t') || (c== '.') || 
  44.      ( c== '[') || (c =='(' ) ||  comment ) {
  45.     /*(void) fprintf(stdout, "=%c",c);*/
  46.     switch (c) {
  47.     case '[':
  48.     case '(':
  49.       comment++;
  50.       break;
  51.     case ']':
  52.     case ')':
  53.       comment--;
  54.       break;
  55.     default:
  56.       break;
  57.     }
  58.     c = nextchar();
  59.   }
  60.  
  61.   if (c == '\0') return (FALSE);
  62.  
  63.   while ( c != ' ' && c != '\n' &&  c != '\t' &&
  64.      c != '.'  && c != '\0' && c != '[' && c!= '(' &&
  65.      i < MAXTOKLEN ) {
  66.     curtok[i++] = c ;
  67.     c = nextchar();
  68.   }
  69.   if ( c== '.' )
  70.     curtok[i++] = c ;
  71.   
  72.   curtok[i] = '\0' ;
  73.   if ( c == '[' || c == '(' )
  74.     comment++;
  75.  
  76.   if (c == '\0') return (FALSE);
  77.     
  78.   /*(void) fprintf(stdout,"---%s---\n",curtok);*/
  79.   return(TRUE);
  80. }
  81.  
  82.  
  83. /* this function parse the current token */
  84. int parsetoken()
  85. {
  86.   int i;
  87.   register int c;
  88.  
  89.  
  90.   /* look for keyword */
  91.   if (curtok[0] == '@') /* keyword case */ 
  92.     return(parse_keyword(curtok));
  93.   
  94.   /* look for roque */
  95.   if (parse_roque(curtok))
  96.     return(TRUE);
  97.  
  98.   /* is it a move numbering ? */
  99.   if ((curtok[0] >= '0') && (curtok[0] <= '9' )) {
  100.     return(parse_number(curtok) );
  101.   }
  102.  
  103.   /* last option: it is a move 
  104.      Syntax of a move:
  105.      [] denote a range]
  106.      () denote a 0/1 (absence/presence)
  107.      
  108.      ((Piece Name)[a-h]([1-8])[x-])(PieceName)[a-h][1-8]
  109.  
  110.      We use an automata here.
  111.      */
  112.   parse_move(curtok);
  113.  
  114.   
  115.   return(TRUE);
  116. }
  117.  
  118.