Exemple #1
0
        /// <summary>
        /// Translates <see cref="QualifiedName"/> according to given naming.
        /// </summary>
        public static QualifiedName TranslateAlias(QualifiedName qname, AliasKind kind, Dictionary <Alias, QualifiedName> aliases, QualifiedName?currentNamespace)
        {
            QualifiedName translated;

            TryTranslateAlias(qname, kind, aliases, currentNamespace, out translated);
            return(translated);
        }
Exemple #2
0
        private SimpleUse AddAlias(Tuple <Span, QualifiedNameRef, NameRef> alias, AliasKind contextType)
        {
            var aliasName = alias.Item3.HasValue
                ? alias.Item3
                : new NameRef(Span.Invalid, alias.Item2.QualifiedName.Name);

            bool added = false;

            switch (contextType)
            {
            case AliasKind.Type:
                added = namingContext.AddAlias(aliasName, alias.Item2);
                break;

            case AliasKind.Function:
                added = namingContext.AddFunctionAlias(aliasName, alias.Item2);
                break;

            case AliasKind.Constant:
                added = namingContext.AddConstantAlias(aliasName, alias.Item2);
                break;
            }
            if (!added)
            {
                this.ErrorSink.Error(aliasName.Span.IsValid ? aliasName.Span : alias.Item2.Span, FatalErrors.AliasAlreadyInUse,
                                     alias.Item2.QualifiedName.ToString(), aliasName.Name.ToString());
            }
            return(new SimpleUse(alias.Item1, alias.Item3.Span, alias.Item2.Span, new Alias(aliasName, contextType), alias.Item2));
        }
Exemple #3
0
        public UseStatement(Span span, List <UseBase> uses, AliasKind kind)
            : base(span)
        {
            Debug.Assert(uses != null);

            _kind = kind;
            _uses = uses.ToArray();
        }
Exemple #4
0
        internal static bool TryParseVariableName(string name, bool caseSensitive, out AliasKind kind, out string id, out int index)
        {
            if (!name.StartsWith("$", StringComparison.Ordinal))
            {
                kind  = AliasKind.DeclaredLocal;
                id    = name;
                index = -1;
                return(true);
            }

            var comparison = caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

            if (string.Equals(name, "$exception", comparison))
            {
                kind  = AliasKind.Exception;
                id    = name;
                index = 0;
                return(true);
            }
            else if (string.Equals(name, "$stowedexception", comparison))
            {
                kind  = AliasKind.StowedException;
                id    = name;
                index = 0;
                return(true);
            }
            // Allow lowercase version of $ReturnValue, even with case-sensitive match.
            else if (name.StartsWith("$ReturnValue", comparison) ||
                     (caseSensitive && name.StartsWith("$returnvalue", comparison)))
            {
                if (TryParseReturnValueIndex(name, out index))
                {
                    Debug.Assert(index >= 0);
                    kind = AliasKind.ReturnValue;
                    id   = name;
                    return(true);
                }
            }
            else
            {
                // Check for object id: "[$][1-9][0-9]*"
                var suffix = name.Substring(1);
                // Leading zeros are not supported.
                if (!suffix.StartsWith("0", comparison) && int.TryParse(suffix, NumberStyles.None, CultureInfo.InvariantCulture, out index))
                {
                    Debug.Assert(index >= 0);
                    kind = AliasKind.ObjectId;
                    id   = suffix;
                    return(true);
                }
            }

            kind  = AliasKind.None;
            id    = null;
            index = -1;
            return(false);
        }
        internal static bool TryParseVariableName(string name, bool caseSensitive, out AliasKind kind, out string id, out int index)
        {
            if (!name.StartsWith("$", StringComparison.Ordinal))
            {
                kind = AliasKind.DeclaredLocal;
                id = name;
                index = -1;
                return true;
            }

            var comparison = caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
            if (string.Equals(name, "$exception", comparison))
            {
                kind = AliasKind.Exception;
                id = name;
                index = 0;
                return true;
            }
            else if (string.Equals(name, "$stowedexception", comparison))
            {
                kind = AliasKind.StowedException;
                id = name;
                index = 0;
                return true;
            }
            // Allow lowercase version of $ReturnValue, even with case-sensitive match.
            else if (name.StartsWith("$ReturnValue", comparison) ||
                (caseSensitive && name.StartsWith("$returnvalue", comparison)))
            {
                if (TryParseReturnValueIndex(name, out index))
                {
                    Debug.Assert(index >= 0);
                    kind = AliasKind.ReturnValue;
                    id = name;
                    return true;
                }
            }
            else
            {
                // Check for object id: "[$][1-9][0-9]*"
                var suffix = name.Substring(1);
                // Leading zeros are not supported.
                if (!suffix.StartsWith("0", comparison) && int.TryParse(suffix, NumberStyles.None, CultureInfo.InvariantCulture, out index))
                {
                    Debug.Assert(index >= 0);
                    kind = AliasKind.ObjectId;
                    id = suffix;
                    return true;
                }
            }

            kind = AliasKind.None;
            id = null;
            index = -1;
            return false;
        }
        internal Alias(AliasKind kind, string name, string fullName, string type, CustomTypeInfo customTypeInfo)
        {
            Debug.Assert(kind != AliasKind.None);
            Debug.Assert(!string.IsNullOrEmpty(fullName));
            Debug.Assert(!string.IsNullOrEmpty(type));

            this.Kind           = kind;
            this.Name           = name;
            this.FullName       = fullName;
            this.Type           = type;
            this.CustomTypeInfo = customTypeInfo;
        }
