LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/preproc - pgc.l (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 103 126 81.7 %
Date: 2020-06-03 10:06:28 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : %top{
       2             : /*-------------------------------------------------------------------------
       3             :  *
       4             :  * pgc.l
       5             :  *    lexical scanner for ecpg
       6             :  *
       7             :  * This is a modified version of src/backend/parser/scan.l
       8             :  *
       9             :  * The ecpg scanner is not backup-free, so the fail rules are
      10             :  * only here to simplify syncing this file with scan.l.
      11             :  *
      12             :  *
      13             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
      14             :  * Portions Copyright (c) 1994, Regents of the University of California
      15             :  *
      16             :  * IDENTIFICATION
      17             :  *    src/interfaces/ecpg/preproc/pgc.l
      18             :  *
      19             :  *-------------------------------------------------------------------------
      20             :  */
      21             : #include "postgres_fe.h"
      22             : 
      23             : #include <ctype.h>
      24             : #include <limits.h>
      25             : 
      26             : #include "common/string.h"
      27             : 
      28             : #include "preproc_extern.h"
      29             : #include "preproc.h"
      30             : }
      31             : 
      32             : %{
      33             : 
      34             : /* LCOV_EXCL_START */
      35             : 
      36             : extern YYSTYPE base_yylval;
      37             : 
      38             : static int      xcdepth = 0;    /* depth of nesting in slash-star comments */
      39             : static char    *dolqstart = NULL;   /* current $foo$ quote start string */
      40             : 
      41             : /*
      42             :  * literalbuf is used to accumulate literal values when multiple rules
      43             :  * are needed to parse a single literal.  Call startlit to reset buffer
      44             :  * to empty, addlit to add text.  Note that the buffer is permanently
      45             :  * malloc'd to the largest size needed so far in the current run.
      46             :  */
      47             : static char    *literalbuf = NULL;      /* expandable buffer */
      48             : static int      literallen;             /* actual current length */
      49             : static int      literalalloc;           /* current allocated buffer size */
      50             : 
      51             : /* Used for detecting global state together with braces_open */
      52             : static int      parenths_open;
      53             : 
      54             : /* Used to tell parse_include() whether the command was #include or #include_next */
      55             : static bool     include_next;
      56             : 
      57             : #define startlit()  (literalbuf[0] = '\0', literallen = 0)
      58             : static void addlit(char *ytext, int yleng);
      59             : static void addlitchar(unsigned char);
      60             : static int  process_integer_literal(const char *token, YYSTYPE *lval);
      61             : static void parse_include(void);
      62             : static bool ecpg_isspace(char ch);
      63             : static bool isdefine(void);
      64             : static bool isinformixdefine(void);
      65             : 
      66             : char *token_start;
      67             : 
      68             : /* vars to keep track of start conditions when scanning literals */
      69             : static int state_before_str_start;
      70             : static int state_before_str_stop;
      71             : 
      72             : struct _yy_buffer
      73             : {
      74             :     YY_BUFFER_STATE     buffer;
      75             :     long                lineno;
      76             :     char               *filename;
      77             :     struct _yy_buffer  *next;
      78             : } *yy_buffer = NULL;
      79             : 
      80             : static char *old;
      81             : 
      82             : #define MAX_NESTED_IF 128
      83             : static short preproc_tos;
      84             : static short ifcond;
      85             : static struct _if_value
      86             : {
      87             :     short condition;
      88             :     short else_branch;
      89             : } stacked_if_value[MAX_NESTED_IF];
      90             : 
      91             : %}
      92             : 
      93             : %option 8bit
      94             : %option never-interactive
      95             : %option nodefault
      96             : %option noinput
      97             : %option noyywrap
      98             : %option warn
      99             : %option yylineno
     100             : %option prefix="base_yy"
     101             : 
     102             : /*
     103             :  * OK, here is a short description of lex/flex rules behavior.
     104             :  * The longest pattern which matches an input string is always chosen.
     105             :  * For equal-length patterns, the first occurring in the rules list is chosen.
     106             :  * INITIAL is the starting state, to which all non-conditional rules apply.
     107             :  * Exclusive states change parsing rules while the state is active.  When in
     108             :  * an exclusive state, only those rules defined for that state apply.
     109             :  *
     110             :  * We use exclusive states for quoted strings, extended comments,
     111             :  * and to eliminate parsing troubles for numeric strings.
     112             :  * Exclusive states:
     113             :  *  <xb> bit string literal
     114             :  *  <xc> extended C-style comments
     115             :  *  <xd> delimited identifiers (double-quoted identifiers)
     116             :  *  <xdc> double-quoted strings in C
     117             :  *  <xh> hexadecimal numeric string
     118             :  *  <xn> national character quoted strings
     119             :  *  <xq> standard quoted strings
     120             :  *  <xqs> quote stop (detect continued strings)
     121             :  *  <xe> extended quoted strings (support backslash escape sequences)
     122             :  *  <xqc> single-quoted strings in C
     123             :  *  <xdolq> $foo$ quoted strings
     124             :  *  <xui> quoted identifier with Unicode escapes
     125             :  *  <xus> quoted string with Unicode escapes
     126             :  *  <xcond> condition of an EXEC SQL IFDEF construct
     127             :  *  <xskip> skipping the inactive part of an EXEC SQL IFDEF construct
     128             :  *
     129             :  * Note: we intentionally don't mimic the backend's <xeu> state; we have
     130             :  * no need to distinguish it from <xe> state.
     131             :  *
     132             :  * Remember to add an <<EOF>> case whenever you add a new exclusive state!
     133             :  * The default one is probably not the right thing.
     134             :  */
     135             : 
     136             : %x xb
     137             : %x xc
     138             : %x xd
     139             : %x xdc
     140             : %x xh
     141             : %x xn
     142             : %x xq
     143             : %x xqs
     144             : %x xe
     145             : %x xqc
     146             : %x xdolq
     147             : %x xui
     148             : %x xus
     149             : %x xcond
     150             : %x xskip
     151             : 
     152             : /* Additional exclusive states that are specific to ECPG */
     153             : %x C SQL incl def def_ident undef
     154             : 
     155             : /*
     156             :  * In order to make the world safe for Windows and Mac clients as well as
     157             :  * Unix ones, we accept either \n or \r as a newline.  A DOS-style \r\n
     158             :  * sequence will be seen as two successive newlines, but that doesn't cause
     159             :  * any problems.  SQL-style comments, which start with -- and extend to the
     160             :  * next newline, are treated as equivalent to a single whitespace character.
     161             :  *
     162             :  * NOTE a fine point: if there is no newline following --, we will absorb
     163             :  * everything to the end of the input as a comment.  This is correct.  Older
     164             :  * versions of Postgres failed to recognize -- as a comment if the input
     165             :  * did not end with a newline.
     166             :  *
     167             :  * XXX perhaps \f (formfeed) should be treated as a newline as well?
     168             :  *
     169             :  * XXX if you change the set of whitespace characters, fix ecpg_isspace()
     170             :  * to agree.
     171             :  */
     172             : 
     173             : space           [ \t\n\r\f]
     174             : horiz_space     [ \t\f]
     175             : newline         [\n\r]
     176             : non_newline     [^\n\r]
     177             : 
     178             : comment         ("--"{non_newline}*)
     179             : 
     180             : whitespace      ({space}+|{comment})
     181             : 
     182             : /*
     183             :  * SQL requires at least one newline in the whitespace separating
     184             :  * string literals that are to be concatenated.  Silly, but who are we
     185             :  * to argue?  Note that {whitespace_with_newline} should not have * after
     186             :  * it, whereas {whitespace} should generally have a * after it...
     187             :  */
     188             : 
     189             : horiz_whitespace        ({horiz_space}|{comment})
     190             : whitespace_with_newline ({horiz_whitespace}*{newline}{whitespace}*)
     191             : 
     192             : quote           '
     193             : /* If we see {quote} then {quotecontinue}, the quoted string continues */
     194             : quotecontinue   {whitespace_with_newline}{quote}
     195             : 
     196             : /*
     197             :  * {quotecontinuefail} is needed to avoid lexer backup when we fail to match
     198             :  * {quotecontinue}.  It might seem that this could just be {whitespace}*,
     199             :  * but if there's a dash after {whitespace_with_newline}, it must be consumed
     200             :  * to see if there's another dash --- which would start a {comment} and thus
     201             :  * allow continuation of the {quotecontinue} token.
     202             :  */
     203             : quotecontinuefail   {whitespace}*"-"?
     204             : 
     205             : /* Bit string
     206             :  */
     207             : xbstart         [bB]{quote}
     208             : xbinside        [^']*
     209             : 
     210             : /* Hexadecimal number */
     211             : xhstart         [xX]{quote}
     212             : xhinside        [^']*
     213             : 
     214             : /* National character */
     215             : xnstart         [nN]{quote}
     216             : 
     217             : /* Quoted string that allows backslash escapes */
     218             : xestart         [eE]{quote}
     219             : xeinside        [^\\']+
     220             : xeescape        [\\][^0-7]
     221             : xeoctesc        [\\][0-7]{1,3}
     222             : xehexesc        [\\]x[0-9A-Fa-f]{1,2}
     223             : xeunicode       [\\](u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})
     224             : 
     225             : /* Extended quote
     226             :  * xqdouble implements embedded quote, ''''
     227             :  */
     228             : xqstart         {quote}
     229             : xqdouble        {quote}{quote}
     230             : xqcquote        [\\]{quote}
     231             : xqinside        [^']+
     232             : 
     233             : /* $foo$ style quotes ("dollar quoting")
     234             :  * The quoted string starts with $foo$ where "foo" is an optional string
     235             :  * in the form of an identifier, except that it may not contain "$",
     236             :  * and extends to the first occurrence of an identical string.
     237             :  * There is *no* processing of the quoted text.
     238             :  *
     239             :  * {dolqfailed} is an error rule to avoid scanner backup when {dolqdelim}
     240             :  * fails to match its trailing "$".
     241             :  */
     242             : dolq_start      [A-Za-z\200-\377_]
     243             : dolq_cont       [A-Za-z\200-\377_0-9]
     244             : dolqdelim       \$({dolq_start}{dolq_cont}*)?\$
     245             : dolqfailed      \${dolq_start}{dolq_cont}*
     246             : dolqinside      [^$]+
     247             : 
     248             : /* Double quote
     249             :  * Allows embedded spaces and other special characters into identifiers.
     250             :  */
     251             : dquote          \"
     252             : xdstart         {dquote}
     253             : xdstop          {dquote}
     254             : xddouble        {dquote}{dquote}
     255             : xdinside        [^"]+
     256             : 
     257             : /* Quoted identifier with Unicode escapes */
     258             : xuistart        [uU]&{dquote}
     259             : 
     260             : /* Quoted string with Unicode escapes */
     261             : xusstart        [uU]&{quote}
     262             : 
     263             : /* special stuff for C strings */
     264             : xdcqq           \\\\
     265             : xdcqdq          \\\"
     266             : xdcother        [^"]
     267             : xdcinside       ({xdcqq}|{xdcqdq}|{xdcother})
     268             : 
     269             : 
     270             : /* C-style comments
     271             :  *
     272             :  * The "extended comment" syntax closely resembles allowable operator syntax.
     273             :  * The tricky part here is to get lex to recognize a string starting with
     274             :  * slash-star as a comment, when interpreting it as an operator would produce
     275             :  * a longer match --- remember lex will prefer a longer match!  Also, if we
     276             :  * have something like plus-slash-star, lex will think this is a 3-character
     277             :  * operator whereas we want to see it as a + operator and a comment start.
     278             :  * The solution is two-fold:
     279             :  * 1. append {op_chars}* to xcstart so that it matches as much text as
     280             :  *    {operator} would. Then the tie-breaker (first matching rule of same
     281             :  *    length) ensures xcstart wins.  We put back the extra stuff with yyless()
     282             :  *    in case it contains a star-slash that should terminate the comment.
     283             :  * 2. In the operator rule, check for slash-star within the operator, and
     284             :  *    if found throw it back with yyless().  This handles the plus-slash-star
     285             :  *    problem.
     286             :  * Dash-dash comments have similar interactions with the operator rule.
     287             :  */
     288             : xcstart         \/\*{op_chars}*
     289             : xcstop          \*+\/
     290             : xcinside        [^*/]+
     291             : 
     292             : digit           [0-9]
     293             : ident_start     [A-Za-z\200-\377_]
     294             : ident_cont      [A-Za-z\200-\377_0-9\$]
     295             : 
     296             : identifier      {ident_start}{ident_cont}*
     297             : 
     298             : array           ({ident_cont}|{whitespace}|[\[\]\+\-\*\%\/\(\)\>\.])*
     299             : 
     300             : /* Assorted special-case operators and operator-like tokens */
     301             : typecast        "::"
     302             : dot_dot         \.\.
     303             : colon_equals    ":="
     304             : 
     305             : /*
     306             :  * These operator-like tokens (unlike the above ones) also match the {operator}
     307             :  * rule, which means that they might be overridden by a longer match if they
     308             :  * are followed by a comment start or a + or - character. Accordingly, if you
     309             :  * add to this list, you must also add corresponding code to the {operator}
     310             :  * block to return the correct token in such cases. (This is not needed in
     311             :  * psqlscan.l since the token value is ignored there.)
     312             :  */
     313             : equals_greater  "=>"
     314             : less_equals     "<="
     315             : greater_equals  ">="
     316             : less_greater    "<>"
     317             : not_equals      "!="
     318             : 
     319             : /*
     320             :  * "self" is the set of chars that should be returned as single-character
     321             :  * tokens.  "op_chars" is the set of chars that can make up "Op" tokens,
     322             :  * which can be one or more characters long (but if a single-char token
     323             :  * appears in the "self" set, it is not to be returned as an Op).  Note
     324             :  * that the sets overlap, but each has some chars that are not in the other.
     325             :  *
     326             :  * If you change either set, adjust the character lists appearing in the
     327             :  * rule for "operator"!
     328             :  */
     329             : self            [,()\[\].;\:\+\-\*\/\%\^\<\>\=]
     330             : op_chars        [\~\!\@\#\^\&\|\`\?\+\-\*\/\%\<\>\=]
     331             : operator        {op_chars}+
     332             : 
     333             : /* we no longer allow unary minus in numbers.
     334             :  * instead we pass it separately to parser. there it gets
     335             :  * coerced via doNegate() -- Leon aug 20 1999
     336             :  *
     337             :  * {decimalfail} is used because we would like "1..10" to lex as 1, dot_dot, 10.
     338             :  *
     339             :  * {realfail1} and {realfail2} are added to prevent the need for scanner
     340             :  * backup when the {real} rule fails to match completely.
     341             :  */
     342             : 
     343             : integer         {digit}+
     344             : decimal         (({digit}*\.{digit}+)|({digit}+\.{digit}*))
     345             : decimalfail     {digit}+\.\.
     346             : real            ({integer}|{decimal})[Ee][-+]?{digit}+
     347             : realfail1       ({integer}|{decimal})[Ee]
     348             : realfail2       ({integer}|{decimal})[Ee][-+]
     349             : 
     350             : param           \${integer}
     351             : 
     352             : /* special characters for other dbms */
     353             : /* we have to react differently in compat mode */
     354             : informix_special    [\$]
     355             : 
     356             : other           .
     357             : 
     358             : /*
     359             :  * Dollar quoted strings are totally opaque, and no escaping is done on them.
     360             :  * Other quoted strings must allow some special characters such as single-quote
     361             :  *  and newline.
     362             :  * Embedded single-quotes are implemented both in the SQL standard
     363             :  *  style of two adjacent single quotes "''" and in the Postgres/Java style
     364             :  *  of escaped-quote "\'".
     365             :  * Other embedded escaped characters are matched explicitly and the leading
     366             :  *  backslash is dropped from the string.
     367             :  * Note that xcstart must appear before operator, as explained above!
     368             :  *  Also whitespace (comment) must appear before operator.
     369             :  */
     370             : 
     371             : /* some stuff needed for ecpg */
     372             : exec            [eE][xX][eE][cC]
     373             : sql             [sS][qQ][lL]
     374             : define          [dD][eE][fF][iI][nN][eE]
     375             : include         [iI][nN][cC][lL][uU][dD][eE]
     376             : include_next    [iI][nN][cC][lL][uU][dD][eE]_[nN][eE][xX][tT]
     377             : import          [iI][mM][pP][oO][rR][tT]
     378             : undef           [uU][nN][dD][eE][fF]
     379             : 
     380             : /* C version of hex number */
     381             : xch             0[xX][0-9A-Fa-f]*
     382             : 
     383             : ccomment        "//".*\n
     384             : 
     385             : if              [iI][fF]
     386             : ifdef           [iI][fF][dD][eE][fF]
     387             : ifndef          [iI][fF][nN][dD][eE][fF]
     388             : else            [eE][lL][sS][eE]
     389             : elif            [eE][lL][iI][fF]
     390             : endif           [eE][nN][dD][iI][fF]
     391             : 
     392             : struct          [sS][tT][rR][uU][cC][tT]
     393             : 
     394             : exec_sql        {exec}{space}*{sql}{space}*
     395             : ipdigit         ({digit}|{digit}{digit}|{digit}{digit}{digit})
     396             : ip              {ipdigit}\.{ipdigit}\.{ipdigit}\.{ipdigit}
     397             : 
     398             : /* we might want to parse all cpp include files */
     399             : cppinclude      {space}*#{include}{space}*
     400             : cppinclude_next     {space}*#{include_next}{space}*
     401             : 
     402             : /* take care of cpp lines, they may also be continued */
     403             : /* first a general line for all commands not starting with "i" */
     404             : /* and then the other commands starting with "i", we have to add these
     405             :  * separately because the cppline production would match on "include" too
     406             :  */
     407             : cppline         {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+\/)|.|\\{space}*{newline})*{newline}
     408             : 
     409             : %%
     410             : 
     411             : %{
     412             :         /* code to execute during start of each call of yylex() */
     413             :         token_start = NULL;
     414             : %}
     415             : 
     416             : <SQL>{
     417             : {whitespace}    {
     418             :                     /* ignore */
     419             :                 }
     420             : } /* <SQL> */
     421             : 
     422             : <C,SQL>{
     423             : {xcstart}       {
     424             :                     token_start = yytext;
     425             :                     state_before_str_start = YYSTATE;
     426             :                     xcdepth = 0;
     427             :                     BEGIN(xc);
     428             :                     /* Put back any characters past slash-star; see above */
     429             :                     yyless(2);
     430             :                     fputs("/*", yyout);
     431             :                 }
     432             : } /* <C,SQL> */
     433             : 
     434             : <xc>{
     435             : {xcstart}       {
     436             :                     if (state_before_str_start == SQL)
     437             :                     {
     438             :                         xcdepth++;
     439             :                         /* Put back any characters past slash-star; see above */
     440             :                         yyless(2);
     441             :                         fputs("/_*", yyout);
     442             :                     }
     443             :                     else if (state_before_str_start == C)
     444             :                     {
     445             :                         ECHO;
     446             :                     }
     447             :                 }
     448             : 
     449             : {xcstop}        {
     450             :                     if (state_before_str_start == SQL)
     451             :                     {
     452             :                         if (xcdepth <= 0)
     453             :                         {
     454             :                             ECHO;
     455             :                             BEGIN(SQL);
     456             :                             token_start = NULL;
     457             :                         }
     458             :                         else
     459             :                         {
     460             :                             xcdepth--;
     461             :                             fputs("*_/", yyout);
     462             :                         }
     463             :                     }
     464             :                     else if (state_before_str_start == C)
     465             :                     {
     466             :                         ECHO;
     467             :                         BEGIN(C);
     468             :                         token_start = NULL;
     469             :                     }
     470             :                 }
     471             : 
     472             : {xcinside}      {
     473             :                     ECHO;
     474             :                 }
     475             : 
     476             : {op_chars}      {
     477             :                     ECHO;
     478             :                 }
     479             : 
     480             : \*+             {
     481             :                     ECHO;
     482             :                 }
     483             : 
     484             : <<EOF>>         {
     485             :                     mmfatal(PARSE_ERROR, "unterminated /* comment");
     486             :                 }
     487             : } /* <xc> */
     488             : 
     489             : <SQL>{
     490             : {xbstart}       {
     491             :                     token_start = yytext;
     492             :                     BEGIN(xb);
     493             :                     startlit();
     494             :                     addlitchar('b');
     495             :                 }
     496             : } /* <SQL> */
     497             : 
     498             : <xh>{xhinside}    |
     499             : <xb>{xbinside}    {
     500             :                     addlit(yytext, yyleng);
     501             :                 }
     502             : <xb><<EOF>>       { mmfatal(PARSE_ERROR, "unterminated bit string literal"); }
     503             : 
     504             : <SQL>{xhstart}    {
     505             :                     token_start = yytext;
     506             :                     BEGIN(xh);
     507             :                     startlit();
     508             :                     addlitchar('x');
     509             :                 }
     510             : <xh><<EOF>>       { mmfatal(PARSE_ERROR, "unterminated hexadecimal string literal"); }
     511             : 
     512             : <C>{xqstart}  {
     513             :                     token_start = yytext;
     514             :                     state_before_str_start = YYSTATE;
     515             :                     BEGIN(xqc);
     516             :                     startlit();
     517             :                 }
     518             : 
     519             : <SQL>{
     520             : {xnstart}       {
     521             :                     /* National character.
     522             :                      * Transfer it as-is to the backend.
     523             :                      */
     524             :                     token_start = yytext;
     525             :                     state_before_str_start = YYSTATE;
     526             :                     BEGIN(xn);
     527             :                     startlit();
     528             :                 }
     529             : 
     530             : {xqstart}       {
     531             :                     token_start = yytext;
     532             :                     state_before_str_start = YYSTATE;
     533             :                     BEGIN(xq);
     534             :                     startlit();
     535             :                 }
     536             : {xestart}       {
     537             :                     token_start = yytext;
     538             :                     state_before_str_start = YYSTATE;
     539             :                     BEGIN(xe);
     540             :                     startlit();
     541             :                 }
     542             : {xusstart}      {
     543             :                     token_start = yytext;
     544             :                     state_before_str_start = YYSTATE;
     545             :                     BEGIN(xus);
     546             :                     startlit();
     547             :                 }
     548             : } /* <SQL> */
     549             : 
     550             : <xb,xh,xq,xqc,xe,xn,xus>{quote} {
     551             :                     /*
     552             :                      * When we are scanning a quoted string and see an end
     553             :                      * quote, we must look ahead for a possible continuation.
     554             :                      * If we don't see one, we know the end quote was in fact
     555             :                      * the end of the string.  To reduce the lexer table size,
     556             :                      * we use a single "xqs" state to do the lookahead for all
     557             :                      * types of strings.
     558             :                      */
     559             :                     state_before_str_stop = YYSTATE;
     560             :                     BEGIN(xqs);
     561             :                 }
     562             : <xqs>{quotecontinue} {
     563             :                     /*
     564             :                      * Found a quote continuation, so return to the in-quote
     565             :                      * state and continue scanning the literal.  Nothing is
     566             :                      * added to the literal's contents.
     567             :                      */
     568             :                     BEGIN(state_before_str_stop);
     569             :                 }
     570             : <xqs>{quotecontinuefail} |
     571             : <xqs>{other} |
     572             : <xqs><<EOF>>  {
     573             :                     /*
     574             :                      * Failed to see a quote continuation.  Throw back
     575             :                      * everything after the end quote, and handle the string
     576             :                      * according to the state we were in previously.
     577             :                      */
     578             :                     yyless(0);
     579             :                     BEGIN(state_before_str_start);
     580             : 
     581             :                     switch (state_before_str_stop)
     582             :                     {
     583             :                         case xb:
     584             :                             if (literalbuf[strspn(literalbuf, "01") + 1] != '\0')
     585             :                                 mmerror(PARSE_ERROR, ET_ERROR, "invalid bit string literal");
     586             :                             base_yylval.str = mm_strdup(literalbuf);
     587             :                             return BCONST;
     588             :                         case xh:
     589             :                             base_yylval.str = mm_strdup(literalbuf);
     590             :                             return XCONST;
     591             :                         case xq:
     592             :                             /* fallthrough */
     593             :                         case xqc:
     594             :                             base_yylval.str = psprintf("'%s'", literalbuf);
     595             :                             return SCONST;
     596             :                         case xe:
     597             :                             base_yylval.str = psprintf("E'%s'", literalbuf);
     598             :                             return SCONST;
     599             :                         case xn:
     600             :                             base_yylval.str = psprintf("N'%s'", literalbuf);
     601             :                             return SCONST;
     602             :                         case xus:
     603             :                             base_yylval.str = psprintf("U&'%s'", literalbuf);
     604             :                             return USCONST;
     605             :                         default:
     606             :                             mmfatal(PARSE_ERROR, "unhandled previous state in xqs\n");
     607             :                     }
     608             :                 }
     609             : 
     610             : <xq,xe,xn,xus>{xqdouble}  { addlitchar('\''); }
     611             : <xqc>{xqcquote}   {
     612             :                     addlitchar('\\');
     613             :                     addlitchar('\'');
     614             :                 }
     615             : <xq,xqc,xn,xus>{xqinside} { addlit(yytext, yyleng); }
     616             : <xe>{xeinside}  {
     617             :                     addlit(yytext, yyleng);
     618             :                 }
     619             : <xe>{xeunicode} {
     620             :                     addlit(yytext, yyleng);
     621             :                 }
     622             : <xe>{xeescape}  {
     623             :                     addlit(yytext, yyleng);
     624             :                 }
     625             : <xe>{xeoctesc}  {
     626             :                     addlit(yytext, yyleng);
     627             :                 }
     628             : <xe>{xehexesc}  {
     629             :                     addlit(yytext, yyleng);
     630             :                 }
     631             : <xe>.         {
     632             :                     /* This is only needed for \ just before EOF */
     633             :                     addlitchar(yytext[0]);
     634             :                 }
     635             : <xq,xqc,xe,xn,xus><<EOF>> { mmfatal(PARSE_ERROR, "unterminated quoted string"); }
     636             : 
     637             : <SQL>{
     638             : {dolqdelim}     {
     639             :                     token_start = yytext;
     640             :                     if (dolqstart)
     641             :                         free(dolqstart);
     642             :                     dolqstart = mm_strdup(yytext);
     643             :                     BEGIN(xdolq);
     644             :                     startlit();
     645             :                     addlit(yytext, yyleng);
     646             :                 }
     647             : {dolqfailed}    {
     648             :                     /* throw back all but the initial "$" */
     649             :                     yyless(1);
     650             :                     /* and treat it as {other} */
     651             :                     return yytext[0];
     652             :                 }
     653             : } /* <SQL> */
     654             : 
     655             : <xdolq>{dolqdelim} {
     656             :                     if (strcmp(yytext, dolqstart) == 0)
     657             :                     {
     658             :                         addlit(yytext, yyleng);
     659             :                         free(dolqstart);
     660             :                         dolqstart = NULL;
     661             :                         BEGIN(SQL);
     662             :                         base_yylval.str = mm_strdup(literalbuf);
     663             :                         return SCONST;
     664             :                     }
     665             :                     else
     666             :                     {
     667             :                         /*
     668             :                          * When we fail to match $...$ to dolqstart, transfer
     669             :                          * the $... part to the output, but put back the final
     670             :                          * $ for rescanning.  Consider $delim$...$junk$delim$
     671             :                          */
     672             :                         addlit(yytext, yyleng - 1);
     673             :                         yyless(yyleng - 1);
     674             :                     }
     675             :                 }
     676             : <xdolq>{dolqinside} {
     677             :                     addlit(yytext, yyleng);
     678             :                 }
     679             : <xdolq>{dolqfailed} {
     680             :                     addlit(yytext, yyleng);
     681             :                 }
     682             : <xdolq>.      {
     683             :                     /* single quote or dollar sign */
     684             :                     addlitchar(yytext[0]);
     685             :                 }
     686             : <xdolq><<EOF>>    { mmfatal(PARSE_ERROR, "unterminated dollar-quoted string"); }
     687             : 
     688             : <SQL>{
     689             : {xdstart}       {
     690             :                     state_before_str_start = YYSTATE;
     691             :                     BEGIN(xd);
     692             :                     startlit();
     693             :                 }
     694             : {xuistart}      {
     695             :                     state_before_str_start = YYSTATE;
     696             :                     BEGIN(xui);
     697             :                     startlit();
     698             :                 }
     699             : } /* <SQL> */
     700             : 
     701             : <xd>{xdstop}  {
     702             :                     BEGIN(state_before_str_start);
     703             :                     if (literallen == 0)
     704             :                         mmerror(PARSE_ERROR, ET_ERROR, "zero-length delimited identifier");
     705             :                     /* The backend will truncate the identifier here. We do not as it does not change the result. */
     706             :                     base_yylval.str = mm_strdup(literalbuf);
     707             :                     return CSTRING;
     708             :                 }
     709             : <xdc>{xdstop} {
     710             :                     BEGIN(state_before_str_start);
     711             :                     base_yylval.str = mm_strdup(literalbuf);
     712             :                     return CSTRING;
     713             :                 }
     714             : <xui>{dquote} {
     715             :                     BEGIN(state_before_str_start);
     716             :                     if (literallen == 2) /* "U&" */
     717             :                         mmerror(PARSE_ERROR, ET_ERROR, "zero-length delimited identifier");
     718             :                     /* The backend will truncate the identifier here. We do not as it does not change the result. */
     719             :                     base_yylval.str = psprintf("U&\"%s\"", literalbuf);
     720             :                     return UIDENT;
     721             :                 }
     722             : <xd,xui>{xddouble}    {
     723             :                     addlitchar('"');
     724             :                 }
     725             : <xd,xui>{xdinside}    {
     726             :                     addlit(yytext, yyleng);
     727             :                 }
     728             : <xd,xui><<EOF>>   { mmfatal(PARSE_ERROR, "unterminated quoted identifier"); }
     729             : <C>{xdstart}  {
     730             :                     state_before_str_start = YYSTATE;
     731             :                     BEGIN(xdc);
     732             :                     startlit();
     733             :                 }
     734             : <xdc>{xdcinside}  {
     735             :                     addlit(yytext, yyleng);
     736             :                 }
     737             : <xdc><<EOF>>  { mmfatal(PARSE_ERROR, "unterminated quoted string"); }
     738             : 
     739             : <SQL>{
     740             : {typecast}      {
     741             :                     return TYPECAST;
     742             :                 }
     743             : 
     744             : {dot_dot}       {
     745             :                     return DOT_DOT;
     746             :                 }
     747             : 
     748             : {colon_equals}  {
     749             :                     return COLON_EQUALS;
     750             :                 }
     751             : 
     752             : {equals_greater} {
     753             :                     return EQUALS_GREATER;
     754             :                 }
     755             : 
     756             : {less_equals}   {
     757             :                     return LESS_EQUALS;
     758             :                 }
     759             : 
     760             : {greater_equals} {
     761             :                     return GREATER_EQUALS;
     762             :                 }
     763             : 
     764             : {less_greater}  {
     765             :                     /* We accept both "<>" and "!=" as meaning NOT_EQUALS */
     766             :                     return NOT_EQUALS;
     767             :                 }
     768             : 
     769             : {not_equals}    {
     770             :                     /* We accept both "<>" and "!=" as meaning NOT_EQUALS */
     771             :                     return NOT_EQUALS;
     772             :                 }
     773             : 
     774             : {informix_special}  {
     775             :               /* are we simulating Informix? */
     776             :                 if (INFORMIX_MODE)
     777             :                 {
     778             :                     unput(':');
     779             :                 }
     780             :                 else
     781             :                     return yytext[0];
     782             :                 }
     783             : 
     784             : {self}          {
     785             :                     /*
     786             :                      * We may find a ';' inside a structure
     787             :                      * definition in a TYPE or VAR statement.
     788             :                      * This is not an EOL marker.
     789             :                      */
     790             :                     if (yytext[0] == ';' && struct_level == 0)
     791             :                         BEGIN(C);
     792             :                     return yytext[0];
     793             :                 }
     794             : 
     795             : {operator}      {
     796             :                     /*
     797             :                      * Check for embedded slash-star or dash-dash; those
     798             :                      * are comment starts, so operator must stop there.
     799             :                      * Note that slash-star or dash-dash at the first
     800             :                      * character will match a prior rule, not this one.
     801             :                      */
     802             :                     int         nchars = yyleng;
     803             :                     char       *slashstar = strstr(yytext, "/*");
     804             :                     char       *dashdash = strstr(yytext, "--");
     805             : 
     806             :                     if (slashstar && dashdash)
     807             :                     {
     808             :                         /* if both appear, take the first one */
     809             :                         if (slashstar > dashdash)
     810             :                             slashstar = dashdash;
     811             :                     }
     812             :                     else if (!slashstar)
     813             :                         slashstar = dashdash;
     814             :                     if (slashstar)
     815             :                         nchars = slashstar - yytext;
     816             : 
     817             :                     /*
     818             :                      * For SQL compatibility, '+' and '-' cannot be the
     819             :                      * last char of a multi-char operator unless the operator
     820             :                      * contains chars that are not in SQL operators.
     821             :                      * The idea is to lex '=-' as two operators, but not
     822             :                      * to forbid operator names like '?-' that could not be
     823             :                      * sequences of SQL operators.
     824             :                      */
     825             :                     if (nchars > 1 &&
     826             :                         (yytext[nchars - 1] == '+' ||
     827             :                          yytext[nchars - 1] == '-'))
     828             :                     {
     829             :                         int         ic;
     830             : 
     831             :                         for (ic = nchars - 2; ic >= 0; ic--)
     832             :                         {
     833             :                             char c = yytext[ic];
     834             :                             if (c == '~' || c == '!' || c == '@' ||
     835             :                                 c == '#' || c == '^' || c == '&' ||
     836             :                                 c == '|' || c == '`' || c == '?' ||
     837             :                                 c == '%')
     838             :                                 break;
     839             :                         }
     840             :                         if (ic < 0)
     841             :                         {
     842             :                             /*
     843             :                              * didn't find a qualifying character, so remove
     844             :                              * all trailing [+-]
     845             :                              */
     846             :                             do {
     847             :                                 nchars--;
     848             :                             } while (nchars > 1 &&
     849             :                                  (yytext[nchars - 1] == '+' ||
     850             :                                   yytext[nchars - 1] == '-'));
     851             :                         }
     852             :                     }
     853             : 
     854             :                     if (nchars < yyleng)
     855             :                     {
     856             :                         /* Strip the unwanted chars from the token */
     857             :                         yyless(nchars);
     858             :                         /*
     859             :                          * If what we have left is only one char, and it's
     860             :                          * one of the characters matching "self", then
     861             :                          * return it as a character token the same way
     862             :                          * that the "self" rule would have.
     863             :                          */
     864             :                         if (nchars == 1 &&
     865             :                             strchr(",()[].;:+-*/%^<>=", yytext[0]))
     866             :                             return yytext[0];
     867             :                         /*
     868             :                          * Likewise, if what we have left is two chars, and
     869             :                          * those match the tokens ">=", "<=", "=>", "<>" or
     870             :                          * "!=", then we must return the appropriate token
     871             :                          * rather than the generic Op.
     872             :                          */
     873             :                         if (nchars == 2)
     874             :                         {
     875             :                             if (yytext[0] == '=' && yytext[1] == '>')
     876             :                                 return EQUALS_GREATER;
     877             :                             if (yytext[0] == '>' && yytext[1] == '=')
     878             :                                 return GREATER_EQUALS;
     879             :                             if (yytext[0] == '<' && yytext[1] == '=')
     880             :                                 return LESS_EQUALS;
     881             :                             if (yytext[0] == '<' && yytext[1] == '>')
     882             :                                 return NOT_EQUALS;
     883             :                             if (yytext[0] == '!' && yytext[1] == '=')
     884             :                                 return NOT_EQUALS;
     885             :                         }
     886             :                     }
     887             : 
     888             :                     base_yylval.str = mm_strdup(yytext);
     889             :                     return Op;
     890             :                 }
     891             : 
     892             : {param}         {
     893             :                     base_yylval.ival = atol(yytext+1);
     894             :                     return PARAM;
     895             :                 }
     896             : 
     897             : {ip}            {
     898             :                     base_yylval.str = mm_strdup(yytext);
     899             :                     return IP;
     900             :                 }
     901             : }  /* <SQL> */
     902             : 
     903             : <C,SQL>{
     904             : {integer}       {
     905             :                     return process_integer_literal(yytext, &base_yylval);
     906             :                 }
     907             : {decimal}       {
     908             :                     base_yylval.str = mm_strdup(yytext);
     909             :                     return FCONST;
     910             :                 }
     911             : {decimalfail}   {
     912             :                     /* throw back the .., and treat as integer */
     913             :                     yyless(yyleng - 2);
     914             :                     return process_integer_literal(yytext, &base_yylval);
     915             :                 }
     916             : {real}          {
     917             :                     base_yylval.str = mm_strdup(yytext);
     918             :                     return FCONST;
     919             :                 }
     920             : {realfail1}     {
     921             :                     /*
     922             :                      * throw back the [Ee], and figure out whether what
     923             :                      * remains is an {integer} or {decimal}.
     924             :                      */
     925             :                     yyless(yyleng - 1);
     926             :                     return process_integer_literal(yytext, &base_yylval);
     927             :                 }
     928             : {realfail2}     {
     929             :                     /* throw back the [Ee][+-], and proceed as above */
     930             :                     yyless(yyleng - 2);
     931             :                     return process_integer_literal(yytext, &base_yylval);
     932             :                 }
     933             : } /* <C,SQL> */
     934             : 
     935             : <SQL>{
     936             : :{identifier}((("->"|\.){identifier})|(\[{array}\]))*  {
     937             :                     base_yylval.str = mm_strdup(yytext+1);
     938             :                     return CVARIABLE;
     939             :                 }
     940             : 
     941             : {identifier}    {
     942             :                     if (!isdefine())
     943             :                     {
     944             :                         int     kwvalue;
     945             : 
     946             :                         /* Is it an SQL/ECPG keyword? */
     947             :                         kwvalue = ScanECPGKeywordLookup(yytext);
     948             :                         if (kwvalue >= 0)
     949             :                             return kwvalue;
     950             : 
     951             :                         /* Is it a C keyword? */
     952             :                         kwvalue = ScanCKeywordLookup(yytext);
     953             :                         if (kwvalue >= 0)
     954             :                             return kwvalue;
     955             : 
     956             :                         /*
     957             :                          * None of the above.  Return it as an identifier.
     958             :                          *
     959             :                          * The backend will attempt to truncate and case-fold
     960             :                          * the identifier, but I see no good reason for ecpg
     961             :                          * to do so; that's just another way that ecpg could get
     962             :                          * out of step with the backend.
     963             :                          */
     964             :                         base_yylval.str = mm_strdup(yytext);
     965             :                         return IDENT;
     966             :                     }
     967             :                 }
     968             : 
     969             : {other}         {
     970             :                     return yytext[0];
     971             :                 }
     972             : } /* <SQL> */
     973             : 
     974             :     /*
     975             :      * Begin ECPG-specific rules
     976             :      */
     977             : 
     978             : <C>{exec_sql}     { BEGIN(SQL); return SQL_START; }
     979             : <C>{informix_special} {
     980             :                         /* are we simulating Informix? */
     981             :                         if (INFORMIX_MODE)
     982             :                         {
     983             :                             BEGIN(SQL);
     984             :                             return SQL_START;
     985             :                         }
     986             :                         else
     987             :                             return S_ANYTHING;
     988             :                      }
     989             : <C>{ccomment}     { ECHO; }
     990             : <C>{xch}          {
     991             :                         char* endptr;
     992             : 
     993             :                         errno = 0;
     994             :                         base_yylval.ival = strtoul((char *)yytext,&endptr,16);
     995             :                         if (*endptr != '\0' || errno == ERANGE)
     996             :                         {
     997             :                             errno = 0;
     998             :                             base_yylval.str = mm_strdup(yytext);
     999             :                             return SCONST;
    1000             :                         }
    1001             :                         return ICONST;
    1002             :                     }
    1003             : <C>{cppinclude}       {
    1004             :                         if (system_includes)
    1005             :                         {
    1006             :                             include_next = false;
    1007             :                             BEGIN(incl);
    1008             :                         }
    1009             :                         else
    1010             :                         {
    1011             :                             base_yylval.str = mm_strdup(yytext);
    1012             :                             return CPP_LINE;
    1013             :                         }
    1014             :                     }
    1015             : <C>{cppinclude_next}      {
    1016             :                         if (system_includes)
    1017             :                         {
    1018             :                             include_next = true;
    1019             :                             BEGIN(incl);
    1020             :                         }
    1021             :                         else
    1022             :                         {
    1023             :                             base_yylval.str = mm_strdup(yytext);
    1024             :                             return CPP_LINE;
    1025             :                         }
    1026             :                     }
    1027             : <C,SQL>{cppline}  {
    1028             :                         base_yylval.str = mm_strdup(yytext);
    1029             :                         return CPP_LINE;
    1030             :                     }
    1031             : <C>{identifier}       {
    1032             :                         /*
    1033             :                          * Try to detect a function name:
    1034             :                          * look for identifiers at the global scope
    1035             :                          * keep the last identifier before the first '(' and '{'
    1036             :                          */
    1037             :                         if (braces_open == 0 && parenths_open == 0)
    1038             :                         {
    1039             :                             if (current_function)
    1040             :                                 free(current_function);
    1041             :                             current_function = mm_strdup(yytext);
    1042             :                         }
    1043             :                         /* Informix uses SQL defines only in SQL space */
    1044             :                         /* however, some defines have to be taken care of for compatibility */
    1045             :                         if ((!INFORMIX_MODE || !isinformixdefine()) && !isdefine())
    1046             :                         {
    1047             :                             int     kwvalue;
    1048             : 
    1049             :                             kwvalue = ScanCKeywordLookup(yytext);
    1050             :                             if (kwvalue >= 0)
    1051             :                                 return kwvalue;
    1052             :                             else
    1053             :                             {
    1054             :                                 base_yylval.str = mm_strdup(yytext);
    1055             :                                 return IDENT;
    1056             :                             }
    1057             :                         }
    1058             :                     }
    1059             : <C>{xcstop}           { mmerror(PARSE_ERROR, ET_ERROR, "nested /* ... */ comments"); }
    1060             : <C>":"              { return ':'; }
    1061             : <C>";"              { return ';'; }
    1062             : <C>","              { return ','; }
    1063             : <C>"*"              { return '*'; }
    1064             : <C>"%"              { return '%'; }
    1065             : <C>"/"              { return '/'; }
    1066             : <C>"+"              { return '+'; }
    1067             : <C>"-"              { return '-'; }
    1068             : <C>"("              { parenths_open++; return '('; }
    1069             : <C>")"              { parenths_open--; return ')'; }
    1070             : <C,xskip>{space}      { ECHO; }
    1071             : <C>\{             { return '{'; }
    1072             : <C>\}             { return '}'; }
    1073             : <C>\[             { return '['; }
    1074             : <C>\]             { return ']'; }
    1075             : <C>\=             { return '='; }
    1076             : <C>"->"              { return S_MEMBER; }
    1077             : <C>">>"               { return S_RSHIFT; }
    1078             : <C>"<<"               { return S_LSHIFT; }
    1079             : <C>"||"             { return S_OR; }
    1080             : <C>"&&"             { return S_AND; }
    1081             : <C>"++"             { return S_INC; }
    1082             : <C>"--"             { return S_DEC; }
    1083             : <C>"=="             { return S_EQUAL; }
    1084             : <C>"!="             { return S_NEQUAL; }
    1085             : <C>"+="             { return S_ADD; }
    1086             : <C>"-="             { return S_SUB; }
    1087             : <C>"*="             { return S_MUL; }
    1088             : <C>"/="             { return S_DIV; }
    1089             : <C>"%="             { return S_MOD; }
    1090             : <C>"->*"         { return S_MEMPOINT; }
    1091             : <C>".*"             { return S_DOTPOINT; }
    1092             : <C>{other}            { return S_ANYTHING; }
    1093             : <C>{exec_sql}{define}{space}* { BEGIN(def_ident); }
    1094             : <C>{informix_special}{define}{space}* {
    1095             :                         /* are we simulating Informix? */
    1096             :                         if (INFORMIX_MODE)
    1097             :                         {
    1098             :                             BEGIN(def_ident);
    1099             :                         }
    1100             :                         else
    1101             :                         {
    1102             :                             yyless(1);
    1103             :                             return S_ANYTHING;
    1104             :                         }
    1105             :                     }
    1106             : <C>{exec_sql}{undef}{space}*      { BEGIN(undef); }
    1107             : <C>{informix_special}{undef}{space}* {
    1108             :                         /* are we simulating Informix? */
    1109             :                         if (INFORMIX_MODE)
    1110             :                         {
    1111             :                             BEGIN(undef);
    1112             :                         }
    1113             :                         else
    1114             :                         {
    1115             :                             yyless(1);
    1116             :                             return S_ANYTHING;
    1117             :                         }
    1118             :                     }
    1119             : <undef>{identifier}{space}*";" {
    1120             :                     struct _defines *ptr, *ptr2 = NULL;
    1121             :                     int i;
    1122             : 
    1123             :                     /*
    1124             :                      *  Skip the ";" and trailing whitespace. Note that yytext
    1125             :                      *  contains at least one non-space character plus the ";"
    1126             :                      */
    1127             :                     for (i = strlen(yytext)-2;
    1128             :                          i > 0 && ecpg_isspace(yytext[i]);
    1129             :                          i-- )
    1130             :                         ;
    1131             :                     yytext[i+1] = '\0';
    1132             : 
    1133             : 
    1134             :                     for (ptr = defines; ptr != NULL; ptr2 = ptr, ptr = ptr->next)
    1135             :                     {
    1136             :                         if (strcmp(yytext, ptr->olddef) == 0)
    1137             :                         {
    1138             :                             if (ptr2 == NULL)
    1139             :                                 defines = ptr->next;
    1140             :                             else
    1141             :                                 ptr2->next = ptr->next;
    1142             :                             free(ptr->newdef);
    1143             :                             free(ptr->olddef);
    1144             :                             free(ptr);
    1145             :                             break;
    1146             :                         }
    1147             :                     }
    1148             : 
    1149             :                     BEGIN(C);
    1150             :                 }
    1151             : <undef>{other}|\n {
    1152             :                         mmfatal(PARSE_ERROR, "missing identifier in EXEC SQL UNDEF command");
    1153             :                         yyterminate();
    1154             :                 }
    1155             : <C>{exec_sql}{include}{space}*    { BEGIN(incl); }
    1156             : <C>{informix_special}{include}{space}* {
    1157             :                       /* are we simulating Informix? */
    1158             :                       if (INFORMIX_MODE)
    1159             :                       {
    1160             :                           BEGIN(incl);
    1161             :                       }
    1162             :                       else
    1163             :                       {
    1164             :                           yyless(1);
    1165             :                           return S_ANYTHING;
    1166             :                       }
    1167             :                     }
    1168             : <C,xskip>{exec_sql}{ifdef}{space}*    { ifcond = true; BEGIN(xcond); }
    1169             : <C,xskip>{informix_special}{ifdef}{space}* {
    1170             :                       /* are we simulating Informix? */
    1171             :                       if (INFORMIX_MODE)
    1172             :                       {
    1173             :                           ifcond = true;
    1174             :                           BEGIN(xcond);
    1175             :                       }
    1176             :                       else
    1177             :                       {
    1178             :                           yyless(1);
    1179             :                           return S_ANYTHING;
    1180             :                       }
    1181             :                     }
    1182             : <C,xskip>{exec_sql}{ifndef}{space}* { ifcond = false; BEGIN(xcond); }
    1183             : <C,xskip>{informix_special}{ifndef}{space}* {
    1184             :                       /* are we simulating Informix? */
    1185             :                       if (INFORMIX_MODE)
    1186             :                       {
    1187             :                           ifcond = false;
    1188             :                           BEGIN(xcond);
    1189             :                       }
    1190             :                       else
    1191             :                       {
    1192             :                           yyless(1);
    1193             :                           return S_ANYTHING;
    1194             :                       }
    1195             :                     }
    1196             : <C,xskip>{exec_sql}{elif}{space}* {   /* pop stack */
    1197             :                         if ( preproc_tos == 0 ) {
    1198             :                             mmfatal(PARSE_ERROR, "missing matching \"EXEC SQL IFDEF\" / \"EXEC SQL IFNDEF\"");
    1199             :                         }
    1200             :                         else if ( stacked_if_value[preproc_tos].else_branch )
    1201             :                             mmfatal(PARSE_ERROR, "missing \"EXEC SQL ENDIF;\"");
    1202             :                         else
    1203             :                             preproc_tos--;
    1204             : 
    1205             :                         ifcond = true; BEGIN(xcond);
    1206             :                     }
    1207             : <C,xskip>{informix_special}{elif}{space}* {
    1208             :                     /* are we simulating Informix? */
    1209             :                     if (INFORMIX_MODE)
    1210             :                     {
    1211             :                         if (preproc_tos == 0)
    1212             :                             mmfatal(PARSE_ERROR, "missing matching \"EXEC SQL IFDEF\" / \"EXEC SQL IFNDEF\"");
    1213             :                         else if (stacked_if_value[preproc_tos].else_branch)
    1214             :                             mmfatal(PARSE_ERROR, "missing \"EXEC SQL ENDIF;\"");
    1215             :                         else
    1216             :                             preproc_tos--;
    1217             : 
    1218             :                         ifcond = true;
    1219             :                         BEGIN(xcond);
    1220             :                     }
    1221             :                     else
    1222             :                     {
    1223             :                         yyless(1);
    1224             :                         return S_ANYTHING;
    1225             :                     }
    1226             :                 }
    1227             : 
    1228             : <C,xskip>{exec_sql}{else}{space}*";" {  /* only exec sql endif pops the stack, so take care of duplicated 'else' */
    1229             :                     if (stacked_if_value[preproc_tos].else_branch)
    1230             :                         mmfatal(PARSE_ERROR, "more than one EXEC SQL ELSE");
    1231             :                     else
    1232             :                     {
    1233             :                         stacked_if_value[preproc_tos].else_branch = true;
    1234             :                         stacked_if_value[preproc_tos].condition =
    1235             :                             (stacked_if_value[preproc_tos-1].condition &&
    1236             :                              !stacked_if_value[preproc_tos].condition);
    1237             : 
    1238             :                         if (stacked_if_value[preproc_tos].condition)
    1239             :                             BEGIN(C);
    1240             :                         else
    1241             :                             BEGIN(xskip);
    1242             :                     }
    1243             :                 }
    1244             : <C,xskip>{informix_special}{else}{space}*";"    {
    1245             :                     /* are we simulating Informix? */
    1246             :                     if (INFORMIX_MODE)
    1247             :                     {
    1248             :                         if (stacked_if_value[preproc_tos].else_branch)
    1249             :                             mmfatal(PARSE_ERROR, "more than one EXEC SQL ELSE");
    1250             :                         else
    1251             :                         {
    1252             :                             stacked_if_value[preproc_tos].else_branch = true;
    1253             :                             stacked_if_value[preproc_tos].condition =
    1254             :                             (stacked_if_value[preproc_tos-1].condition &&
    1255             :                              !stacked_if_value[preproc_tos].condition);
    1256             : 
    1257             :                             if (stacked_if_value[preproc_tos].condition)
    1258             :                                 BEGIN(C);
    1259             :                             else
    1260             :                                 BEGIN(xskip);
    1261             :                         }
    1262             :                     }
    1263             :                     else
    1264             :                     {
    1265             :                         yyless(1);
    1266             :                         return S_ANYTHING;
    1267             :                     }
    1268             :                 }
    1269             : <C,xskip>{exec_sql}{endif}{space}*";" {
    1270             :                     if (preproc_tos == 0)
    1271             :                         mmfatal(PARSE_ERROR, "unmatched EXEC SQL ENDIF");
    1272             :                     else
    1273             :                         preproc_tos--;
    1274             : 
    1275             :                     if (stacked_if_value[preproc_tos].condition)
    1276             :                        BEGIN(C);
    1277             :                     else
    1278             :                        BEGIN(xskip);
    1279             :                 }
    1280             : <C,xskip>{informix_special}{endif}{space}*";"   {
    1281             :                     /* are we simulating Informix? */
    1282             :                     if (INFORMIX_MODE)
    1283             :                     {
    1284             :                         if (preproc_tos == 0)
    1285             :                             mmfatal(PARSE_ERROR, "unmatched EXEC SQL ENDIF");
    1286             :                         else
    1287             :                             preproc_tos--;
    1288             : 
    1289             :                         if (stacked_if_value[preproc_tos].condition)
    1290             :                             BEGIN(C);
    1291             :                         else
    1292             :                             BEGIN(xskip);
    1293             :                     }
    1294             :                     else
    1295             :                     {
    1296             :                         yyless(1);
    1297             :                         return S_ANYTHING;
    1298             :                     }
    1299             :                 }
    1300             : 
    1301             : <xskip>{other}        { /* ignore */ }
    1302             : 
    1303             : <xcond>{identifier}{space}*";" {
    1304             :                     if (preproc_tos >= MAX_NESTED_IF-1)
    1305             :                         mmfatal(PARSE_ERROR, "too many nested EXEC SQL IFDEF conditions");
    1306             :                     else
    1307             :                     {
    1308             :                         struct _defines *defptr;
    1309             :                         unsigned int i;
    1310             : 
    1311             :                         /*
    1312             :                          *  Skip the ";" and trailing whitespace. Note that yytext
    1313             :                          *  contains at least one non-space character plus the ";"
    1314             :                          */
    1315             :                         for (i = strlen(yytext)-2;
    1316             :                              i > 0 && ecpg_isspace(yytext[i]);
    1317             :                              i-- )
    1318             :                             ;
    1319             :                         yytext[i+1] = '\0';
    1320             : 
    1321             :                         for (defptr = defines;
    1322             :                              defptr != NULL &&
    1323             :                              strcmp(yytext, defptr->olddef) != 0;
    1324             :                              defptr = defptr->next)
    1325             :                             /* skip */ ;
    1326             : 
    1327             :                         preproc_tos++;
    1328             :                         stacked_if_value[preproc_tos].else_branch = false;
    1329             :                         stacked_if_value[preproc_tos].condition =
    1330             :                         (defptr ? ifcond : !ifcond) && stacked_if_value[preproc_tos-1].condition;
    1331             :                     }
    1332             : 
    1333             :                     if (stacked_if_value[preproc_tos].condition)
    1334             :                         BEGIN(C);
    1335             :                     else
    1336             :                         BEGIN(xskip);
    1337             :                 }
    1338             : 
    1339             : <xcond>{other}|\n {
    1340             :                 mmfatal(PARSE_ERROR, "missing identifier in EXEC SQL IFDEF command");
    1341             :                 yyterminate();
    1342             :             }
    1343             : <def_ident>{identifier} {
    1344             :                 old = mm_strdup(yytext);
    1345             :                 BEGIN(def);
    1346             :                 startlit();
    1347             :             }
    1348             : <def_ident>{other}|\n {
    1349             :                 mmfatal(PARSE_ERROR, "missing identifier in EXEC SQL DEFINE command");
    1350             :                 yyterminate();
    1351             :             }
    1352             : <def>{space}*";"    {
    1353             :                         struct _defines *ptr, *this;
    1354             : 
    1355             :                         for (ptr = defines; ptr != NULL; ptr = ptr->next)
    1356             :                         {
    1357             :                              if (strcmp(old, ptr->olddef) == 0)
    1358             :                              {
    1359             :                                 free(ptr->newdef);
    1360             :                                 ptr->newdef = mm_strdup(literalbuf);
    1361             :                              }
    1362             :                         }
    1363             :                         if (ptr == NULL)
    1364             :                         {
    1365             :                             this = (struct _defines *) mm_alloc(sizeof(struct _defines));
    1366             : 
    1367             :                             /* initial definition */
    1368             :                             this->olddef = old;
    1369             :                             this->newdef = mm_strdup(literalbuf);
    1370             :                             this->next = defines;
    1371             :                             this->used = NULL;
    1372             :                             defines = this;
    1373             :                         }
    1374             : 
    1375             :                         BEGIN(C);
    1376             :                     }
    1377             : <def>[^;]         { addlit(yytext, yyleng); }
    1378             : <incl>\<[^\>]+\>{space}*";"?       {   parse_include(); }
    1379             : <incl>{dquote}{xdinside}{dquote}{space}*";"?    {   parse_include(); }
    1380             : <incl>[^;\<\>\"]+";"     { parse_include(); }
    1381             : <incl>{other}|\n      {
    1382             :                     mmfatal(PARSE_ERROR, "syntax error in EXEC SQL INCLUDE command");
    1383             :                     yyterminate();
    1384             :                 }
    1385             : 
    1386             : <<EOF>>             {
    1387             :                     if (yy_buffer == NULL)
    1388             :                     {
    1389             :                         if ( preproc_tos > 0 )
    1390             :                         {
    1391             :                             preproc_tos = 0;
    1392             :                             mmfatal(PARSE_ERROR, "missing \"EXEC SQL ENDIF;\"");
    1393             :                         }
    1394             :                         yyterminate();
    1395             :                     }
    1396             :                     else
    1397             :                     {
    1398             :                         struct _yy_buffer *yb = yy_buffer;
    1399             :                         int i;
    1400             :                         struct _defines *ptr;
    1401             : 
    1402             :                         for (ptr = defines; ptr; ptr = ptr->next)
    1403             :                             if (ptr->used == yy_buffer)
    1404             :                             {
    1405             :                                 ptr->used = NULL;
    1406             :                                 break;
    1407             :                             }
    1408             : 
    1409             :                         if (yyin != NULL)
    1410             :                             fclose(yyin);
    1411             : 
    1412             :                         yy_delete_buffer( YY_CURRENT_BUFFER );
    1413             :                         yy_switch_to_buffer(yy_buffer->buffer);
    1414             : 
    1415             :                         yylineno = yy_buffer->lineno;
    1416             : 
    1417             :                         /* We have to output the filename only if we change files here */
    1418             :                         i = strcmp(input_filename, yy_buffer->filename);
    1419             : 
    1420             :                         free(input_filename);
    1421             :                         input_filename = yy_buffer->filename;
    1422             : 
    1423             :                         yy_buffer = yy_buffer->next;
    1424             :                         free(yb);
    1425             : 
    1426             :                         if (i != 0)
    1427             :                             output_line_number();
    1428             : 
    1429             :                     }
    1430             :                 }
    1431             : 
    1432             : <INITIAL>{other}|\n   { mmfatal(PARSE_ERROR, "internal error: unreachable state; please report this to <%s>", PACKAGE_BUGREPORT); }
    1433             : 
    1434             : %%
    1435             : 
    1436             : /* LCOV_EXCL_STOP */
    1437             : 
    1438             : void
    1439             : lex_init(void)
    1440         124 : {
    1441             :     braces_open = 0;
    1442         124 :     parenths_open = 0;
    1443         124 :     current_function = NULL;
    1444         124 : 
    1445             :     preproc_tos = 0;
    1446         124 :     yylineno = 1;
    1447         124 :     ifcond = true;
    1448         124 :     stacked_if_value[preproc_tos].condition = ifcond;
    1449         124 :     stacked_if_value[preproc_tos].else_branch = false;
    1450         124 : 
    1451             :     /* initialize literal buffer to a reasonable but expansible size */
    1452             :     if (literalbuf == NULL)
    1453         124 :     {
    1454             :         literalalloc = 1024;
    1455         124 :         literalbuf = (char *) mm_alloc(literalalloc);
    1456         124 :     }
    1457             :     startlit();
    1458         124 : 
    1459             :     BEGIN(C);
    1460         124 : }
    1461         124 : 
    1462             : static void
    1463             : addlit(char *ytext, int yleng)
    1464       45796 : {
    1465             :     /* enlarge buffer if needed */
    1466             :     if ((literallen+yleng) >= literalalloc)
    1467       45796 :     {
    1468             :         do
    1469             :             literalalloc *= 2;
    1470           0 :         while ((literallen+yleng) >= literalalloc);
    1471           0 :         literalbuf = (char *) realloc(literalbuf, literalalloc);
    1472           0 :     }
    1473             :     /* append new data, add trailing null */
    1474             :     memcpy(literalbuf+literallen, ytext, yleng);
    1475       45796 :     literallen += yleng;
    1476       45796 :     literalbuf[literallen] = '\0';
    1477       45796 : }
    1478       45796 : 
    1479             : static void
    1480             : addlitchar(unsigned char ychar)
    1481           4 : {
    1482             :     /* enlarge buffer if needed */
    1483             :     if ((literallen+1) >= literalalloc)
    1484           4 :     {
    1485             :         literalalloc *= 2;
    1486           0 :         literalbuf = (char *) realloc(literalbuf, literalalloc);
    1487           0 :     }
    1488             :     /* append new data, add trailing null */
    1489             :     literalbuf[literallen] = ychar;
    1490           4 :     literallen += 1;
    1491           4 :     literalbuf[literallen] = '\0';
    1492           4 : }
    1493           4 : 
    1494             : /*
    1495             :  * Process {integer}.  Note this will also do the right thing with {decimal},
    1496             :  * ie digits and a decimal point.
    1497             :  */
    1498             : static int
    1499             : process_integer_literal(const char *token, YYSTYPE *lval)
    1500        2234 : {
    1501             :     int         val;
    1502             :     char       *endptr;
    1503             : 
    1504             :     errno = 0;
    1505        2234 :     val = strtoint(token, &endptr, 10);
    1506        2234 :     if (*endptr != '\0' || errno == ERANGE)
    1507        2234 :     {
    1508             :         /* integer too large (or contains decimal pt), treat it as a float */
    1509             :         lval->str = mm_strdup(token);
    1510          12 :         return FCONST;
    1511          12 :     }
    1512             :     lval->ival = val;
    1513        2222 :     return ICONST;
    1514        2222 : }
    1515             : 
    1516             : static void
    1517             : parse_include(void)
    1518         162 : {
    1519             :     /* got the include file name */
    1520             :     struct _yy_buffer *yb;
    1521             :     struct _include_path *ip;
    1522             :     char inc_file[MAXPGPATH];
    1523             :     unsigned int i;
    1524             : 
    1525             :     yb = mm_alloc(sizeof(struct _yy_buffer));
    1526         162 : 
    1527             :     yb->buffer = YY_CURRENT_BUFFER;
    1528         162 :     yb->lineno = yylineno;
    1529         162 :     yb->filename = input_filename;
    1530         162 :     yb->next = yy_buffer;
    1531         162 : 
    1532             :     yy_buffer = yb;
    1533         162 : 
    1534             :     /*
    1535             :      * skip the ";" if there is one and trailing whitespace. Note that
    1536             :      * yytext contains at least one non-space character plus the ";"
    1537             :      */
    1538             :     for (i = strlen(yytext)-2;
    1539         164 :          i > 0 && ecpg_isspace(yytext[i]);
    1540         164 :          i--)
    1541           2 :         ;
    1542             : 
    1543             :     if (yytext[i] == ';')
    1544         162 :         i--;
    1545           0 : 
    1546             :     yytext[i+1] = '\0';
    1547         162 : 
    1548             :     yyin = NULL;
    1549         162 : 
    1550             :     /* If file name is enclosed in '"' remove these and look only in '.' */
    1551             :     /* Informix does look into all include paths though, except filename starts with '/' */
    1552             :     if (yytext[0] == '"' && yytext[i] == '"' &&
    1553         162 :         ((compat != ECPG_COMPAT_INFORMIX && compat != ECPG_COMPAT_INFORMIX_SE) || yytext[1] == '/'))
    1554           0 :     {
    1555             :         yytext[i] = '\0';
    1556           0 :         memmove(yytext, yytext+1, strlen(yytext));
    1557           0 : 
    1558             :         strlcpy(inc_file, yytext, sizeof(inc_file));
    1559           0 :         yyin = fopen(inc_file, "r");
    1560           0 :         if (!yyin)
    1561           0 :         {
    1562             :             if (strlen(inc_file) <= 2 || strcmp(inc_file + strlen(inc_file) - 2, ".h") != 0)
    1563           0 :             {
    1564             :                 strcat(inc_file, ".h");
    1565           0 :                 yyin = fopen(inc_file, "r");
    1566           0 :             }
    1567             :         }
    1568             : 
    1569             :     }
    1570             :     else
    1571             :     {
    1572             :         if ((yytext[0] == '"' && yytext[i] == '"') || (yytext[0] == '<' && yytext[i] == '>'))
    1573         162 :         {
    1574             :             yytext[i] = '\0';
    1575           4 :             memmove(yytext, yytext+1, strlen(yytext));
    1576           4 :         }
    1577             : 
    1578             :         for (ip = include_paths; yyin == NULL && ip != NULL; ip = ip->next)
    1579         444 :         {
    1580             :             if (strlen(ip->path) + strlen(yytext) + 4 > MAXPGPATH)
    1581         282 :             {
    1582             :                 fprintf(stderr, _("Error: include path \"%s/%s\" is too long on line %d, skipping\n"), ip->path, yytext, yylineno);
    1583           0 :                 continue;
    1584           0 :             }
    1585             :             snprintf (inc_file, sizeof(inc_file), "%s/%s", ip->path, yytext);
    1586         282 :             yyin = fopen(inc_file, "r");
    1587         282 :             if (!yyin)
    1588         282 :             {
    1589             :                 if (strcmp(inc_file + strlen(inc_file) - 2, ".h") != 0)
    1590         260 :                 {
    1591             :                     strcat(inc_file, ".h");
    1592         254 :                     yyin = fopen( inc_file, "r" );
    1593         254 :                 }
    1594             :             }
    1595             :             /* if the command was "include_next" we have to disregard the first hit */
    1596             :             if (yyin && include_next)
    1597         282 :             {
    1598             :                 fclose (yyin);
    1599           0 :                 yyin = NULL;
    1600           0 :                 include_next = false;
    1601           0 :             }
    1602             :         }
    1603             :     }
    1604             :     if (!yyin)
    1605         162 :         mmfatal(NO_INCLUDE_FILE, "could not open include file \"%s\" on line %d", yytext, yylineno);
    1606           0 : 
    1607             :     input_filename = mm_strdup(inc_file);
    1608         162 :     yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE ));
    1609         162 :     yylineno = 1;
    1610         162 :     output_line_number();
    1611         162 : 
    1612             :     BEGIN(C);
    1613         162 : }
    1614         162 : 
    1615             : /*
    1616             :  * ecpg_isspace() --- return true if flex scanner considers char whitespace
    1617             :  */
    1618             : static bool
    1619             : ecpg_isspace(char ch)
    1620         180 : {
    1621             :     if (ch == ' ' ||
    1622         180 :         ch == '\t' ||
    1623         180 :         ch == '\n' ||
    1624         178 :         ch == '\r' ||
    1625         178 :         ch == '\f')
    1626             :         return true;
    1627           2 :     return false;
    1628         178 : }
    1629             : 
    1630             : static bool isdefine(void)
    1631       25960 : {
    1632             :     struct _defines *ptr;
    1633             : 
    1634             :     /* is it a define? */
    1635             :     for (ptr = defines; ptr; ptr = ptr->next)
    1636      118138 :     {
    1637             :         if (strcmp(yytext, ptr->olddef) == 0 && ptr->used == NULL)
    1638       92332 :         {
    1639             :             struct _yy_buffer *yb;
    1640             : 
    1641             :             yb = mm_alloc(sizeof(struct _yy_buffer));
    1642         154 : 
    1643             :             yb->buffer =  YY_CURRENT_BUFFER;
    1644         154 :             yb->lineno = yylineno;
    1645         154 :             yb->filename = mm_strdup(input_filename);
    1646         154 :             yb->next = yy_buffer;
    1647         154 : 
    1648             :             ptr->used = yy_buffer = yb;
    1649         154 : 
    1650             :             yy_scan_string(ptr->newdef);
    1651         154 :             return true;
    1652         154 :         }
    1653             :     }
    1654             : 
    1655             :     return false;
    1656       25806 : }
    1657             : 
    1658             : static bool isinformixdefine(void)
    1659        3464 : {
    1660             :     const char *new = NULL;
    1661        3464 : 
    1662             :     if (strcmp(yytext, "dec_t") == 0)
    1663        3464 :         new = "decimal";
    1664           2 :     else if (strcmp(yytext, "intrvl_t") == 0)
    1665        3462 :         new = "interval";
    1666           0 :     else if (strcmp(yytext, "dtime_t") == 0)
    1667        3462 :         new = "timestamp";
    1668           0 : 
    1669             :     if (new)
    1670        3464 :     {
    1671             :         struct _yy_buffer *yb;
    1672             : 
    1673             :         yb = mm_alloc(sizeof(struct _yy_buffer));
    1674           2 : 
    1675             :         yb->buffer =  YY_CURRENT_BUFFER;
    1676           2 :         yb->lineno = yylineno;
    1677           2 :         yb->filename = mm_strdup(input_filename);
    1678           2 :         yb->next = yy_buffer;
    1679           2 :         yy_buffer = yb;
    1680           2 : 
    1681             :         yy_scan_string(new);
    1682           2 :         return true;
    1683           2 :     }
    1684             : 
    1685             :     return false;
    1686        3462 : }

Generated by: LCOV version 1.13