Ejemplo n.º 1
0
        private NonlocalStatement ParseNonlocalStmt() {
            if (_functions != null && _functions.Count == 0 && _classDepth == 0) {
                ReportSyntaxError("nonlocal declaration not allowed at module level");
            }

            Eat(TokenKind.KeywordNonlocal);
            string localWhiteSpace = _tokenWhiteSpace;
            var start = GetStart();
            List<string> commaWhiteSpace;
            List<string> namesWhiteSpace;
            
            var l = ReadNameList(out commaWhiteSpace, out namesWhiteSpace);
            var names = l.ToArray();
            NonlocalStatement ret = new NonlocalStatement(names);
            ret.SetLoc(start, GetEnd());
            if (_verbatim) {
                AddPreceedingWhiteSpace(ret, localWhiteSpace);
                AddListWhiteSpace(ret, commaWhiteSpace.ToArray());
                AddNamesWhiteSpace(ret, namesWhiteSpace.ToArray());
            }
            return ret;
        }
Ejemplo n.º 2
0
 public override bool Walk(NonlocalStatement node) {
     CanComplete = IsActualExpression(node.Names);
     CommitByDefault = true;
     return base.Walk(node);
 }
Ejemplo n.º 3
0
        public override bool Walk(NonlocalStatement node) {
            foreach (NameExpression nameNode in node.Names) {
                string n = nameNode.Name;
                if (n == null) {
                    continue;
                }

                PythonVariable conflict;
                // Check current scope for conflicting variable
                bool assignedLocal = false;
                if (_currentScope.TryGetVariable(n, out conflict)) {
                    // conflict?
                    switch (conflict.Kind) {
                        case VariableKind.Global:
                            ReportSyntaxError(String.Format("name '{0}' is nonlocal and global", n), node);
                            break;
                        case VariableKind.Local:
                            assignedLocal = true;
                            ReportSyntaxWarning(
                                String.Format(
                                    System.Globalization.CultureInfo.InvariantCulture,
                                    "name '{0}' is assigned to before nonlocal declaration",
                                    n
                                ),
                                node
                            );
                            break;
                        case VariableKind.Parameter:
                            ReportSyntaxError(
                                String.Format(
                                    System.Globalization.CultureInfo.InvariantCulture,
                                    "name '{0}' is a parameter and nonlocal",
                                    n),
                                node);
                            break;
                    }
                }

                // Check for the name being referenced previously. If it has been, issue warning.
                if (_currentScope.IsReferenced(n) && !assignedLocal) {
                    ReportSyntaxWarning(
                        String.Format(
                        System.Globalization.CultureInfo.InvariantCulture,
                        "name '{0}' is used prior to nonlocal declaration",
                        n),
                    node);
                }


                if (conflict == null) {
                    // no previously definied variables, add it to the current scope
                    _currentScope.CreateVariable(n, VariableKind.Nonlocal);
                }
                _currentScope.AddNonLocalVariable(nameNode);
                nameNode.AddVariableReference(_globalScope, _bindRefs, Reference(n));
            }
            return true;
        }
Ejemplo n.º 4
0
        public override bool Walk(NonlocalStatement node) {
            UpdateLineInfo(node);

            return base.Walk(node);
        }
Ejemplo n.º 5
0
 public override bool Walk(NonlocalStatement node) {
     foreach (var name in node.Names) {
         if (name.Name != null) {
             _scope.AddVariable(name.Name, CreateVariableInDeclaredScope(name));
         }
     }
     return false;
 }
Ejemplo n.º 6
0
 public override void PostWalk(NonlocalStatement node) { PostWalkWorker(node); }
Ejemplo n.º 7
0
 // NonlocalStatement
 public override bool Walk(NonlocalStatement node) { return ShouldWalkWorker(node); }