Exemplo n.º 1
0
 public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
 {
     if (args.Length == 0) {
         return unit.Scope.GetOrMakeNodeValue(node, n => new IteratorInfo(_indexTypes, _iterClass, n));
     }
     return NamespaceSet.Empty;
 }
Exemplo n.º 2
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            if (args.Length == 2) {
                _list.AppendItem(node, unit, args[1]);
            }

            return ProjectState._noneInst.SelfSet;
        }
Exemplo n.º 3
0
 public FromImportStatement(ModuleName root, NameExpression/*!*/[] names, NameExpression[] asNames, bool fromFuture, bool forceAbsolute)
 {
     _root = root;
     _names = names;
     _asNames = asNames;
     _fromFuture = fromFuture;
     _forceAbsolute = forceAbsolute;
 }
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            if (args.Length == 1) {
                _generator.AddSend(node, unit, args[0]);
            }

            _generator.Yields.AddDependency(unit);

            return _generator.Yields.Types;
        }
Exemplo n.º 5
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            if (args.Length == 1) {
                foreach (var type in args[0]) {
                    _list.AppendItem(node, unit, type.GetEnumeratorTypes(node, unit));
                }
            }

            return ProjectState._noneInst.SelfSet;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Performs a call operation propagating the argument types into any user defined functions
        /// or classes and returns the set of types which result from the call.
        /// </summary>
        public static INamespaceSet Call(this INamespaceSet self, Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            var res = NamespaceSet.Empty;
            foreach (var ns in self) {
                var call = ns.Call(node, unit, args, keywordArgNames);
                Debug.Assert(call != null);

                res = res.Union(call);
            }

            return res;
        }
Exemplo n.º 7
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            var realArgs = args;
            if (_inst != null) {
                realArgs = Utils.Concat(_inst.SelfSet, args);
            }

            if (node is CallExpression) {
                return _call((CallExpression)node, unit, realArgs, keywordArgNames) ?? NamespaceSet.Empty;
            }
            return NamespaceSet.Empty;
        }
Exemplo n.º 8
0
        public FunctionDefinition(NameExpression name, Parameter[] parameters, Statement body, DecoratorStatement decorators = null)
        {
            if (name == null) {
                _name = new NameExpression("<lambda>");
                _isLambda = true;
            } else {
                _name = name;
            }

            _parameters = parameters;
            _body = body;
            _decorators = decorators;
        }
Exemplo n.º 9
0
 public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
 {
     if (args.Length >= 1) {
         var instance = NamespaceSet.Empty;
         foreach (var n in args[0]) {
             var bci = n as BuiltinClassInfo;
             var ci = n as ClassInfo;
             if (bci != null) {
                 instance = instance.Union(bci.Instance);
             } else if (ci != null) {
                 instance = instance.Union(ci.Instance);
             }
         }
         return instance;
     }
     return ProjectState._objectType.Instance;
 }
Exemplo n.º 10
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            var realArgs = args;
            if (_inst != null) {
                realArgs = Utils.Concat(_inst.SelfSet, args);
            }

            var analyzed = _original.Call(node, unit, args, keywordArgNames);
            var res = _call((CallExpression)node, unit, realArgs, keywordArgNames);
            if (res == null) {
                return analyzed;
            } else if (analyzed.Count == 0) {
                return res;
            } else {
                return res.Union(analyzed);
            }
        }
Exemplo n.º 11
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            if (args.Length == 1) {
                var res = unit.Scope.GetOrMakeNodeValue(
                    node,
                    (node_) => MakeFromIndexes(node_)
                ) as SequenceInfo;

                List<INamespaceSet> seqTypes = new List<INamespaceSet>();
                foreach (var type in args[0]) {
                    SequenceInfo seqInfo = type as SequenceInfo;
                    if (seqInfo != null) {
                        for (int i = 0; i < seqInfo.IndexTypes.Length; i++) {
                            if (seqTypes.Count == i) {
                                seqTypes.Add(seqInfo.IndexTypes[i].Types);
                            } else {
                                seqTypes[i] = seqTypes[i].Union(seqInfo.IndexTypes[i].Types);
                            }
                        }
                    } else {
                        var defaultIndexType = type.GetIndex(node, unit, ProjectState.GetConstant(0));
                        if (seqTypes.Count == 0) {
                            seqTypes.Add(defaultIndexType);
                        } else {
                            seqTypes[0] = seqTypes[0].Union(defaultIndexType);
                        }
                    }
                }

                res.AddTypes(unit, seqTypes.ToArray());

                return res;
            }

            return base.Call(node, unit, args, keywordArgNames);
        }
Exemplo n.º 12
0
 public RelativeModuleName(NameExpression[] names, int dotCount)
     : base(names)
 {
     _dotCount = dotCount;
 }
