@@ -164,109 +164,13 @@ public static IEnumerable<TContext> GetDescendents<TContext>(this ParserRuleCont
164
164
return listener . Matches ;
165
165
}
166
166
167
- //public static IEnumerable<T> GetChildren<T>(this RuleContext context)
168
- //{
169
- // if (context == null)
170
- // {
171
- // yield break;
172
- // }
173
-
174
- // for (var index = 0; index < context.ChildCount; index++)
175
- // {
176
- // var child = context.GetChild(index);
177
- // if (child is T)
178
- // {
179
- // yield return (T)child;
180
- // }
181
- // }
182
- //}
183
-
184
- //public static bool HasParent(this RuleContext context, RuleContext parent)
185
- //{
186
- // if (context == null)
187
- // {
188
- // return false;
189
- // }
190
- // if (context == parent)
191
- // {
192
- // return true;
193
- // }
194
- // return HasParent(context.Parent, parent);
195
- //}
196
-
197
- //public static TContext FindChild<TContext>(this ParserRuleContext context) where TContext : ParserRuleContext
198
- //{
199
- // if (context == null)
200
- // {
201
- // return default;
202
- // }
203
-
204
- // for (var index = 0; index < context.ChildCount; index++)
205
- // {
206
- // var child = context.GetChild(index);
207
- // if (context.GetChild(index) is TContext)
208
- // {
209
- // return (TContext)child;
210
- // }
211
- // }
212
- // return default;
213
- //}
214
-
215
- //public static bool HasChildToken(this IParseTree context, string token)
216
- //{
217
- // for (var index = 0; index < context.ChildCount; index++)
218
- // {
219
- // var child = context.GetChild(index);
220
- // if (context.GetChild(index).GetText().Equals(token))
221
- // {
222
- // return true;
223
- // }
224
- // }
225
- // return false;
226
- //}
227
-
228
- public static T GetDescendent < T > ( this IParseTree context )
229
- {
230
- if ( context == null )
231
- {
232
- return default ;
233
- }
234
-
235
- for ( var index = 0 ; index < context . ChildCount ; index ++ )
236
- {
237
- var child = context . GetChild ( index ) ;
238
- if ( context . GetChild ( index ) is T )
239
- {
240
- return ( T ) child ;
241
- }
242
-
243
- var descendent = child . GetDescendent < T > ( ) ;
244
- if ( descendent != null )
245
- {
246
- return descendent ;
247
- }
248
- }
249
-
250
- return default ;
251
- }
252
-
253
- public static IEnumerable < IParseTree > GetDescendents ( this IParseTree context )
167
+ /// <summary>
168
+ /// Try to get the first child of the generic context type.
169
+ /// </summary>
170
+ public static bool TryGetChildContext < TContext > ( this ParserRuleContext ctxt , out TContext opCtxt ) where TContext : ParserRuleContext
254
171
{
255
- if ( context == null )
256
- {
257
- yield break ;
258
- }
259
-
260
- for ( var index = 0 ; index < context . ChildCount ; index ++ )
261
- {
262
- var child = context . GetChild ( index ) ;
263
- yield return child ;
264
-
265
- foreach ( var node in child . GetDescendents ( ) )
266
- {
267
- yield return node ;
268
- }
269
- }
172
+ opCtxt = ctxt . GetChild < TContext > ( ) ;
173
+ return opCtxt != null ;
270
174
}
271
175
272
176
private class ChildNodeListener < TContext > : VBAParserBaseListener where TContext : ParserRuleContext
@@ -283,11 +187,5 @@ public override void EnterEveryRule(ParserRuleContext context)
283
187
}
284
188
}
285
189
}
286
-
287
- public static bool TryGetChildContext < TContext > ( this ParserRuleContext ctxt , out TContext opCtxt ) where TContext : ParserRuleContext
288
- {
289
- opCtxt = ctxt . GetChild < TContext > ( ) ;
290
- return opCtxt != null ;
291
- }
292
190
}
293
191
}
0 commit comments