PostgreSQL Source Code git master
cubescan.l
Go to the documentation of this file.
1%top{
2/*
3 * A scanner for EMP-style numeric ranges
4 * contrib/cube/cubescan.l
5 */
6
7#include "postgres.h"
8
9#include "cubedata.h"
10#include "cubeparse.h" /* must be after cubedata.h for YYSTYPE and NDBOX */
11}
12
13%{
14/* LCOV_EXCL_START */
15
16/* No reason to constrain amount of data slurped */
17#define YY_READ_BUF_SIZE 16777216
18
19/* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
20#undef fprintf
21#define fprintf(file, fmt, msg) fprintf_to_ereport(fmt, msg)
22
23static void
24fprintf_to_ereport(const char *fmt, const char *msg)
25{
26 ereport(ERROR, (errmsg_internal("%s", msg)));
27}
static void fprintf_to_ereport(const char *fmt, const char *msg)
Definition: cubescan.l:24
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1158
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
28%}
29
30%option reentrant
31%option bison-bridge
32%option 8bit
33%option never-interactive
34%option nodefault
35%option noinput
36%option nounput
37%option noyywrap
38%option noyyalloc
39%option noyyrealloc
40%option noyyfree
41%option warn
42%option prefix="cube_yy"
43
44
45n [0-9]+
46integer [+-]?{n}
47real [+-]?({n}\.{n}?|\.{n})
48float ({integer}|{real})([eE]{integer})?
49infinity [+-]?[iI][nN][fF]([iI][nN][iI][tT][yY])?
50NaN [nN][aA][nN]
51
52%%
53
54{float} *yylval = yytext; return CUBEFLOAT;
55{infinity} *yylval = yytext; return CUBEFLOAT;
56{NaN} *yylval = yytext; return CUBEFLOAT;
57\[ *yylval = "("; return O_BRACKET;
58\] *yylval = ")"; return C_BRACKET;
59\‍( *yylval = "("; return O_PAREN;
60\‍) *yylval = ")"; return C_PAREN;
61\, *yylval = ","; return COMMA;
62[ \t\n\r\f\v]+ /* discard spaces */
63. return yytext[0]; /* alert parser of the garbage */
64
65%%
66
67/* LCOV_EXCL_STOP */
68
69/* result and scanbuflen are not used, but Bison expects this signature */
70void
71cube_yyerror(NDBOX **result, Size scanbuflen,
72 struct Node *escontext,
73 yyscan_t yyscanner,
74 const char *message)
75{
76 struct yyguts_t *yyg = (struct yyguts_t *) yyscanner; /* needed for yytext
77 * macro */
78
79 if (*yytext == YY_END_OF_BUFFER_CHAR)
80 {
81 errsave(escontext,
82 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
83 errmsg("invalid input syntax for cube"),
84 /* translator: %s is typically "syntax error" */
85 errdetail("%s at end of input", message)));
86 }
87 else
88 {
89 errsave(escontext,
90 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
91 errmsg("invalid input syntax for cube"),
92 /* translator: first %s is typically "syntax error" */
93 errdetail("%s at or near \"%s\"", message, yytext)));
94 }
95}
96
97
98/*
99 * Called before any actual parsing is done
100 */
101void
102cube_scanner_init(const char *str, Size *scanbuflen, yyscan_t *yyscannerp)
103{
104 Size slen = strlen(str);
105 yyscan_t yyscanner;
106
107 if (yylex_init(yyscannerp) != 0)
108 elog(ERROR, "yylex_init() failed: %m");
109
110 yyscanner = *yyscannerp;
111
112 yy_scan_bytes(str, slen, yyscanner);
113 *scanbuflen = slen;
114}
115
116
117/*
118 * Called after parsing is done to clean up after cube_scanner_init()
119 */
120void
122{
123 yylex_destroy(yyscanner);
124}
125
126/*
127 * Interface functions to make flex use palloc() instead of malloc().
128 * It'd be better to make these static, but flex insists otherwise.
129 */
130
131void *
132yyalloc(yy_size_t size, yyscan_t yyscanner)
133{
134 return palloc(size);
135}
136
137void *
138yyrealloc(void *ptr, yy_size_t size, yyscan_t yyscanner)
139{
140 if (ptr)
141 return repalloc(ptr, size);
142 else
143 return palloc(size);
144}
145
146void
147yyfree(void *ptr, yyscan_t yyscanner)
148{
149 if (ptr)
150 pfree(ptr);
151}
size_t Size
Definition: c.h:576
void * yyscan_t
Definition: cubedata.h:67
void * yyrealloc(void *ptr, yy_size_t size, yyscan_t yyscanner)
Definition: cubescan.l:138
void cube_yyerror(NDBOX **result, Size scanbuflen, struct Node *escontext, yyscan_t yyscanner, const char *message)
Definition: cubescan.l:71
void cube_scanner_init(const char *str, Size *scanbuflen, yyscan_t *yyscannerp)
Definition: cubescan.l:102
void cube_scanner_finish(yyscan_t yyscanner)
Definition: cubescan.l:121
void yyfree(void *ptr, yyscan_t yyscanner)
Definition: cubescan.l:147
void * yyalloc(yy_size_t size, yyscan_t yyscanner)
Definition: cubescan.l:132
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define errsave(context,...)
Definition: elog.h:261
#define elog(elevel,...)
Definition: elog.h:225
const char * str
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:2170
void pfree(void *pointer)
Definition: mcxt.c:2150
void * palloc(Size size)
Definition: mcxt.c:1943
Definition: cubedata.h:10
Definition: nodes.h:135