Exemplo n.º 13
0
 public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
 {
     var res = NamespaceSet.Empty;
     foreach (var member in _members) {
         res = res.Union(member.Call(node, unit, args, keywordArgNames));
     }
     return res;
 }
Exemplo n.º 14
0
            public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
            {
                _myDict._keysAndValues.AddDependency(unit);

                if (_list == null) {
                    var valuesVar = new VariableDef();
                    _myDict._keysAndValues.CopyValuesTo(valuesVar);
                    _list = new ListInfo(new[] { valuesVar }, unit.ProjectState._listType, node);
                }
                return _list;
            }
Exemplo n.º 15
0
            public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
            {
                _myDict._keysAndValues.AddDependency(unit);

                return _myDict._keysAndValues.AllValueTypes;
            }
Exemplo n.º 16
0
            public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
            {
                if (args.Length >= 1) {
                    foreach (var type in args[0]) {
                        DictionaryInfo otherDict = type as DictionaryInfo;
                        if (otherDict != null && !Object.ReferenceEquals(otherDict, _myDict)) {
                            _myDict._keysAndValues.CopyFrom(otherDict._keysAndValues);
                        }
                    }
                }
                // TODO: Process keyword args and add those values to our dictionary, plus a string key

                return NamespaceSet.Empty;
            }
Exemplo n.º 17
0
            public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
            {
                _myDict._keysAndValues.AddDependency(unit);

                if (_list == null) {
                    _list = new IteratorInfo(new[] { _myDict.KeyValueTupleVariable }, unit.ProjectState._dictItemsType, node);
                }
                return _list;
            }
Exemplo n.º 18
0
        private void UpdateFromImport(JAst curAst, FromImportStatement fromImport)
        {
            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);
            using (var edit = _buffer.CreateEdit()) {
                edit.Delete(span.Start.Index, span.Length + fromImport.GetTrailingWhitespace(curAst).Length);
                edit.Insert(span.Start.Index, newCode);
                edit.Apply();
            }
        }
Exemplo n.º 19
0
        private bool ProcessFutureStatements(int start, NameExpression/*!*/[] names, bool fromFuture)
        {
            if (!_fromFutureAllowed) {
                ReportSyntaxError(start, GetEnd(), "from __future__ imports must occur at the beginning of the file");
            }
            if (names.Length == 1 && names[0].Name == "*") {
                ReportSyntaxError(start, GetEnd(), "future statement does not support import *");
            }
            fromFuture = true;
            foreach (var name in names) {
                if (name.Name == "nested_scopes") {

                    // v2.4
                } else if (name.Name == "division") {
                    _languageFeatures |= FutureOptions.TrueDivision;
                } else if (name.Name == "generators") {

                    // v2.5:
                } else if (_langVersion >= JLanguageVersion.V71 && name.Name == "with_statement") {
                    _languageFeatures |= FutureOptions.WithStatement;
                    _tokenizer.WithStatement = true;
                } else if (_langVersion >= JLanguageVersion.V71 && name.Name == "absolute_import") {
                    _languageFeatures |= FutureOptions.AbsoluteImports;

                    // v2.6:
                } else if (_langVersion >= JLanguageVersion.V81 && name.Name == "print_function") {
                    _languageFeatures |= FutureOptions.PrintFunction;
                    _tokenizer.PrintFunction = true;
                } else if (_langVersion >= JLanguageVersion.V81 && name.Name == "unicode_literals") {
                    _tokenizer.UnicodeLiterals = true;
                    _languageFeatures |= FutureOptions.UnicodeLiterals;
                } else {
                    string strName = name.Name;

                    if (strName != "braces") {
                        ReportSyntaxError(start, GetEnd(), "future feature is not defined: " + strName);
                    } else {
                        // match CJ error message
                        ReportSyntaxError(start, GetEnd(), "not a chance");
                    }
                }
            }
            return fromFuture;
        }
Exemplo n.º 20
0
 /*!*/
 private NameExpression MakeName(Name name)
 {
     var res = new NameExpression(name.RealName);
     AddVerbatimName(name, res);
     return res;
 }
