Esempio n. 1
0
 private void Verify(PythonNameBinder binder)
 {
     if (ContainsImportStar && IsClosure)
     {
         binder.ReportSyntaxError(
             String.Format(
                 System.Globalization.CultureInfo.InvariantCulture,
                 "import * is not allowed in function '{0}' because it is a nested function",
                 Name),
             this);
     }
     if (ContainsImportStar && Parent is FunctionDefinition)
     {
         binder.ReportSyntaxError(
             String.Format(
                 System.Globalization.CultureInfo.InvariantCulture,
                 "import * is not allowed in function '{0}' because it is a nested function",
                 Name),
             this);
     }
     if (ContainsImportStar && ContainsNestedFreeVariables)
     {
         binder.ReportSyntaxError(
             String.Format(
                 System.Globalization.CultureInfo.InvariantCulture,
                 "import * is not allowed in function '{0}' because it contains a nested function with free variables",
                 Name),
             this);
     }
     if (ContainsUnqualifiedExec && ContainsNestedFreeVariables)
     {
         binder.ReportSyntaxError(
             String.Format(
                 System.Globalization.CultureInfo.InvariantCulture,
                 "unqualified exec is not allowed in function '{0}' because it contains a nested function with free variables",
                 Name),
             this);
     }
     if (ContainsUnqualifiedExec && IsClosure)
     {
         binder.ReportSyntaxError(
             String.Format(
                 System.Globalization.CultureInfo.InvariantCulture,
                 "unqualified exec is not allowed in function '{0}' because it is a nested function",
                 Name),
             this);
     }
 }
Esempio n. 2
0
 public DeleteBinder(PythonNameBinder binder)
 {
     _binder = binder;
 }
Esempio n. 3
0
        internal static void BindAst(PythonAst ast, ErrorSink context)
        {
            PythonNameBinder binder = new PythonNameBinder(ast, context);

            binder.Bind(ast);
        }
Esempio n. 4
0
 internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference)
 {
     return(EnsureVariable(reference.Name));
 }
Esempio n. 5
0
 internal override void Bind(PythonNameBinder binder)
 {
     base.Bind(binder);
     Verify(binder);
 }
Esempio n. 6
0
 internal override void Bind(PythonNameBinder binder) {
     base.Bind(binder);
     Verify(binder);
 }
Esempio n. 7
0
 internal override PythonVariable BindReference(PythonNameBinder binder, string name)
 {
     return(EnsureVariable(name));
 }
Esempio n. 8
0
 public ParameterBinder(PythonNameBinder binder)
 {
     _binder = binder;
 }
Esempio n. 9
0
        internal virtual void FinishBind(PythonNameBinder binder)
        {
            List <ClosureInfo> closureVariables = null;

            if (_nonLocalVars != null)
            {
                foreach (var variableName in _nonLocalVars)
                {
                    bool bound = false;
                    for (ScopeStatement parent = Parent; parent != null; parent = parent.Parent)
                    {
                        PythonVariable variable;

                        if (parent.TryBindOuter(this, variableName.Name, false, out variable))
                        {
                            bound = !variable.IsGlobal;
                            break;
                        }
                    }

                    if (!bound)
                    {
                        binder.ReportSyntaxError(String.Format("no binding for nonlocal '{0}' found", variableName.Name), variableName);
                    }
                }
            }

            if (FreeVariables != null && FreeVariables.Count > 0)
            {
                closureVariables = new List <ClosureInfo>();

                foreach (var variable in FreeVariables)
                {
                    closureVariables.Add(new ClosureInfo(variable, !(this is ClassDefinition)));
                }
            }

            if (Variables != null && Variables.Count > 0)
            {
                if (closureVariables == null)
                {
                    closureVariables = new List <ClosureInfo>();
                }

                foreach (PythonVariable variable in Variables.Values)
                {
                    if (!HasClosureVariable(closureVariables, variable) &&
                        !variable.IsGlobal && (variable.AccessedInNestedScope || ExposesLocalVariable(variable)))
                    {
                        closureVariables.Add(new ClosureInfo(variable, true));
                    }

                    if (variable.Kind == VariableKind.Local)
                    {
                        Debug.Assert(variable.Scope == this);
                    }
                }
            }

            if (closureVariables != null)
            {
                _closureVariables = closureVariables.ToArray();
            }

            // no longer needed
            _references = null;
        }
