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
|
/* query-builder.vala
*
* Copyright (C) 2008 Johann Prieur <johann.prieur@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
using Gee;
namespace People {
/**
* QueryBuilder objects are used to generate a {@link Backend.Query} tree from a
* string-formatted query.
*/
public class QueryBuilder : Object {
private enum Symbol {
NOT = 271, // TokenType.LAST + 1
AND,
OR,
EQUALS,
CONTAINS
}
private Scanner scanner;
construct {
ScannerConfig config = ScannerConfig ();
config.cset_skip_characters = " \t\r\n";
config.cset_identifier_first = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
config.cset_identifier_nth = "abcdefghijklmnopqrstuvwxyz" + "." +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789";
config.cpair_comment_single = ";\n";
config.case_sensitive = false;
config.skip_comment_multi = true;
config.skip_comment_single = true;
config.scan_comment_multi = true;
config.scan_identifier = true;
config.scan_identifier_1char = true;
config.scan_identifier_NULL = true;
config.scan_symbols = true;
config.scan_string_sq = true;
config.scan_string_dq = true;
config.numbers_2_int = true;
config.int_2_float = false;
config.identifier_2_string = false;
config.char_2_token = true;
config.symbol_2_token = false;
config.scope_0_fallback = false;
scanner = new Scanner (config);
var scope = 0;
scanner.scope_add_symbol (scope, "NOT", (void*) Symbol.NOT);
scanner.scope_add_symbol (scope, "AND", (void*) Symbol.AND);
scanner.scope_add_symbol (scope, "OR", (void*) Symbol.OR);
scanner.scope_add_symbol (scope, "EQUALS", (void*) Symbol.EQUALS);
scanner.scope_add_symbol (scope, "CONTAINS", (void*) Symbol.CONTAINS);
scanner.set_scope (scope);
}
/**
* Builds the {@link Backend.Query} tree corresponding to the given query.
*
* @param query a S-Expression formatted query
* @return a Query tree
*/
public Backend.Query build (string query) {
Backend.Query structure = null;
scanner.input_text (query, (uint) query.size);
structure = parse_expression ();
// if (!scanner.eof ()) {
// // FIXME: throw an exception
// structure = null;
// }
return structure;
}
private Backend.Query? parse_expression () {
Backend.Query query = null;
var token = scanner.get_next_token ();
switch (token) {
case TokenType.EOF:
break;
case TokenType.LEFT_PAREN:
query = parse_list ();
break;
case TokenType.IDENTIFIER:
var str_ident = scanner.value.identifier;
query = new Backend.FieldQuery (str_ident);
break;
case TokenType.STRING:
var str_value = scanner.value.string;
query = new Backend.ValueQuery (str_value);
break;
case TokenType.INT:
var str_value = scanner.value.int.to_string ();
query = new Backend.ValueQuery (str_value);
break;
default:
// FIXME: throw an exception
return null;
}
return query;
}
private Backend.Query? parse_list () {
Backend.Query query = null;
var token = scanner.get_next_token ();
switch (token) {
case TokenType.SYMBOL:
var symbol = (Symbol) scanner.value.symbol;
var expressions = parse_expressions ();
token = scanner.get_next_token (); /* consumes the left ')' */
if (token != TokenType.RIGHT_PAREN) {
// FIXME: throw an exception
return null;
}
switch (symbol) {
// TODO: check if the length of the list is OK with the symbol
case Symbol.NOT:
if (expressions.size != 1) {
// FIXME: throw an exception
return null;
}
query = new Backend.CompositeQuery (Backend.QueryOperator.NOT, expressions);
break;
case Symbol.AND:
if (expressions.size <= 1) {
// FIXME: throw an exception
return null;
}
query = Backend.Query.and (expressions);
break;
case Symbol.OR:
if (expressions.size <= 1) {
// FIXME: throw an exception
return null;
}
query = Backend.Query.or (expressions);
break;
case Symbol.EQUALS:
if (expressions.size != 2) {
// FIXME: throw an exception
return null;
}
query = new Backend.CompositeQuery (Backend.QueryOperator.EQUALS, expressions);
break;
case Symbol.CONTAINS:
if (expressions.size != 2) {
// FIXME: throw an exception
return null;
}
query = new Backend.CompositeQuery (Backend.QueryOperator.CONTAINS, expressions);
break;
default:
break;
}
break;
default:
// FIXME: throw an exception
return null;
break;
}
return query;
}
private Gee.List<Backend.Query> parse_expressions () {
var expressions = new Gee.ArrayList<Backend.Query> ();
TokenType token;
while (((token = scanner.peek_next_token ()) != TokenType.RIGHT_PAREN) &&
(token != TokenType.EOF)) {
var expression = parse_expression ();
if (expression != null) {
expressions.add (expression);
}
}
return expressions;
}
}
}
|