Skip to content

Commit 5a38bd8

Browse files
authored
Move HqlToken.PossibleId to HqlParser.IsPossibleId method and remove castings (#3377)
1 parent 7a1bf08 commit 5a38bd8

File tree

2 files changed

+36
-44
lines changed

2 files changed

+36
-44
lines changed

src/NHibernate/Hql/Ast/ANTLR/HqlParser.cs

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ public void WeakKeywords()
147147
// Case 2: The current token is after FROM and before '.'.
148148
if (t != IDENT && input.LA(-1) == FROM && ((input.LA(2) == DOT) || (input.LA(2) == IDENT) || (input.LA(2) == -1)))
149149
{
150-
HqlToken hqlToken = input.LT(1) as HqlToken;
151-
if (hqlToken != null && hqlToken.PossibleId)
150+
var hqlToken = input.LT(1);
151+
if (IsPossibleId(hqlToken))
152152
{
153153
hqlToken.Type = IDENT;
154154
if (log.IsDebugEnabled())
@@ -192,8 +192,8 @@ public void WeakKeywords2()
192192
// Case 2: The current token is after FROM and before '.'.
193193
if (t != IDENT && input.LA(-1) == FROM && input.LA(2) == DOT)
194194
{
195-
HqlToken hqlToken = (HqlToken)input.LT(1);
196-
if (hqlToken.PossibleId)
195+
var hqlToken = input.LT(1);
196+
if (IsPossibleId(hqlToken))
197197
{
198198
hqlToken.Type = IDENT;
199199
if (log.IsDebugEnabled())
@@ -290,16 +290,8 @@ public IASTNode NegateNode(IASTNode node)
290290
}
291291
}
292292

293-
public IASTNode ProcessEqualityExpression(object o)
293+
public IASTNode ProcessEqualityExpression(IASTNode x)
294294
{
295-
IASTNode x = o as IASTNode;
296-
297-
if (x == null)
298-
{
299-
log.Warn("processEqualityExpression() : No expression to process!");
300-
return null;
301-
}
302-
303295
int type = x.Type;
304296
if (type == EQ || type == NE)
305297
{
@@ -336,11 +328,11 @@ public void HandleDotIdent()
336328
if (input.LA(1) == DOT && input.LA(2) != IDENT)
337329
{
338330
// See if the second lookahed token can be an identifier.
339-
HqlToken t = input.LT(2) as HqlToken;
340-
if (t != null && t.PossibleId)
331+
var t = input.LT(2);
332+
if (IsPossibleId(t))
341333
{
342334
// Set it!
343-
input.LT(2).Type = IDENT;
335+
t.Type = IDENT;
344336
if (log.IsDebugEnabled())
345337
{
346338
log.Debug("handleDotIdent() : new LT(2) token - {0}", input.LT(1));
@@ -401,37 +393,38 @@ public IASTNode ProcessMemberOf(IToken n, IASTNode p, IASTNode root)
401393

402394
public IASTNode HandleIdentifierError(IToken token, RecognitionException ex)
403395
{
404-
if (token is HqlToken)
396+
// ... and the token could be an identifier and the error is
397+
// a mismatched token error ...
398+
if (IsPossibleId(token) && (ex is MismatchedTokenException mte)
399+
// ... and the expected token type was an identifier, then:
400+
&& mte.Expecting == IDENT)
405401
{
406-
HqlToken hqlToken = (HqlToken)token;
402+
// Use the token as an identifier.
403+
_parseErrorHandler.ReportWarning("Keyword '"
404+
+ token.Text
405+
+ "' is being interpreted as an identifier due to: " + mte.Message);
407406

408-
// ... and the token could be an identifer and the error is
409-
// a mismatched token error ...
410-
if (hqlToken.PossibleId && (ex is MismatchedTokenException))
411-
{
412-
MismatchedTokenException mte = (MismatchedTokenException)ex;
413-
414-
// ... and the expected token type was an identifier, then:
415-
if (mte.Expecting == IDENT)
416-
{
417-
// Use the token as an identifier.
418-
_parseErrorHandler.ReportWarning("Keyword '"
419-
+ token.Text
420-
+ "' is being interpreted as an identifier due to: " + mte.Message);
407+
// Add the token to the AST.
421408

422-
// Add the token to the AST.
409+
token.Type = WEIRD_IDENT;
423410

424-
token.Type = WEIRD_IDENT;
425-
426-
input.Consume();
427-
return (IASTNode) adaptor.Create(token);
428-
}
429-
}
411+
input.Consume();
412+
return (IASTNode) adaptor.Create(token);
430413
}
431-
414+
432415
// Otherwise, handle the error normally.
433416
ReflectHelper.PreserveStackTrace(ex);
434417
throw ex;
435418
}
419+
420+
/// <summary>
421+
/// Indicates if the token could be an identifier.
422+
/// </summary>
423+
/// <param name="token"></param>
424+
public static bool IsPossibleId(IToken token)
425+
{
426+
var type = token.Type;
427+
return type >= 0 && type < possibleIds.Length && possibleIds[type];
428+
}
436429
}
437430
}

src/NHibernate/Hql/Ast/ANTLR/HqlToken.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ public HqlToken(IToken other)
3939
/// <summary>
4040
/// Indicates if the token could be an identifier.
4141
/// </summary>
42-
public bool PossibleId
43-
{
44-
get { return HqlParser.possibleIds[Type]; }
45-
}
42+
// Since 5.5
43+
[Obsolete("Use HqlParser.IsPossibleId method instead.")]
44+
public bool PossibleId => HqlParser.IsPossibleId(this);
4645

4746
/// <summary>
4847
/// Returns the previous token type.
@@ -62,7 +61,7 @@ public override string ToString()
6261
+ Text
6362
+ "\",<" + Type + "> previously: <" + PreviousType + ">,line="
6463
+ Line + ",col="
65-
+ CharPositionInLine + ",possibleID=" + PossibleId + "]";
64+
+ CharPositionInLine + ",possibleID=" + HqlParser.IsPossibleId(this) + "]";
6665
}
6766
}
6867
}

0 commit comments

Comments
 (0)