-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLike.kt
312 lines (240 loc) · 7.21 KB
/
Like.kt
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
fun main(args: Array<String>) {
var cnt = readLine()!!.toInt();
while(cnt-- > 0) {
val request = readLine()!!;
println(like(request));
}
}
fun like(request:String):String {
val ast = parse(SeekableString(request));
//println(ast.toString());
return if ( match(ast.nodes[1],0,ast.nodes[0].value?:"", 0) ) "YES" else "NO";
}
fun parse(request:SeekableString):AST
{
var root = AST(AST.Token.EXPRESSION, null);
//println(request.toString());
getApostrophe(request);
root.addNode(getString(request));
getApostrophe(request);
getSpace(request);
getOP(request);
getSpace(request);
getApostrophe(request);
root.addNode(getRegExp(request));
getApostrophe(request);
return root;
}
fun match(pattern:AST, patternPos:Int, string:String, pos:Int):Boolean
{
var strPos = pos;
for (i in patternPos..pattern.nodes.size-1) {
val node = pattern.nodes[i];
when(node.type) {
AST.Token.CHAR -> { if ( strPos >= string.length || node.value!![0] != string[strPos]) { return false } }
AST.Token.ANY_SYMBOL -> { if (strPos >= string.length) {return false} }
AST.Token.ANY_STRING -> { return match_anystring(pattern, i, string, strPos) }
AST.Token.RANGE -> { if (strPos >= string.length || !in_range(node, string[strPos])){return false} }
AST.Token.INVERSE_RANGE -> { if (strPos >= string.length || in_range(node, string[strPos])){return false} }
else -> { throw IllegalArgumentException("Unknown type "+node.type.toString()); }
}
strPos++;
}
if (strPos != string.length)
return false;
return true;
}
fun match_anystring(pattern:AST, patternPos:Int, string:String, pos:Int ):Boolean
{
var pp = patternPos;
while (pp < pattern.nodes.size && pattern.nodes[pp].type == AST.Token.ANY_STRING)
pp++;
if (pp == pattern.nodes.size - 1 && pattern.nodes[pp].type == AST.Token.ANY_STRING)
return true;
for (i in pos..string.length) {
if (match(pattern, pp, string, i)) {
return true;
}
}
return false;
}
fun in_range(node:AST, symb:Char):Boolean {
val range:RangeAST = node as RangeAST;
//println("in_range: "+range.compiled.toString());
return range.compiled.contains(symb);
}
fun getRegExp(request:SeekableString):AST {
var ast:AST=AST(AST.Token.REGEXP,null);
loop@ while(true) {
val c = request.nextChar();
when(c) {
null -> { break@loop }
'_' -> { ast.addNode(AST(AST.Token.ANY_SYMBOL, null)) }
'%' -> { ast.addNode(AST(AST.Token.ANY_STRING, null)) }
'[' -> {
ast.addNode(getRange(request));
val closingBracket = request.nextChar();
if (closingBracket != ']') {
throw IllegalArgumentException("No closing bracket");
}
}
'\'' -> { if ( request.peek() == '\'' ) {
request.nextChar();
ast.addNode(AST(AST.Token.CHAR, "'"))
}
else {
request.back();
break@loop;
}
}
else -> {
ast.addNode(AST(AST.Token.CHAR, c.toString()))
}
}
}
return ast;
}
//
//RANGE: CHAR+RANGE|SYMBOL_RANGE+RANGE|EMPTY
//SYMBOL_RANGE: CHAR-CHAR
fun getRange(request:SeekableString):AST {
val ast:AST;
val flag = request.nextChar();
if (flag == '^') {
ast = RangeAST(AST.Token.INVERSE_RANGE,null);
}
else {
request.back();
ast = RangeAST(AST.Token.RANGE,null);
}
while (request.peek() != ']') {
val c = request.nextChar();
if (request.peek() == '-' && request.peek(1) != ']') {
request.back();
ast.addNode(getSymbolRange(request));
}
else {
ast.addNode(AST(AST.Token.CHAR, c.toString()));
}
}
return ast;
}
fun getSymbolRange(request:SeekableString):AST?
{
val from = request.nextChar().toString();
request.nextChar();
val to = request.nextChar().toString();
return AST(AST.Token.SYMBOL_RANGE, null)
.addNode(AST(AST.Token.CHAR, from))
.addNode(AST(AST.Token.CHAR, to));
}
fun getOP(request:SeekableString)
{
if (
request.nextChar() != 'l' ||
request.nextChar() != 'i' ||
request.nextChar() != 'k' ||
request.nextChar() != 'e'
) {
throw IllegalArgumentException("LIKE keyword required");
}
}
fun getApostrophe(request:SeekableString)
{
val c = request.nextChar();
if (c != '\'') {
throw IllegalArgumentException("Apostrophe required");
}
}
fun getSpace(request:SeekableString)
{
while (request.peek() == ' ') {request.nextChar()};
}
fun getString(request:SeekableString):AST
{
var result = "";
while (true) {
val c = request.nextChar();
if (c == null)
break;
if (c == '\'') {
if (request.peek() == '\'') {
request.nextChar();
result += '\''
}
else {
request.back();
break;
}
}
else {
result += c;
}
}
return AST(AST.Token.STRING, result);
}
class RangeAST(type:Token, value:String?):AST(type, value)
{
val compiled:Set<Char> by lazy {
nodes.map {
when (it.type) {
AST.Token.CHAR -> { listOf(it.value!![0]) }
AST.Token.SYMBOL_RANGE -> { ( it.nodes[0].value!![0]..it.nodes[1].value!![0] ).toList() }
else -> {throw IllegalArgumentException("Unknown type "+it.type.toString())}
}
}
.flatten()
.toSet()
}
}
open class AST(type:Token, value:String?)
{
val nodes:MutableList<AST>;
val type = type;
val value=value;
init {
nodes = mutableListOf<AST>();
}
enum class Token {
ANY_SYMBOL, ANY_STRING, RANGE, STRING, OP, REGEXP, EXPRESSION, CHAR, INVERSE_RANGE, SYMBOL_RANGE
}
fun addNode(n:AST?):AST {
if (n != null) {
nodes.add(n)
}
return this;
}
override fun toString():String {
return "{ " + type.toString() +" "+ value + " "+ nodes.toString()+ "}";
}
}
class SeekableString(str:String)
{
var position = 0;
val str = str;
fun nextChar():Char? {
val rc = this.peek();
if (rc != null)
position++;
return rc;
}
fun back() {
if (position>0)
position--;
}
fun peek():Char? {
if (position >= str.length) {
return null;
}
return str[position];
}
fun peek(n:Int):Char? {
if (position+n >= str.length) {
return null;
}
return str[position+n];
}
override fun toString():String {
return str.toString();
}
}