Ejemplo n.º 1
0
        ///////////////////////////////////////////////////////////////////////

        public StringList GetImportNames(
            string pattern,
            bool keys,
            bool tailOnly
            )
        {
            CheckDisposed();

            StringList list = new StringList();

            if (imports == null)
            {
                return(list);
            }

            if (pattern != null)
            {
                pattern = ScriptOps.MakeCommandName(pattern);
            }

            foreach (KeyValuePair <string, object> pair in imports)
            {
                IAlias alias = pair.Value as IAlias;

                if (alias == null)
                {
                    continue;
                }

                if (keys)
                {
                    string importName = pair.Key;

                    string importNameTailOnly = NamespaceOps.TailOnly(
                        importName);

                    if ((pattern == null) || StringOps.Match(
                            interpreter, MatchMode.Glob, importNameTailOnly,
                            pattern, false))
                    {
                        list.Add(tailOnly ? importNameTailOnly : importName);
                    }
                }
                else
                {
                    string aliasName = NamespaceOps.GetAliasName(alias);

                    string aliasNameTailOnly = NamespaceOps.TailOnly(
                        aliasName);

                    if ((pattern == null) || StringOps.Match(
                            interpreter, MatchMode.Glob, aliasNameTailOnly,
                            pattern, false))
                    {
                        list.Add(tailOnly ? aliasNameTailOnly : aliasName);
                    }
                }
            }

            return(list);
        }
Ejemplo n.º 2
0
        ///////////////////////////////////////////////////////////////////////

        public ReturnCode RenameImport(
            string oldQualifiedName,
            string newQualifiedName,
            bool strict,
            ref Result error
            )
        {
            CheckDisposed();

            if (imports == null)
            {
                error = String.Format(
                    "imports not available in namespace \"{0}\"",
                    GetDisplayName());

                return(ReturnCode.Error);
            }

            INamespace oldNamespace = NamespaceOps.LookupParent(
                interpreter, oldQualifiedName, false, true, false, ref error);

            if (oldNamespace == null)
            {
                return(ReturnCode.Error);
            }

            INamespace newNamespace = NamespaceOps.LookupParent(
                interpreter, newQualifiedName, false, true, false, ref error);

            if (newNamespace == null)
            {
                return(ReturnCode.Error);
            }

            int count = 0;

            ObjectDictionary localImports = new ObjectDictionary(
                (IDictionary <string, object>)imports);

            foreach (KeyValuePair <string, object> pair in localImports)
            {
                IAlias alias = pair.Value as IAlias;

                if (alias == null)
                {
                    continue;
                }

                string aliasName = NamespaceOps.GetAliasName(alias);

                if (NamespaceOps.IsSame(
                        alias.TargetNamespace, oldNamespace) &&
                    StringOps.Match(interpreter, MatchMode.Glob, aliasName,
                                    ScriptOps.MakeCommandName(oldQualifiedName), false))
                {
                    alias.TargetNamespace = newNamespace;
                    NamespaceOps.SetAliasName(alias, newQualifiedName);

                    return(ReturnCode.Ok);
                }
                else if (strict)
                {
                    error = String.Format(
                        "import \"{0}\" is not an alias in namespace \"{1}\"",
                        oldQualifiedName, GetDisplayName());

                    return(ReturnCode.Error);
                }
            }

            if (strict && (count == 0))
            {
                error = String.Format(
                    "no imports matched name \"{0}\" in namespace \"{1}\"",
                    oldQualifiedName, GetDisplayName());

                return(ReturnCode.Error);
            }

            return(ReturnCode.Ok);
        }
