PostgreSQL Source Code git master
specscanner.l
Go to the documentation of this file.
1%top{
2/*-------------------------------------------------------------------------
3 *
4 * specscanner.l
5 * a lexical scanner for an isolation test specification
6 *
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 *-------------------------------------------------------------------------
11 */
12#include "postgres_fe.h"
13
14/*
15 * NB: include specparse.h only AFTER including isolationtester.h, because
16 * isolationtester.h includes node definitions needed for YYSTYPE.
17 */
18#include "isolationtester.h"
19#include "specparse.h"
20}
21
22%{
23static int yyline = 1; /* line number for error reporting */
24
25#define LITBUF_INIT 1024 /* initial size of litbuf */
26static char *litbuf = NULL;
27static size_t litbufsize = 0;
28static size_t litbufpos = 0;
29
30static void addlitchar(char c);
31
32/* LCOV_EXCL_START */
33
char * c
static size_t litbufsize
Definition: specscanner.l:27
static size_t litbufpos
Definition: specscanner.l:28
static void addlitchar(char c)
Definition: specscanner.l:150
static int yyline
Definition: specscanner.l:23
static char * litbuf
Definition: specscanner.l:26
34%}
35
36%option 8bit
37%option never-interactive
38%option nodefault
39%option noinput
40%option nounput
41%option noyywrap
42%option warn
43%option prefix="spec_yy"
44
45
46%x sql
47%x qident
48
49non_newline [^\n\r]
50space [ \t\r\f]
51
52comment ("#"{non_newline}*)
53
54digit [0-9]
55ident_start [A-Za-z\200-\377_]
56ident_cont [A-Za-z\200-\377_0-9\$]
57
58identifier {ident_start}{ident_cont}*
59
60self [,()*]
61
62%%
63
64%{
65 /* Allocate litbuf in first call of yylex() */
66 if (litbuf == NULL)
67 {
68 litbuf = pg_malloc(LITBUF_INIT);
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define LITBUF_INIT
Definition: specscanner.l:25
69 litbufsize = LITBUF_INIT;
70 }
71%}
72
73 /* Keywords (must appear before the {identifier} rule!) */
74notices { return NOTICES; }
75permutation { return PERMUTATION; }
76session { return SESSION; }
77setup { return SETUP; }
78step { return STEP; }
#define STEP
Definition: mbuf.c:37
79teardown { return TEARDOWN; }
80
81 /* Whitespace and comments */
82[\n] { yyline++; }
83{comment} { /* ignore */ }
84{space} { /* ignore */ }
85
86 /* Plain identifiers */
87{identifier} {
88 spec_yylval.str = pg_strdup(yytext);
89 return(identifier);
90 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
91
92 /* Quoted identifiers: "foo" */
93\" {
94 litbufpos = 0;
95 BEGIN(qident);
96 }
97<qident>\"\" { addlitchar(yytext[0]); }
98<qident>\" {
99 litbuf[litbufpos] = '\0';
100 spec_yylval.str = pg_strdup(litbuf);
101 BEGIN(INITIAL);
102 return(identifier);
103 }
104<qident>. { addlitchar(yytext[0]); }
105<qident>\n { spec_yyerror("unexpected newline in quoted identifier"); }
void spec_yyerror(const char *message)
Definition: specscanner.l:163
106<qident><<EOF>> { spec_yyerror("unterminated quoted identifier"); }
107
108 /* SQL blocks: { UPDATE ... } */
109 /* We trim leading/trailing whitespace, otherwise they're unprocessed */
110"{"{space}* {
111
112 litbufpos = 0;
113 BEGIN(sql);
114 }
115<sql>{space}*"}" {
116 litbuf[litbufpos] = '\0';
117 spec_yylval.str = pg_strdup(litbuf);
118 BEGIN(INITIAL);
119 return(sqlblock);
120 }
121<sql>. {
122 addlitchar(yytext[0]);
123 }
124<sql>\n {
125 yyline++;
126 addlitchar(yytext[0]);
127 }
128<sql><<EOF>> {
129 spec_yyerror("unterminated sql block");
130 }
131
132 /* Numbers and punctuation */
133{digit}+ {
134 spec_yylval.integer = atoi(yytext);
135 return INTEGER;
136 }
137
138{self} { return yytext[0]; }
139
140 /* Anything else is an error */
141. {
142 fprintf(stderr, "syntax error at line %d: unexpected character \"%s\"\n", yyline, yytext);
143 exit(1);
144 }
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
exit(1)
145%%
146
147/* LCOV_EXCL_STOP */
148
149static void
151{
152 /* We must always leave room to add a trailing \0 */
153 if (litbufpos >= litbufsize - 1)
154 {
155 /* Double the size of litbuf if it gets full */
158 }
159 litbuf[litbufpos++] = c;
160}
161
162void
163spec_yyerror(const char *message)
164{
165 fprintf(stderr, "%s at line %d\n", message, yyline);
166 exit(1);
167}
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65