Exemple #7
0
        internal Alias(AliasKind kind, string name, string fullName, string type, ReadOnlyCollection <byte> customTypeInfo = null, Guid customTypeInfoId = default(Guid))
        {
            Debug.Assert(kind != AliasKind.None);
            Debug.Assert(!string.IsNullOrEmpty(fullName));
            Debug.Assert(!string.IsNullOrEmpty(type));
            Debug.Assert((customTypeInfo == null) == (customTypeInfoId == default(Guid)));

            this.Kind             = kind;
            this.Name             = name;
            this.FullName         = fullName;
            this.Type             = type;
            this.CustomTypeInfo   = customTypeInfo;
            this.CustomTypeInfoId = customTypeInfoId;
        }
Exemple #8
0
        private bool AddAlias(Name name, AliasKind kind, QualifiedName qname)
        {
            Debug.Assert(!string.IsNullOrEmpty(name.Value));

            var dict  = EnsureAliases();
            var count = dict.Count;
            var alias = new Alias(name, kind);

            //
            dict[alias] = qname;

            //
            return(count != dict.Count);  // item was added
        }
        internal Alias(AliasKind kind, string name, string fullName, string type, ReadOnlyCollection<byte> customTypeInfo = null, Guid customTypeInfoId = default(Guid))
        {
            Debug.Assert(kind != AliasKind.None);
            Debug.Assert(!string.IsNullOrEmpty(fullName));
            Debug.Assert(!string.IsNullOrEmpty(type));
            Debug.Assert((customTypeInfo == null) == (customTypeInfoId == default(Guid)));

            this.Kind = kind;
            this.Name = name;
            this.FullName = fullName;
            this.Type = type;
            this.CustomTypeInfo = customTypeInfo;
            this.CustomTypeInfoId = customTypeInfoId;
        }
Exemple #10
0
        /// <summary>
        /// Gets qualified name matching given alias.
        /// </summary>
        public bool TryGetAlias(Name name, AliasKind kind, out QualifiedName qname)
        {
            var dict = _aliases;

            if (dict != null)
            {
                return(dict.TryGetValue(new Alias(name, kind), out qname));
            }
            else
            {
                qname = default(QualifiedName);
                return(false);
            }
        }
Exemple #11
0
        private static async Task <IEnumerable <string> > GetSourceAsync(AliasKind kind)
        {
            switch (kind)
            {
            case AliasKind.City:
                return(JsonConvert.DeserializeObject <List <string> >(await ProceduralSources.ProceduralSourceFiles.AliasCitiesJsonAsync()));

            case AliasKind.Island:
                return(JsonConvert.DeserializeObject <List <string> >(await ProceduralSources.ProceduralSourceFiles.AliasIslandsJsonAsync()));

            case AliasKind.Planet:
                return(JsonConvert.DeserializeObject <List <string> >(await ProceduralSources.ProceduralSourceFiles.AliasPlanetsJsonAsync()));

            default: return(new List <string>());
            }
        }
Exemple #12
0
        private async Task <MarkovChain <char> > GetChainAsync(AliasKind kind)
        {
            if (this.chainCache.ContainsKey(kind))
            {
                return(this.chainCache[kind]);
            }

            var chain = new MarkovChain <char>(KindOrder[kind]);

            foreach (var item in await GetSourceAsync(kind))
            {
                chain.Add(item);
            }

            if (!this.chainCache.ContainsKey(kind))
            {
                this.chainCache.Add(kind, chain);
            }

            return(chain);
        }
Exemple #13
0
        internal static string GetTypeName(InspectionContext context, AliasKind kind, string id, int index)
        {
            switch (kind)
            {
            case AliasKind.Exception:
                return(context.GetExceptionTypeName());

            case AliasKind.StowedException:
                return(context.GetStowedExceptionTypeName());

            case AliasKind.ReturnValue:
                return(context.GetReturnValueTypeName(index));

            case AliasKind.ObjectId:
            case AliasKind.DeclaredLocal:
                return(context.GetObjectTypeNameById(id));

            default:
                throw ExceptionUtilities.UnexpectedValue(kind);
            }
        }
Exemple #14
0
 /// <summary>
 /// Creates new alias.
 /// </summary>
 /// <param name="name">Alias name.</param>
 /// <param name="kind">Alias type.</param>
 public Alias(string name, AliasKind kind)
     : this(new Name(name), kind)
 {
 }
Exemple #15
0
 /// <summary>
 /// Creates new alias.
 /// </summary>
 /// <param name="name">Alias name.</param>
 /// <param name="kind">Alias type.</param>
 public Alias(Name name, AliasKind kind)
 {
     Name = name;
     Kind = kind;
 }
