// PythonAst public override bool Walk(PythonAst node) { return Location >= node.StartIndex && Location <= node.EndIndex; }
public StaticPythonParse(PythonAst tree, IAnalysisCookie cookie) { Tree = tree; Cookie = cookie; }
public ISet <AnalysisModuleKey> GetDependencies(PythonAst ast) => _dependencies;
// PythonAst public virtual bool Walk(PythonAst node) { return true; }
public SetComprehensionAnalysisUnit(Comprehension node, PythonAst parent, AnalysisUnit outerUnit, InterpreterScope outerScope) : base(node, parent, new ComprehensionScope(new SetInfo(outerUnit.ProjectState, node), node, outerScope), outerUnit) { }
public static bool IsAltForm(this Node node, PythonAst ast) { object dummy; if (ast.TryGetAttribute(node, NodeAttributes.IsAltFormValue, out dummy)) { return true; } else { return false; } }
public static string[] GetVerbatimNames(this Node node, PythonAst ast) { object names; if (ast.TryGetAttribute(node, NodeAttributes.VerbatimNames, out names)) { return (string[])names; } else { return null; } }
public GlobalScope(IPythonModule module, PythonAst ast) : base(null, null, module) { _ast = ast; DeclareBuiltinVariables(); }
public override int GetBodyStart(PythonAst ast) { return(ast.IndexToLocation(((FunctionDefinition)Node).HeaderIndex).Index); }
public static Expression FindExpression(this PythonAst ast, int index, FindExpressionOptions options) => new ExpressionFinder(ast, options).GetExpression(index) as Expression;
public static Expression FindExpression(this PythonAst ast, SourceLocation location, FindExpressionOptions options) => new ExpressionFinder(ast, options).GetExpression(location) as Expression;
public QualifiedFunctionNameWalker(PythonAst ast, int lineNumber, string expectedFuncName) { _ast = ast; _lineNumber = lineNumber; _expectedFuncName = expectedFuncName; }
public ListComprehensionAnalysisUnit(Comprehension node, PythonAst parent, AnalysisUnit outerUnit, InterpreterScope outerScope) : base(node, parent, new ComprehensionScope(new ListInfo(VariableDef.EmptyArray, outerUnit.ProjectState.ClassInfos[BuiltinTypeId.List], node), node, outerScope), outerUnit) { }
public DictionaryComprehensionAnalysisUnit(Comprehension node, PythonAst parent, AnalysisUnit outerUnit, InterpreterScope outerScope) : base(node, parent, new ComprehensionScope(new DictionaryInfo(outerUnit.ProjectEntry, node), node, outerScope), outerUnit) { }
public static string GetSecondWhiteSpace(this Node node, PythonAst ast) { return GetWhiteSpace(node, ast, NodeAttributes.SecondPreceedingWhiteSpace); }
internal static SourceLocation GetStartIncludingIndentation(this Node self, PythonAst ast) { return(ast.IndexToLocation(self.StartIndex - (self.GetIndentationLevel(ast) ?? "").Length)); }
public static string GetFifthWhiteSpace(this Node node, PythonAst ast) { return GetWhiteSpace(node, ast, NodeAttributes.FifthPreceedingWhiteSpace); }
internal static SourceLocation GetStartIncludingLeadingWhiteSpace(this Node self, PythonAst ast) { return(ast.IndexToLocation(self.StartIndex - (self.GetLeadingWhiteSpace(ast) ?? "").Length)); }
public static bool IsIncompleteNode(this Node node, PythonAst ast) { object dummy; if (ast.TryGetAttribute(node, NodeAttributes.ErrorIncompleteNode, out dummy)) { return true; } else { return false; } }
public SelectionTarget(Dictionary <ScopeStatement, SourceLocation> insertLocations, ScopeStatement[] parents, PythonAst ast) { Parents = parents; InsertLocations = insertLocations; Ast = ast ?? throw new ArgumentNullException(nameof(ast)); }
public static void AddVariableReference(this Node node, PythonAst ast, bool bindNames, object reference) { if (bindNames) { ast.SetAttribute(node, VariableReference, reference); } }
private CallExpression GetCall(PythonAst ast) { var statements = (ast.Body as SuiteStatement)?.Statements; return(statements?.OfType <ExpressionStatement>().FirstOrDefault(e => e.Expression is CallExpression)?.Expression as CallExpression); }
// PythonAst public override bool Walk(PythonAst node) { return false; }
internal static int GetStartIncludingIndentation(this Node self, PythonAst ast) { return(self.StartIndex - (self.GetIndentationLevel(ast) ?? "").Length); }
internal static string GetDefaultValue(PythonAnalyzer state, Parameter curParam, PythonAst tree) { var v = curParam.DefaultValue; if (v == null) { return(null); } else if (v is ConstantExpression ce) { return(ce.GetConstantRepr(state.LanguageVersion)); } else if (v is NameExpression ne) { return(ne.Name); } else if (v is DictionaryExpression dict) { return(dict.Items.Any() ? "{...}" : "{}"); } else if (v is ListExpression list) { return(list.Items.Any() ? "[...]" : "[]"); } else if (v is TupleExpression tuple) { return(tuple.Items.Any() ? "(...)" : "()"); } else { return(v.ToCodeString(tree, CodeFormattingOptions.Traditional).Trim()); } }
public ExpressionFinder(PythonAst ast, FindExpressionOptions options) { Ast = ast; Options = options; }
public override void PostWalk(PythonAst node) { Scope.PopScope(); base.PostWalk(node); }
public static Node GetNode(PythonAst ast, SourceLocation location, FindExpressionOptions options) { var finder = new ExpressionFinder(ast, options); return(finder.GetExpression(location)); }
private void AnalyzeStdLib(string outdir) { var allFiles = new List <List <string> >(); foreach (var dir in _dirs) { CollectFiles(dir, allFiles); } foreach (var files in allFiles) { if (files.Count > 0) { Console.WriteLine("Now analyzing: {0}", Path.GetDirectoryName(files[0])); var projectState = new PythonAnalyzer(new CPythonInterpreter(new TypeDatabase(_indir, _version.Is3x())), _version); var modules = new List <IPythonProjectEntry>(); for (int i = 0; i < files.Count; i++) { string modName = PythonAnalyzer.PathToModuleName(files[i]); modules.Add(projectState.AddModule(modName, files[i])); } var nodes = new List <PythonAst>(); for (int i = 0; i < modules.Count; i++) { PythonAst ast = null; try { var sourceUnit = new StreamReader(files[i]); ast = Parser.CreateParser(sourceUnit, Microsoft.PythonTools.Parsing.ErrorSink.Null, PythonLanguageVersion.V27).ParseFile(); } catch (Exception) { } nodes.Add(ast); } for (int i = 0; i < modules.Count; i++) { var ast = nodes[i]; if (ast != null) { modules[i].UpdateTree(ast, null); } } for (int i = 0; i < modules.Count; i++) { var ast = nodes[i]; if (ast != null) { modules[i].Analyze(true); } } if (modules.Count > 0) { modules[0].AnalysisGroup.AnalyzeQueuedEntries(); } new SaveAnalysis().Save(projectState, outdir); } } }
public MemberTargetExpressionWalker(PythonAst ast, int location) : base(location) { _ast = ast; }
internal static string GetWhiteSpace(Node node, PythonAst ast, object kind, string defaultValue = " ") { object whitespace; if (ast.TryGetAttribute(node, kind, out whitespace)) { return (string)whitespace; } else { return defaultValue; } }
public KeywordWalker(PythonAst ast, int location, int endLocation) : base(location) { _ast = ast; _endLocation = endLocation; }
public static string GetFourthWhiteSpaceDefaultNull(this Node node, PythonAst ast) { return GetWhiteSpace(node, ast, NodeAttributes.FourthPreceedingWhiteSpace, null); }
public override int GetBodyStart(PythonAst ast) { return(((ClassDefinition)Node).HeaderIndex); }
public static string GetExtraVerbatimText(this Node node, PythonAst ast) { return GetWhiteSpace(node, ast, NodeAttributes.ExtraVerbatimText, null); }
// PythonAst public override bool Walk(PythonAst node) { return(false); }
public static bool IsMissingCloseGrouping(this Node node, PythonAst ast) { object dummy; if (ast.TryGetAttribute(node, NodeAttributes.ErrorMissingCloseGrouping, out dummy)) { return true; } else { return false; } }
public override void PostWalk(PythonAst node) { }
public static string[] GetNamesWhiteSpace(this Node node, PythonAst ast) { object whitespace; if (ast.TryGetAttribute(node, NodeAttributes.NamesWhiteSpace, out whitespace)) { return (string[])whitespace; } else { return null; } }
// PythonAst public virtual bool Walk(PythonAst node) { return(true); }
public static string GetVerbatimImage(this Node node, PythonAst ast) { object image; if (ast.TryGetAttribute(node, NodeAttributes.VerbatimImage, out image)) { return (string)image; } else { return null; } }
public virtual void PostWalk(PythonAst node) { }
public static void AddVariable(this Parameter node, PythonAst ast, bool bindNames, PythonVariable variable) { if (bindNames) { ast.SetAttribute(node, Variable, variable); } }
public OutOfProcMethodExtractor(PythonAst ast, string code) { _extractor = new MethodExtractor(ast, code); }
protected virtual PythonWalker PrepareWalker(AstPythonInterpreter interpreter, PythonAst ast) { #if DEBUG // In debug builds we let you F12 to the scraped file var filePath = string.IsNullOrEmpty(_filePath) ? null : interpreter.ModuleCache.GetCacheFilePath(_filePath); var uri = string.IsNullOrEmpty(filePath) ? null : new Uri(filePath); const bool includeLocations = true; #else const string filePath = null; const Uri uri = null; const bool includeLocations = false; #endif return(new AstAnalysisWalker( interpreter, interpreter.CurrentPathResolver, ast, this, filePath, uri, _members, includeLocations, warnAboutUndefinedValues: true, suppressBuiltinLookup: false )); }
internal static string GetDefaultValue(PythonAnalyzer state, Parameter curParam, PythonAst tree) { if (curParam.DefaultValue != null) { // TODO: Support all possible expressions for default values, we should // probably have a PythonAst walker for expressions or we should add ToCodeString() // onto Python ASTs so they can round trip ConstantExpression defaultValue = curParam.DefaultValue as ConstantExpression; if (defaultValue != null) { return(defaultValue.GetConstantRepr(state.LanguageVersion)); } else { NameExpression nameExpr = curParam.DefaultValue as NameExpression; if (nameExpr != null) { return(nameExpr.Name); } else { DictionaryExpression dict = curParam.DefaultValue as DictionaryExpression; if (dict != null) { if (dict.Items.Count == 0) { return("{}"); } else { return("{...}"); } } else { ListExpression list = curParam.DefaultValue as ListExpression; if (list != null) { if (list.Items.Count == 0) { return("[]"); } else { return("[...]"); } } else { TupleExpression tuple = curParam.DefaultValue as TupleExpression; if (tuple != null) { if (tuple.Items.Count == 0) { return("()"); } else { return("(...)"); } } else { return(curParam.DefaultValue.ToCodeString(tree)); } } } } } } return(null); }