2
0

scanner.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*-------------------------------------------------------------------------
  2. *
  3. * scanner.h
  4. * API for the core scanner (flex machine)
  5. *
  6. * The core scanner is also used by PL/pgSQL, so we provide a public API
  7. * for it. However, the rest of the backend is only expected to use the
  8. * higher-level API provided by parser.h.
  9. *
  10. *
  11. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  12. * Portions Copyright (c) 1994, Regents of the University of California
  13. *
  14. * src/include/parser/scanner.h
  15. *
  16. *-------------------------------------------------------------------------
  17. */
  18. #ifndef SCANNER_H
  19. #define SCANNER_H
  20. #include "common/keywords.h"
  21. /*
  22. * The scanner returns extra data about scanned tokens in this union type.
  23. * Note that this is a subset of the fields used in YYSTYPE of the bison
  24. * parsers built atop the scanner.
  25. */
  26. typedef union core_YYSTYPE
  27. {
  28. int ival; /* for integer literals */
  29. char *str; /* for identifiers and non-integer literals */
  30. const char *keyword; /* canonical spelling of keywords */
  31. } core_YYSTYPE;
  32. /*
  33. * We track token locations in terms of byte offsets from the start of the
  34. * source string, not the column number/line number representation that
  35. * bison uses by default. Also, to minimize overhead we track only one
  36. * location (usually the first token location) for each construct, not
  37. * the beginning and ending locations as bison does by default. It's
  38. * therefore sufficient to make YYLTYPE an int.
  39. */
  40. #define YYLTYPE int
  41. /*
  42. * Another important component of the scanner's API is the token code numbers.
  43. * However, those are not defined in this file, because bison insists on
  44. * defining them for itself. The token codes used by the core scanner are
  45. * the ASCII characters plus these:
  46. * %token <str> IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op
  47. * %token <ival> ICONST PARAM
  48. * %token TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER
  49. * %token LESS_EQUALS GREATER_EQUALS NOT_EQUALS
  50. * The above token definitions *must* be the first ones declared in any
  51. * bison parser built atop this scanner, so that they will have consistent
  52. * numbers assigned to them (specifically, IDENT = 258 and so on).
  53. */
  54. /*
  55. * The YY_EXTRA data that a flex scanner allows us to pass around.
  56. * Private state needed by the core scanner goes here. Note that the actual
  57. * yy_extra struct may be larger and have this as its first component, thus
  58. * allowing the calling parser to keep some fields of its own in YY_EXTRA.
  59. */
  60. typedef struct core_yy_extra_type
  61. {
  62. /*
  63. * The string the scanner is physically scanning. We keep this mainly so
  64. * that we can cheaply compute the offset of the current token (yytext).
  65. */
  66. char *scanbuf;
  67. Size scanbuflen;
  68. /*
  69. * The keyword list to use, and the associated grammar token codes.
  70. */
  71. const ScanKeywordList *keywordlist;
  72. const uint16 *keyword_tokens;
  73. /*
  74. * Scanner settings to use. These are initialized from the corresponding
  75. * GUC variables by scanner_init(). Callers can modify them after
  76. * scanner_init() if they don't want the scanner's behavior to follow the
  77. * prevailing GUC settings.
  78. */
  79. int backslash_quote;
  80. bool escape_string_warning;
  81. bool standard_conforming_strings;
  82. /*
  83. * literalbuf is used to accumulate literal values when multiple rules are
  84. * needed to parse a single literal. Call startlit() to reset buffer to
  85. * empty, addlit() to add text. NOTE: the string in literalbuf is NOT
  86. * necessarily null-terminated, but there always IS room to add a trailing
  87. * null at offset literallen. We store a null only when we need it.
  88. */
  89. char *literalbuf; /* palloc'd expandable buffer */
  90. int literallen; /* actual current string length */
  91. int literalalloc; /* current allocated buffer size */
  92. /*
  93. * Random assorted scanner state.
  94. */
  95. int state_before_str_stop; /* start cond. before end quote */
  96. int xcdepth; /* depth of nesting in slash-star comments */
  97. char *dolqstart; /* current $foo$ quote start string */
  98. YYLTYPE save_yylloc; /* one-element stack for PUSH_YYLLOC() */
  99. /* first part of UTF16 surrogate pair for Unicode escapes */
  100. int32 utf16_first_part;
  101. /* state variables for literal-lexing warnings */
  102. bool warn_on_first_escape;
  103. bool saw_non_ascii;
  104. } core_yy_extra_type;
  105. /*
  106. * The type of yyscanner is opaque outside scan.l.
  107. */
  108. typedef void *core_yyscan_t;
  109. /* Support for scanner_errposition_callback function */
  110. typedef struct ScannerCallbackState
  111. {
  112. core_yyscan_t yyscanner;
  113. int location;
  114. ErrorContextCallback errcallback;
  115. } ScannerCallbackState;
  116. /* Constant data exported from parser/scan.l */
  117. extern PGDLLIMPORT const uint16 ScanKeywordTokens[];
  118. /* Entry points in parser/scan.l */
  119. extern core_yyscan_t scanner_init(const char *str,
  120. core_yy_extra_type *yyext,
  121. const ScanKeywordList *keywordlist,
  122. const uint16 *keyword_tokens);
  123. extern void scanner_finish(core_yyscan_t yyscanner);
  124. extern int core_yylex(core_YYSTYPE *lvalp, YYLTYPE *llocp,
  125. core_yyscan_t yyscanner);
  126. extern int scanner_errposition(int location, core_yyscan_t yyscanner);
  127. extern void setup_scanner_errposition_callback(ScannerCallbackState *scbstate,
  128. core_yyscan_t yyscanner,
  129. int location);
  130. extern void cancel_scanner_errposition_callback(ScannerCallbackState *scbstate);
  131. extern void scanner_yyerror(const char *message, core_yyscan_t yyscanner) pg_attribute_noreturn();
  132. #endif /* SCANNER_H */