Exemplo n.º 1
0
        // GlobalStatement
        public override bool Walk(GlobalStatement node)
        {
            node.Parent = _currentScope;

            foreach (string n in node.Names)
            {
                PythonVariable conflict;
                // Check current scope for conflicting variable
                bool assignedGlobal = false;
                if (_currentScope.TryGetVariable(n, out conflict))
                {
                    // conflict?
                    switch (conflict.Kind)
                    {
                    case VariableKind.Global:
                    case VariableKind.Local:
                        assignedGlobal = true;
                        ReportSyntaxWarning($"name '{n}' is assigned to before global declaration", node);
                        break;

                    case VariableKind.Parameter:
                        ReportSyntaxError($"name '{n}' is local and global", node);
                        break;
                    }
                }

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


                // Create the variable in the global context and mark it as global
                PythonVariable variable = _globalScope.EnsureGlobalVariable(n);
                variable.Kind = VariableKind.Global;

                if (conflict == null)
                {
                    // no previously definied variables, add it to the current scope
                    _currentScope.AddGlobalVariable(variable);
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        // GlobalStatement
        public override bool Walk(GlobalStatement node)
        {
            node.Parent = _currentScope;

            foreach (string n in node.Names)
            {
                PythonVariable conflict;
                // Check current scope for conflicting variable
                bool assignedGlobal = false;
                if (_currentScope.TryGetVariable(n, out conflict))
                {
                    // conflict?
                    switch (conflict.Kind)
                    {
                    case VariableKind.Global:
                        break;

                    case VariableKind.Local:
                        assignedGlobal = true;
                        ReportSyntaxError($"name '{n}' is assigned to before global declaration", node);
                        break;

                    case VariableKind.Parameter:
                        ReportSyntaxError($"name '{n}' is parameter and global", node);
                        break;

                    case VariableKind.Nonlocal:
                        ReportSyntaxError($"name '{n}' is nonlocal and global", node);
                        break;
                    }
                }

                // Check for the name being referenced previously
                if (_currentScope.IsReferenced(n) && !assignedGlobal)
                {
                    ReportSyntaxError($"name '{n}' is used prior to global declaration", node);
                }

                // Create the variable in the global context or mark it as global
                PythonVariable variable = _globalScope.EnsureGlobalVariable(n);

                if (conflict == null)
                {
                    // no previously defined variables, add it to the current scope
                    _currentScope.AddGlobalVariable(variable);
                }
            }
            return(true);
        }
Exemplo n.º 3
0
        // GlobalStatement
        public override bool Walk(GlobalStatement node)
        {
            node.Parent = _currentScope;

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

                        case VariableKind.Parameter:
                            ReportSyntaxError(
                                String.Format(
                                    System.Globalization.CultureInfo.InvariantCulture,
                                    "Name '{0}' is a function parameter and declared global",
                                    n),
                                node);
                            break;
                    }
                }

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

                // Create the variable in the global context and mark it as global
                PythonVariable variable = _globalScope.EnsureGlobalVariable(n);
                variable.Kind = VariableKind.Global;

                if (conflict == null) {
                    // no previously definied variables, add it to the current scope
                    _currentScope.AddGlobalVariable(variable);
                }
            }
            return true;
        }
Exemplo n.º 4
0
 public override void PostWalk(GlobalStatement node)
 {
     Content("");
 }
Exemplo n.º 5
0
        public override bool Walk(GlobalStatement node)
        {
            foreach (var name in node.Names)
            {
                if (IsDefined(name))
                {
                    sink.Add(src, String.Format("Variable \"{0}\" is already declared before or some value already is assigned to it.", name), node.Span, PARAM_AS_GLOBBAL_VARAIBLE, Severity.Warning);
                }

                sink.Add(src, String.Format("Variable \"{0}\" will point to GLOBAL variable. Incorrect usage of global variables my lead to hardly detectable bugs.", name), node.Span, GLOBBAL_VARAIBLE, Severity.Warning);
                Define(name);
            }

            return false;
        }
Exemplo n.º 6
0
 //global_stmt: 'global' NAME (',' NAME)*
 private GlobalStatement ParseGlobalStmt() {
     Eat(TokenKind.KeywordGlobal);
     SourceLocation start = GetStart();
     List<SymbolId> l = new List<SymbolId>();
     l.Add(ReadName());
     while (MaybeEat(TokenKind.Comma)) {
         l.Add(ReadName());
     }
     SymbolId[] names = l.ToArray();
     GlobalStatement ret = new GlobalStatement(names);
     ret.SetLoc(start, GetEnd());
     return ret;
 }
Exemplo n.º 7
0
 // GlobalStatement
 public override bool Walk(GlobalStatement node)
 {
     foreach (SymbolId n in node.Names) {
         Global(n);
     }
     return true;
 }
 // GlobalStatement
 public bool Walk(GlobalStatement node)
 {
     return Process(node);
 }
Exemplo n.º 9
0
 public void Visit(PyAst.GlobalStatement node) => throw CreateNotImplementedEx();
 public override bool Walk(GlobalStatement node)
 {
     Emit(node); return false;
 }
Exemplo n.º 11
0
 public virtual void PostWalk(GlobalStatement node)
 {
 }
Exemplo n.º 12
0
 // GlobalStatement
 public virtual bool Walk(GlobalStatement node)
 {
     return true;
 }
Exemplo n.º 13
0
 // GlobalStmt
 public override bool Walk(GlobalStatement node)
 {
     foreach (SymbolId n in node.Names) {
         if (current != null) {
             Binding binding;
             if (current.Bindings.TryGetValue(n, out binding)) {
                 if (binding.IsParameter) {
                     ReportSyntaxError(String.Format("name '{0}' is a function parameter and declared global", n.GetString()), node);
                 } else if (binding.IsAssigned) {
                     ReportSyntaxWarning(String.Format("Variable {0} assigned before global declaration", n.GetString()), node);
                 } else if (binding.IsFree) {
                     ReportSyntaxWarning(String.Format("Variable {0} used before global declaration", n.GetString()), node);
                 }
             }
             current.BindGlobal(n);
         }
     }
     return true;
 }
Exemplo n.º 14
0
 internal Global(GlobalStatement stmt)
     : this() {
     _names = new PythonList(stmt.Names);
 }
 public void PostWalk(GlobalStatement node)
 {
     PostProcess(node);
 }
Exemplo n.º 16
0
 //global_stmt: 'global' NAME (',' NAME)*
 private GlobalStatement ParseGlobalStmt() {
     Eat(TokenKind.KeywordGlobal);
     var start = GetStart();
     List<string> l = new List<string>();
     l.Add(ReadName());
     while (MaybeEat(TokenKind.Comma)) {
         l.Add(ReadName());
     }
     string[] names = l.ToArray();
     GlobalStatement ret = new GlobalStatement(names);
     ret.SetLoc(_globalParent, start, GetEnd());
     return ret;
 }
Exemplo n.º 17
0
		public override bool Walk(GlobalStatement node)
		{
			writer.WriteLine("Global");
			return base.Walk(node);
		}