/// <summary> /// Walks through the code units beneath this document. /// </summary> /// <param name="callback">Callback executed when a code unit is visited.</param> /// <param name="codeUnitTypes">The types of code units to visit.</param> public void WalkCodeModel(CodeUnitVisitor <object> callback, params CodeUnitType[] codeUnitTypes) { Param.RequireNotNull(callback, "callback"); Param.Ignore(codeUnitTypes); CodeWalker <object> .Start(this, callback, null, codeUnitTypes); }
/// <summary> /// Walks through the code units beneath this document. /// </summary> /// <param name="callback">Callback executed when a code unit is visited.</param> /// <param name="context">The optional visitor context data.</param> /// <typeparam name="T">The type of the context item.</typeparam> public void WalkCodeModel <T>(CodeUnitVisitor <T> callback, T context) { Param.RequireNotNull(callback, "callback"); Param.Ignore(context); CodeWalker <T> .Start(this, callback, context, null); }
/// <summary> /// Walks through the children of the given code unit. /// </summary> /// <param name="codeUnit">The code unit to walk.</param> /// <param name="callback">Callback executed when a code unit is visited.</param> /// <param name="parentElement">The parent element of the code unit, if any.</param> /// <param name="parentStatement">The parent statement of the code unit, if any.</param> /// <param name="parentExpression">The parent expression of the code unit, if any.</param> /// <param name="parentQueryClause">The parent query clause of the code unit, if any.</param> /// <param name="parentToken">The parent token of the code unit, if any.</param> /// <param name="context">Optional context.</param> /// <param name="codeUnitTypes">The types of code units to visit.</param> /// <returns>Returns true to continue walking, false otherwise.</returns> private static bool WalkCodeUnitChildren( CodeUnit codeUnit, CodeUnitVisitor <T> callback, Element parentElement, Statement parentStatement, Expression parentExpression, QueryClause parentQueryClause, Token parentToken, T context, CodeUnitType[] codeUnitTypes) { Param.AssertNotNull(codeUnit, "codeUnit"); Param.Ignore(codeUnitTypes); Param.AssertNotNull(callback, "callback"); Param.Ignore(parentElement); Param.Ignore(parentStatement); Param.Ignore(parentExpression); Param.Ignore(parentQueryClause); Param.Ignore(parentToken); Param.Ignore(context); if (codeUnit.Children != null && codeUnit.Children.Count > 0) { for (CodeUnit child = codeUnit.FindFirstChild(); child != null; child = child.FindNextSibling()) { if (!WalkCodeUnit(child, callback, parentElement, parentStatement, parentExpression, parentQueryClause, parentToken, context, codeUnitTypes)) { return(false); } } } return(true); }
/// <summary> /// Creates and starts a code walker. /// </summary> /// <param name="root">The root code unit to walk through.</param> /// <param name="callback">Callback executed when a code unit is visited.</param> /// <param name="context">The optional visitor context data.</param> /// <param name="codeUnitTypes">The types of code units to visit.</param> public static void Start(CodeUnit root, CodeUnitVisitor <T> callback, T context, params CodeUnitType[] codeUnitTypes) { Param.AssertNotNull(root, "root"); Param.AssertNotNull(callback, "callback"); Param.Ignore(context); Param.Ignore(codeUnitTypes); WalkCodeUnit(root, callback, context, codeUnitTypes); }
/// <summary> /// Walks through the code units beneath this document. /// </summary> /// <param name="callback">Callback executed when a code unit is visited.</param> public void WalkCodeModel(CodeUnitVisitor <object> callback) { Param.RequireNotNull(callback, "callback"); CodeWalker <object> .Start(this, callback, null, null); }
/// <summary> /// Visits the given code unit and walks through its children. /// </summary> /// <param name="codeUnit">The code unit to walk.</param> /// <param name="callback">Callback executed when a code unit is visited.</param> /// <param name="context">Optional context.</param> /// <param name="codeUnitTypes">The types of code units to visit.</param> /// <returns>Returns true to continue walking, false otherwise.</returns> private static bool WalkCodeUnit(CodeUnit codeUnit, CodeUnitVisitor <T> callback, T context, CodeUnitType[] codeUnitTypes) { Param.AssertNotNull(codeUnit, "codeUnit"); Param.AssertNotNull(callback, "callback"); Param.Ignore(context); Param.Ignore(codeUnitTypes); // Gather up the various parent types for this code unit. Element parentElement = null; Statement parentStatement = null; Expression parentExpression = null; QueryClause parentQueryClause = null; Token parentToken = null; CodeUnit parent = codeUnit.Parent; while (parent != null) { switch (parent.CodeUnitType) { case CodeUnitType.Element: if (parentElement != null) { parentElement = (Element)parent; // Elements are the top-level code units, so we can stop now. parent = null; } break; case CodeUnitType.Statement: if (parentStatement != null) { parentStatement = (Statement)parent; } break; case CodeUnitType.Expression: if (parentExpression != null) { parentExpression = (Expression)parent; } break; case CodeUnitType.QueryClause: if (parentQueryClause != null) { parentQueryClause = (QueryClause)parent; } break; case CodeUnitType.LexicalElement: if (parentToken != null) { parentToken = (Token)parent; } break; } parent = parent == null ? null : parent.Parent; } return(WalkCodeUnit(codeUnit, callback, parentElement, parentStatement, parentExpression, parentQueryClause, parentToken, context, codeUnitTypes)); }
/// <summary> /// Visits the given code unit and walks through its children. /// </summary> /// <param name="codeUnit">The code unit to walk.</param> /// <param name="callback">Callback executed when a code unit is visited.</param> /// <param name="parentElement">The parent element of the code unit, if any.</param> /// <param name="parentStatement">The parent statement of the code unit, if any.</param> /// <param name="parentExpression">The parent expression of the code unit, if any.</param> /// <param name="parentQueryClause">The parent query clause of the code unit, if any.</param> /// <param name="parentToken">The parent token of the code unit, if any.</param> /// <param name="context">Optional context.</param> /// <param name="codeUnitTypes">The types of code units to visit.</param> /// <returns>Returns true to continue walking, false otherwise.</returns> private static bool WalkCodeUnit( CodeUnit codeUnit, CodeUnitVisitor <T> callback, Element parentElement, Statement parentStatement, Expression parentExpression, QueryClause parentQueryClause, Token parentToken, T context, CodeUnitType[] codeUnitTypes) { Param.AssertNotNull(codeUnit, "codeUnit"); Param.AssertNotNull(callback, "callback"); Param.Ignore(parentElement); Param.Ignore(parentStatement); Param.Ignore(parentExpression); Param.Ignore(parentQueryClause); Param.Ignore(parentToken); Param.Ignore(context); Param.Ignore(codeUnitTypes); if (CompareCodeUnitType(codeUnitTypes, codeUnit.CodeUnitType)) { if (!callback(codeUnit, parentElement, parentStatement, parentExpression, parentQueryClause, parentToken, context)) { return(false); } } if (codeUnit.Children != null && codeUnit.Children.Count > 0) { switch (codeUnit.CodeUnitType) { case CodeUnitType.Element: parentElement = (Element)codeUnit; break; case CodeUnitType.Statement: parentStatement = (Statement)codeUnit; break; case CodeUnitType.Expression: parentExpression = (Expression)codeUnit; break; case CodeUnitType.QueryClause: parentQueryClause = (QueryClause)codeUnit; break; case CodeUnitType.LexicalElement: if (codeUnit.Is(LexicalElementType.Token)) { parentToken = (Token)codeUnit; } break; } return(WalkCodeUnitChildren(codeUnit, callback, parentElement, parentStatement, parentExpression, parentQueryClause, parentToken, context, codeUnitTypes)); } return(true); }