private void Init(List<GrammarNodeV> childrenIn, ChoiceD refIn)
 {
     reference = refIn;
     children = childrenIn;
 }
 /// <summary>
 /// Creates a visual node that has a reference to the definitive ChoiceD that was passed.
 /// </summary>
 /// <param name="ChoiceD"></param>
 public ChoiceV(ChoiceD refIn)
 {
     Init(new List<GrammarNodeV>(), refIn);
 }
 public ChoiceV(List<GrammarNodeV> children, ChoiceD refIn)
 {
     Init(children, refIn);
 }
 public void visit(ChoiceD choiceD)
 {
     if (!found)
     {
         foreach (GrammarNodeD node in choiceD.Children)
             node.accept(this);
     }
 }
        public void visit(ChoiceD choiceD)
        {
            List<GrammarNodeV> children = new List<GrammarNodeV>();

            foreach (GrammarNodeD child in choiceD.Children)
            {
                // make children
                child.accept(this);
                children.Add(current);
            }

            ChoiceV choiceV = new ChoiceV(children, choiceD);
            choiceD.AddReference(choiceV);

            current = choiceV;
        }
        /// <summary>
        /// Returns the rule structure, without the ChoiceLineD
        /// </summary>
        /// <param name="productionRuleLexemes"></param>
        /// <returns></returns>
        private void CreateRuleDataStructure(List<Lexeme> productionRuleLexemes, ChoiceLineD choiceLineD)
        {
            //GrammarNodeD returnVal;
            OperationD current = choiceLineD; // points to which level we are at
            //returnVal = current;

            // Split by Choice as this has the highest precedence
            List<Lexeme> choiceLexemes = new List<Lexeme>();
            ArrayList choices = new ArrayList();

            foreach (Lexeme lexeme in productionRuleLexemes)
            {
                if (lexeme.Type == LexemeType.Choice)
                {
                    choices.Add(choiceLexemes);
                    choiceLexemes = new List<Lexeme>();
                }
                else
                {
                    choiceLexemes.Add(lexeme);
                }
            }

            // add last choice list
            choices.Add(choiceLexemes);

            // put in a choiceD
            ChoiceD choiceD = new ChoiceD(current);
            current.appendChild(choiceD);
            current = choiceD; // move down a level

            // add the concatenated items for each Choice
            foreach (List<Lexeme> concatLexemes in choices)
            {
                ConcatenationD concatenation = new ConcatenationD(current);

                foreach (Lexeme lexeme in concatLexemes)
                {
                    if (lexeme.Type == LexemeType.Nonterminal)
                    {
                        // create nonterminal tail from the nonterminalhead
                        NonterminalHeadD nontHead = getNonterminalHead(lexeme.Value);
                        if (nontHead == null)
                            throw new Exception("Could not find a defined rule for the Nonterminal: " + lexeme.Value);

                        NonterminalTailD nontTail = new NonterminalTailD(nontHead, concatenation);

                        // add the tail to current children
                        concatenation.appendChild(nontTail);
                    }
                    else if (lexeme.Type == LexemeType.Terminal)
                    {
                        TerminalD terminalD = new TerminalD(lexeme.Value, concatenation);
                        concatenation.appendChild(terminalD);
                    }
                    else if (lexeme.Type == LexemeType.Undefined)
                    {
                        UndefinedD undefinedD = new UndefinedD(concatenation);
                        concatenation.appendChild(undefinedD);
                    }
                    else if (lexeme.Type != LexemeType.Concatenation)
                    {
                        // Lexes should only be of type Nont, T and Concat. Error
                        throw new Exception("Lexeme was not of type Nonterminal, Terminal or Concatenation");
                    }
                }

                current.appendChild(concatenation);
            }
        }
        /// <summary>
        /// Creates a new nonterminal and add's it to the defintive grammar. 
        /// It's rule is a simple Nont -> ChoiceLineD -> ConcatD -> Undefined
        /// </summary>
        /// <param name="name"></param>
        public NonterminalHeadD CreateNewNonterminal(String name)
        {
            if (model.DefinitiveGrammar.getNonterminal(name) != null)
                throw new Exception("The given name already exists as a nonterminal, dummy");

            // build nonterminal
            NonterminalHeadD newNonterminal = new NonterminalHeadD(name);
            ChoiceLineD choiceLineD = new ChoiceLineD(newNonterminal);
            newNonterminal.Rule = choiceLineD;

            ChoiceD choiceD = new ChoiceD(choiceLineD);
            newNonterminal.Rule.appendChild(choiceD);

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

            concatD.appendChild(new UndefinedD(concatD));

            // add to model
            model.DefinitiveGrammar.Nonterminals.Add(newNonterminal);

            return newNonterminal;
        }
        public void visit(ChoiceD choiceD)
        {
            foreach (GrammarNodeD node in choiceD.Children)
            {
                node.accept(this);

                if (node != choiceD.Children[choiceD.Children.Count - 1])
                    representation.Inlines.Add(new Run(choiceOperator));
            }
        }