/
HqlParser.cs
423 lines (385 loc) · 12.3 KB
/
HqlParser.cs
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System;
using Antlr.Runtime;
using NHibernate.Hql.Ast.ANTLR.Tree;
using IToken = Antlr.Runtime.IToken;
using RecognitionException = Antlr.Runtime.RecognitionException;
namespace NHibernate.Hql.Ast.ANTLR
{
[CLSCompliant(false)]
public partial class HqlParser
{
private static readonly IInternalLogger log = LoggerProvider.LoggerFor(typeof(HqlParser));
internal static readonly bool[] possibleIds;
static HqlParser()
{
possibleIds = new bool[tokenNames.Length];
possibleIds[COUNT] = true;
possibleIds[ALL] = true;
possibleIds[ANY] = true;
possibleIds[AND] = true;
possibleIds[AS] = true;
possibleIds[ASCENDING] = true;
possibleIds[AVG] = true;
possibleIds[BETWEEN] = true;
possibleIds[CLASS] = true;
possibleIds[DELETE] = true;
possibleIds[DESCENDING] = true;
possibleIds[DISTINCT] = true;
possibleIds[ELEMENTS] = true;
possibleIds[ESCAPE] = true;
possibleIds[EXISTS] = true;
possibleIds[FALSE] = true;
possibleIds[FETCH] = true;
possibleIds[FROM] = true;
possibleIds[FULL] = true;
possibleIds[GROUP] = true;
possibleIds[HAVING] = true;
possibleIds[IN] = true;
possibleIds[INDICES] = true;
possibleIds[INNER] = true;
possibleIds[INSERT] = true;
possibleIds[INTO] = true;
possibleIds[IS] = true;
possibleIds[JOIN] = true;
possibleIds[LEFT] = true;
possibleIds[LIKE] = true;
possibleIds[MAX] = true;
possibleIds[MIN] = true;
possibleIds[NEW] = true;
possibleIds[NOT] = true;
possibleIds[NULL] = true;
possibleIds[OR] = true;
possibleIds[ORDER] = true;
possibleIds[OUTER] = true;
possibleIds[PROPERTIES] = true;
possibleIds[RIGHT] = true;
possibleIds[SELECT] = true;
possibleIds[SET] = true;
possibleIds[SOME] = true;
possibleIds[SUM] = true;
possibleIds[TRUE] = true;
possibleIds[UNION] = true;
possibleIds[UPDATE] = true;
possibleIds[VERSIONED] = true;
possibleIds[WHERE] = true;
possibleIds[LITERAL_by] = true;
possibleIds[CASE] = true;
possibleIds[END] = true;
possibleIds[ELSE] = true;
possibleIds[THEN] = true;
possibleIds[WHEN] = true;
possibleIds[ON] = true;
possibleIds[WITH] = true;
possibleIds[BOTH] = true;
possibleIds[EMPTY] = true;
possibleIds[LEADING] = true;
possibleIds[MEMBER] = true;
possibleIds[OBJECT] = true;
possibleIds[OF] = true;
possibleIds[TRAILING] = true;
}
/** True if this is a filter query (allow no FROM clause). **/
private bool filter;
private IParseErrorHandler _parseErrorHandler = new ErrorCounter();
public IParseErrorHandler ParseErrorHandler
{
get { return _parseErrorHandler; }
set { _parseErrorHandler = value; }
}
public bool Filter
{
get { return filter; }
set { filter = value; }
}
public override void ReportError(RecognitionException e)
{
_parseErrorHandler.ReportError(e);
}
protected override object RecoverFromMismatchedToken(IIntStream input, int ttype, BitSet follow)
{
throw new MismatchedTokenException(ttype, input);
}
public void WeakKeywords()
{
int t = input.LA(1);
switch (t)
{
case ORDER:
case GROUP:
// Case 1: Multi token keywords GROUP BY and ORDER BY
// The next token ( LT(2) ) should be 'by'... otherwise, this is just an ident.
if (input.LA(2) != LITERAL_by)
{
input.LT(1).Type = IDENT;
if (log.IsDebugEnabled)
{
log.Debug("weakKeywords() : new LT(1) token - " + input.LT(1));
}
}
break;
default:
// Case 2: The current token is after FROM and before '.'.
if (t != IDENT && input.LA(-1) == FROM && ((input.LA(2) == DOT) || (input.LA(2) == IDENT) || (input.LA(2) == -1)))
{
HqlToken hqlToken = input.LT(1) as HqlToken;
if (hqlToken != null && hqlToken.PossibleId)
{
hqlToken.Type = IDENT;
if (log.IsDebugEnabled)
{
log.Debug("weakKeywords() : new LT(1) token - " + input.LT(1));
}
}
}
break;
}
}
public void WeakKeywords2()
{
/*
* path
* alias in class? path
* in open path close
* alias in elements open path close
* elements open path close
* alias in path dot elements
*/
int t = input.LA(1);
switch (t)
{
case ORDER:
case GROUP:
// Case 1: Multi token keywords GROUP BY and ORDER BY
// The next token ( LT(2) ) should be 'by'... otherwise, this is just an ident.
if (input.LA(2) != LITERAL_by)
{
input.LT(1).Type = IDENT;
if (log.IsDebugEnabled)
{
log.Debug("weakKeywords() : new LT(1) token - " + input.LT(1));
}
}
break;
default:
// Case 2: The current token is after FROM and before '.'.
if (t != IDENT && input.LA(-1) == FROM && input.LA(2) == DOT)
{
HqlToken hqlToken = (HqlToken)input.LT(1);
if (hqlToken.PossibleId)
{
hqlToken.Type = IDENT;
if (log.IsDebugEnabled)
{
log.Debug("weakKeywords() : new LT(1) token - " + input.LT(1));
}
}
}
break;
}
}
public IASTNode NegateNode(IASTNode node)
{
// TODO - copy code from HqlParser.java
switch (node.Type)
{
case OR:
node.Type = AND;
node.Text = "{and}";
NegateNode(node.GetChild(0));
NegateNode(node.GetChild(1));
return node;
case AND:
node.Type = OR;
node.Text = "{or}";
NegateNode(node.GetChild(0));
NegateNode(node.GetChild(1));
return node;
case EQ:
node.Type = NE;
node.Text = "{not}" + node.Text;
return node; // (NOT (EQ a b) ) => (NE a b)
case NE:
node.Type = EQ;
node.Text = "{not}" + node.Text;
return node; // (NOT (NE a b) ) => (EQ a b)
case GT:
node.Type = LE;
node.Text = "{not}" + node.Text;
return node; // (NOT (GT a b) ) => (LE a b)
case LT:
node.Type = GE;
node.Text = "{not}" + node.Text;
return node; // (NOT (LT a b) ) => (GE a b)
case GE:
node.Type = LT;
node.Text = "{not}" + node.Text;
return node; // (NOT (GE a b) ) => (LT a b)
case LE:
node.Type = GT;
node.Text = "{not}" + node.Text;
return node; // (NOT (LE a b) ) => (GT a b)
case LIKE:
node.Type = NOT_LIKE;
node.Text = "{not}" + node.Text;
return node; // (NOT (LIKE a b) ) => (NOT_LIKE a b)
case NOT_LIKE:
node.Type = LIKE;
node.Text = "{not}" + node.Text;
return node; // (NOT (NOT_LIKE a b) ) => (LIKE a b)
case IN:
node.Type = NOT_IN;
node.Text = "{not}" + node.Text;
return node;
case NOT_IN:
node.Type = IN;
node.Text = "{not}" + node.Text;
return node;
case IS_NULL:
node.Type = IS_NOT_NULL;
node.Text = "{not}" + node.Text;
return node; // (NOT (IS_NULL a b) ) => (IS_NOT_NULL a b)
case IS_NOT_NULL:
node.Type = IS_NULL;
node.Text = "{not}" + node.Text;
return node; // (NOT (IS_NOT_NULL a b) ) => (IS_NULL a b)
case BETWEEN:
node.Type = NOT_BETWEEN;
node.Text = "{not}" + node.Text;
return node; // (NOT (BETWEEN a b) ) => (NOT_BETWEEN a b)
case NOT_BETWEEN:
node.Type = BETWEEN;
node.Text = "{not}" + node.Text;
return node; // (NOT (NOT_BETWEEN a b) ) => (BETWEEN a b)
/* This can never happen because this rule will always eliminate the child NOT.
case NOT:
return x.getFirstChild(); // (NOT (NOT x) ) => (x)
*/
default:
IASTNode not = (IASTNode) TreeAdaptor.Create(NOT, "not");
not.AddChild(node);
return not;
}
}
public IASTNode ProcessEqualityExpression(object o)
{
IASTNode x = o as IASTNode;
if (x == null)
{
log.Warn("processEqualityExpression() : No expression to process!");
return null;
}
int type = x.Type;
if (type == EQ || type == NE)
{
bool negated = type == NE;
if (x.ChildCount == 2)
{
IASTNode a = x.GetChild(0);
IASTNode b = x.GetChild(1);
// (EQ NULL b) => (IS_NULL b)
if (a.Type == NULL && b.Type != NULL)
{
return CreateIsNullParent(b, negated);
}
// (EQ a NULL) => (IS_NULL a)
if (b.Type == NULL && a.Type != NULL)
{
return CreateIsNullParent(a, negated);
}
if (b.Type == EMPTY)
{
return ProcessIsEmpty(a, negated);
}
}
}
return x;
}
public void HandleDotIdent()
{
// This handles HHH-354, where there is a strange property name in a where clause.
// If the lookahead contains a DOT then something that isn't an IDENT...
if (input.LA(1) == DOT && input.LA(2) != IDENT)
{
// See if the second lookahed token can be an identifier.
HqlToken t = input.LT(2) as HqlToken;
if (t != null && t.PossibleId)
{
// Set it!
input.LT(2).Type = IDENT;
if (log.IsDebugEnabled)
{
log.Debug("handleDotIdent() : new LT(2) token - " + input.LT(1));
}
}
}
}
private IASTNode CreateIsNullParent(IASTNode node, bool negated)
{
int type = negated ? IS_NOT_NULL : IS_NULL;
string text = negated ? "is not null" : "is null";
return (IASTNode)adaptor.BecomeRoot(adaptor.Create(type, text), node);
}
private IASTNode ProcessIsEmpty(IASTNode node, bool negated)
{
// NOTE: Because we're using ASTUtil.createParent(), the tree must be created from the bottom up.
// IS EMPTY x => (EXISTS (QUERY (SELECT_FROM (FROM x) ) ) )
IASTNode ast = CreateSubquery(node);
ast = (IASTNode)adaptor.BecomeRoot(adaptor.Create(EXISTS, "exists"), ast);
// Add NOT if it's negated.
if (!negated)
{
ast = (IASTNode)adaptor.BecomeRoot(adaptor.Create(NOT, "not"), ast);
}
return ast;
}
private IASTNode CreateSubquery(IASTNode node)
{
return (IASTNode)adaptor.BecomeRoot(
adaptor.Create(QUERY, "QUERY"),
adaptor.BecomeRoot(
adaptor.Create(SELECT_FROM, "SELECT_FROM"),
adaptor.BecomeRoot(
adaptor.Create(FROM, "from"),
adaptor.BecomeRoot(
adaptor.Create(RANGE, "RANGE"),
node)))
);
}
public IASTNode ProcessMemberOf(IToken n, IASTNode p, IASTNode root)
{
ASTFactory factory = new ASTFactory(adaptor);
return factory.CreateNode(n == null ? IN : NOT_IN,
n == null ? "in" : "not in",
root.IsNil && root.ChildCount == 1 ? root.GetChild(0) : root,
factory.CreateNode(IN_LIST, "inList",
CreateSubquery(p)));
}
public IASTNode HandleIdentifierError(IToken token, RecognitionException ex)
{
if (token is HqlToken)
{
HqlToken hqlToken = (HqlToken)token;
// ... and the token could be an identifer and the error is
// a mismatched token error ...
if (hqlToken.PossibleId && (ex is MismatchedTokenException))
{
MismatchedTokenException mte = (MismatchedTokenException)ex;
// ... and the expected token type was an identifier, then:
if (mte.Expecting == IDENT)
{
// Use the token as an identifier.
_parseErrorHandler.ReportWarning("Keyword '"
+ token.Text
+ "' is being interpreted as an identifier due to: " + mte.Message);
// Add the token to the AST.
token.Type = WEIRD_IDENT;
input.Consume();
return (IASTNode) adaptor.Create(token);
}
}
}
// Otherwise, handle the error normally.
throw ex;
}
}
}