Exemplo n.º 21
0
        //arglist: (argument ',')* (argument [',']| '*' expression [',' '**' expression] | '**' expression)
        //argument: [expression '='] expression    # Really [keyword '='] expression
        private Arg[] FinishArgumentList(Arg first, List<string> commaWhiteSpace, out bool ateTerminator)
        {
            const TokenKind terminator = TokenKind.RightParenthesis;
            List<Arg> l = new List<Arg>();

            if (first != null) {
                l.Add(first);
            }

            // Parse remaining arguments
            while (true) {
                if (MaybeEat(terminator)) {
                    ateTerminator = true;
                    break;
                }
                int start;
                Arg a;
                if (MaybeEat(TokenKind.Multiply)) {
                    string starWhiteSpace = _tokenWhiteSpace;
                    start = GetStart();
                    Expression t = ParseExpression();
                    var name = new NameExpression("*");
                    a = new Arg(name, t);
                    if (_verbatim) {
                        AddPreceedingWhiteSpace(name, starWhiteSpace);
                    }
                } else if (MaybeEat(TokenKind.Power)) {
                    string starStarWhiteSpace = _tokenWhiteSpace;
                    start = GetStart();
                    Expression t = ParseExpression();
                    var name = new NameExpression("**");
                    a = new Arg(name, t);
                    if (_verbatim) {
                        AddPreceedingWhiteSpace(name, starStarWhiteSpace);
                    }
                } else {
                    Expression e = ParseExpression();
                    start = e.StartIndex;
                    if (MaybeEat(TokenKind.Assign)) {
                        a = FinishKeywordArgument(e);
                        CheckUniqueArgument(l, a);
                    } else {
                        a = new Arg(e);
                    }
                }
                a.SetLoc(start, GetEnd());
                l.Add(a);
                if (MaybeEat(TokenKind.Comma)) {
                    if (commaWhiteSpace != null) {
                        commaWhiteSpace.Add(_tokenWhiteSpace);
                    }
                } else {
                    ateTerminator = Eat(terminator);
                    break;
                }
            }

            return l.ToArray();
        }
Exemplo n.º 22
0
 public virtual IEnumerable<AnalysisValue> Call(ISet<AnalysisValue>[] args, NameExpression[] keywordArgNames)
 {
     return EmptySet<AnalysisValue>.Instance;
 }
Exemplo n.º 23
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            ISet<AnalysisValue>[] extArgs = new ISet<AnalysisValue>[args.Length];
            for (int i = 0; i < args.Length; i++) {
                HashSet<AnalysisValue> set;
                extArgs[i] = set = new HashSet<AnalysisValue>();
                foreach (var argValue in args[i]) {
                    set.Add(argValue.AsExternal());
                }
            }

            return ToNamespaceSet(_value.Call(extArgs, keywordArgNames));
        }
 public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
 {
     return _generator.GetEnumeratorTypes(node, unit);
 }
Exemplo n.º 25
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            var callArgs = ArgumentSet.FromArgs(FunctionDefinition, unit, args, keywordArgNames);

            if (_allCalls == null) {
                _allCalls = new Dictionary<CallChain, FunctionAnalysisUnit>();
            }

            FunctionAnalysisUnit calledUnit;
            bool updateArguments = true;

            if (callArgs.Count == 0 || (ProjectState.Limits.UnifyCallsToNew && Name == "__new__")) {
                calledUnit = (FunctionAnalysisUnit)AnalysisUnit;
            } else {
                var chain = new CallChain(node, unit, unit.ProjectState.Limits.CallDepth);
                if (!_allCalls.TryGetValue(chain, out calledUnit)) {
                    if (unit.ForEval) {
                        // Call expressions that weren't analyzed get the union result
                        // of all calls to this function.
                        var res = NamespaceSet.Empty;
                        foreach (var call in _allCalls.Values) {
                            res = res.Union(call.ReturnValue.TypesNoCopy);
                        }
                        return res;
                    } else {
                        _allCalls[chain] = calledUnit = new FunctionAnalysisUnit((FunctionAnalysisUnit)AnalysisUnit, chain, callArgs);
                        updateArguments = false;
                    }
                }
            }

            if (updateArguments && calledUnit.UpdateParameters(callArgs)) {
            #if DEBUG
                // Checks whether these arguments can be added ad nauseum.
                if (calledUnit.UpdateParameters(callArgs) && calledUnit.UpdateParameters(callArgs) && calledUnit.UpdateParameters(callArgs)) {
                    AnalysisLog.Add("BadArgs", calledUnit, callArgs);
                }
            #endif
                AnalysisLog.UpdateUnit(calledUnit);
            }

            calledUnit.ReturnValue.AddDependency(unit);
            return calledUnit.ReturnValue.Types;
        }
Exemplo n.º 26
0
            public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
            {
                _myDict._keysAndValues.AddDependency(unit);

                if (args.Length == 1) {
                    return _myDict._keysAndValues.GetValueType(args[0]);
                } else if (args.Length >= 2) {
                    return _myDict._keysAndValues.GetValueType(args[0]).Union(args[1]);
                }

                return NamespaceSet.Empty;
            }
Exemplo n.º 27
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(JAst 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;
        }
Exemplo n.º 28
0
 public AssignmentScopeNode(JAst ast, AssignmentStatement assign, NameExpression name)
 {
     _assign = assign;
     _name = name;
     _ast = ast;
 }
Exemplo n.º 29
0
 public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
 {
     return _original.Call(node, unit, args, keywordArgNames);
 }
Exemplo n.º 30
0
 public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
 {
     return _list.UnionType;
 }