/// <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); }
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)); }
public UseStatement(Span span, List <UseBase> uses, AliasKind kind) : base(span) { Debug.Assert(uses != null); _kind = kind; _uses = uses.ToArray(); }
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; }
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; }
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; }
/// <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); } }
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>()); } }
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); }
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); } }
/// <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) { }
/// <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; }
public virtual LangElement Use(Span span, IEnumerable <UseBase> uses, AliasKind kind) { return(new UseStatement(span, uses.AsArray(), kind)); }
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)); } }
public override LangElement Use(Span span, IEnumerable <UseBase> uses, AliasKind kind) => CountLE(base.Use(span, uses, kind));
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); } }
/// <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); }
public Task <string> GenerateAsync(AliasKind kind, int seed) => this.GenerateAsync(kind, new Random(seed));