コード例 #1
0
ファイル: OverviewWalker.cs プロジェクト: jsschultz/PTVS
 public override bool Walk(FromImportStatement node) {
     UpdateChildRanges(node);
     
     var asNames = node.AsNames ?? node.Names;
     int len = Math.Min(node.Names.Count, asNames.Count);
     for (int i = 0; i < len; i++) {
         var nameNode = asNames[i] ?? node.Names[i];
         if (nameNode != null) {
             if (nameNode.Name == "*") {
                 _scope.ContainsImportStar = true;
             } else {
                 CreateVariableInDeclaredScope(nameNode);
             }
         }
     }
     return base.Walk(node);
 }
コード例 #2
0
ファイル: CoverageMapper.cs プロジェクト: jsschultz/PTVS
        public override bool Walk(FromImportStatement node) {
            UpdateLineInfo(node);

            return base.Walk(node);
        }
コード例 #3
0
ファイル: ImportRemover.cs プロジェクト: omnimark/PTVS
 public UpdatedFromImportStatement(FromImportStatement fromImport) {
     _fromImport = fromImport;
 }
コード例 #4
0
ファイル: ImportRemover.cs プロジェクト: omnimark/PTVS
            public override bool Walk(FromImportStatement node) {
                if (InTargetScope && !node.IsFromFuture && !(_scopes[_scopes.Count - 1] is ClassDefinition)) {
                    for (int i = 0; i < node.Names.Count; i++) {
                        if (node.Names[i].Name == "*") {
                            // ignore from .. import *
                            continue;
                        }

                        if (node.AsNames != null && node.AsNames[i] != null) {
                            TrackImport(node, node.AsNames[i].Name);
                        } else {
                            TrackImport(node, node.Names[i].Name);
                        }
                    }
                }
                return base.Walk(node);
            }
コード例 #5
0
ファイル: Parser.cs プロジェクト: omnimark/PTVS
        // 'from' relative_module 'import' identifier ['as' name] (',' identifier ['as' name]) *
        // 'from' relative_module 'import' '(' identifier ['as' name] (',' identifier ['as' name])* [','] ')'        
        // 'from' module 'import' "*"                                        
        private FromImportStatement ParseFromImportStmt() {
            Eat(TokenKind.KeywordFrom);
            string fromWhiteSpace = _tokenWhiteSpace;
            var start = GetStart();
            ModuleName dname = ParseRelativeModuleName();

            bool ateImport = Eat(TokenKind.KeywordImport);
            string importWhiteSpace = _tokenWhiteSpace;

            bool ateParen = ateImport && MaybeEat(TokenKind.LeftParenthesis);
            string parenWhiteSpace = ateParen ? _tokenWhiteSpace : null;

            NameExpression/*!*/[] names;
            NameExpression[] asNames;
            bool fromFuture = false;

            List<string> namesWhiteSpace = null;
            if (ateImport) {
                if (MaybeEat(TokenKind.Multiply)) {
                    if (_langVersion.Is3x() && ((_functions != null && _functions.Count > 0) || _classDepth > 0)) {
                        ReportSyntaxError(start, GetEnd(), "import * only allowed at module level");
                    }

                    if (_verbatim) {
                        namesWhiteSpace = new List<string>() { _tokenWhiteSpace };
                    }
                    names = new[] { new NameExpression("*") };
                    asNames = null;
                } else {
                    List<NameExpression/*!*/> l = new List<NameExpression>();
                    List<NameExpression> las = new List<NameExpression>();
                    ParseAsNameList(l, las, out namesWhiteSpace);

                    names = l.ToArray();
                    asNames = las.ToArray();
                }
            } else {
                names = EmptyNames;
                asNames = EmptyNames;
            }

            // Process from __future__ statement
            if (dname.Names.Count == 1 && dname.Names[0].Name == "__future__") {
                fromFuture = ProcessFutureStatements(start, names, fromFuture);
            }

            bool ateRightParen = false;
            if (ateParen) {
                ateRightParen = Eat(TokenKind.RightParenthesis);
            }

            FromImportStatement ret = new FromImportStatement(dname, names, asNames, fromFuture, AbsoluteImports);
            if (_verbatim) {
                AddPreceedingWhiteSpace(ret, fromWhiteSpace);
                AddSecondPreceedingWhiteSpace(ret, importWhiteSpace);
                if (namesWhiteSpace != null) {
                    AddNamesWhiteSpace(ret, namesWhiteSpace.ToArray());
                }
                if (ateParen) {
                    AddThirdPreceedingWhiteSpace(ret, parenWhiteSpace);
                    AddFourthPreceedingWhiteSpace(ret, _tokenWhiteSpace);
                    if (!ateRightParen) {
                        AddErrorMissingCloseGrouping(ret);
                    }
                } else {
                    AddIsAltForm(ret);
                }
                if (!ateImport) {
                    AddErrorIsIncompleteNode(ret);
                }
                
            }
            ret.SetLoc(start, GetEnd());
            return ret;
        }
