@@ -35,6 +35,11 @@ public static Selection GetSelection(this ParserRuleContext context)
35
35
/// </summary>
36
36
public static bool Contains ( this ParserRuleContext context , ParserRuleContext otherContextInSameParseTree )
37
37
{
38
+ if ( context is null || otherContextInSameParseTree is null )
39
+ {
40
+ return false ;
41
+ }
42
+
38
43
return context . start . TokenIndex <= otherContextInSameParseTree . start . TokenIndex
39
44
&& context . stop . TokenIndex >= otherContextInSameParseTree . stop . TokenIndex ;
40
45
}
@@ -44,7 +49,7 @@ public static bool Contains(this ParserRuleContext context, ParserRuleContext ot
44
49
/// </summary>
45
50
public static IEnumerable < IToken > GetTokens ( this ParserRuleContext context , CommonTokenStream tokenStream )
46
51
{
47
- var sourceInterval = context . SourceInterval ;
52
+ var sourceInterval = context ? . SourceInterval ?? Interval . Invalid ;
48
53
if ( sourceInterval . Equals ( Interval . Invalid ) || sourceInterval . b < sourceInterval . a )
49
54
{
50
55
return new List < IToken > ( ) ;
@@ -58,7 +63,7 @@ public static IEnumerable<IToken> GetTokens(this ParserRuleContext context, Comm
58
63
public static string GetText ( this ParserRuleContext context , ICharStream stream )
59
64
{
60
65
// Can be null if the input is empty it seems.
61
- if ( context . Stop == null )
66
+ if ( context ? . Stop == null )
62
67
{
63
68
return string . Empty ;
64
69
}
@@ -86,7 +91,7 @@ public static TContext GetChild<TContext>(this ParserRuleContext context) where
86
91
/// <summary>
87
92
/// Determines if any of the context's ancestors are the generic Type.
88
93
/// </summary>
89
- public static bool IsDescendentOf < TContext > ( this ParserRuleContext context )
94
+ public static bool IsDescendentOf < TContext > ( this ParserRuleContext context ) where TContext : ParserRuleContext
90
95
{
91
96
if ( context == null )
92
97
{
@@ -129,7 +134,7 @@ private static bool IsDescendentOf_Recursive(IParseTree context, IParseTree targ
129
134
/// <summary>
130
135
/// Returns the context's first ancestor of the generic Type.
131
136
/// </summary>
132
- public static TContext GetAncestor < TContext > ( this ParserRuleContext context )
137
+ public static TContext GetAncestor < TContext > ( this ParserRuleContext context ) where TContext : ParserRuleContext
133
138
{
134
139
switch ( context )
135
140
{
@@ -145,13 +150,13 @@ public static TContext GetAncestor<TContext>(this ParserRuleContext context)
145
150
/// <summary>
146
151
/// Tries to return the context's first ancestor of the generic Type.
147
152
/// </summary>
148
- public static bool TryGetAncestor < TContext > ( this ParserRuleContext context , out TContext ancestor )
153
+ public static bool TryGetAncestor < TContext > ( this ParserRuleContext context , out TContext ancestor ) where TContext : ParserRuleContext
149
154
{
150
155
ancestor = context . GetAncestor < TContext > ( ) ;
151
156
return ancestor != null ;
152
157
}
153
158
154
- private static TContext GetAncestor_Recursive < TContext > ( ParserRuleContext context )
159
+ private static TContext GetAncestor_Recursive < TContext > ( ParserRuleContext context ) where TContext : ParserRuleContext
155
160
{
156
161
switch ( context )
157
162
{
@@ -258,7 +263,7 @@ public static bool TryGetChildContext<TContext>(this ParserRuleContext ctxt, out
258
263
public static VBAParser . EndOfLineContext GetFirstEndOfLine ( this VBAParser . EndOfStatementContext endOfStatement )
259
264
{
260
265
//This dedicated method exists for performance reasons on hot-paths.
261
- var individualEndOfStatements = endOfStatement . individualNonEOFEndOfStatement ( ) ;
266
+ var individualEndOfStatements = endOfStatement ? . individualNonEOFEndOfStatement ( ) ;
262
267
263
268
if ( individualEndOfStatements == null )
264
269
{
0 commit comments