home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gwm18a.zip / wool.lex < prev    next >
Text File  |  1995-07-03  |  7KB  |  292 lines

  1. /* Copyright 1989 GROUPE BULL -- See license conditions in file COPYRIGHT
  2. ** Copyright 1989 Massachusetts Institute of Technology
  3. **/
  4. /****************************\
  5. *                  *
  6. *  Lex generator for Wool   *
  7. *                  *
  8. \****************************/
  9.  
  10. %{
  11.  
  12. /* to have always at least MAX_TOKEN_LENGTH chars in all lexes */
  13. #undef YYLMAX
  14. #define YYLMAX MAX_TOKEN_LENGTH
  15.  
  16. /* here goes the definition of lex I/O, as macros for efficiency */
  17.  
  18. /* first, we forget the previous definitions, */
  19. #undef output
  20. #undef input
  21. #undef unput
  22. /* which were :
  23. #define output(c) putc(c,yyout)
  24. #define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):\
  25. getc(yyin))==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
  26. # define unput(c) {yytchar= (c);if(yytchar=='\n')yylineno--;\
  27. *yysptr++=yytchar;}
  28. */
  29.  
  30. /* So here are OUR macros */
  31.  
  32. static int yyin_is_string;    /* 0 ==> reading from STREAM yyin */
  33.                 /* 1 ==> reading from STRING yystrin */
  34. static int yyout_is_string;    /* 0 ==> printing on STREAM yyout */
  35.         /* 1 ==> printing on WOOL_STRING_STREAM yystrout */
  36. static char *yystrin;        /* current pointer on input string */
  37. static WOOL_STRING_STREAM yystrout; /* current wool output stream */
  38.  
  39. #define output(c) {\
  40.     if(yyout_is_string){\
  41.         *(yystrout->ptr)++ = c;\
  42.         *(yystrout->ptr) = '\0';\
  43.         if((yystrout->ptr) >= (yystrout->last)){\
  44.         yyoutflush();\
  45.     }\
  46.     }else{\
  47.     putc(c,yyout);\
  48.     }\
  49. }
  50.  
  51. #define input() (((\
  52.         yytchar =\
  53.         (yysptr>yysbuf ?\
  54.         U(*--yysptr)\
  55.     :   (yyin_is_string ?\
  56.             ( *yystrin ? *yystrin++ : 0)\
  57.         :   getc(yyin))))\
  58.     ==10 ?\
  59.         (yylineno++,yytchar)\
  60.     :   yytchar)\
  61.     ==EOF ?\
  62.         0\
  63.     :   yytchar)
  64.  
  65. #define unput(c) {\
  66.     yytchar= (c);\
  67.     if(yytchar=='\n')\
  68.         yylineno--;\
  69.     *yysptr++=yytchar;\
  70. }
  71.  
  72. /* externally callable function for unput:
  73.  */
  74.  
  75. wool_unput(buffer)
  76. char *buffer;
  77. {
  78.     while (*buffer) {
  79.     unput(*buffer);
  80.     buffer++;
  81.     }
  82. }
  83.  
  84. /* counting the parentheses -- hack for wool_pool */
  85.  
  86. #define INC_PAR wool_pool_parentheses_level++
  87. #define DEC_PAR if(wool_pool_parentheses_level)wool_pool_parentheses_level--
  88. %}
  89.  
  90. FNCP    [^-+\004 \t\n"'()0-9]
  91. FNC    [^\004 \t\n"'()0-9]
  92. NC    [^\004 \t\n"'()]
  93. SIGN    [-+]
  94.  
  95. %%
  96. .[\010]        ;                /* handles backspacing */
  97. .[\177]        ;                /* handles deleting */
  98. \"([^\\\"\n]|\\(.|\n)|\"\")*\"  return(STRING); /* strings */
  99. \"([^\\\"\n]|\\(.|\n)|\"\")*\n  return(NON_CLOSED_STRING); /* error */
  100. \;.*\n        ;                /* comments */
  101. [-+]?[0-9]+    return(NUMBER);            /* integers */
  102. 0[xX][0-9a-fA-F]+    return(HEX_NUMBER);    /* hex integers */
  103. "("        {INC_PAR; return(LEFTPAR);}    /* start of list */
  104. ")"        {DEC_PAR; return(RIGHTPAR);}    /* end of list */
  105. "'"        return(QUOTECHAR);        /* quoting */
  106. "{"        {INC_PAR; return(LEFTBRA);}    /* (progn */
  107. "}"        {DEC_PAR; return(RIGHTBRA);}    /* ) */
  108. {FNCP}{NC}*    return (NAME);            /* identifier */
  109. {SIGN}{FNC}{NC}*    return (NAME);        /* +foo */
  110. {SIGN}        return (NAME);            /* + */
  111. [ \t\n]        ;                /* blanks */
  112. [\004]        return(END_OF_FILE);        /* pseudo-EOF handling */
  113. .        ;                /* skip over control codes */
  114. %%
  115.  
  116. /**********************\
  117. *                *
  118. *  WOOL's I/O package  *
  119. *                *
  120. \**********************/
  121.  
  122. /* 
  123.  * yywrap
  124.  * we treat EOF (or EOString) as a TOKEN, for yacc.
  125.  */
  126.  
  127. yywrap(){        /* to make EOF a "normal" character */
  128.     unput('\004');    /* EOF is pushed back on the input as ^D */
  129.     return 0;        /* tell Lex there is more to read */
  130. }
  131.  
  132. /*
  133.  * yyinflush
  134.  * to flush wool input buffers (i.e. the unput buffer)
  135.  * stores old input buffer in arg (should be ~ 16) if not NULL
  136.  */
  137.  
  138. void
  139. yyinflush(wool_old_unput_buffer)
  140. char *wool_old_unput_buffer;
  141. {
  142.     if (wool_old_unput_buffer) {
  143.     if (yysptr != yysbuf)
  144.         strncpy(wool_old_unput_buffer, yysbuf, yysptr - yysbuf);
  145.     wool_old_unput_buffer[yysptr - yysbuf] = '\0';
  146.     }
  147.     yysptr = yysbuf;
  148. }
  149.  
  150. /*
  151.  * yyoutflush
  152.  * to flush wool output buffer.
  153.  */
  154.  
  155. void
  156. yyoutflush(){
  157.     if(yyout_is_string){
  158.     ASSERT(yystrout->overflow_handler);
  159. /*XXX-UWE-XXX*/
  160.     (*(yystrout->overflow_handler))(yystrout);
  161.     /* yystrout->ptr = yystrout-> buffer; */
  162. /*XXX-UWE-XXX*/
  163.     }else{
  164.     fflush(yyout);
  165.     }
  166. }
  167.  
  168. /*
  169.  * wool_input_redirect
  170.  * to set wool's parsing to the stream or string argument.
  171.  * arg1 = type (0 = string, 1 = stream);
  172.  * arg2 = stream or string
  173.  * arg3 = POINTER to (FILE *) or (char *) where will go the old stream
  174.  *        (if NULL not set)(returns the old type)
  175.  * arg4 = where to save contents of unput buffer (if not NULL)
  176.  */
  177.  
  178. int wool_input_redirect(type, stream, oldstream_p, old_buffer_contents)
  179. int type;
  180. char *stream;
  181. char **oldstream_p;
  182. char *old_buffer_contents;
  183. {
  184.     int oldtype = yyin_is_string;
  185.     if(oldstream_p) *oldstream_p =
  186.         (oldtype ? (char *) yystrin : (char *) yyin);
  187.     yyinflush(old_buffer_contents);
  188.     if(yyin_is_string = type){
  189.     yystrin = stream;
  190.     }else{
  191.     yyin = (FILE *) stream;
  192.     }
  193.     return oldtype;
  194. }
  195.  
  196. /*
  197.  * wool_output_redirect
  198.  * to set wool's outputs to the stream or string argument.
  199.  * arg1 = type (0 = string, 1 = stream);
  200.  * arg2 = stream or string
  201.  * arg4 = POINTER to (FILE *) or WOOL_STRING_STREAM tu put the old stream
  202.  *        (if NULL not set)(returns the old type)
  203.  */
  204.  
  205. wool_output_redirect(type, stream, oldstream_p)
  206. int type;
  207. char *stream;
  208. char **oldstream_p;
  209. {
  210.     int oldtype = yyout_is_string;
  211.     if(oldstream_p) *oldstream_p =
  212.         (oldtype ? (char *) yystrout : (char *) yyout);
  213.     yyoutflush();
  214.     if(yyout_is_string = type){
  215.     yystrout = (WOOL_STRING_STREAM) stream;
  216.     }else{
  217.     yyout = (FILE *) stream;
  218.     }
  219.     return oldtype;
  220. }
  221.  
  222. /*
  223.  * now, some functions to provide a printf - like service on wool's output.
  224.  */
  225.  
  226. /* prints a string */
  227.  
  228. void
  229. wool_puts(string)
  230. register char *string;
  231. {
  232.     while(*string) output(*string++);
  233. }
  234.  
  235. /* put a newline */
  236.  
  237. wool_newline()
  238. {
  239.     output('\n');
  240. }
  241.  
  242. /* does a format with ONE arg. */
  243.  
  244. wool_printf(string, arg)
  245. register char *string;
  246. char *arg;
  247. {
  248.     static char wool_temp_string[MAX_TEMP_STRING_SIZE];
  249.     sprintf(wool_temp_string, string, arg);
  250.     wool_puts(wool_temp_string);
  251. }
  252.  
  253. /* prints a char */
  254.  
  255. void
  256. wool_putchar(c)
  257. char c;
  258. {
  259.     output(c);
  260. }
  261.  
  262. /*
  263.  * function to make a WOOL_STRING_STREAM of a given capacity
  264.  * arg1 = capactity in bytes
  265.  * arg2 = user function to be called on buffer when overflow occurs
  266.  */
  267.  
  268. WOOL_STRING_STREAM WOOL_STRING_STREAM_make(nbytes, handler)
  269. int nbytes;
  270. int (* handler)();
  271. {
  272.     WOOL_STRING_STREAM str = (WOOL_STRING_STREAM)
  273.         Malloc(sizeof(struct _WOOL_STRING_STREAM));
  274.     str->buffer = (char *) Malloc(nbytes);
  275.     *str->buffer = '\0';        /*XXX-UWE-XXX*/
  276.     str->ptr = str->buffer;
  277.     str->last = str->buffer + nbytes -1;
  278.     str->overflow_handler = handler;
  279.     return str;
  280. }
  281.  
  282. /* 
  283.  * and the function to free a stream
  284.  */
  285.  
  286. WOOL_STRING_STREAM_free (str)
  287. WOOL_STRING_STREAM str;
  288. {
  289.     Free(str->buffer);
  290.     Free(str);
  291. }
  292.