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
283
284
285
286
287
288
289
290
291
292
293
|
/*****************************************************************************
Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
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 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 Street, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
/**
* @file fts/fts0pars.y
* FTS parser: input file for the GNU Bison parser generator
*
* Created 2007/5/9 Sunny Bains
*/
%{
#include "ha_prototypes.h"
#include "mem0mem.h"
#include "fts0ast.h"
#include "fts0blex.h"
#include "fts0tlex.h"
#include "fts0pars.h"
#include <my_sys.h>
extern int fts_lexer(YYSTYPE*, fts_lexer_t*);
extern int fts_blexer(YYSTYPE*, yyscan_t);
extern int fts_tlexer(YYSTYPE*, yyscan_t);
extern int ftserror(const char* p);
/* Required for reentrant parser */
#define ftslex fts_lexer
#define YYERROR_VERBOSE
/* For passing an argument to yyparse() */
#define YYPARSE_PARAM state
#define YYLEX_PARAM ((fts_ast_state_t*) state)->lexer
typedef int (*fts_scanner)(YYSTYPE* val, yyscan_t yyscanner);
struct fts_lexer_struct {
fts_scanner scanner;
void* yyscanner;
};
%}
%union {
int oper;
fts_ast_string_t* token;
fts_ast_node_t* node;
};
/* Enable re-entrant parser */
%pure_parser
%token<oper> FTS_OPER
%token<token> FTS_TEXT FTS_TERM FTS_NUMB
%type<node> prefix term text expr sub_expr expr_lst query
%nonassoc '+' '-' '~' '<' '>'
%%
query : expr_lst {
$$ = $1;
((fts_ast_state_t*) state)->root = $$;
}
;
expr_lst: /* Empty */ {
$$ = NULL;
}
| expr_lst expr {
$$ = $1;
if (!$$) {
$$ = fts_ast_create_node_list(state, $2);
} else {
fts_ast_add_node($$, $2);
}
}
| expr_lst sub_expr {
$$ = $1;
$$ = fts_ast_create_node_list(state, $1);
if (!$$) {
$$ = $2;
} else {
fts_ast_add_node($$, $2);
}
}
;
sub_expr: '(' expr_lst ')' {
$$ = $2;
if ($$) {
$$ = fts_ast_create_node_subexp_list(state, $$);
}
}
| prefix '(' expr_lst ')' {
$$ = fts_ast_create_node_list(state, $1);
if ($3) {
fts_ast_add_node($$,
fts_ast_create_node_subexp_list(state, $3));
}
}
;
expr : term {
$$ = $1;
}
| text {
$$ = $1;
}
| term '*' {
fts_ast_term_set_wildcard($1);
}
| text '@' FTS_NUMB {
fts_ast_text_set_distance($1, fts_ast_string_to_ul($3, 10));
fts_ast_string_free($3);
}
| prefix term '*' {
$$ = fts_ast_create_node_list(state, $1);
fts_ast_add_node($$, $2);
fts_ast_term_set_wildcard($2);
}
| prefix term {
$$ = fts_ast_create_node_list(state, $1);
fts_ast_add_node($$, $2);
}
| prefix text '@' FTS_NUMB {
$$ = fts_ast_create_node_list(state, $1);
fts_ast_add_node($$, $2);
fts_ast_text_set_distance($2, fts_ast_string_to_ul($4, 10));
fts_ast_string_free($4);
}
| prefix text {
$$ = fts_ast_create_node_list(state, $1);
fts_ast_add_node($$, $2);
}
;
prefix : '-' {
$$ = fts_ast_create_node_oper(state, FTS_IGNORE);
}
| '+' {
$$ = fts_ast_create_node_oper(state, FTS_EXIST);
}
| '~' {
$$ = fts_ast_create_node_oper(state, FTS_NEGATE);
}
| '<' {
$$ = fts_ast_create_node_oper(state, FTS_DECR_RATING);
}
| '>' {
$$ = fts_ast_create_node_oper(state, FTS_INCR_RATING);
}
;
term : FTS_TERM {
$$ = fts_ast_create_node_term(state, $1);
fts_ast_string_free($1);
}
| FTS_NUMB {
$$ = fts_ast_create_node_term(state, $1);
fts_ast_string_free($1);
}
/* Ignore leading '*' */
| '*' term {
$$ = $2;
}
;
text : FTS_TEXT {
$$ = fts_ast_create_node_text(state, $1);
fts_ast_string_free($1);
}
;
%%
/********************************************************************
*/
int
ftserror(
/*=====*/
const char* p)
{
fprintf(stderr, "%s\n", p);
return(0);
}
/********************************************************************
Create a fts_lexer_t instance.*/
fts_lexer_t*
fts_lexer_create(
/*=============*/
ibool boolean_mode,
const byte* query,
ulint query_len)
{
fts_lexer_t* fts_lexer = static_cast<fts_lexer_t*>(
ut_malloc_nokey(sizeof(fts_lexer_t)));
if (boolean_mode) {
fts0blex_init(&fts_lexer->yyscanner);
fts0b_scan_bytes((char*) query, (int) query_len, fts_lexer->yyscanner);
fts_lexer->scanner = fts_blexer;
/* FIXME: Debugging */
/* fts0bset_debug(1 , fts_lexer->yyscanner); */
} else {
fts0tlex_init(&fts_lexer->yyscanner);
fts0t_scan_bytes((char*) query, (int) query_len, fts_lexer->yyscanner);
fts_lexer->scanner = fts_tlexer;
}
return(fts_lexer);
}
/********************************************************************
Free an fts_lexer_t instance.*/
void
fts_lexer_free(
/*===========*/
fts_lexer_t* fts_lexer)
{
if (fts_lexer->scanner == fts_blexer) {
fts0blex_destroy(fts_lexer->yyscanner);
} else {
fts0tlex_destroy(fts_lexer->yyscanner);
}
ut_free(fts_lexer);
}
/********************************************************************
Call the appropaiate scanner.*/
int
fts_lexer(
/*======*/
YYSTYPE* val,
fts_lexer_t* fts_lexer)
{
fts_scanner func_ptr;
func_ptr = fts_lexer->scanner;
return(func_ptr(val, fts_lexer->yyscanner));
}
/********************************************************************
Parse the query.*/
int
fts_parse(
/*======*/
fts_ast_state_t* state)
{
return(ftsparse(state));
}
|