home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gwm18a.zip / wool.flex < prev    next >
Text File  |  1995-07-03  |  7KB  |  293 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 YY_INPUT
  21. #undef ECHO
  22.  
  23. /* So here are OUR macros */
  24.  
  25. static int yyin_is_string;    /* 0 ==> reading from STREAM yyin */
  26.                 /* 1 ==> reading from STRING yystrin */
  27. static int yyout_is_string;    /* 0 ==> printing on STREAM yyout */
  28.         /* 1 ==> printing on WOOL_STRING_STREAM yystrout */
  29. static char *yystrin;        /* current pointer on input string */
  30. static WOOL_STRING_STREAM yystrout; /* current wool output stream */
  31. int yylineno=0;            /* flex doesn't have this */
  32.  
  33. #define output(c) {\
  34.     if(yyout_is_string){\
  35.         *(yystrout->ptr)++ = c;\
  36.         *(yystrout->ptr) = '\0';\
  37.         if((yystrout->ptr) >= (yystrout->last)){\
  38.         yyoutflush();\
  39.     }\
  40.     }else{\
  41.     putc(c,yyout);\
  42.     }\
  43. }
  44.  
  45. #define ECHO    {\
  46.     int i;\
  47.     for (i=0; i<yyleng; i++)\
  48.         output(yytext[i]);\
  49.     }
  50.  
  51.  
  52. #define YY_INPUT(buf, result, max_size) \
  53.     if(yyin_is_string) {\
  54.         strncpy(buf, yystrin, max_size-1);\
  55.         yystrin += result = strlen(buf);\
  56.     } else {\
  57.         if((result = read(fileno(yyin), buf, max_size)) < 0)\
  58.             YY_FATAL_ERROR("read() in flex scanner failed");\
  59.     }
  60. /*
  61. #define YY_INPUT(buf, result, max_size) \
  62.     if(yyin_is_string)\
  63.         (*buf = *yystrin)? yystrin++:0;\
  64.         result=1;\
  65.     } else {\
  66.         if((result = read(fileno(yyin), buf, max_size)) < 0)\
  67.             YY_FATAL_ERROR("read() in flex scanner failed");\
  68.     }
  69. */
  70.  
  71. /* handling of wool buffer stack */
  72. #define WOOL_MAX_BUFFER_STACK    50
  73. static YY_BUFFER_STATE    wool_buffer_stack[WOOL_MAX_BUFFER_STACK];
  74. static int wool_buffer_sp = 0;
  75.  
  76.  
  77. /* counting the parentheses -- hack for wool_pool */
  78.  
  79. #define INC_PAR wool_pool_parentheses_level++
  80. #define DEC_PAR if(wool_pool_parentheses_level)wool_pool_parentheses_level--
  81. %}
  82.  
  83. FNCP    [^-+\004 \t\n"'()0-9]
  84. FNC    [^\004 \t\n"'()0-9]
  85. NC    [^\004 \t\n"'()]
  86. SIGN    [-+]
  87.  
  88. %%
  89. \n        yylineno++; REJECT;        /* That's faster than in the macro */
  90. .[\010]        ;                /* handles backspacing */
  91. .[\177]        ;                /* handles deleting */
  92. \"([^\\\"\n]|\\(.|\n)|\"\")*\"  return(STRING); /* strings */
  93. \"([^\\\"\n]|\\(.|\n)|\"\")*\n  return(NON_CLOSED_STRING); /* error */
  94. \;.*\n        ;                /* comments */
  95. [-+]?[0-9]+    return(NUMBER);            /* integers */
  96. 0[xX][0-9a-fA-F]+    return(HEX_NUMBER);    /* hex integers */
  97. "("        {INC_PAR; return(LEFTPAR);}    /* start of list */
  98. ")"        {DEC_PAR; return(RIGHTPAR);}    /* end of list */
  99. "'"        return(QUOTECHAR);        /* quoting */
  100. "{"        {INC_PAR; return(LEFTBRA);}    /* (progn */
  101. "}"        {DEC_PAR; return(RIGHTBRA);}    /* ) */
  102. {FNCP}{NC}*    return (NAME);            /* identifier */
  103. {SIGN}{FNC}{NC}*    return (NAME);        /* +foo */
  104. {SIGN}        return (NAME);            /* + */
  105. [ \t\n]        ;                /* blanks */
  106. <<EOF>>        return(END_OF_FILE);        /* pseudo-EOF handling */
  107. .        ;                /* skip over control codes */
  108. %%
  109.  
  110. /**********************\
  111. *                *
  112. *  WOOL's I/O package  *
  113. *                *
  114. \**********************/
  115.  
  116.  
  117.  
  118. /* externally callable function for unput:
  119.  * Doesn't do anything, since we solved include files by
  120.  * using switch_buffer() calls.
  121.  */
  122.  
  123. wool_unput(buffer)
  124. char *buffer;
  125. {
  126. }
  127.  
  128.  
  129. /*
  130.  * yyoutflush
  131.  * to flush wool output buffer.
  132.  */
  133.  
  134. void
  135. yyoutflush(){
  136.     if(yyout_is_string){
  137.         ASSERT(yystrout->overflow_handler);
  138. /*XXX-UWE-XXX*/
  139.     (*(yystrout->overflow_handler))(yystrout);
  140.     /* yystrout->ptr = yystrout-> buffer; */
  141. /*XXX-UWE-XXX*/
  142.     }else{
  143.     fflush(yyout);
  144.     }
  145. }
  146.  
  147. /*
  148.  * wool_input_redirect
  149.  * to set wool's parsing to the stream or string argument.
  150.  * arg1 = type (0 = string, 1 = stream);
  151.  * arg2 = stream or string
  152.  * arg3 = POINTER to (FILE *) or (char *) where will go the old stream
  153.  *        (if NULL not set)(returns the old type)
  154.  * arg4 = used to be:
  155.  *        where to save contents of unput buffer (if not NULL)
  156.  *      now is: 
  157.  *        if not NULL, open new buffer; otherwise pop most
  158.  *        recent.
  159.  */
  160.  
  161. int wool_input_redirect(type, stream, oldstream_p, old_buffer_contents)
  162. int type;
  163. char *stream;
  164. char **oldstream_p;
  165. char *old_buffer_contents;
  166. {
  167.     int oldtype = yyin_is_string;
  168.  
  169.     if(oldstream_p) *oldstream_p =
  170.         (oldtype ? (char *) yystrin : (char *) yyin);
  171.     if(yyin_is_string = type){
  172.     yystrin = stream;
  173.     }else{
  174.     yyin = (FILE *) stream;
  175.     }
  176.  
  177.     if(old_buffer_contents != NULL) {
  178.         /* open new buffer */
  179.  
  180.     if(wool_buffer_sp >= WOOL_MAX_BUFFER_STACK)
  181.         YY_FATAL_ERROR("wool_buffer_stack overflow");
  182.  
  183.     wool_buffer_stack[wool_buffer_sp++] = YY_CURRENT_BUFFER;
  184.     yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  185.     } else{
  186.     /* pop most recent buffer */
  187.  
  188.     if(--wool_buffer_sp < 0)
  189.         YY_FATAL_ERROR("wool_buffer_stack empty");
  190.  
  191.     yy_delete_buffer( YY_CURRENT_BUFFER );
  192.     yy_switch_to_buffer(wool_buffer_stack[wool_buffer_sp]);
  193.     }
  194.     return oldtype;
  195. }
  196.  
  197. /*
  198.  * wool_output_redirect
  199.  * to set wool's outputs to the stream or string argument.
  200.  * arg1 = type (0 = string, 1 = stream);
  201.  * arg2 = stream or string
  202.  * arg4 = POINTER to (FILE *) or WOOL_STRING_STREAM tu put the old stream
  203.  *        (if NULL not set)(returns the old type)
  204.  */
  205.  
  206. wool_output_redirect(type, stream, oldstream_p)
  207. int type;
  208. char *stream;
  209. char **oldstream_p;
  210. {
  211.     int oldtype = yyout_is_string;
  212.     if(oldstream_p) *oldstream_p =
  213.         (oldtype ? (char *) yystrout : (char *) yyout);
  214.     yyoutflush();
  215.     if(yyout_is_string = type){
  216.     yystrout = (WOOL_STRING_STREAM) stream;
  217.     }else{
  218.     yyout = (FILE *) stream;
  219.     }
  220.     return oldtype;
  221. }
  222.  
  223. /*
  224.  * now, some functions to provide a printf - like service on wool's output.
  225.  */
  226.  
  227. /* prints a string */
  228.  
  229. void
  230. wool_puts(string)
  231. register char *string;
  232. {
  233.     while(*string) output(*string++);
  234. }
  235.  
  236. /* put a newline */
  237.  
  238. wool_newline()
  239. {
  240.     output('\n');
  241. }
  242.  
  243. /* does a format with ONE arg. */
  244.  
  245. wool_printf(string, arg)
  246. register char *string;
  247. char *arg;
  248. {
  249.     static char wool_temp_string[MAX_TEMP_STRING_SIZE];
  250.     sprintf(wool_temp_string, string, arg);
  251.     wool_puts(wool_temp_string);
  252. }
  253.  
  254. /* prints a char */
  255.  
  256. void
  257. wool_putchar(c)
  258. char c;
  259. {
  260.     output(c);
  261. }
  262.  
  263. /*
  264.  * function to make a WOOL_STRING_STREAM of a given capacity
  265.  * arg1 = capactity in bytes
  266.  * arg2 = user function to be called on buffer when overflow occurs
  267.  */
  268.  
  269. WOOL_STRING_STREAM WOOL_STRING_STREAM_make(nbytes, handler)
  270. int nbytes;
  271. int (* handler)();
  272. {
  273.     WOOL_STRING_STREAM str = (WOOL_STRING_STREAM)
  274.         Malloc(sizeof(struct _WOOL_STRING_STREAM));
  275.     str->buffer = (char *) Malloc(nbytes);
  276.     *str->buffer = '\0';        /*XXX-UWE-XXX*/
  277.     str->ptr = str->buffer;
  278.     str->last = str->buffer + nbytes -1;
  279.     str->overflow_handler = handler;
  280.     return str;
  281. }
  282.  
  283. /* 
  284.  * and the function to free a stream
  285.  */
  286.  
  287. WOOL_STRING_STREAM_free (str)
  288. WOOL_STRING_STREAM str;
  289. {
  290.     Free(str->buffer);
  291.     Free(str);
  292. }
  293.