Esempio n. 1
0
 // PythonAst
 public override bool Walk(PythonAst node) { return Location >= node.StartIndex && Location <= node.EndIndex; }
Esempio n. 2
0
 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; }
Esempio n. 5
0
 public SetComprehensionAnalysisUnit(Comprehension node, PythonAst parent, AnalysisUnit outerUnit, InterpreterScope outerScope)
     : base(node, parent,
            new ComprehensionScope(new SetInfo(outerUnit.ProjectState, node), node, outerScope),
            outerUnit)
 {
 }
Esempio n. 6
0
 public static bool IsAltForm(this Node node, PythonAst ast) {
     object dummy;
     if (ast.TryGetAttribute(node, NodeAttributes.IsAltFormValue, out dummy)) {
         return true;
     } else {
         return false;
     }
 }
Esempio n. 7
0
 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();
 }
Esempio n. 9
0
 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;
Esempio n. 12
0
 public QualifiedFunctionNameWalker(PythonAst ast, int lineNumber, string expectedFuncName)
 {
     _ast              = ast;
     _lineNumber       = lineNumber;
     _expectedFuncName = expectedFuncName;
 }
Esempio n. 13
0
 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)
 {
 }
Esempio n. 14
0
 public DictionaryComprehensionAnalysisUnit(Comprehension node, PythonAst parent, AnalysisUnit outerUnit, InterpreterScope outerScope)
     : base(node, parent,
            new ComprehensionScope(new DictionaryInfo(outerUnit.ProjectEntry, node), node, outerScope),
            outerUnit)
 {
 }
Esempio n. 15
0
 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));
 }
Esempio n. 17
0
 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));
 }
Esempio n. 19
0
 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));
 }
Esempio n. 21
0
 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);
        }
Esempio n. 23
0
 // PythonAst
 public override bool Walk(PythonAst node) { return false; }
Esempio n. 24
0
 internal static int GetStartIncludingIndentation(this Node self, PythonAst ast)
 {
     return(self.StartIndex - (self.GetIndentationLevel(ast) ?? "").Length);
 }
Esempio n. 25
0
        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());
            }
        }
Esempio n. 26
0
 public ExpressionFinder(PythonAst ast, FindExpressionOptions options)
 {
     Ast     = ast;
     Options = options;
 }
Esempio n. 27
0
 public override void PostWalk(PythonAst node)
 {
     Scope.PopScope();
     base.PostWalk(node);
 }
Esempio n. 28
0
        public static Node GetNode(PythonAst ast, SourceLocation location, FindExpressionOptions options)
        {
            var finder = new ExpressionFinder(ast, options);

            return(finder.GetExpression(location));
        }
Esempio n. 29
0
        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);
                }
            }
        }
Esempio n. 30
0
 public MemberTargetExpressionWalker(PythonAst ast, int location) : base(location)
 {
     _ast = ast;
 }
Esempio n. 31
0
 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;
     }
 }
Esempio n. 32
0
 public KeywordWalker(PythonAst ast, int location, int endLocation) : base(location)
 {
     _ast         = ast;
     _endLocation = endLocation;
 }
Esempio n. 33
0
 public static string GetFourthWhiteSpaceDefaultNull(this Node node, PythonAst ast) {
     return GetWhiteSpace(node, ast, NodeAttributes.FourthPreceedingWhiteSpace, null);
 }
Esempio n. 34
0
 public override int GetBodyStart(PythonAst ast)
 {
     return(((ClassDefinition)Node).HeaderIndex);
 }
Esempio n. 35
0
 public static string GetExtraVerbatimText(this Node node, PythonAst ast) {
     return GetWhiteSpace(node, ast, NodeAttributes.ExtraVerbatimText, null);
 }
Esempio n. 36
0
 // PythonAst
 public override bool Walk(PythonAst node)
 {
     return(false);
 }
Esempio n. 37
0
 public static bool IsMissingCloseGrouping(this Node node, PythonAst ast) {
     object dummy;
     if (ast.TryGetAttribute(node, NodeAttributes.ErrorMissingCloseGrouping, out dummy)) {
         return true;
     } else {
         return false;
     }
 }
Esempio n. 38
0
 public override void PostWalk(PythonAst node)
 {
 }
Esempio n. 39
0
 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;
     }
 }
Esempio n. 40
0
 // PythonAst
 public virtual bool Walk(PythonAst node)
 {
     return(true);
 }
Esempio n. 41
0
 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;
     }
 }
Esempio n. 42
0
 public virtual void PostWalk(PythonAst node)
 {
 }
Esempio n. 43
0
 public static void AddVariable(this Parameter node, PythonAst ast, bool bindNames, PythonVariable variable) {
     if (bindNames) {
         ast.SetAttribute(node, Variable, variable);
     }
 }
Esempio n. 44
0
 public OutOfProcMethodExtractor(PythonAst ast, string code)
 {
     _extractor = new MethodExtractor(ast, code);
 }
Esempio n. 45
0
 public virtual void PostWalk(PythonAst node) { }
Esempio n. 46
0
        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
                       ));
        }
Esempio n. 47
0
 public override void PostWalk(PythonAst node) { }
Esempio n. 48
0
 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);
 }