Ejemplo n.º 3
0
        ///////////////////////////////////////////////////////////////////////

        public ReturnCode RemoveImports(
            string qualifiedPattern,
            bool strict,
            ref Result error
            )
        {
            CheckDisposed();

            if (imports == null)
            {
                error = String.Format(
                    "imports not available in namespace \"{0}\"",
                    GetDisplayName());

                return(ReturnCode.Error);
            }

            INamespace patternNamespace = null;

            if ((qualifiedPattern != null) && (interpreter != null))
            {
                patternNamespace = NamespaceOps.LookupParent(
                    interpreter, qualifiedPattern, false, true, false,
                    ref error);

                if (patternNamespace == null)
                {
                    return(ReturnCode.Error);
                }
            }

            int count = 0;

            ObjectDictionary localImports = new ObjectDictionary(
                (IDictionary <string, object>)imports);

            foreach (KeyValuePair <string, object> pair in localImports)
            {
                IAlias alias      = pair.Value as IAlias;
                string aliasName  = NamespaceOps.GetAliasName(alias);
                string originName = GetOriginName(aliasName);

                if ((qualifiedPattern == null) ||
                    StringOps.Match(interpreter, MatchMode.Glob, aliasName,
                                    ScriptOps.MakeCommandName(qualifiedPattern), false) ||
                    ((originName != null) &&
                     StringOps.Match(interpreter, MatchMode.Glob, originName,
                                     ScriptOps.MakeCommandName(qualifiedPattern), false)))
                {
                    INamespace originNamespace = null;

                    if (originName != null)
                    {
                        originNamespace = NamespaceOps.LookupParent(
                            interpreter, originName, false, true, false,
                            ref error);

                        if (originNamespace == null)
                        {
                            return(ReturnCode.Error);
                        }
                    }

                    if ((patternNamespace == null) || ((alias != null) &&
                                                       (NamespaceOps.IsSame(
                                                            alias.TargetNamespace, patternNamespace) ||
                                                        NamespaceOps.IsSame(
                                                            originNamespace, patternNamespace))))
                    {
                        if ((alias != null) && (interpreter != null))
                        {
                            string nameToken = alias.NameToken;

                            if ((nameToken != null) &&
                                interpreter.RemoveAliasAndCommand(
                                    alias.NameToken, null, false,
                                    ref error) != ReturnCode.Ok)
                            {
                                return(ReturnCode.Error);
                            }
                        }

                        count += imports.Remove(pair.Key) ? 1 : 0;
                    }
                }
            }

            if (strict && (count == 0))
            {
                error = String.Format(
                    "no imports matched pattern \"{0}\" in namespace \"{1}\"",
                    qualifiedPattern, GetDisplayName());

                return(ReturnCode.Error);
            }

            return(ReturnCode.Ok);
        }
Ejemplo n.º 4
0
        ///////////////////////////////////////////////////////////////////////

        public ReturnCode GetImport(
            string qualifiedImportName,
            ref string qualifiedExportName,
            ref Result error
            )
        {
            CheckDisposed();

            if (qualifiedImportName == null)
            {
                error = "invalid import name";
                return(ReturnCode.Error);
            }

            if (!NamespaceOps.IsQualifiedName(qualifiedImportName))
            {
                error = "import name must be qualified";
                return(ReturnCode.Error);
            }

            if (interpreter == null)
            {
                error = "invalid interpreter";
                return(ReturnCode.Error);
            }

            if (imports == null)
            {
                error = String.Format(
                    "imports not available in namespace \"{0}\"",
                    GetDisplayName());

                return(ReturnCode.Error);
            }

            object @object;

            if (imports.TryGetValue(qualifiedImportName, out @object))
            {
                IAlias alias = @object as IAlias;

                if (alias != null)
                {
                    qualifiedExportName = NamespaceOps.GetAliasName(alias);
                    return(ReturnCode.Ok);
                }
                else
                {
                    error = String.Format(
                        "import name \"{0}\" is not an alias",
                        qualifiedImportName);
                }
            }
            else
            {
                error = String.Format(
                    "import name \"{0}\" not found",
                    qualifiedImportName);
            }

            return(ReturnCode.Error);
        }