Esempio n. 10
0
 internal abstract PythonVariable BindReference(PythonNameBinder binder, string name);
Esempio n. 11
0
 internal override void FinishBind(PythonNameBinder binder)
 {
 }
Esempio n. 12
0
 internal override PythonVariable BindReference(PythonNameBinder binder, string name)
 {
     return EnsureVariable(name);
 }
Esempio n. 13
0
        internal virtual void FinishBind(PythonNameBinder binder) {
            List<ClosureInfo> closureVariables = null;

            if (_nonLocalVars != null) {
                foreach (var variableName in _nonLocalVars) {
                    bool bound = false;
                    for (ScopeStatement parent = Parent; parent != null; parent = parent.Parent) {
                        PythonVariable variable;

                        if (parent.TryBindOuter(this, variableName.Name, false, out variable)) {
                            bound = !variable.IsGlobal;
                            break;
                        }
                    }

                    if (!bound) {
                        binder.ReportSyntaxError(String.Format("no binding for nonlocal '{0}' found", variableName.Name), variableName);
                    }
                }
            }

            if (FreeVariables != null && FreeVariables.Count > 0) {
                closureVariables = new List<ClosureInfo>();

                foreach (var variable in FreeVariables) {
                    closureVariables.Add(new ClosureInfo(variable, !(this is ClassDefinition)));
                }
            }

            if (Variables != null && Variables.Count > 0) {
                if (closureVariables == null) {
                    closureVariables = new List<ClosureInfo>();
                }

                foreach (PythonVariable variable in Variables.Values) {
                    if (!HasClosureVariable(closureVariables, variable) &&
                        !variable.IsGlobal && (variable.AccessedInNestedScope || ExposesLocalVariable(variable))) {
                        closureVariables.Add(new ClosureInfo(variable, true));
                    }

                    if (variable.Kind == VariableKind.Local) {
                        Debug.Assert(variable.Scope == this);
                    }
                }
            }

            if (closureVariables != null) {
                _closureVariables = closureVariables.ToArray();
            }

            // no longer needed
            _references = null;
        }
