Esempio n. 1
0
 // FromImportStatement
 public virtual bool Walk(FromImportStatement node)
 {
     return(true);
 }
Esempio n. 2
0
 // FromImportStatement
 public override bool Walk(FromImportStatement node)
 {
     return(Contains(node));
 }
Esempio n. 3
0
 public override void PostWalk(FromImportStatement node)
 {
 }
Esempio n. 4
0
 // FromImportStatement
 public override bool Walk(FromImportStatement node)
 {
     return(false);
 }
 public virtual void PostWalk(FromImportStatement node)
 {
 }
Esempio n. 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");
            }

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

            for (int i = 0, write = 0; i < Names.Count; i++)
            {
                var 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);
        }