本文整理汇总了C#中Ast.Visit方法的典型用法代码示例。如果您正苦于以下问题:C# Ast.Visit方法的具体用法?C# Ast.Visit怎么用?C# Ast.Visit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ast
的用法示例。
在下文中一共展示了Ast.Visit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindCommandAtPosition
/// <summary>
/// Finds the symbol (always Command type) at a given file location
/// </summary>
/// <param name="scriptAst">The abstract syntax tree of the given script</param>
/// <param name="lineNumber">The line number of the cursor for the given script</param>
/// <param name="columnNumber">The column number of the cursor for the given script</param>
/// <returns>SymbolReference of found command</returns>
public static SymbolReference FindCommandAtPosition(Ast scriptAst, int lineNumber, int columnNumber)
{
FindCommandVisitor commandVisitor = new FindCommandVisitor(lineNumber, columnNumber);
scriptAst.Visit(commandVisitor);
return commandVisitor.FoundCommandReference;
}
示例2: FindDotSourcedIncludes
/// <summary>
/// Finds all files dot sourced in a script
/// </summary>
/// <param name="scriptAst">The abstract syntax tree of the given script</param>
/// <returns></returns>
public static string[] FindDotSourcedIncludes(Ast scriptAst)
{
FindDotSourcedVisitor dotSourcedVisitor = new FindDotSourcedVisitor();
scriptAst.Visit(dotSourcedVisitor);
return dotSourcedVisitor.DotSourcedFiles.ToArray();
}
示例3: GetText
public string GetText(Ast ast)
{
var visitor = new GetTextAstVisitor(this);
ast.Visit(visitor);
return visitor.Text;
}
示例4: AnalyzeScript
/// <summary>
/// AnalyzeScript: check that Write-Host or Console.Write are not used.
/// </summary>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
records = new List<DiagnosticRecord>();
this.fileName = fileName;
ast.Visit(this);
return records;
}
示例5: FindDefinitionOfSymbol
/// <summary>
/// Finds the definition of the symbol
/// </summary>
/// <param name="scriptAst">The abstract syntax tree of the given script</param>
/// <param name="symbolReference">The symbol that we are looking for the definition of</param>
/// <returns>A SymbolReference of the definition of the symbolReference</returns>
public static SymbolReference FindDefinitionOfSymbol(
Ast scriptAst,
SymbolReference symbolReference)
{
FindDeclartionVisitor declarationVisitor =
new FindDeclartionVisitor(
symbolReference);
scriptAst.Visit(declarationVisitor);
return declarationVisitor.FoundDeclartion;
}
示例6: AnalyzeScript
/// <summary>
/// AnalyzeScript: Analyzes the ast to check that cmdlets have help.
/// </summary>
/// <param name="ast">The script's ast</param>
/// <param name="fileName">The name of the script</param>
/// <returns>A List of diagnostic results of this rule</returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
DiagnosticRecords.Clear();
this.fileName = fileName;
exportedFunctions = Helper.Instance.GetExportedFunction(ast);
ast.Visit(this);
return DiagnosticRecords;
}
示例7: AnalyzeScript
/// <summary>
/// AnalyzeScript: Checks that objects return in a cmdlet have their types declared in OutputType Attribute
/// </summary>
/// <param name="ast">The script's ast</param>
/// <param name="fileName">The name of the script</param>
/// <returns>A List of diagnostic results of this rule</returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
DiagnosticRecords.Clear();
this.fileName = fileName;
_classes = ast.FindAll(item => item is TypeDefinitionAst && ((item as TypeDefinitionAst).IsClass), true).Cast<TypeDefinitionAst>();
ast.Visit(this);
return DiagnosticRecords;
}
示例8: EvaluateAst
public object EvaluateAst(Ast expressionAst, bool writeSideEffectsToPipeline)
{
var subVisitor = this.CloneSub(writeSideEffectsToPipeline);
expressionAst.Visit(subVisitor);
var result = subVisitor._pipelineCommandRuntime.OutputStream.Read();
if (result.Count == 0)
{
return null;
}
else if (result.Count == 1)
{
return result.Single();
}
return result.ToArray();
}
示例9: FindSymbolsInDocument
/// <summary>
/// Finds all symbols in a script
/// </summary>
/// <param name="scriptAst">The abstract syntax tree of the given script</param>
/// <param name="powerShellVersion">The PowerShell version the Ast was generated from</param>
/// <returns>A collection of SymbolReference objects</returns>
static public IEnumerable<SymbolReference> FindSymbolsInDocument(Ast scriptAst, Version powerShellVersion)
{
IEnumerable<SymbolReference> symbolReferences = null;
if (powerShellVersion >= new Version(5,0))
{
#if PowerShellv5
FindSymbolsVisitor2 findSymbolsVisitor = new FindSymbolsVisitor2();
scriptAst.Visit(findSymbolsVisitor);
symbolReferences = findSymbolsVisitor.SymbolReferences;
#endif
}
else
{
FindSymbolsVisitor findSymbolsVisitor = new FindSymbolsVisitor();
scriptAst.Visit(findSymbolsVisitor);
symbolReferences = findSymbolsVisitor.SymbolReferences;
}
return symbolReferences;
}
示例10: Validate
private string Validate(Ast rootAst)
{
if (_parseErrors != null && _parseErrors.Length > 0)
{
// Move the cursor to the point of error
_current = _parseErrors[0].Extent.EndOffset;
return _parseErrors[0].Message;
}
var validationVisitor = new CommandValidationVisitor(rootAst);
rootAst.Visit(validationVisitor);
if (!string.IsNullOrWhiteSpace(validationVisitor.detectedError))
{
return validationVisitor.detectedError;
}
return null;
}
示例11: EvaluateAst
object EvaluateAst(Ast expressionAst, bool writeSideEffectsToPipeline)
{
var subVisitor = this.CloneSub(writeSideEffectsToPipeline);
expressionAst.Visit(subVisitor);
var result = subVisitor._pipelineCommandRuntime.outputResults.Read();
if (result.Count == 0) return null;
if (result.Count == 1) return result.Single();
return result;
}
示例12: EvaluateLoopBodyAst
public bool EvaluateLoopBodyAst(Ast expressionAst, string loopLabel)
{
var loopCanContinue = true;
try
{
expressionAst.Visit(this);
}
catch (LoopFlowException e)
{
if (!String.IsNullOrEmpty(e.Label) && !e.Label.Equals(loopLabel))
{
throw;
}
loopCanContinue = e is ContinueException;
}
// return if the loop can continue execution or should break
return loopCanContinue;
}
示例13: GenerateWhileLoop
internal void GenerateWhileLoop(string loopLabel,
Action generateCondition,
Action generateLoopBody,
Ast continueAction = null)
{
// We model the flow graph like this (if continueAction is null, the first part is slightly different):
// goto L
// :ContinueTarget
// continueAction
// :L
// if (condition)
// {
// loop body
// // break -> goto BreakTarget
// // continue -> goto ContinueTarget
// goto ContinueTarget
// }
// :BreakTarget
var continueBlock = new Block();
if (continueAction != null)
{
var blockAfterContinue = new Block();
// Represent the goto over the condition before the first iteration.
_currentBlock.FlowsTo(blockAfterContinue);
_currentBlock = continueBlock;
continueAction.Visit(this.Decorator);
_currentBlock.FlowsTo(blockAfterContinue);
_currentBlock = blockAfterContinue;
}
else
{
_currentBlock.FlowsTo(continueBlock);
_currentBlock = continueBlock;
}
var bodyBlock = new Block();
var breakBlock = new Block();
// Condition can be null from an uncommon for loop: for() {}
if (generateCondition != null)
{
generateCondition();
_currentBlock.FlowsTo(breakBlock);
}
_loopTargets.Add(new LoopGotoTargets(loopLabel ?? "", breakBlock, continueBlock));
_currentBlock.FlowsTo(bodyBlock);
_currentBlock = bodyBlock;
generateLoopBody();
_currentBlock.FlowsTo(continueBlock);
_currentBlock = breakBlock;
_loopTargets.RemoveAt(_loopTargets.Count - 1);
}
示例14: VisitChildren
private void VisitChildren(Ast ast, object newParentObject)
{
this.ParentObjectStack.Push(newParentObject);
ast.Visit(this);
this.ParentObjectStack.Pop();
}
示例15: AnalyzeImpl
/// <summary>
/// Used to analyze scriptbloct, functionmemberast or functiondefinitionast
/// </summary>
/// <param name="ast"></param>
/// <returns></returns>
public void AnalyzeImpl(Ast ast, VariableAnalysis outerAnalysis)
{
if (!(ast is ScriptBlockAst || ast is FunctionMemberAst || ast is FunctionDefinitionAst))
{
return;
}
_variables = FindAllVariablesVisitor.Visit(ast);
Init();
if (ast is FunctionMemberAst || ast is FunctionDefinitionAst)
{
IEnumerable<ParameterAst> parameters = FindParameters(ast, ast.GetType());
if (parameters != null)
{
ProcessParameters(parameters);
}
}
else
{
ScriptBlockAst sbAst = ast as ScriptBlockAst;
if (sbAst != null && sbAst.ParamBlock != null && sbAst.ParamBlock.Parameters != null)
{
ProcessParameters(sbAst.ParamBlock.Parameters);
}
}
if (ast is FunctionMemberAst)
{
(ast as FunctionMemberAst).Body.Visit(this.Decorator);
}
else if (ast is FunctionDefinitionAst)
{
(ast as FunctionDefinitionAst).Body.Visit(this.Decorator);
}
else
{
ast.Visit(this.Decorator);
}
Ast parent = ast;
while (parent.Parent != null)
{
parent = parent.Parent;
}
List<TypeDefinitionAst> classes = parent.FindAll(item =>
item is TypeDefinitionAst && (item as TypeDefinitionAst).IsClass, true)
.Cast<TypeDefinitionAst>().ToList();
if (outerAnalysis != null)
{
// Initialize the variables from outside
var outerDictionary = outerAnalysis.InternalVariablesDictionary;
foreach (var details in outerDictionary.Values)
{
if (details.DefinedBlock != null)
{
var assignTarget = new AssignmentTarget(details.RealName, details.Type);
assignTarget.Constant = details.Constant;
if (!_variables.ContainsKey(assignTarget.Name))
{
_variables.Add(assignTarget.Name, new VariableAnalysisDetails
{
Name = assignTarget.Name,
RealName = assignTarget.Name,
Type = assignTarget.Type
});
}
Entry.AddFirstAst(assignTarget);
}
}
foreach (var key in _variables.Keys)
{
if (outerDictionary.ContainsKey(key))
{
var outerItem = outerDictionary[key];
var innerItem = _variables[key];
innerItem.Constant = outerItem.Constant;
innerItem.Name = outerItem.Name;
innerItem.RealName = outerItem.RealName;
innerItem.Type = outerItem.Type;
}
}
}
var dictionaries = Block.SparseSimpleConstants(_variables, Entry, classes);
VariablesDictionary = dictionaries.Item1;
InternalVariablesDictionary = new Dictionary<string, VariableAnalysisDetails>(StringComparer.OrdinalIgnoreCase);
foreach (var KVP in dictionaries.Item2)
{
//.........这里部分代码省略.........