LCOV - code coverage report
Current view: top level - src/backend/parser - scan.l (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 115 131 87.8 %
Date: 2021-12-04 23:09:10 Functions: 16 18 88.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : %top{
       2             : /*-------------------------------------------------------------------------
       3             :  *
       4             :  * scan.l
       5             :  *    lexical scanner for PostgreSQL
       6             :  *
       7             :  * NOTE NOTE NOTE:
       8             :  *
       9             :  * The rules in this file must be kept in sync with src/fe_utils/psqlscan.l
      10             :  * and src/interfaces/ecpg/preproc/pgc.l!
      11             :  *
      12             :  * The rules are designed so that the scanner never has to backtrack,
      13             :  * in the sense that there is always a rule that can match the input
      14             :  * consumed so far (the rule action may internally throw back some input
      15             :  * with yyless(), however).  As explained in the flex manual, this makes
      16             :  * for a useful speed increase --- several percent faster when measuring
      17             :  * raw parsing (Flex + Bison).  The extra complexity is mostly in the rules
      18             :  * for handling float numbers and continued string literals.  If you change
      19             :  * the lexical rules, verify that you haven't broken the no-backtrack
      20             :  * property by running flex with the "-b" option and checking that the
      21             :  * resulting "lex.backup" file says that no backing up is needed.  (As of
      22             :  * Postgres 9.2, this check is made automatically by the Makefile.)
      23             :  *
      24             :  *
      25             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
      26             :  * Portions Copyright (c) 1994, Regents of the University of California
      27             :  *
      28             :  * IDENTIFICATION
      29             :  *    src/backend/parser/scan.l
      30             :  *
      31             :  *-------------------------------------------------------------------------
      32             :  */
      33             : #include "postgres.h"
      34             : 
      35             : #include <ctype.h>
      36             : #include <unistd.h>
      37             : 
      38             : #include "common/string.h"
      39             : #include "parser/gramparse.h"
      40             : #include "parser/parser.h"        /* only needed for GUC variables */
      41             : #include "parser/scansup.h"
      42             : #include "port/pg_bitutils.h"
      43             : #include "mb/pg_wchar.h"
      44             : }
      45             : 
      46             : %{
      47             : 
      48             : /* LCOV_EXCL_START */
      49             : 
      50             : /* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
      51             : #undef fprintf
      52             : #define fprintf(file, fmt, msg)  fprintf_to_ereport(fmt, msg)
      53             : 
      54             : static void
      55             : fprintf_to_ereport(const char *fmt, const char *msg)
      56             : {
      57             :     ereport(ERROR, (errmsg_internal("%s", msg)));
      58             : }
      59             : 
      60             : /*
      61             :  * GUC variables.  This is a DIRECT violation of the warning given at the
      62             :  * head of gram.y, ie flex/bison code must not depend on any GUC variables;
      63             :  * as such, changing their values can induce very unintuitive behavior.
      64             :  * But we shall have to live with it until we can remove these variables.
      65             :  */
      66             : int         backslash_quote = BACKSLASH_QUOTE_SAFE_ENCODING;
      67             : bool        escape_string_warning = true;
      68             : bool        standard_conforming_strings = true;
      69             : 
      70             : /*
      71             :  * Constant data exported from this file.  This array maps from the
      72             :  * zero-based keyword numbers returned by ScanKeywordLookup to the
      73             :  * Bison token numbers needed by gram.y.  This is exported because
      74             :  * callers need to pass it to scanner_init, if they are using the
      75             :  * standard keyword list ScanKeywords.
      76             :  */
      77             : #define PG_KEYWORD(kwname, value, category, collabel) value,
      78             : 
      79             : const uint16 ScanKeywordTokens[] = {
      80             : #include "parser/kwlist.h"
      81             : };
      82             : 
      83             : #undef PG_KEYWORD
      84             : 
      85             : /*
      86             :  * Set the type of YYSTYPE.
      87             :  */
      88             : #define YYSTYPE core_YYSTYPE
      89             : 
      90             : /*
      91             :  * Set the type of yyextra.  All state variables used by the scanner should
      92             :  * be in yyextra, *not* statically allocated.
      93             :  */
      94             : #define YY_EXTRA_TYPE core_yy_extra_type *
      95             : 
      96             : /*
      97             :  * Each call to yylex must set yylloc to the location of the found token
      98             :  * (expressed as a byte offset from the start of the input text).
      99             :  * When we parse a token that requires multiple lexer rules to process,
     100             :  * this should be done in the first such rule, else yylloc will point
     101             :  * into the middle of the token.
     102             :  */
     103             : #define SET_YYLLOC()  (*(yylloc) = yytext - yyextra->scanbuf)
     104             : 
     105             : /*
     106             :  * Advance yylloc by the given number of bytes.
     107             :  */
     108             : #define ADVANCE_YYLLOC(delta)  ( *(yylloc) += (delta) )
     109             : 
     110             : /*
     111             :  * Sometimes, we do want yylloc to point into the middle of a token; this is
     112             :  * useful for instance to throw an error about an escape sequence within a
     113             :  * string literal.  But if we find no error there, we want to revert yylloc
     114             :  * to the token start, so that that's the location reported to the parser.
     115             :  * Use PUSH_YYLLOC/POP_YYLLOC to save/restore yylloc around such code.
     116             :  * (Currently the implied "stack" is just one location, but someday we might
     117             :  * need to nest these.)
     118             :  */
     119             : #define PUSH_YYLLOC()   (yyextra->save_yylloc = *(yylloc))
     120             : #define POP_YYLLOC()    (*(yylloc) = yyextra->save_yylloc)
     121             : 
     122             : #define startlit()  ( yyextra->literallen = 0 )
     123             : static void addlit(char *ytext, int yleng, core_yyscan_t yyscanner);
     124             : static void addlitchar(unsigned char ychar, core_yyscan_t yyscanner);
     125             : static char *litbufdup(core_yyscan_t yyscanner);
     126             : static unsigned char unescape_single_char(unsigned char c, core_yyscan_t yyscanner);
     127             : static int  process_integer_literal(const char *token, YYSTYPE *lval);
     128             : static void addunicode(pg_wchar c, yyscan_t yyscanner);
     129             : 
     130             : #define yyerror(msg)  scanner_yyerror(msg, yyscanner)
     131             : 
     132             : #define lexer_errposition()  scanner_errposition(*(yylloc), yyscanner)
     133             : 
     134             : static void check_string_escape_warning(unsigned char ychar, core_yyscan_t yyscanner);
     135             : static void check_escape_warning(core_yyscan_t yyscanner);
     136             : 
     137             : /*
     138             :  * Work around a bug in flex 2.5.35: it emits a couple of functions that
     139             :  * it forgets to emit declarations for.  Since we use -Wmissing-prototypes,
     140             :  * this would cause warnings.  Providing our own declarations should be
     141             :  * harmless even when the bug gets fixed.
     142             :  */
     143             : extern int  core_yyget_column(yyscan_t yyscanner);
     144             : extern void core_yyset_column(int column_no, yyscan_t yyscanner);
     145             : 
     146             : %}
     147             : 
     148             : %option reentrant
     149             : %option bison-bridge
     150             : %option bison-locations
     151             : %option 8bit
     152             : %option never-interactive
     153             : %option nodefault
     154             : %option noinput
     155             : %option nounput
     156             : %option noyywrap
     157             : %option noyyalloc
     158             : %option noyyrealloc
     159             : %option noyyfree
     160             : %option warn
     161             : %option prefix="core_yy"
     162             : 
     163             : /*
     164             :  * OK, here is a short description of lex/flex rules behavior.
     165             :  * The longest pattern which matches an input string is always chosen.
     166             :  * For equal-length patterns, the first occurring in the rules list is chosen.
     167             :  * INITIAL is the starting state, to which all non-conditional rules apply.
     168             :  * Exclusive states change parsing rules while the state is active.  When in
     169             :  * an exclusive state, only those rules defined for that state apply.
     170             :  *
     171             :  * We use exclusive states for quoted strings, extended comments,
     172             :  * and to eliminate parsing troubles for numeric strings.
     173             :  * Exclusive states:
     174             :  *  <xb> bit string literal
     175             :  *  <xc> extended C-style comments
     176             :  *  <xd> delimited identifiers (double-quoted identifiers)
     177             :  *  <xh> hexadecimal byte string
     178             :  *  <xq> standard quoted strings
     179             :  *  <xqs> quote stop (detect continued strings)
     180             :  *  <xe> extended quoted strings (support backslash escape sequences)
     181             :  *  <xdolq> $foo$ quoted strings
     182             :  *  <xui> quoted identifier with Unicode escapes
     183             :  *  <xus> quoted string with Unicode escapes
     184             :  *  <xeu> Unicode surrogate pair in extended quoted string
     185             :  *
     186             :  * Remember to add an <<EOF>> case whenever you add a new exclusive state!
     187             :  * The default one is probably not the right thing.
     188             :  */
     189             : 
     190             : %x xb
     191             : %x xc
     192             : %x xd
     193             : %x xh
     194             : %x xq
     195             : %x xqs
     196             : %x xe
     197             : %x xdolq
     198             : %x xui
     199             : %x xus
     200             : %x xeu
     201             : 
     202             : /*
     203             :  * In order to make the world safe for Windows and Mac clients as well as
     204             :  * Unix ones, we accept either \n or \r as a newline.  A DOS-style \r\n
     205             :  * sequence will be seen as two successive newlines, but that doesn't cause
     206             :  * any problems.  Comments that start with -- and extend to the next
     207             :  * newline are treated as equivalent to a single whitespace character.
     208             :  *
     209             :  * NOTE a fine point: if there is no newline following --, we will absorb
     210             :  * everything to the end of the input as a comment.  This is correct.  Older
     211             :  * versions of Postgres failed to recognize -- as a comment if the input
     212             :  * did not end with a newline.
     213             :  *
     214             :  * XXX perhaps \f (formfeed) should be treated as a newline as well?
     215             :  *
     216             :  * XXX if you change the set of whitespace characters, fix scanner_isspace()
     217             :  * to agree.
     218             :  */
     219             : 
     220             : space           [ \t\n\r\f]
     221             : horiz_space     [ \t\f]
     222             : newline         [\n\r]
     223             : non_newline     [^\n\r]
     224             : 
     225             : comment         ("--"{non_newline}*)
     226             : 
     227             : whitespace      ({space}+|{comment})
     228             : 
     229             : /*
     230             :  * SQL requires at least one newline in the whitespace separating
     231             :  * string literals that are to be concatenated.  Silly, but who are we
     232             :  * to argue?  Note that {whitespace_with_newline} should not have * after
     233             :  * it, whereas {whitespace} should generally have a * after it...
     234             :  */
     235             : 
     236             : special_whitespace      ({space}+|{comment}{newline})
     237             : horiz_whitespace        ({horiz_space}|{comment})
     238             : whitespace_with_newline ({horiz_whitespace}*{newline}{special_whitespace}*)
     239             : 
     240             : quote           '
     241             : /* If we see {quote} then {quotecontinue}, the quoted string continues */
     242             : quotecontinue   {whitespace_with_newline}{quote}
     243             : 
     244             : /*
     245             :  * {quotecontinuefail} is needed to avoid lexer backup when we fail to match
     246             :  * {quotecontinue}.  It might seem that this could just be {whitespace}*,
     247             :  * but if there's a dash after {whitespace_with_newline}, it must be consumed
     248             :  * to see if there's another dash --- which would start a {comment} and thus
     249             :  * allow continuation of the {quotecontinue} token.
     250             :  */
     251             : quotecontinuefail   {whitespace}*"-"?
     252             : 
     253             : /* Bit string
     254             :  * It is tempting to scan the string for only those characters
     255             :  * which are allowed. However, this leads to silently swallowed
     256             :  * characters if illegal characters are included in the string.
     257             :  * For example, if xbinside is [01] then B'ABCD' is interpreted
     258             :  * as a zero-length string, and the ABCD' is lost!
     259             :  * Better to pass the string forward and let the input routines
     260             :  * validate the contents.
     261             :  */
     262             : xbstart         [bB]{quote}
     263             : xbinside        [^']*
     264             : 
     265             : /* Hexadecimal byte string */
     266             : xhstart         [xX]{quote}
     267             : xhinside        [^']*
     268             : 
     269             : /* National character */
     270             : xnstart         [nN]{quote}
     271             : 
     272             : /* Quoted string that allows backslash escapes */
     273             : xestart         [eE]{quote}
     274             : xeinside        [^\\']+
     275             : xeescape        [\\][^0-7]
     276             : xeoctesc        [\\][0-7]{1,3}
     277             : xehexesc        [\\]x[0-9A-Fa-f]{1,2}
     278             : xeunicode       [\\](u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})
     279             : xeunicodefail   [\\](u[0-9A-Fa-f]{0,3}|U[0-9A-Fa-f]{0,7})
     280             : 
     281             : /* Extended quote
     282             :  * xqdouble implements embedded quote, ''''
     283             :  */
     284             : xqstart         {quote}
     285             : xqdouble        {quote}{quote}
     286             : xqinside        [^']+
     287             : 
     288             : /* $foo$ style quotes ("dollar quoting")
     289             :  * The quoted string starts with $foo$ where "foo" is an optional string
     290             :  * in the form of an identifier, except that it may not contain "$",
     291             :  * and extends to the first occurrence of an identical string.
     292             :  * There is *no* processing of the quoted text.
     293             :  *
     294             :  * {dolqfailed} is an error rule to avoid scanner backup when {dolqdelim}
     295             :  * fails to match its trailing "$".
     296             :  */
     297             : dolq_start      [A-Za-z\200-\377_]
     298             : dolq_cont       [A-Za-z\200-\377_0-9]
     299             : dolqdelim       \$({dolq_start}{dolq_cont}*)?\$
     300             : dolqfailed      \${dolq_start}{dolq_cont}*
     301             : dolqinside      [^$]+
     302             : 
     303             : /* Double quote
     304             :  * Allows embedded spaces and other special characters into identifiers.
     305             :  */
     306             : dquote          \"
     307             : xdstart         {dquote}
     308             : xdstop          {dquote}
     309             : xddouble        {dquote}{dquote}
     310             : xdinside        [^"]+
     311             : 
     312             : /* Quoted identifier with Unicode escapes */
     313             : xuistart        [uU]&{dquote}
     314             : 
     315             : /* Quoted string with Unicode escapes */
     316             : xusstart        [uU]&{quote}
     317             : 
     318             : /* error rule to avoid backup */
     319             : xufailed        [uU]&
     320             : 
     321             : 
     322             : /* C-style comments
     323             :  *
     324             :  * The "extended comment" syntax closely resembles allowable operator syntax.
     325             :  * The tricky part here is to get lex to recognize a string starting with
     326             :  * slash-star as a comment, when interpreting it as an operator would produce
     327             :  * a longer match --- remember lex will prefer a longer match!  Also, if we
     328             :  * have something like plus-slash-star, lex will think this is a 3-character
     329             :  * operator whereas we want to see it as a + operator and a comment start.
     330             :  * The solution is two-fold:
     331             :  * 1. append {op_chars}* to xcstart so that it matches as much text as
     332             :  *    {operator} would. Then the tie-breaker (first matching rule of same
     333             :  *    length) ensures xcstart wins.  We put back the extra stuff with yyless()
     334             :  *    in case it contains a star-slash that should terminate the comment.
     335             :  * 2. In the operator rule, check for slash-star within the operator, and
     336             :  *    if found throw it back with yyless().  This handles the plus-slash-star
     337             :  *    problem.
     338             :  * Dash-dash comments have similar interactions with the operator rule.
     339             :  */
     340             : xcstart         \/\*{op_chars}*
     341             : xcstop          \*+\/
     342             : xcinside        [^*/]+
     343             : 
     344             : ident_start     [A-Za-z\200-\377_]
     345             : ident_cont      [A-Za-z\200-\377_0-9\$]
     346             : 
     347             : identifier      {ident_start}{ident_cont}*
     348             : 
     349             : /* Assorted special-case operators and operator-like tokens */
     350             : typecast        "::"
     351             : dot_dot         \.\.
     352             : colon_equals    ":="
     353             : 
     354             : /*
     355             :  * These operator-like tokens (unlike the above ones) also match the {operator}
     356             :  * rule, which means that they might be overridden by a longer match if they
     357             :  * are followed by a comment start or a + or - character. Accordingly, if you
     358             :  * add to this list, you must also add corresponding code to the {operator}
     359             :  * block to return the correct token in such cases. (This is not needed in
     360             :  * psqlscan.l since the token value is ignored there.)
     361             :  */
     362             : equals_greater  "=>"
     363             : less_equals     "<="
     364             : greater_equals  ">="
     365             : less_greater    "<>"
     366             : not_equals      "!="
     367             : 
     368             : /*
     369             :  * "self" is the set of chars that should be returned as single-character
     370             :  * tokens.  "op_chars" is the set of chars that can make up "Op" tokens,
     371             :  * which can be one or more characters long (but if a single-char token
     372             :  * appears in the "self" set, it is not to be returned as an Op).  Note
     373             :  * that the sets overlap, but each has some chars that are not in the other.
     374             :  *
     375             :  * If you change either set, adjust the character lists appearing in the
     376             :  * rule for "operator"!
     377             :  */
     378             : self            [,()\[\].;\:\+\-\*\/\%\^\<\>\=]
     379             : op_chars        [\~\!\@\#\^\&\|\`\?\+\-\*\/\%\<\>\=]
     380             : operator        {op_chars}+
     381             : 
     382             : /*
     383             :  * Numbers
     384             :  *
     385             :  * Unary minus is not part of a number here.  Instead we pass it separately to
     386             :  * the parser, and there it gets coerced via doNegate().
     387             :  *
     388             :  * {decimalfail} is used because we would like "1..10" to lex as 1, dot_dot, 10.
     389             :  *
     390             :  * {realfail1} and {realfail2} are added to prevent the need for scanner
     391             :  * backup when the {real} rule fails to match completely.
     392             :  */
     393             : digit           [0-9]
     394             : 
     395             : integer         {digit}+
     396             : decimal         (({digit}*\.{digit}+)|({digit}+\.{digit}*))
     397             : decimalfail     {digit}+\.\.
     398             : real            ({integer}|{decimal})[Ee][-+]?{digit}+
     399             : realfail1       ({integer}|{decimal})[Ee]
     400             : realfail2       ({integer}|{decimal})[Ee][-+]
     401             : 
     402             : param           \${integer}
     403             : 
     404             : other           .
     405             : 
     406             : /*
     407             :  * Dollar quoted strings are totally opaque, and no escaping is done on them.
     408             :  * Other quoted strings must allow some special characters such as single-quote
     409             :  *  and newline.
     410             :  * Embedded single-quotes are implemented both in the SQL standard
     411             :  *  style of two adjacent single quotes "''" and in the Postgres/Java style
     412             :  *  of escaped-quote "\'".
     413             :  * Other embedded escaped characters are matched explicitly and the leading
     414             :  *  backslash is dropped from the string.
     415             :  * Note that xcstart must appear before operator, as explained above!
     416             :  *  Also whitespace (comment) must appear before operator.
     417             :  */
     418             : 
     419             : %%
     420             : 
     421             : {whitespace}    {
     422             :                     /* ignore */
     423             :                 }
     424             : 
     425             : {xcstart}       {
     426             :                     /* Set location in case of syntax error in comment */
     427             :                     SET_YYLLOC();
     428             :                     yyextra->xcdepth = 0;
     429             :                     BEGIN(xc);
     430             :                     /* Put back any characters past slash-star; see above */
     431             :                     yyless(2);
     432             :                 }
     433             : 
     434             : <xc>{
     435             : {xcstart}       {
     436             :                     (yyextra->xcdepth)++;
     437             :                     /* Put back any characters past slash-star; see above */
     438             :                     yyless(2);
     439             :                 }
     440             : 
     441             : {xcstop}        {
     442             :                     if (yyextra->xcdepth <= 0)
     443             :                         BEGIN(INITIAL);
     444             :                     else
     445             :                         (yyextra->xcdepth)--;
     446             :                 }
     447             : 
     448             : {xcinside}      {
     449             :                     /* ignore */
     450             :                 }
     451             : 
     452             : {op_chars}      {
     453             :                     /* ignore */
     454             :                 }
     455             : 
     456             : \*+             {
     457             :                     /* ignore */
     458             :                 }
     459             : 
     460             : <<EOF>>         {
     461             :                     yyerror("unterminated /* comment");
     462             :                 }
     463             : } /* <xc> */
     464             : 
     465             : {xbstart}       {
     466             :                     /* Binary bit type.
     467             :                      * At some point we should simply pass the string
     468             :                      * forward to the parser and label it there.
     469             :                      * In the meantime, place a leading "b" on the string
     470             :                      * to mark it for the input routine as a binary string.
     471             :                      */
     472             :                     SET_YYLLOC();
     473             :                     BEGIN(xb);
     474             :                     startlit();
     475             :                     addlitchar('b', yyscanner);
     476             :                 }
     477             : <xh>{xhinside}    |
     478             : <xb>{xbinside}    {
     479             :                     addlit(yytext, yyleng, yyscanner);
     480             :                 }
     481             : <xb><<EOF>>       { yyerror("unterminated bit string literal"); }
     482             : 
     483             : {xhstart}       {
     484             :                     /* Hexadecimal bit type.
     485             :                      * At some point we should simply pass the string
     486             :                      * forward to the parser and label it there.
     487             :                      * In the meantime, place a leading "x" on the string
     488             :                      * to mark it for the input routine as a hex string.
     489             :                      */
     490             :                     SET_YYLLOC();
     491             :                     BEGIN(xh);
     492             :                     startlit();
     493             :                     addlitchar('x', yyscanner);
     494             :                 }
     495             : <xh><<EOF>>       { yyerror("unterminated hexadecimal string literal"); }
     496             : 
     497             : {xnstart}       {
     498             :                     /* National character.
     499             :                      * We will pass this along as a normal character string,
     500             :                      * but preceded with an internally-generated "NCHAR".
     501             :                      */
     502             :                     int     kwnum;
     503             : 
     504             :                     SET_YYLLOC();
     505             :                     yyless(1);  /* eat only 'n' this time */
     506             : 
     507             :                     kwnum = ScanKeywordLookup("nchar",
     508             :                                               yyextra->keywordlist);
     509             :                     if (kwnum >= 0)
     510             :                     {
     511             :                         yylval->keyword = GetScanKeyword(kwnum,
     512             :                                                          yyextra->keywordlist);
     513             :                         return yyextra->keyword_tokens[kwnum];
     514             :                     }
     515             :                     else
     516             :                     {
     517             :                         /* If NCHAR isn't a keyword, just return "n" */
     518             :                         yylval->str = pstrdup("n");
     519             :                         return IDENT;
     520             :                     }
     521             :                 }
     522             : 
     523             : {xqstart}       {
     524             :                     yyextra->warn_on_first_escape = true;
     525             :                     yyextra->saw_non_ascii = false;
     526             :                     SET_YYLLOC();
     527             :                     if (yyextra->standard_conforming_strings)
     528             :                         BEGIN(xq);
     529             :                     else
     530             :                         BEGIN(xe);
     531             :                     startlit();
     532             :                 }
     533             : {xestart}       {
     534             :                     yyextra->warn_on_first_escape = false;
     535             :                     yyextra->saw_non_ascii = false;
     536             :                     SET_YYLLOC();
     537             :                     BEGIN(xe);
     538             :                     startlit();
     539             :                 }
     540             : {xusstart}      {
     541             :                     SET_YYLLOC();
     542             :                     if (!yyextra->standard_conforming_strings)
     543             :                         ereport(ERROR,
     544             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     545             :                                  errmsg("unsafe use of string constant with Unicode escapes"),
     546             :                                  errdetail("String constants with Unicode escapes cannot be used when standard_conforming_strings is off."),
     547             :                                  lexer_errposition()));
     548             :                     BEGIN(xus);
     549             :                     startlit();
     550             :                 }
     551             : 
     552             : <xb,xh,xq,xe,xus>{quote} {
     553             :                     /*
     554             :                      * When we are scanning a quoted string and see an end
     555             :                      * quote, we must look ahead for a possible continuation.
     556             :                      * If we don't see one, we know the end quote was in fact
     557             :                      * the end of the string.  To reduce the lexer table size,
     558             :                      * we use a single "xqs" state to do the lookahead for all
     559             :                      * types of strings.
     560             :                      */
     561             :                     yyextra->state_before_str_stop = YYSTATE;
     562             :                     BEGIN(xqs);
     563             :                 }
     564             : <xqs>{quotecontinue} {
     565             :                     /*
     566             :                      * Found a quote continuation, so return to the in-quote
     567             :                      * state and continue scanning the literal.  Nothing is
     568             :                      * added to the literal's contents.
     569             :                      */
     570             :                     BEGIN(yyextra->state_before_str_stop);
     571             :                 }
     572             : <xqs>{quotecontinuefail} |
     573             : <xqs>{other} |
     574             : <xqs><<EOF>>  {
     575             :                     /*
     576             :                      * Failed to see a quote continuation.  Throw back
     577             :                      * everything after the end quote, and handle the string
     578             :                      * according to the state we were in previously.
     579             :                      */
     580             :                     yyless(0);
     581             :                     BEGIN(INITIAL);
     582             : 
     583             :                     switch (yyextra->state_before_str_stop)
     584             :                     {
     585             :                         case xb:
     586             :                             yylval->str = litbufdup(yyscanner);
     587             :                             return BCONST;
     588             :                         case xh:
     589             :                             yylval->str = litbufdup(yyscanner);
     590             :                             return XCONST;
     591             :                         case xq:
     592             :                         case xe:
     593             :                             /*
     594             :                              * Check that the data remains valid, if it might
     595             :                              * have been made invalid by unescaping any chars.
     596             :                              */
     597             :                             if (yyextra->saw_non_ascii)
     598             :                                 pg_verifymbstr(yyextra->literalbuf,
     599             :                                                yyextra->literallen,
     600             :                                                false);
     601             :                             yylval->str = litbufdup(yyscanner);
     602             :                             return SCONST;
     603             :                         case xus:
     604             :                             yylval->str = litbufdup(yyscanner);
     605             :                             return USCONST;
     606             :                         default:
     607             :                             yyerror("unhandled previous state in xqs");
     608             :                     }
     609             :                 }
     610             : 
     611             : <xq,xe,xus>{xqdouble} {
     612             :                     addlitchar('\'', yyscanner);
     613             :                 }
     614             : <xq,xus>{xqinside}  {
     615             :                     addlit(yytext, yyleng, yyscanner);
     616             :                 }
     617             : <xe>{xeinside}  {
     618             :                     addlit(yytext, yyleng, yyscanner);
     619             :                 }
     620             : <xe>{xeunicode} {
     621             :                     pg_wchar    c = strtoul(yytext + 2, NULL, 16);
     622             : 
     623             :                     /*
     624             :                      * For consistency with other productions, issue any
     625             :                      * escape warning with cursor pointing to start of string.
     626             :                      * We might want to change that, someday.
     627             :                      */
     628             :                     check_escape_warning(yyscanner);
     629             : 
     630             :                     /* Remember start of overall string token ... */
     631             :                     PUSH_YYLLOC();
     632             :                     /* ... and set the error cursor to point at this esc seq */
     633             :                     SET_YYLLOC();
     634             : 
     635             :                     if (is_utf16_surrogate_first(c))
     636             :                     {
     637             :                         yyextra->utf16_first_part = c;
     638             :                         BEGIN(xeu);
     639             :                     }
     640             :                     else if (is_utf16_surrogate_second(c))
     641             :                         yyerror("invalid Unicode surrogate pair");
     642             :                     else
     643             :                         addunicode(c, yyscanner);
     644             : 
     645             :                     /* Restore yylloc to be start of string token */
     646             :                     POP_YYLLOC();
     647             :                 }
     648             : <xeu>{xeunicode} {
     649             :                     pg_wchar    c = strtoul(yytext + 2, NULL, 16);
     650             : 
     651             :                     /* Remember start of overall string token ... */
     652             :                     PUSH_YYLLOC();
     653             :                     /* ... and set the error cursor to point at this esc seq */
     654             :                     SET_YYLLOC();
     655             : 
     656             :                     if (!is_utf16_surrogate_second(c))
     657             :                         yyerror("invalid Unicode surrogate pair");
     658             : 
     659             :                     c = surrogate_pair_to_codepoint(yyextra->utf16_first_part, c);
     660             : 
     661             :                     addunicode(c, yyscanner);
     662             : 
     663             :                     /* Restore yylloc to be start of string token */
     664             :                     POP_YYLLOC();
     665             : 
     666             :                     BEGIN(xe);
     667             :                 }
     668             : <xeu>. |
     669             : <xeu>\n |
     670             : <xeu><<EOF>>  {
     671             :                     /* Set the error cursor to point at missing esc seq */
     672             :                     SET_YYLLOC();
     673             :                     yyerror("invalid Unicode surrogate pair");
     674             :                 }
     675             : <xe,xeu>{xeunicodefail}   {
     676             :                     /* Set the error cursor to point at malformed esc seq */
     677             :                     SET_YYLLOC();
     678             :                     ereport(ERROR,
     679             :                             (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
     680             :                              errmsg("invalid Unicode escape"),
     681             :                              errhint("Unicode escapes must be \\uXXXX or \\UXXXXXXXX."),
     682             :                              lexer_errposition()));
     683             :                 }
     684             : <xe>{xeescape}  {
     685             :                     if (yytext[1] == '\'')
     686             :                     {
     687             :                         if (yyextra->backslash_quote == BACKSLASH_QUOTE_OFF ||
     688             :                             (yyextra->backslash_quote == BACKSLASH_QUOTE_SAFE_ENCODING &&
     689             :                              PG_ENCODING_IS_CLIENT_ONLY(pg_get_client_encoding())))
     690             :                             ereport(ERROR,
     691             :                                     (errcode(ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER),
     692             :                                      errmsg("unsafe use of \\' in a string literal"),
     693             :                                      errhint("Use '' to write quotes in strings. \\' is insecure in client-only encodings."),
     694             :                                      lexer_errposition()));
     695             :                     }
     696             :                     check_string_escape_warning(yytext[1], yyscanner);
     697             :                     addlitchar(unescape_single_char(yytext[1], yyscanner),
     698             :                                yyscanner);
     699             :                 }
     700             : <xe>{xeoctesc}  {
     701             :                     unsigned char c = strtoul(yytext + 1, NULL, 8);
     702             : 
     703             :                     check_escape_warning(yyscanner);
     704             :                     addlitchar(c, yyscanner);
     705             :                     if (c == '\0' || IS_HIGHBIT_SET(c))
     706             :                         yyextra->saw_non_ascii = true;
     707             :                 }
     708             : <xe>{xehexesc}  {
     709             :                     unsigned char c = strtoul(yytext + 2, NULL, 16);
     710             : 
     711             :                     check_escape_warning(yyscanner);
     712             :                     addlitchar(c, yyscanner);
     713             :                     if (c == '\0' || IS_HIGHBIT_SET(c))
     714             :                         yyextra->saw_non_ascii = true;
     715             :                 }
     716             : <xe>.         {
     717             :                     /* This is only needed for \ just before EOF */
     718             :                     addlitchar(yytext[0], yyscanner);
     719             :                 }
     720             : <xq,xe,xus><<EOF>>        { yyerror("unterminated quoted string"); }
     721             : 
     722             : {dolqdelim}     {
     723             :                     SET_YYLLOC();
     724             :                     yyextra->dolqstart = pstrdup(yytext);
     725             :                     BEGIN(xdolq);
     726             :                     startlit();
     727             :                 }
     728             : {dolqfailed}    {
     729             :                     SET_YYLLOC();
     730             :                     /* throw back all but the initial "$" */
     731             :                     yyless(1);
     732             :                     /* and treat it as {other} */
     733             :                     return yytext[0];
     734             :                 }
     735             : <xdolq>{dolqdelim} {
     736             :                     if (strcmp(yytext, yyextra->dolqstart) == 0)
     737             :                     {
     738             :                         pfree(yyextra->dolqstart);
     739             :                         yyextra->dolqstart = NULL;
     740             :                         BEGIN(INITIAL);
     741             :                         yylval->str = litbufdup(yyscanner);
     742             :                         return SCONST;
     743             :                     }
     744             :                     else
     745             :                     {
     746             :                         /*
     747             :                          * When we fail to match $...$ to dolqstart, transfer
     748             :                          * the $... part to the output, but put back the final
     749             :                          * $ for rescanning.  Consider $delim$...$junk$delim$
     750             :                          */
     751             :                         addlit(yytext, yyleng - 1, yyscanner);
     752             :                         yyless(yyleng - 1);
     753             :                     }
     754             :                 }
     755             : <xdolq>{dolqinside} {
     756             :                     addlit(yytext, yyleng, yyscanner);
     757             :                 }
     758             : <xdolq>{dolqfailed} {
     759             :                     addlit(yytext, yyleng, yyscanner);
     760             :                 }
     761             : <xdolq>.      {
     762             :                     /* This is only needed for $ inside the quoted text */
     763             :                     addlitchar(yytext[0], yyscanner);
     764             :                 }
     765             : <xdolq><<EOF>>    { yyerror("unterminated dollar-quoted string"); }
     766             : 
     767             : {xdstart}       {
     768             :                     SET_YYLLOC();
     769             :                     BEGIN(xd);
     770             :                     startlit();
     771             :                 }
     772             : {xuistart}      {
     773             :                     SET_YYLLOC();
     774             :                     BEGIN(xui);
     775             :                     startlit();
     776             :                 }
     777             : <xd>{xdstop}  {
     778             :                     char       *ident;
     779             : 
     780             :                     BEGIN(INITIAL);
     781             :                     if (yyextra->literallen == 0)
     782             :                         yyerror("zero-length delimited identifier");
     783             :                     ident = litbufdup(yyscanner);
     784             :                     if (yyextra->literallen >= NAMEDATALEN)
     785             :                         truncate_identifier(ident, yyextra->literallen, true);
     786             :                     yylval->str = ident;
     787             :                     return IDENT;
     788             :                 }
     789             : <xui>{dquote} {
     790             :                     BEGIN(INITIAL);
     791             :                     if (yyextra->literallen == 0)
     792             :                         yyerror("zero-length delimited identifier");
     793             :                     /* can't truncate till after we de-escape the ident */
     794             :                     yylval->str = litbufdup(yyscanner);
     795             :                     return UIDENT;
     796             :                 }
     797             : <xd,xui>{xddouble}    {
     798             :                     addlitchar('"', yyscanner);
     799             :                 }
     800             : <xd,xui>{xdinside}    {
     801             :                     addlit(yytext, yyleng, yyscanner);
     802             :                 }
     803             : <xd,xui><<EOF>>       { yyerror("unterminated quoted identifier"); }
     804             : 
     805             : {xufailed}  {
     806             :                     char       *ident;
     807             : 
     808             :                     SET_YYLLOC();
     809             :                     /* throw back all but the initial u/U */
     810             :                     yyless(1);
     811             :                     /* and treat it as {identifier} */
     812             :                     ident = downcase_truncate_identifier(yytext, yyleng, true);
     813             :                     yylval->str = ident;
     814             :                     return IDENT;
     815             :                 }
     816             : 
     817             : {typecast}      {
     818             :                     SET_YYLLOC();
     819             :                     return TYPECAST;
     820             :                 }
     821             : 
     822             : {dot_dot}       {
     823             :                     SET_YYLLOC();
     824             :                     return DOT_DOT;
     825             :                 }
     826             : 
     827             : {colon_equals}  {
     828             :                     SET_YYLLOC();
     829             :                     return COLON_EQUALS;
     830             :                 }
     831             : 
     832             : {equals_greater} {
     833             :                     SET_YYLLOC();
     834             :                     return EQUALS_GREATER;
     835             :                 }
     836             : 
     837             : {less_equals}   {
     838             :                     SET_YYLLOC();
     839             :                     return LESS_EQUALS;
     840             :                 }
     841             : 
     842             : {greater_equals} {
     843             :                     SET_YYLLOC();
     844             :                     return GREATER_EQUALS;
     845             :                 }
     846             : 
     847             : {less_greater}  {
     848             :                     /* We accept both "<>" and "!=" as meaning NOT_EQUALS */
     849             :                     SET_YYLLOC();
     850             :                     return NOT_EQUALS;
     851             :                 }
     852             : 
     853             : {not_equals}    {
     854             :                     /* We accept both "<>" and "!=" as meaning NOT_EQUALS */
     855             :                     SET_YYLLOC();
     856             :                     return NOT_EQUALS;
     857             :                 }
     858             : 
     859             : {self}          {
     860             :                     SET_YYLLOC();
     861             :                     return yytext[0];
     862             :                 }
     863             : 
     864             : {operator}      {
     865             :                     /*
     866             :                      * Check for embedded slash-star or dash-dash; those
     867             :                      * are comment starts, so operator must stop there.
     868             :                      * Note that slash-star or dash-dash at the first
     869             :                      * character will match a prior rule, not this one.
     870             :                      */
     871             :                     int         nchars = yyleng;
     872             :                     char       *slashstar = strstr(yytext, "/*");
     873             :                     char       *dashdash = strstr(yytext, "--");
     874             : 
     875             :                     if (slashstar && dashdash)
     876             :                     {
     877             :                         /* if both appear, take the first one */
     878             :                         if (slashstar > dashdash)
     879             :                             slashstar = dashdash;
     880             :                     }
     881             :                     else if (!slashstar)
     882             :                         slashstar = dashdash;
     883             :                     if (slashstar)
     884             :                         nchars = slashstar - yytext;
     885             : 
     886             :                     /*
     887             :                      * For SQL compatibility, '+' and '-' cannot be the
     888             :                      * last char of a multi-char operator unless the operator
     889             :                      * contains chars that are not in SQL operators.
     890             :                      * The idea is to lex '=-' as two operators, but not
     891             :                      * to forbid operator names like '?-' that could not be
     892             :                      * sequences of SQL operators.
     893             :                      */
     894             :                     if (nchars > 1 &&
     895             :                         (yytext[nchars - 1] == '+' ||
     896             :                          yytext[nchars - 1] == '-'))
     897             :                     {
     898             :                         int         ic;
     899             : 
     900             :                         for (ic = nchars - 2; ic >= 0; ic--)
     901             :                         {
     902             :                             char c = yytext[ic];
     903             :                             if (c == '~' || c == '!' || c == '@' ||
     904             :                                 c == '#' || c == '^' || c == '&' ||
     905             :                                 c == '|' || c == '`' || c == '?' ||
     906             :                                 c == '%')
     907             :                                 break;
     908             :                         }
     909             :                         if (ic < 0)
     910             :                         {
     911             :                             /*
     912             :                              * didn't find a qualifying character, so remove
     913             :                              * all trailing [+-]
     914             :                              */
     915             :                             do {
     916             :                                 nchars--;
     917             :                             } while (nchars > 1 &&
     918             :                                  (yytext[nchars - 1] == '+' ||
     919             :                                   yytext[nchars - 1] == '-'));
     920             :                         }
     921             :                     }
     922             : 
     923             :                     SET_YYLLOC();
     924             : 
     925             :                     if (nchars < yyleng)
     926             :                     {
     927             :                         /* Strip the unwanted chars from the token */
     928             :                         yyless(nchars);
     929             :                         /*
     930             :                          * If what we have left is only one char, and it's
     931             :                          * one of the characters matching "self", then
     932             :                          * return it as a character token the same way
     933             :                          * that the "self" rule would have.
     934             :                          */
     935             :                         if (nchars == 1 &&
     936             :                             strchr(",()[].;:+-*/%^<>=", yytext[0]))
     937             :                             return yytext[0];
     938             :                         /*
     939             :                          * Likewise, if what we have left is two chars, and
     940             :                          * those match the tokens ">=", "<=", "=>", "<>" or
     941             :                          * "!=", then we must return the appropriate token
     942             :                          * rather than the generic Op.
     943             :                          */
     944             :                         if (nchars == 2)
     945             :                         {
     946             :                             if (yytext[0] == '=' && yytext[1] == '>')
     947             :                                 return EQUALS_GREATER;
     948             :                             if (yytext[0] == '>' && yytext[1] == '=')
     949             :                                 return GREATER_EQUALS;
     950             :                             if (yytext[0] == '<' && yytext[1] == '=')
     951             :                                 return LESS_EQUALS;
     952             :                             if (yytext[0] == '<' && yytext[1] == '>')
     953             :                                 return NOT_EQUALS;
     954             :                             if (yytext[0] == '!' && yytext[1] == '=')
     955             :                                 return NOT_EQUALS;
     956             :                         }
     957             :                     }
     958             : 
     959             :                     /*
     960             :                      * Complain if operator is too long.  Unlike the case
     961             :                      * for identifiers, we make this an error not a notice-
     962             :                      * and-truncate, because the odds are we are looking at
     963             :                      * a syntactic mistake anyway.
     964             :                      */
     965             :                     if (nchars >= NAMEDATALEN)
     966             :                         yyerror("operator too long");
     967             : 
     968             :                     yylval->str = pstrdup(yytext);
     969             :                     return Op;
     970             :                 }
     971             : 
     972             : {param}         {
     973             :                     SET_YYLLOC();
     974             :                     yylval->ival = atol(yytext + 1);
     975             :                     return PARAM;
     976             :                 }
     977             : 
     978             : {integer}       {
     979             :                     SET_YYLLOC();
     980             :                     return process_integer_literal(yytext, yylval);
     981             :                 }
     982             : {decimal}       {
     983             :                     SET_YYLLOC();
     984             :                     yylval->str = pstrdup(yytext);
     985             :                     return FCONST;
     986             :                 }
     987             : {decimalfail}   {
     988             :                     /* throw back the .., and treat as integer */
     989             :                     yyless(yyleng - 2);
     990             :                     SET_YYLLOC();
     991             :                     return process_integer_literal(yytext, yylval);
     992             :                 }
     993             : {real}          {
     994             :                     SET_YYLLOC();
     995             :                     yylval->str = pstrdup(yytext);
     996             :                     return FCONST;
     997             :                 }
     998             : {realfail1}     {
     999             :                     /*
    1000             :                      * throw back the [Ee], and figure out whether what
    1001             :                      * remains is an {integer} or {decimal}.
    1002             :                      */
    1003             :                     yyless(yyleng - 1);
    1004             :                     SET_YYLLOC();
    1005             :                     return process_integer_literal(yytext, yylval);
    1006             :                 }
    1007             : {realfail2}     {
    1008             :                     /* throw back the [Ee][+-], and proceed as above */
    1009             :                     yyless(yyleng - 2);
    1010             :                     SET_YYLLOC();
    1011             :                     return process_integer_literal(yytext, yylval);
    1012             :                 }
    1013             : 
    1014             : 
    1015             : {identifier}    {
    1016             :                     int         kwnum;
    1017             :                     char       *ident;
    1018             : 
    1019             :                     SET_YYLLOC();
    1020             : 
    1021             :                     /* Is it a keyword? */
    1022             :                     kwnum = ScanKeywordLookup(yytext,
    1023             :                                               yyextra->keywordlist);
    1024             :                     if (kwnum >= 0)
    1025             :                     {
    1026             :                         yylval->keyword = GetScanKeyword(kwnum,
    1027             :                                                          yyextra->keywordlist);
    1028             :                         return yyextra->keyword_tokens[kwnum];
    1029             :                     }
    1030             : 
    1031             :                     /*
    1032             :                      * No.  Convert the identifier to lower case, and truncate
    1033             :                      * if necessary.
    1034             :                      */
    1035             :                     ident = downcase_truncate_identifier(yytext, yyleng, true);
    1036             :                     yylval->str = ident;
    1037             :                     return IDENT;
    1038             :                 }
    1039             : 
    1040             : {other}         {
    1041             :                     SET_YYLLOC();
    1042             :                     return yytext[0];
    1043             :                 }
    1044             : 
    1045             : <<EOF>>         {
    1046             :                     SET_YYLLOC();
    1047             :                     yyterminate();
    1048             :                 }
    1049             : 
    1050             : %%
    1051             : 
    1052             : /* LCOV_EXCL_STOP */
    1053             : 
    1054             : /*
    1055             :  * Arrange access to yyextra for subroutines of the main yylex() function.
    1056             :  * We expect each subroutine to have a yyscanner parameter.  Rather than
    1057             :  * use the yyget_xxx functions, which might or might not get inlined by the
    1058             :  * compiler, we cheat just a bit and cast yyscanner to the right type.
    1059             :  */
    1060             : #undef yyextra
    1061             : #define yyextra  (((struct yyguts_t *) yyscanner)->yyextra_r)
    1062             : 
    1063             : /* Likewise for a couple of other things we need. */
    1064             : #undef yylloc
    1065             : #define yylloc  (((struct yyguts_t *) yyscanner)->yylloc_r)
    1066             : #undef yyleng
    1067             : #define yyleng  (((struct yyguts_t *) yyscanner)->yyleng_r)
    1068             : 
    1069             : 
    1070             : /*
    1071             :  * scanner_errposition
    1072             :  *      Report a lexer or grammar error cursor position, if possible.
    1073             :  *
    1074             :  * This is expected to be used within an ereport() call, or via an error
    1075             :  * callback such as setup_scanner_errposition_callback().  The return value
    1076             :  * is a dummy (always 0, in fact).
    1077             :  *
    1078             :  * Note that this can only be used for messages emitted during raw parsing
    1079             :  * (essentially, scan.l, parser.c, and gram.y), since it requires the
    1080             :  * yyscanner struct to still be available.
    1081             :  */
    1082             : int
    1083         626 : scanner_errposition(int location, core_yyscan_t yyscanner)
    1084             : {
    1085             :     int         pos;
    1086             : 
    1087         626 :     if (location < 0)
    1088           0 :         return 0;               /* no-op if location is unknown */
    1089             : 
    1090             :     /* Convert byte offset to character number */
    1091         626 :     pos = pg_mbstrlen_with_len(yyextra->scanbuf, location) + 1;
    1092             :     /* And pass it to the ereport mechanism */
    1093         626 :     return errposition(pos);
    1094             : }
    1095             : 
    1096             : /*
    1097             :  * Error context callback for inserting scanner error location.
    1098             :  *
    1099             :  * Note that this will be called for *any* error occurring while the
    1100             :  * callback is installed.  We avoid inserting an irrelevant error location
    1101             :  * if the error is a query cancel --- are there any other important cases?
    1102             :  */
    1103             : static void
    1104          24 : scb_error_callback(void *arg)
    1105             : {
    1106          24 :     ScannerCallbackState *scbstate = (ScannerCallbackState *) arg;
    1107             : 
    1108          24 :     if (geterrcode() != ERRCODE_QUERY_CANCELED)
    1109          24 :         (void) scanner_errposition(scbstate->location, scbstate->yyscanner);
    1110          24 : }
    1111             : 
    1112             : /*
    1113             :  * setup_scanner_errposition_callback
    1114             :  *      Arrange for non-scanner errors to report an error position
    1115             :  *
    1116             :  * Sometimes the scanner calls functions that aren't part of the scanner
    1117             :  * subsystem and can't reasonably be passed the yyscanner pointer; yet
    1118             :  * we would like any errors thrown in those functions to be tagged with an
    1119             :  * error location.  Use this function to set up an error context stack
    1120             :  * entry that will accomplish that.  Usage pattern:
    1121             :  *
    1122             :  *      declare a local variable "ScannerCallbackState scbstate"
    1123             :  *      ...
    1124             :  *      setup_scanner_errposition_callback(&scbstate, yyscanner, location);
    1125             :  *      call function that might throw error;
    1126             :  *      cancel_scanner_errposition_callback(&scbstate);
    1127             :  */
    1128             : void
    1129         352 : setup_scanner_errposition_callback(ScannerCallbackState *scbstate,
    1130             :                                    core_yyscan_t yyscanner,
    1131             :                                    int location)
    1132             : {
    1133             :     /* Setup error traceback support for ereport() */
    1134         352 :     scbstate->yyscanner = yyscanner;
    1135         352 :     scbstate->location = location;
    1136         352 :     scbstate->errcallback.callback = scb_error_callback;
    1137         352 :     scbstate->errcallback.arg = (void *) scbstate;
    1138         352 :     scbstate->errcallback.previous = error_context_stack;
    1139         352 :     error_context_stack = &scbstate->errcallback;
    1140         352 : }
    1141             : 
    1142             : /*
    1143             :  * Cancel a previously-set-up errposition callback.
    1144             :  */
    1145             : void
    1146         328 : cancel_scanner_errposition_callback(ScannerCallbackState *scbstate)
    1147             : {
    1148             :     /* Pop the error context stack */
    1149         328 :     error_context_stack = scbstate->errcallback.previous;
    1150         328 : }
    1151             : 
    1152             : /*
    1153             :  * scanner_yyerror
    1154             :  *      Report a lexer or grammar error.
    1155             :  *
    1156             :  * The message's cursor position is whatever YYLLOC was last set to,
    1157             :  * ie, the start of the current token if called within yylex(), or the
    1158             :  * most recently lexed token if called from the grammar.
    1159             :  * This is OK for syntax error messages from the Bison parser, because Bison
    1160             :  * parsers report error as soon as the first unparsable token is reached.
    1161             :  * Beware of using yyerror for other purposes, as the cursor position might
    1162             :  * be misleading!
    1163             :  */
    1164             : void
    1165         426 : scanner_yyerror(const char *message, core_yyscan_t yyscanner)
    1166             : {
    1167         426 :     const char *loc = yyextra->scanbuf + *yylloc;
    1168             : 
    1169         426 :     if (*loc == YY_END_OF_BUFFER_CHAR)
    1170             :     {
    1171          12 :         ereport(ERROR,
    1172             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1173             :         /* translator: %s is typically the translation of "syntax error" */
    1174             :                  errmsg("%s at end of input", _(message)),
    1175             :                  lexer_errposition()));
    1176             :     }
    1177             :     else
    1178             :     {
    1179         414 :         ereport(ERROR,
    1180             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1181             :         /* translator: first %s is typically the translation of "syntax error" */
    1182             :                  errmsg("%s at or near \"%s\"", _(message), loc),
    1183             :                  lexer_errposition()));
    1184             :     }
    1185             : }
    1186             : 
    1187             : 
    1188             : /*
    1189             :  * Called before any actual parsing is done
    1190             :  */
    1191             : core_yyscan_t
    1192      781588 : scanner_init(const char *str,
    1193             :              core_yy_extra_type *yyext,
    1194             :              const ScanKeywordList *keywordlist,
    1195             :              const uint16 *keyword_tokens)
    1196             : {
    1197      781588 :     Size        slen = strlen(str);
    1198             :     yyscan_t    scanner;
    1199             : 
    1200      781588 :     if (yylex_init(&scanner) != 0)
    1201           0 :         elog(ERROR, "yylex_init() failed: %m");
    1202             : 
    1203      781588 :     core_yyset_extra(yyext, scanner);
    1204             : 
    1205      781588 :     yyext->keywordlist = keywordlist;
    1206      781588 :     yyext->keyword_tokens = keyword_tokens;
    1207             : 
    1208      781588 :     yyext->backslash_quote = backslash_quote;
    1209      781588 :     yyext->escape_string_warning = escape_string_warning;
    1210      781588 :     yyext->standard_conforming_strings = standard_conforming_strings;
    1211             : 
    1212             :     /*
    1213             :      * Make a scan buffer with special termination needed by flex.
    1214             :      */
    1215      781588 :     yyext->scanbuf = (char *) palloc(slen + 2);
    1216      781588 :     yyext->scanbuflen = slen;
    1217      781588 :     memcpy(yyext->scanbuf, str, slen);
    1218      781588 :     yyext->scanbuf[slen] = yyext->scanbuf[slen + 1] = YY_END_OF_BUFFER_CHAR;
    1219      781588 :     yy_scan_buffer(yyext->scanbuf, slen + 2, scanner);
    1220             : 
    1221             :     /* initialize literal buffer to a reasonable but expansible size */
    1222      781588 :     yyext->literalalloc = 1024;
    1223      781588 :     yyext->literalbuf = (char *) palloc(yyext->literalalloc);
    1224      781588 :     yyext->literallen = 0;
    1225             : 
    1226      781588 :     return scanner;
    1227             : }
    1228             : 
    1229             : 
    1230             : /*
    1231             :  * Called after parsing is done to clean up after scanner_init()
    1232             :  */
    1233             : void
    1234      780896 : scanner_finish(core_yyscan_t yyscanner)
    1235             : {
    1236             :     /*
    1237             :      * We don't bother to call yylex_destroy(), because all it would do is
    1238             :      * pfree a small amount of control storage.  It's cheaper to leak the
    1239             :      * storage until the parsing context is destroyed.  The amount of space
    1240             :      * involved is usually negligible compared to the output parse tree
    1241             :      * anyway.
    1242             :      *
    1243             :      * We do bother to pfree the scanbuf and literal buffer, but only if they
    1244             :      * represent a nontrivial amount of space.  The 8K cutoff is arbitrary.
    1245             :      */
    1246      780896 :     if (yyextra->scanbuflen >= 8192)
    1247          64 :         pfree(yyextra->scanbuf);
    1248      780896 :     if (yyextra->literalalloc >= 8192)
    1249          18 :         pfree(yyextra->literalbuf);
    1250      780896 : }
    1251             : 
    1252             : 
    1253             : static void
    1254     1080246 : addlit(char *ytext, int yleng, core_yyscan_t yyscanner)
    1255             : {
    1256             :     /* enlarge buffer if needed */
    1257     1080246 :     if ((yyextra->literallen + yleng) >= yyextra->literalalloc)
    1258             :     {
    1259         138 :         yyextra->literalalloc = pg_nextpower2_32(yyextra->literallen + yleng + 1);
    1260         138 :         yyextra->literalbuf = (char *) repalloc(yyextra->literalbuf,
    1261         138 :                                                 yyextra->literalalloc);
    1262             :     }
    1263             :     /* append new data */
    1264     1080246 :     memcpy(yyextra->literalbuf + yyextra->literallen, ytext, yleng);
    1265     1080246 :     yyextra->literallen += yleng;
    1266     1080246 : }
    1267             : 
    1268             : 
    1269             : static void
    1270       12406 : addlitchar(unsigned char ychar, core_yyscan_t yyscanner)
    1271             : {
    1272             :     /* enlarge buffer if needed */
    1273       12406 :     if ((yyextra->literallen + 1) >= yyextra->literalalloc)
    1274             :     {
    1275           0 :         yyextra->literalalloc *= 2;
    1276           0 :         yyextra->literalbuf = (char *) repalloc(yyextra->literalbuf,
    1277           0 :                                                 yyextra->literalalloc);
    1278             :     }
    1279             :     /* append new data */
    1280       12406 :     yyextra->literalbuf[yyextra->literallen] = ychar;
    1281       12406 :     yyextra->literallen += 1;
    1282       12406 : }
    1283             : 
    1284             : 
    1285             : /*
    1286             :  * Create a palloc'd copy of literalbuf, adding a trailing null.
    1287             :  */
    1288             : static char *
    1289     1088778 : litbufdup(core_yyscan_t yyscanner)
    1290             : {
    1291     1088778 :     int         llen = yyextra->literallen;
    1292             :     char       *new;
    1293             : 
    1294     1088778 :     new = palloc(llen + 1);
    1295     1088778 :     memcpy(new, yyextra->literalbuf, llen);
    1296     1088778 :     new[llen] = '\0';
    1297     1088778 :     return new;
    1298             : }
    1299             : 
    1300             : /*
    1301             :  * Process {integer}.  Note this will also do the right thing with {decimal},
    1302             :  * ie digits and a decimal point.
    1303             :  */
    1304             : static int
    1305      516350 : process_integer_literal(const char *token, YYSTYPE *lval)
    1306             : {
    1307             :     int         val;
    1308             :     char       *endptr;
    1309             : 
    1310      516350 :     errno = 0;
    1311      516350 :     val = strtoint(token, &endptr, 10);
    1312      516350 :     if (*endptr != '\0' || errno == ERANGE)
    1313             :     {
    1314             :         /* integer too large (or contains decimal pt), treat it as a float */
    1315         684 :         lval->str = pstrdup(token);
    1316         684 :         return FCONST;
    1317             :     }
    1318      515666 :     lval->ival = val;
    1319      515666 :     return ICONST;
    1320             : }
    1321             : 
    1322             : static void
    1323         108 : addunicode(pg_wchar c, core_yyscan_t yyscanner)
    1324             : {
    1325             :     ScannerCallbackState scbstate;
    1326             :     char        buf[MAX_UNICODE_EQUIVALENT_STRING + 1];
    1327             : 
    1328         108 :     if (!is_valid_unicode_codepoint(c))
    1329           4 :         yyerror("invalid Unicode escape value");
    1330             : 
    1331             :     /*
    1332             :      * We expect that pg_unicode_to_server() will complain about any
    1333             :      * unconvertible code point, so we don't have to set saw_non_ascii.
    1334             :      */
    1335         104 :     setup_scanner_errposition_callback(&scbstate, yyscanner, *(yylloc));
    1336         104 :     pg_unicode_to_server(c, (unsigned char *) buf);
    1337         104 :     cancel_scanner_errposition_callback(&scbstate);
    1338         104 :     addlit(buf, strlen(buf), yyscanner);
    1339         104 : }
    1340             : 
    1341             : static unsigned char
    1342        2540 : unescape_single_char(unsigned char c, core_yyscan_t yyscanner)
    1343             : {
    1344        2540 :     switch (c)
    1345             :     {
    1346          26 :         case 'b':
    1347          26 :             return '\b';
    1348           2 :         case 'f':
    1349           2 :             return '\f';
    1350         582 :         case 'n':
    1351         582 :             return '\n';
    1352          56 :         case 'r':
    1353          56 :             return '\r';
    1354          18 :         case 't':
    1355          18 :             return '\t';
    1356        1856 :         default:
    1357             :             /* check for backslash followed by non-7-bit-ASCII */
    1358        1856 :             if (c == '\0' || IS_HIGHBIT_SET(c))
    1359           0 :                 yyextra->saw_non_ascii = true;
    1360             : 
    1361        1856 :             return c;
    1362             :     }
    1363             : }
    1364             : 
    1365             : static void
    1366        2540 : check_string_escape_warning(unsigned char ychar, core_yyscan_t yyscanner)
    1367             : {
    1368        2540 :     if (ychar == '\'')
    1369             :     {
    1370          24 :         if (yyextra->warn_on_first_escape && yyextra->escape_string_warning)
    1371           0 :             ereport(WARNING,
    1372             :                     (errcode(ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER),
    1373             :                      errmsg("nonstandard use of \\' in a string literal"),
    1374             :                      errhint("Use '' to write quotes in strings, or use the escape string syntax (E'...')."),
    1375             :                      lexer_errposition()));
    1376          24 :         yyextra->warn_on_first_escape = false;   /* warn only once per string */
    1377             :     }
    1378        2516 :     else if (ychar == '\\')
    1379             :     {
    1380        1832 :         if (yyextra->warn_on_first_escape && yyextra->escape_string_warning)
    1381          44 :             ereport(WARNING,
    1382             :                     (errcode(ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER),
    1383             :                      errmsg("nonstandard use of \\\\ in a string literal"),
    1384             :                      errhint("Use the escape string syntax for backslashes, e.g., E'\\\\'."),
    1385             :                      lexer_errposition()));
    1386        1832 :         yyextra->warn_on_first_escape = false;   /* warn only once per string */
    1387             :     }
    1388             :     else
    1389         684 :         check_escape_warning(yyscanner);
    1390        2540 : }
    1391             : 
    1392             : static void
    1393         892 : check_escape_warning(core_yyscan_t yyscanner)
    1394             : {
    1395         892 :     if (yyextra->warn_on_first_escape && yyextra->escape_string_warning)
    1396           0 :         ereport(WARNING,
    1397             :                 (errcode(ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER),
    1398             :                  errmsg("nonstandard use of escape in a string literal"),
    1399             :         errhint("Use the escape string syntax for escapes, e.g., E'\\r\\n'."),
    1400             :                  lexer_errposition()));
    1401         892 :     yyextra->warn_on_first_escape = false;       /* warn only once per string */
    1402         892 : }
    1403             : 
    1404             : /*
    1405             :  * Interface functions to make flex use palloc() instead of malloc().
    1406             :  * It'd be better to make these static, but flex insists otherwise.
    1407             :  */
    1408             : 
    1409             : void *
    1410     2344764 : core_yyalloc(yy_size_t bytes, core_yyscan_t yyscanner)
    1411             : {
    1412     2344764 :     return palloc(bytes);
    1413             : }
    1414             : 
    1415             : void *
    1416           0 : core_yyrealloc(void *ptr, yy_size_t bytes, core_yyscan_t yyscanner)
    1417             : {
    1418           0 :     if (ptr)
    1419           0 :         return repalloc(ptr, bytes);
    1420             :     else
    1421           0 :         return palloc(bytes);
    1422             : }
    1423             : 
    1424             : void
    1425           0 : core_yyfree(void *ptr, core_yyscan_t yyscanner)
    1426             : {
    1427           0 :     if (ptr)
    1428           0 :         pfree(ptr);
    1429           0 : }

Generated by: LCOV version 1.14