Example #1
0
        public override AstNode Visit(NamespaceDefinition node)
        {
            // Handle nested names.
            string        fullname    = node.GetName();
            StringBuilder nameBuilder = new StringBuilder();

            int numscopes = 0;

            for (int i = 0; i < fullname.Length; i++)
            {
                char c = fullname[i];
                if (c != '.')
                {
                    nameBuilder.Append(c);
                    if (i + 1 < fullname.Length)
                    {
                        continue;
                    }
                }

                // Expect full name.
                if (c == '.' && i + 1 == fullname.Length)
                {
                    Error(node, "expected complete namespace name.");
                }

                // Find an already declared namespace.
                string name = nameBuilder.ToString();
                nameBuilder.Length = 0;
                ScopeMember old = currentContainer.FindMember(name);
                if (old != null)
                {
                    if (!old.IsNamespace())
                    {
                        Error(node, "defining namespace collides with another thing.");
                    }

                    // Store a reference in the node.
                    node.SetNamespace((Namespace)old);
                }
                else
                {
                    // Cast the current scope.
                    Namespace space = (Namespace)currentContainer;

                    // Create, name and add the new namespace.
                    Namespace newNamespace = new Namespace(space);
                    newNamespace.SetName(name);
                    space.AddMember(newNamespace);

                    // Store a reference in the node.
                    node.SetNamespace(newNamespace);
                }

                // Update the scope.
                PushScope(node.GetNamespace());

                // Increase the number of scopes.
                numscopes++;
            }

            // Visit the children.
            VisitList(node.GetChildren());

            // Restore the scopes.
            for (int i = 0; i < numscopes; i++)
            {
                PopScope();
            }

            return(node);
        }