public void appendChild(GrammarNodeD child)
        {
            if (child is NonterminalHeadD)
                throw new Exception("An operations children must be of type NonterminalTailTailD.");

            children.Add(child);
        }
        /// <summary>
        /// Used in Analysis, we reverse the compilation by going back to Lexemes
        /// </summary>
        /// <param name="grammarNodeD"></param>
        /// <returns></returns>
        public static List<Lexeme> Reverse(GrammarNodeD grammarNodeD)
        {
            if (grammarNodeD is OperationD)
                return Reverse(grammarNodeD as OperationD);

            List<Lexeme> returnVal = new List<Lexeme>();
            if (grammarNodeD is TerminalD)
            {
                returnVal.Add(Reverse((TerminalD)grammarNodeD));
                return returnVal;
            }

            if (grammarNodeD is NonterminalTailD)
            {
                returnVal.Add(Reverse((NonterminalTailD)grammarNodeD));
                return returnVal;
            }

            if (grammarNodeD is UndefinedD)
            {
                returnVal.Add(Reverse((UndefinedD)grammarNodeD));
                return returnVal;
            }

            throw new NotImplementedException();
        }
        /// <summary>
        /// Will put child at the position in the children
        /// If position is at end will append to end
        /// </summary>
        /// <param name="position"></param>
        public void insertChild(int position, GrammarNodeD child)
        {
            if (position > children.Count || children.Count < 0)
                throw new Exception("The position attempted [" + position + "] was outside the bounds of the children array");

            if (position == children.Count)
                appendChild(child);
            else
                children.Insert(position, child);
        }
        public void AddChoice(GrammarNodeD grammarNodeD)
        {
            // assert parent is concatenation
            ConcatenationD parent = grammarNodeD.Parent as ConcatenationD;
            if (parent == null)
                throw new Exception("The grammarNodeD parent is not of type concatenation");

            // assert parent parent is choiceD
            ChoiceD parentParent = parent.Parent as ChoiceD;
            if (parentParent == null)
                throw new Exception("The grammarNodeD parent parent is not of type choiceD");

            ConcatenationD concatD = new ConcatenationD(parentParent);
            parentParent.appendChild(concatD);

            concatD.appendChild(new UndefinedD(concatD));
        }
 public TerminalD(String nameIn, GrammarNodeD parentIn)
 {
     Init(nameIn, parentIn);
 }
 private void Init(string nameIn, GrammarNodeD parentIn)
 {
     name = nameIn;
     references = new List<TerminalV>();
     parent = parentIn;
 }
 /// <summary>
 /// Creates a new class that has a reference to a nonterminal head D. For use in the definitive grammar when defining a rule.
 /// </summary>
 /// <param name="nonterminalHeadD"></param>
 public NonterminalTailD(NonterminalHeadD nonterminalHeadD, GrammarNodeD parentIn)
 {
     Init(nonterminalHeadD, parentIn);
 }
 private void Init(NonterminalHeadD nonterminalHeadD, GrammarNodeD parentIn)
 {
     reference = nonterminalHeadD;
     parent = parentIn;
 }
 public GrammarNodeVReplaceVisitor(GrammarNodeD targetIn, GrammarNodeD itemIn)
 {
     target = targetIn;
     newItem = itemIn;
 }
        private void createItem(GrammarNodeD parent)
        {
            GrammarNodeD newItem;

            switch (symbolType)
            {
                case VADG.Global.SymbolType.Undefined:
                    newItem = new UndefinedD(parent);
                    break;
                default: throw new Exception("The visitor does not support that type yet, get on it!");
            }

            item = newItem;
        }
        public void AddBefore(SymbolV symbolV, GrammarNodeD itemToAdd)
        {
            GrammarNodeVAddBeforeVisitor visitor = new GrammarNodeVAddBeforeVisitor(symbolV, itemToAdd);

            startSymbol.accept(visitor);
        }
 public GrammarNodeVAddChoiceVisitor(GrammarNodeD targetIn)
 {
     target = targetIn;
 }
 public UndefinedD(GrammarNodeD parentIn)
 {
     references = new List<UndefinedV>();
     parent = parentIn;
 }
 private void Init(List<GrammarNodeD> childrenIn, List<ChoiceV> referencesIn, GrammarNodeD parentIn)
 {
     children = childrenIn;
     references = referencesIn;
     parent = parentIn;
 }
        public GrammarNodeD Replace(GrammarNodeD itemToReplace, VADG.Global.SymbolType symbolType, string name)
        {
            OperationD parent = itemToReplace.Parent as OperationD;
            if (parent == null)
                throw new Exception("The item to replace does not have an operation D as a parent, fail.");

            // the rule we are editing
            NonterminalHeadD rule = itemToReplace.getMyRule();
            if (rule == null)
                throw new Exception("The item to replace could not find a nonterminalhead D associated with it.");

            // remove old item
            int index = parent.Children.IndexOf(itemToReplace);
            parent.Children.Remove(itemToReplace);
            itemToReplace.Dispose();

            // create new item to add
            GrammarNodeD newItem;
            if (symbolType == VADG.Global.SymbolType.Terminal)
            {
                // create new Terminal
                newItem = new TerminalD(name, parent);
            }
            else
            {
                // If the nont head doesn't exist, create and add to grammar
                NonterminalHeadD nontHead = model.DefinitiveGrammar.getNonterminal(name);

                if (nontHead == null)
                    nontHead = CreateNewNonterminal(name);

                newItem = nontHead.CreateNonterminalTailD(parent);
            }

            // insert new item into parent
            parent.insertChild(index, newItem);
            return newItem;
        }
        public void Delete(GrammarNodeD grammarNodeD)
        {
            if (grammarNodeD.Parent is ConcatenationD)
            {
                ConcatenationD parent = grammarNodeD.Parent as ConcatenationD;

                // do the removal
                grammarNodeD.Dispose();
                parent.Children.Remove(grammarNodeD);

                if (parent.Children.Count == 0)
                    // add undefined
                    parent.appendChild(new UndefinedD(parent));
            }
            else if (grammarNodeD.Parent is ChoiceD)
            {
                // removing a choice
                ChoiceD parent = grammarNodeD.Parent as ChoiceD;

                // do removal
                grammarNodeD.Dispose();
                parent.Children.Remove(grammarNodeD);

                if (parent.Children.Count == 0)
                {
                    ConcatenationD concatD = new ConcatenationD(parent);
                    parent.appendChild(concatD);
                    concatD.appendChild(new UndefinedD(concatD));
                }
            }
            else
                throw new Exception("The parent was not of type concatenation, I don't know what to do...");
        }
 /// <summary>
 /// Creates a new class that is a nonterminal tail for use in the definitive grammar, used to define a rule.
 /// </summary>
 /// <returns></returns>
 public NonterminalTailD CreateNonterminalTailD(GrammarNodeD parentIn)
 {
     return new NonterminalTailD(this, parentIn);
 }
 public ConcatenationD(GrammarNodeD parentIn)
 {
     Init(new List<GrammarNodeD>(), new List<ConcatenationV>(), parentIn);
 }
 public GrammarNodeVAddAfterVisitor(GrammarNodeV targetIn, GrammarNodeD itemIn)
 {
     target = targetIn;
     item = itemIn;
 }
 public ChoiceD(GrammarNodeD parentIn)
 {
     Init(new List<GrammarNodeD>(), new List<ChoiceV>(), parentIn);
 }
 public GrammarNodeVDeleteVisitor(GrammarNodeD targetIn)
 {
     target = targetIn;
 }
 public GrammarNodeVAddBeforeVisitor(GrammarNodeV targetIn, GrammarNodeD itemIn)
 {
     target = targetIn;
     item = itemIn;
 }
        public void Replace(GrammarNodeD itemToReplace, GrammarNodeD newItem)
        {
            GrammarNodeVReplaceVisitor visitor = new GrammarNodeVReplaceVisitor(itemToReplace, newItem);

            startSymbol.accept(visitor);
        }