コード例 #6
0
        /// <summary>
        /// Returns a new FromImport statement that is identical to this one but has
        /// removed the specified import statement.  Otherwise preserves any attributes
        /// for the statement.
        ///
        /// New in 1.1.
        /// <param name="ast">The parent AST whose attributes should be updated for the new node.</param>
        /// <param name="index">The index in Names of the import to be removed.</param>
        /// </summary>
        public FromImportStatement RemoveImport(PythonAst ast, int index)
        {
            if (index < 0 || index >= Names.Count)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            if (ast == null)
            {
                throw new ArgumentNullException("ast");
            }

            NameExpression[] names            = new NameExpression[Names.Count - 1];
            NameExpression[] asNames          = AsNames == null ? null : new NameExpression[AsNames.Count - 1];
            var           asNameWhiteSpace    = this.GetNamesWhiteSpace(ast);
            List <string> newAsNameWhiteSpace = new List <string>();
            int           importIndex         = ImportIndex;
            int           asIndex             = 0;

            for (int i = 0, write = 0; i < Names.Count; i++)
            {
                bool includingCurrentName = i != index;

                // track the white space, this needs to be kept in sync w/ ToCodeString and how the
                // parser creates the white space.

                if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
                {
                    if (write > 0)
                    {
                        if (includingCurrentName)
                        {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                        else
                        {
                            asIndex++;
                        }
                    }
                    else if (i > 0)
                    {
                        asIndex++;
                    }
                }

                if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
                {
                    if (includingCurrentName)
                    {
                        if (newAsNameWhiteSpace.Count == 0)
                        {
                            // no matter what we want the 1st entry to have the whitespace after the import keyword
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[0]);
                            asIndex++;
                        }
                        else
                        {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                    }
                    else
                    {
                        asIndex++;
                    }
                }

                if (includingCurrentName)
                {
                    names[write] = Names[i];

                    if (AsNames != null)
                    {
                        asNames[write] = AsNames[i];
                    }

                    write++;
                }

                if (AsNames != null && AsNames[i] != null)
                {
                    if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
                    {
                        if (i != index)
                        {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                        else
                        {
                            asIndex++;
                        }
                    }

                    if (AsNames[i].Name.Length != 0)
                    {
                        if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
                        {
                            if (i != index)
                            {
                                newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                            }
                            else
                            {
                                asIndex++;
                            }
                        }
                    }
                    else
                    {
                        asIndex++;
                    }
                }
            }

            if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
            {
                // trailing comma
                newAsNameWhiteSpace.Add(asNameWhiteSpace[asNameWhiteSpace.Length - 1]);
            }

            var res = new FromImportStatement(Root, names, asNames, IsFromFuture, ForceAbsolute, importIndex);

            ast.CopyAttributes(this, res);
            ast.SetAttribute(res, NodeAttributes.NamesWhiteSpace, newAsNameWhiteSpace.ToArray());

            return(res);
        }
コード例 #7
0
ファイル: MethodExtractor.cs プロジェクト: wenh123/PTVS
 public override bool Walk(FromImportStatement node) {
     var vars = node.Variables;
     var refs = node.GetReferences(_root);
     if (refs != null) { // from .. import * will have null refs
         for (int i = 0; i < vars.Length; i++) {
             if (vars[i] != null) {
                 _allWrites.Add(refs[i]);
                 _allWrittenVariables.Add(vars[i]);
             }
         }
     }
     return base.Walk(node);
 }
コード例 #8
0
        private static AP.ChangeInfo UpdateFromImport(
            PythonAst curAst,
            FromImportStatement fromImport,
            string name
        ) {
            NameExpression[] names = new NameExpression[fromImport.Names.Count + 1];
            NameExpression[] asNames = fromImport.AsNames == null ? null : new NameExpression[fromImport.AsNames.Count + 1];
            NameExpression newName = new NameExpression(name);
            for (int i = 0; i < fromImport.Names.Count; i++) {
                names[i] = fromImport.Names[i];
            }
            names[fromImport.Names.Count] = newName;

            if (asNames != null) {
                for (int i = 0; i < fromImport.AsNames.Count; i++) {
                    asNames[i] = fromImport.AsNames[i];
                }
            }

            var newImport = new FromImportStatement((ModuleName)fromImport.Root, names, asNames, fromImport.IsFromFuture, fromImport.ForceAbsolute);
            curAst.CopyAttributes(fromImport, newImport);

            var newCode = newImport.ToCodeString(curAst);

            var span = fromImport.GetSpan(curAst);
            int leadingWhiteSpaceLength = (fromImport.GetLeadingWhiteSpace(curAst) ?? "").Length;
            return new AP.ChangeInfo() {
                start = span.Start.Index - leadingWhiteSpaceLength,
                length = span.Length + leadingWhiteSpaceLength,
                newText = newCode
            };
        }
コード例 #9
0
ファイル: FlowChecker.cs プロジェクト: omnimark/PTVS
 // FromImportStmt
 public override bool Walk(FromImportStatement node) {
     if (node.Names.Count != 1 || node.Names[0].Name != "*") {
         for (int i = 0; i < node.Names.Count; i++) {
             Define(node.AsNames[i] != null ? node.AsNames[i].Name : node.Names[i].Name);
         }
     }
     return true;
 }
コード例 #10
0
ファイル: MethodExtractor.cs プロジェクト: wenh123/PTVS
 public override bool Walk(FromImportStatement node) {
     if (node.Names.Count == 1 && node.Names[0].Name == "*") {
         ContainsImportStar = true;
     }
     return base.Walk(node);
 }
コード例 #11
0
ファイル: EnclosingNodeWalker.cs プロジェクト: omnimark/PTVS
 public override void PostWalk(FromImportStatement node) { PostWalkWorker(node); }
コード例 #12
0
ファイル: EnclosingNodeWalker.cs プロジェクト: omnimark/PTVS
 // FromImportStatement
 public override bool Walk(FromImportStatement node) { return ShouldWalkWorker(node); }
コード例 #13
0
ファイル: FromImportStatement.cs プロジェクト: wenh123/PTVS
        /// <summary>
        /// Returns a new FromImport statement that is identical to this one but has
        /// removed the specified import statement.  Otherwise preserves any attributes
        /// for the statement.
        /// 
        /// New in 1.1.
        /// <param name="ast">The parent AST whose attributes should be updated for the new node.</param>
        /// <param name="index">The index in Names of the import to be removed.</param>
        /// </summary>
        public FromImportStatement RemoveImport(PythonAst ast, int index) {
            if (index < 0 || index >= _names.Length) {
                throw new ArgumentOutOfRangeException("index");
            }
            if (ast == null) {
                throw new ArgumentNullException("ast");
            }

            NameExpression[] names = new NameExpression[_names.Length - 1];
            NameExpression[] asNames = _asNames == null ? null : new NameExpression[_asNames.Length - 1];
            var asNameWhiteSpace = this.GetNamesWhiteSpace(ast);
            List<string> newAsNameWhiteSpace = new List<string>();
            int asIndex = 0;
            for (int i = 0, write = 0; i < _names.Length; i++) {
                bool includingCurrentName = i != index;

                // track the white space, this needs to be kept in sync w/ ToCodeString and how the
                // parser creates the white space.

                if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) {
                    if (write > 0) {
                        if (includingCurrentName) {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        } else {
                            asIndex++;
                        }
                    } else if (i > 0) {
                        asIndex++;
                    }
                }

                if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) {
                    if (includingCurrentName) {
                        if (newAsNameWhiteSpace.Count == 0) {
                            // no matter what we want the 1st entry to have the whitespace after the import keyword
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[0]);
                            asIndex++;
                        } else {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                    } else {
                        asIndex++;
                    }
                }

                if (includingCurrentName) {
                    names[write] = _names[i];

                    if (_asNames != null) {
                        asNames[write] = _asNames[i];
                    }

                    write++;
                }

                if (AsNames != null && AsNames[i] != null) {
                    if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) {
                        if (i != index) {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        } else {
                            asIndex++;
                        }
                    }

                    if (_asNames[i].Name.Length != 0) {
                        if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) {
                            if (i != index) {
                                newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                            } else {
                                asIndex++;
                            }
                        }
                    } else {
                        asIndex++;
                    }
                }
            }

            if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) {
                // trailing comma
                newAsNameWhiteSpace.Add(asNameWhiteSpace[asNameWhiteSpace.Length - 1]);
            }

            var res = new FromImportStatement(_root, names, asNames, IsFromFuture, ForceAbsolute);
            ast.CopyAttributes(this, res);
            ast.SetAttribute(res, NodeAttributes.NamesWhiteSpace, newAsNameWhiteSpace.ToArray());

            return res;
        }