Esempio n. 14
0
        internal virtual void Bind(PythonNameBinder binder) {
            if (_references != null) {
                foreach (var refList in _references.Values) {
                    foreach (var reference in refList) {
                        PythonVariable variable;
                        reference.Variable = variable = BindReference(binder, reference.Name);

                        // Accessing outer scope variable which is being deleted?
                        if (variable != null) {
                            if (variable.Deleted && variable.Scope != this && !variable.Scope.IsGlobal && binder.LanguageVersion < PythonLanguageVersion.V32) {

                                // report syntax error
                                binder.ReportSyntaxError(
                                    String.Format(
                                        System.Globalization.CultureInfo.InvariantCulture,
                                        "can not delete variable '{0}' referenced in nested scope",
                                        reference.Name
                                        ),
                                    this);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 15
0
 internal abstract PythonVariable BindReference(PythonNameBinder binder, string name);
Esempio n. 16
0
        internal static void BindAst(PythonLanguageVersion langVersion, PythonAst ast, ErrorSink context, bool bindReferences)
        {
            PythonNameBinder binder = new PythonNameBinder(langVersion, ast, context, bindReferences);

            binder.Bind(ast);
        }
Esempio n. 17
0
 public DefineBinder(PythonNameBinder binder)
 {
     _binder = binder;
 }
Esempio n. 18
0
 public DeleteBinder(PythonNameBinder binder) {
     _binder = binder;
 }
Esempio n. 19
0
 internal override void FinishBind(PythonNameBinder binder)
 {
 }
Esempio n. 20
0
 internal static void BindAst(PythonLanguageVersion langVersion, PythonAst ast, ErrorSink context, bool bindReferences) {
     PythonNameBinder binder = new PythonNameBinder(langVersion, ast, context, bindReferences);
     binder.Bind(ast);
 }
Esempio n. 21
0
        internal override PythonVariable BindReference(PythonNameBinder binder, string name) {
            PythonVariable variable;

            // First try variables local to this scope
            if (TryGetVariable(name, out variable) && variable.Kind != VariableKind.Nonlocal) {
                if (variable.Kind == VariableKind.Global) {
                    AddReferencedGlobal(name);
                }
                return variable;
            }

            // Try to bind in outer scopes
            for (ScopeStatement parent = Parent; parent != null; parent = parent.Parent) {
                if (parent.TryBindOuter(this, name, true, out variable)) {
                    return variable;
                }
            }

            return null;
        }
Esempio n. 22
0
 public DefineBinder(PythonNameBinder binder) {
     _binder = binder;
 }
Esempio n. 23
0
 private void Verify(PythonNameBinder binder) {
     if (ContainsImportStar && IsClosure) {
         binder.ReportSyntaxError(
             String.Format(
                 System.Globalization.CultureInfo.InvariantCulture,
                 "import * is not allowed in function '{0}' because it is a nested function",
                 Name),
             this);
     }
     if (ContainsImportStar && Parent is FunctionDefinition) {
         binder.ReportSyntaxError(
             String.Format(
                 System.Globalization.CultureInfo.InvariantCulture,
                 "import * is not allowed in function '{0}' because it is a nested function",
                 Name),
             this);
     }
     if (ContainsImportStar && ContainsNestedFreeVariables) {
         binder.ReportSyntaxError(
             String.Format(
                 System.Globalization.CultureInfo.InvariantCulture,
                 "import * is not allowed in function '{0}' because it contains a nested function with free variables",
                 Name),
             this);
     }
     if (ContainsUnqualifiedExec && ContainsNestedFreeVariables) {
         binder.ReportSyntaxError(
             String.Format(
                 System.Globalization.CultureInfo.InvariantCulture,
                 "unqualified exec is not allowed in function '{0}' because it contains a nested function with free variables",
                 Name),
             this);
     }
     if (ContainsUnqualifiedExec && IsClosure) {
         binder.ReportSyntaxError(
             String.Format(
                 System.Globalization.CultureInfo.InvariantCulture,
                 "unqualified exec is not allowed in function '{0}' because it is a nested function",
                 Name),
             this);
     }
 }
Esempio n. 24
0
 public ParameterBinder(PythonNameBinder binder) {
     _binder = binder;
 }
Esempio n. 25
0
        internal override PythonVariable BindReference(PythonNameBinder binder, string name) {
            PythonVariable variable;

            // Python semantics: The variables bound local in the class
            // scope are accessed by name - the dictionary behavior of classes
            if (TryGetVariable(name, out variable)) {
                // TODO: This results in doing a dictionary lookup to get/set the local,
                // when it should probably be an uninitialized check / global lookup for gets
                // and a direct set
                if (variable.Kind == VariableKind.Global) {
                    AddReferencedGlobal(name);
                } else if (variable.Kind == VariableKind.Local) {
                    return null;
                }

                return variable;
            }

            // Try to bind in outer scopes, if we have an unqualified exec we need to leave the
            // variables as free for the same reason that locals are accessed by name.
            for (ScopeStatement parent = Parent; parent != null; parent = parent.Parent) {
                if (parent.TryBindOuter(this, name, true, out variable)) {
                    return variable;
                }
            }

            return null;
        }
Esempio n. 26
0
 internal abstract PythonVariable BindReference(PythonNameBinder binder, PythonReference reference);