public static IDictionary <string, NestedScope> BuildHeirarchy(IEnumerable <string> packageNames, IErrorCache errorCache, IDictionary <string, NestedScope> existing = null)
        {
            var orderedPairs = packageNames
                               .Distinct(StringComparer.OrdinalIgnoreCase)
                               .Where(name => existing == null || !existing.ContainsKey(name))
                               .Select(Parse)
                               .OrderBy(t => t.Item1.Length);

            var dict = existing ?? new Dictionary <string, NestedScope>(StringComparer.OrdinalIgnoreCase);

            foreach (var pair in orderedPairs)
            {
                NestedScope parent = null;
                if (pair.Item1 != "" && !dict.TryGetValue(pair.Item1, out parent))
                {
                    errorCache.AddError($"Could not locate parent '{pair.Item1}' when building heirarchy.");
                }

                var scope = new NestedScope(parent)
                {
                    Name = pair.Item2
                };
                dict.Add(pair.Item2, scope);
            }

            return(dict);
        }
        public NestedScope(NestedScope parent = null)
        {
            Parent   = parent;
            Children = new List <NestedScope>();
            Aspects  = new Dictionary <Type, INestedAspect>();

            parent?.Children.Add(this);
        }
        // TODO - Need to test these methods
        private NestedScope Clone(NestedScope parent)
        {
            var clone = new NestedScope(parent)
            {
                Name = Name
            };

            foreach (var pair in Aspects)
            {
                var aspectClone = pair.Value.Clone();
                aspectClone.Scope = clone;
                clone.Aspects.Add(pair.Key, aspectClone);
            }
            return(clone);
        }
 public static bool TryFindVariable(this NestedScope scope, string fullName, out Variable result)
 {
     return(scope.GetAspect <VariableSet>().TryFind(fullName, out result));
 }
 public static VariableInfo FirstVarOfName(this NestedScope scope, string variableName, IErrorCache errorCache)
 {
     return(scope.GetAspect <VariableSet>().FirstVarOfName(variableName, errorCache));
 }
 public static Variable FindVariable(this NestedScope scope, string fullName)
 {
     return(scope.GetAspect <VariableSet>().Find(fullName));
 }
 public static bool TryFindVariable(this NestedScope scope, VariableRef refName, out Variable result)
 {
     return(scope.GetAspect <VariableSet>().TryFind(refName, out result));
 }
 public static Variable FindVariable(this NestedScope scope, VariableRef refName)
 {
     return(scope.GetAspect <VariableSet>().Find(refName));
 }
 public static IEnumerable <Variable> Variables(this NestedScope scope, bool recurse = true)
 {
     return(!recurse
         ? scope.GetAspect <VariableSet>()
         : scope.Flatten().SelectMany(s => s.GetAspect <VariableSet>()));
 }
 public static NestedScope Store(this NestedScope scope, Variable variable)
 {
     scope.GetAspect <VariableSet>().Store(variable);
     return(scope);
 }