コード例 #14
0
ファイル: PythonNameBinder.cs プロジェクト: wenh123/PTVS
 // FromImportStatement
 public override bool Walk(FromImportStatement node) {
     if (node.Names.Count != 1 || node.Names[0].Name !="*") {
         PythonVariable[] variables = new PythonVariable[node.Names.Count];
         PythonReference[] references = null;
         if (_bindRefs) {
             references = new PythonReference[node.Names.Count];
         }
         for (int i = 0; i < node.Names.Count; i++) {
             variables[i] = DefineName(node.AsNames[i] != null ? node.AsNames[i].Name : node.Names[i].Name);
             if (references != null) {
                 references[i] = Reference(variables[i].Name);
             }
         }
         node.Variables = variables;
         node.AddVariableReference(_ast, _bindRefs, references);
     } else {
         Debug.Assert(_currentScope != null);
         _currentScope.ContainsImportStar = true;
         _currentScope.NeedsLocalsDictionary = true;
         _currentScope.HasLateBoundVariableSets = true;
     }
     return true;
 }
コード例 #15
0
ファイル: AstAnalysisWalker.cs プロジェクト: jsschultz/PTVS
        public override bool Walk(FromImportStatement node) {
            var m = _scope.Peek();
            if (node.Root.MakeString() == "__future__") {
                return false;
            }

            if (m != null && node.Names != null) {
                try {
                    for (int i = 0; i < node.Names.Count; ++i) {
                        var n = node.AsNames?[i] ?? node.Names[i];
                        if (n != null) {
                            m[n.Name] = new AstPythonConstant(
                                _interpreter.GetBuiltinType(BuiltinTypeId.Unknown),
                                GetLoc(n)
                            );
                        }
                    }
                } catch (IndexOutOfRangeException) {
                }
            }
            return false;
        }
