-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathdebug_lo_scanner.ll
283 lines (238 loc) · 6.02 KB
/
debug_lo_scanner.ll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
%top{
/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* LOCK ORDER, flex scanner for arc data file. */
/*
flex %option nounistd does not work:
https://github.com/westes/flex/issues/345
Work around: define YY_NO_UNISTD_H explicitly
*/
#ifndef YY_NO_UNISTD_H
#define YY_NO_UNISTD_H
#endif
}
%option noinput
%option nounput
%option noyywrap
%option nodefault
%option never-interactive
/* Broken in flex: %option nounistd */
%option 8bit
%option yylineno
%option reentrant
%option extra-type="struct MEM_ROOT *"
%option bison-bridge
%option bison-locations
%{
#include <string.h>
#include "my_alloc.h"
#include "sql/debug_lo_parser.h"
#include "mysql/components/services/log_builtins.h"
#include "mysqld_error.h"
#define YY_DECL int LOCK_ORDER_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param, yyscan_t yyscanner)
#define YY_USER_ACTION \
yylloc_param->first_line = yylineno; \
yylloc_param->last_line = yylineno; \
yylloc_param->first_column = yycolumn; \
yylloc_param->last_column = yycolumn+yyleng; \
yycolumn += yyleng;
/*
Allocate string token on the caller memory root.
*/
static char* lo_alloc_string(MEM_ROOT *mem_root, const char* str, size_t length) {
char * result;
result = static_cast<char*> (mem_root->Alloc(length + 1));
if (result != nullptr) {
strncpy(result, str, length + 1);
result[length] = '\0';
}
return result;
}
%}
WHITE_SPACE [ \t]+
NEW_LINE [\n]
COMMENT_LINE [#][^\n]*
STR [^"]*
%%
"ARC" {
return ARC_SYM;
}
"BIND" {
return BIND_SYM;
}
"CONSTRAINT" {
return CONSTRAINT_SYM;
}
"COMMENT" {
return COMMENT_SYM;
}
"DEBUG" {
return DEBUG_SYM;
}
"FLAGS" {
return FLAGS_SYM;
}
"FROM" {
return FROM_SYM;
}
"IGNORED" {
return IGNORED_SYM;
}
"LOOP" {
return LOOP_SYM;
}
"NODE" {
return NODE_SYM;
}
"OP" {
return OP_SYM;
}
"RECURSIVE" {
return RECURSIVE_SYM;
}
"STATE" {
return STATE_SYM;
}
"TO" {
return TO_SYM;
}
"TRACE" {
return TRACE_SYM;
}
"UNFAIR" {
return UNFAIR_SYM;
}
"\""{STR}"\"" {
char * str = lo_alloc_string (yyextra, yytext + 1, yyleng - 2);
yylval->m_str = str;
return STR_SYM;
}
{WHITE_SPACE} {}
{COMMENT_LINE} {}
{NEW_LINE} {}
. {
char msg[256];
snprintf(msg, sizeof(msg), "Unrecognized character: %s", yytext);
/* message format = "Lock order scanner: (%d:%d) - (%d:%d) : %s" */
LogErr(ERROR_LEVEL, ER_LOCK_ORDER_SCANNER_SYNTAX,
yylloc_param->first_line,
yylloc_param->first_column,
yylloc_param->last_line,
yylloc_param->last_column,
msg);
}
%%
/*
Helper tool to debug syntax.
Build with:
gcc -DTEST_MAIN debug_lo_scanner.cc
Run with
a.out lock_order_dependencies.txt
*/
#ifdef TEST_MAIN
#include "debug_lo_scanner.h"
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "usage: %s /path/to/lo_arc_data.txt\n", argv[0]);
return 1;
}
const char* filename = argv[1];
FILE *data = fopen(filename, "r");
if (data == nullptr) {
perror(filename);
return 2;
}
yyscan_t my_scanner;
LOCK_ORDER_lex_init(& my_scanner);
LOCK_ORDER_set_in(data, my_scanner);
YYSTYPE my_yylval;
YYLTYPE my_yylloc;
int tok;
while (tok = LOCK_ORDER_lex(& my_yylval, & my_yylloc, my_scanner))
{
fprintf(stdout,
"(%d:%d - %d:%d) ",
my_yylloc.first_line,
my_yylloc.first_column,
my_yylloc.last_line,
my_yylloc.last_column);
switch (tok)
{
case ARC_SYM:
fprintf(stdout, "token arc\n");
break;
case BIND_SYM:
fprintf(stdout, "token bind\n");
break;
case CONSTRAINT_SYM:
fprintf(stdout, "token constraint\n");
break;
case COMMENT_SYM:
fprintf(stdout, "token comment\n");
break;
case DEBUG_SYM:
fprintf(stdout, "token debug\n");
break;
case FLAGS_SYM:
fprintf(stdout, "token flags\n");
break;
case FROM_SYM:
fprintf(stdout, "token from\n");
break;
case IGNORED_SYM:
fprintf(stdout, "token ignored\n");
break;
case LOOP_SYM:
fprintf(stdout, "token loop\n");
break;
case NODE_SYM:
fprintf(stdout, "token node\n");
break;
case OP_SYM:
fprintf(stdout, "token op\n");
break;
case RECURSIVE_SYM:
fprintf(stdout, "token recursive\n");
break;
case STATE_SYM:
fprintf(stdout, "token state\n");
break;
case TO_SYM:
fprintf(stdout, "token to\n");
break;
case TRACE_SYM:
fprintf(stdout, "token trace\n");
break;
case UNFAIR_SYM:
fprintf(stdout, "token unfair\n");
break;
case STR_SYM:
fprintf(stdout, "token str\n");
break;
default:
fprintf(stdout, "unknown token %d\n", tok);
break;
}
}
LOCK_ORDER_lex_destroy(my_scanner);
fclose(data);
}
#endif /* TEST_MAIN */