home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / parser / scan.lex < prev    next >
Encoding:
Text File  |  1993-05-02  |  4.5 KB  |  220 lines

  1. %{
  2. static char *scan_l =
  3.     "$Header: /private/postgres/src/parser/RCS/scan.lex,v 1.24 1992/07/28 19:11:28 mao Exp $";
  4. /**********************************************************************
  5.   scan.l
  6.   lexical scanner for POSTGRES 
  7.  **********************************************************************/
  8.  
  9. #include <ctype.h>
  10. #include <stdlib.h>
  11.  
  12. #include "parser/parse.h"
  13. #include "nodes/pg_lisp.h"
  14. #include "parser/atoms.h"
  15.  
  16.  
  17. extern int StringInput;
  18. extern char *TheString;
  19. extern char *Ch;
  20. char *InputFrag;
  21. int FragLen;
  22.  
  23. /*
  24.  * kai:
  25.  * Porting this from Unix standard lex to Linux' flex wasn't fun. First of all,
  26.  * they use very different input styles (lex does it character by character,
  27.  * while flex uses a big buffer). And then there are functions in parse_query.c
  28.  * which reverse the order of scanning. Nevertheless I hope everything is
  29.  * working now.
  30.  *
  31.  * It seems that StringInput == 0 does not happen (the old code in
  32.  * parse_query.c would break badly for queries with or without a from-clause),
  33.  * but I am not sure, whether other statements are passed directly or not.
  34.  */
  35.  
  36. #undef YY_INPUT
  37. #define YY_INPUT(buf,result,max_len) \
  38.     if (StringInput) { \
  39.         int len; \
  40.         if (InputFrag && FragLen <= 0) \
  41.         InputFrag = 0; \
  42.         if (InputFrag) { \
  43.         if((len = FragLen) > max_len) \
  44.             len = max_len; \
  45.         result = len; \
  46.         bcopy(InputFrag, (char *) buf, len); \
  47.         InputFrag += len; \
  48.         FragLen -= len; \
  49.         } else { \
  50.         if(Ch == NULL) \
  51.             Ch = TheString; \
  52.         if((len = strlen(Ch)) > max_len) \
  53.             len = max_len; \
  54.         result = len; \
  55.         bcopy(Ch, (char *) buf, len); \
  56.         Ch += len; \
  57.         } \
  58.     } else { \
  59.         if ( (result = read( stdin, (char *) buf, max_len )) < 0 ) \
  60.         YY_FATAL_ERROR( "read() in flex scanner failed" ); \
  61.     }
  62.  
  63. #undef YY_BUF_SIZE
  64. #define YY_BUF_SIZE \
  65.     (StringInput ? \
  66.         strlen(TheString) + 4 : \
  67.         (YY_READ_BUF_SIZE * 2) /* size of default input buffer */)
  68.  
  69. extern LispValue yylval;
  70. %}
  71.  
  72. digit        [0-9]
  73. letter        [_A-Za-z]
  74. letter_or_digit    [_A-Za-z0-9]
  75.  
  76. identifier    {letter}{letter_or_digit}*
  77.  
  78. self        [,()\[\].;$\:\+\-\*\/\<\>\=\|]
  79. op_and_self    [\~\!\@\#\%\^\&\|\`\?\$\:\+\-\*\/\<\>\=]
  80. op_only        [\~\!\@\#\%\^\&\`\?]
  81.  
  82. operator    ({op_and_self}{op_and_self}+)|{op_only}+
  83. string        \"
  84. specialstr    \`
  85. character    "'"
  86.  
  87. integer        {digit}+
  88. real        {digit}+\.{digit}+([Ee][-+]?{digit}+)?
  89.  
  90. param        \${integer}
  91.  
  92. comment        "/*"
  93.  
  94. space        [ \t\n\f]
  95. other        .
  96.  
  97. %%
  98. {comment}    { scancmnt();        }
  99. "::"        { return TYPECAST;    }
  100. {specialstr}    {
  101.             char buf[8192];
  102.             scanspecial(buf,sizeof(buf));
  103.             yylval = lispString(buf);
  104.             return (SCONST);
  105.         }
  106. {self}        { return (yytext[0]);    }
  107. {operator}    {
  108.             yylval = lispString(yytext);
  109.             return (Op);
  110.         }
  111. {param}            {       yylval = lispInteger(atoi(&yytext[1]));        
  112.                     return (PARAM);
  113.                 }
  114. {integer}    {
  115.             yylval = lispInteger(atoi(yytext));        
  116.             return (ICONST);
  117.         }
  118. {real}        {
  119.             yylval = lispFloat(atof(yytext));
  120.             return (FCONST);
  121.         }
  122. {string}    {
  123.             char buf[8192];
  124.             scanstr(buf,sizeof(buf));
  125.             yylval = lispString(buf);
  126.             return (SCONST);
  127.         }
  128.  
  129. {character}    {
  130.             char buf[2];
  131.             scanchar(buf);
  132.             yylval = lispString(buf);
  133.             return (CCONST);
  134.         }
  135. {identifier}    {
  136.             ScanKeyword    *keyword;
  137.  
  138.             keyword = ScanKeywordLookup(yytext);
  139.             if (keyword != NULL) {
  140.                 yylval = lispAtom(keyword->name);
  141.                 return (keyword->value);
  142.             } else {
  143.                 yylval = (LispValue) lispName(yytext);
  144.                 return (IDENT);
  145.             }
  146.         }
  147. {space}        { /* void */        }
  148. {other}        { return (yytext[0]);    }
  149.  
  150. %%
  151.  
  152.  
  153. /*
  154.  * Switch to new input (discard the read buffer)
  155.  */
  156. void NewInput(void)
  157. {
  158.     if(yy_current_buffer)
  159.     YY_NEW_FILE;
  160. }
  161.  
  162.  
  163. /*
  164.  * Get a character out of flex's buffer
  165.  */
  166. int DoInput(void)
  167. {
  168.     char *yy_cp = yy_c_buf_p;
  169.     char *yy_bp = yytext;
  170.  
  171.     /* undo effects of setting up yytext */
  172.     *yy_cp = yy_hold_char;
  173.     if ( yy_c_buf_p >= &yy_current_buffer->yy_ch_buf[yy_n_chars] )
  174.     switch ( yy_get_next_buffer() ) {
  175.         case EOB_ACT_CONTINUE_SCAN:
  176.         break;
  177.  
  178.         case EOB_ACT_LAST_MATCH:
  179.         case EOB_ACT_END_OF_FILE:
  180.         return -1;
  181.     }
  182.  
  183.     yy_cp++;
  184.     yy_c_buf_p = yy_cp;
  185.     YY_DO_BEFORE_ACTION; /* set up yytext again */ \
  186.  
  187.     return yy_cp[-1];
  188. }
  189.  
  190.  
  191. /*
  192.  * This function determines the current position of the lexical scanning
  193.  * relative to TheString.
  194.  */
  195. char *CurScan(void)
  196. {
  197.     return (InputFrag ? InputFrag : Ch) +
  198.        (yy_c_buf_p - &yy_current_buffer->yy_ch_buf[yy_n_chars]);
  199. }
  200.  
  201.  
  202. /*
  203.  * Put a character back into flex's buffer
  204.  */
  205. void DoUnput(char c)
  206. {
  207.     yyunput(c, yytext);
  208. }
  209.  
  210.  
  211. /*
  212.  * Delete the input buffer.
  213.  */
  214. void DeleteBuffer(void)
  215. {
  216.     if(yy_current_buffer)
  217.     yy_delete_buffer(yy_current_buffer);
  218.     yy_init = 1;
  219. }
  220.