コード例 #16
0
 public override bool Walk(FromImportStatement node) {
     Debug.Assert(_head != null);
     if (node.Root != null) {
         foreach (var name in node.Root.Names) {
             if (name != null && !string.IsNullOrEmpty(name.Name)) {
                 AddSpan(Tuple.Create(name.Name, Span.FromBounds(name.StartIndex, name.EndIndex)), PythonPredefinedClassificationTypeNames.Module);
             }
         }
     }
     if (node.Names != null) {
         foreach (var name in node.Names) {
             if (name != null && !string.IsNullOrEmpty(name.Name)) {
                 _head.Names.Add(Tuple.Create(name.Name, Span.FromBounds(name.StartIndex, name.EndIndex)));
             }
         }
     }
     return base.Walk(node);
 }
コード例 #17
0
        private static void UpdateFromImport(
            PythonAst curAst,
            ITextBuffer buffer,
            FromImportStatement fromImport,
            string name
        ) {
            NameExpression[] names = new NameExpression[fromImport.Names.Count + 1];
            NameExpression[] asNames = fromImport.AsNames == null ? null : new NameExpression[fromImport.AsNames.Count + 1];
            NameExpression newName = new NameExpression(name);
            for (int i = 0; i < fromImport.Names.Count; i++) {
                names[i] = fromImport.Names[i];
            }
            names[fromImport.Names.Count] = newName;

            if (asNames != null) {
                for (int i = 0; i < fromImport.AsNames.Count; i++) {
                    asNames[i] = fromImport.AsNames[i];
                }
            }

            var newImport = new FromImportStatement((ModuleName)fromImport.Root, names, asNames, fromImport.IsFromFuture, fromImport.ForceAbsolute);
            curAst.CopyAttributes(fromImport, newImport);

            var newCode = newImport.ToCodeString(curAst);

            var span = fromImport.GetSpan(curAst);
            int leadingWhiteSpaceLength = (fromImport.GetLeadingWhiteSpace(curAst) ?? "").Length;
            using (var edit = buffer.CreateEdit()) {
                edit.Delete(span.Start.Index - leadingWhiteSpaceLength, span.Length + leadingWhiteSpaceLength);
                edit.Insert(span.Start.Index, newCode);
                edit.Apply();
            }
        }
コード例 #18
0
 public override bool Walk(FromImportStatement node) {
     var name = node.Root.MakeString();
     if (!_analyzer.IsModuleResolved(_entry, name, node.ForceAbsolute)) {
         Imports.Add(Tuple.Create(name, node.Root));
     }
     return base.Walk(node);
 }
コード例 #19
0
ファイル: ImportRemover.cs プロジェクト: omnimark/PTVS
 public RemovedFromImportStatement(FromImportStatement fromImport) {
     _fromImport = fromImport;
 }
コード例 #20
0
ファイル: ExtractMethodTests.cs プロジェクト: RussBaz/PTVS
            public override bool Walk(FromImportStatement node) {
                var vars = node.Variables;
                for (int i = 0; i < vars.Length; i++) {
                    if (vars[i] != null) {
                        _names.Add(vars[i].Name);
                    }
                }

                return base.Walk(node);
            }
コード例 #21
0
ファイル: OutliningWalker.cs プロジェクト: RussBaz/PTVS
 public override bool Walk(FromImportStatement node) {
     AddTagIfNecessary(node);
     return base.Walk(node);
 }