Exemple #16
0
 public virtual LangElement Use(Span span, IEnumerable <UseBase> uses, AliasKind kind)
 {
     return(new UseStatement(span, uses.AsArray(), kind));
 }
Exemple #17
0
        private TranslatedQualifiedName TranslateFallbackQualifiedName(QualifiedNameRef qname, AliasKind kind)
        {
            // aliasing
            QualifiedName tmp;

            if (qname.QualifiedName.IsSimpleName && this.namingContext.Aliases != null &&
                this.namingContext.Aliases.TryGetValue(new Alias(qname.QualifiedName.Name, kind), out tmp))
            {
                return(new TranslatedQualifiedName(tmp, qname.Span, qname, null));
            }

            //
            QualifiedName translatedQName;
            bool          translated = TranslateNamespace(qname, out translatedQName);

            if (!translatedQName.IsFullyQualifiedName && translatedQName.IsSimpleName && !IsInGlobalNamespace)
            {
                // "\foo"
                var fallbackQName = new QualifiedName(translatedQName.Name)
                {
                    IsFullyQualifiedName = true
                };
                // "namespace\foo"
                translatedQName = new QualifiedName(translatedQName.Name, namingContext.CurrentNamespace.Value.Namespaces)
                {
                    IsFullyQualifiedName = true
                };
                return(new TranslatedQualifiedName(translatedQName, qname.Span, qname, fallbackQName));
            }
            else
            {
                translatedQName.IsFullyQualifiedName = true;  // just ensure
                return(new TranslatedQualifiedName(translatedQName, qname.Span, qname, null));
            }
        }
Exemple #18
0
 public override LangElement Use(Span span, IEnumerable <UseBase> uses, AliasKind kind)
 => CountLE(base.Use(span, uses, kind));
Exemple #19
0
 public async Task <string> GenerateAsync(AliasKind kind, Random random)
 {
     return(string.Concat((await this.GetChainAsync(kind)).Chain(random)).ToTitleCase());
 }
 internal static string GetTypeName(InspectionContext context, AliasKind kind, string id, int index)
 {
     switch (kind)
     {
         case AliasKind.Exception:
             return context.GetExceptionTypeName();
         case AliasKind.StowedException:
             return context.GetStowedExceptionTypeName();
         case AliasKind.ReturnValue:
             return context.GetReturnValueTypeName(index);
         case AliasKind.ObjectId:
         case AliasKind.DeclaredLocal:
             return context.GetObjectTypeNameById(id);
         default:
             throw ExceptionUtilities.UnexpectedValue(kind);
     }
 }
Exemple #21
0
        /// <summary>
        /// Builds <see cref="QualifiedName"/> with first element aliased if posible.
        /// </summary>
        /// <param name="qname">Qualified name to translate.</param>
        /// <param name="kind">Type of the translated alias.</param>
        /// <param name="aliases">Enumeration of aliases.</param>
        /// <param name="currentNamespace">Current namespace to be prepended if no alias is found.</param>
        /// <param name="translated">Qualified name that has been tralated according to given naming context.</param>
        /// <returns>Indication if the name has been translated or not.</returns>
        public static bool TryTranslateAlias(QualifiedName qname, AliasKind kind, Dictionary <Alias, QualifiedName> aliases, QualifiedName?currentNamespace, out QualifiedName translated)
        {
            if (!qname.IsFullyQualifiedName)
            {
                // get first part of the qualified name:
                string first = qname.IsSimpleName ? qname.Name.Value : qname.Namespaces[0].Value;

                // return the alias if found:
                QualifiedName alias;
                if (aliases != null && aliases.TryGetValue(new Alias(first, kind), out alias))
                {
                    if (qname.IsSimpleName)
                    {
                        translated = alias;
                        translated.IsFullyQualifiedName = true;
                    }
                    else
                    {
                        // [ alias.namespaces, alias.name, qname.namespaces+1 ]
                        Name[] names = new Name[qname.namespaces.Length + alias.namespaces.Length];
                        for (int i = 0; i < alias.namespaces.Length; ++i)
                        {
                            names[i] = alias.namespaces[i];
                        }
                        names[alias.namespaces.Length] = alias.name;
                        for (int j = 1; j < qname.namespaces.Length; ++j)
                        {
                            names[alias.namespaces.Length + j] = qname.namespaces[j];
                        }

                        translated = new QualifiedName(qname.name, names)
                        {
                            IsFullyQualifiedName = true
                        };
                    }
                    return(true);
                }
                else
                {
                    if (currentNamespace.HasValue)
                    {
                        Debug.Assert(string.IsNullOrEmpty(currentNamespace.Value.Name.Value));
                        translated = new QualifiedName(qname, currentNamespace.Value)
                        {
                            IsFullyQualifiedName = true
                        };
                        return(true);
                    }
                    else
                    {
                        translated = new QualifiedName(qname.Name, qname.Namespaces)
                        {
                            IsFullyQualifiedName = true
                        };
                        return(false);
                    }
                }
            }
            translated = qname;
            return(false);
        }
Exemple #22
0
 public Task <string> GenerateAsync(AliasKind kind, int seed) => this.GenerateAsync(kind, new Random(seed));