private void InitStartingTokensWithOption(Rule <IN> rule, OptionClause <IN> option,
                                           Dictionary <string, NonTerminal <IN> > nonTerminals)
 {
     if (option.Clause is TerminalClause <IN> term)
     {
         InitStartingTokensWithTerminal(rule, term);
     }
     else if (option.Clause is NonTerminalClause <IN> nonTerminal)
     {
         InitStartingTokensWithNonTerminal(rule, nonTerminal, nonTerminals);
     }
     else if (option.Clause is GroupClause <IN> group)
     {
         // TODO XXX
     }
 }
        public SyntaxParseResult <IN> ParseOption(IList <Token <IN> > tokens, OptionClause <IN> clause, Rule <IN> rule,
                                                  int position)
        {
            var result          = new SyntaxParseResult <IN>();
            var currentPosition = position;
            var innerClause     = clause.Clause;

            SyntaxParseResult <IN> innerResult = null;


            if (innerClause is TerminalClause <IN> )
            {
                innerResult = ParseTerminal(tokens, innerClause as TerminalClause <IN>, currentPosition);
            }
            else if (innerClause is NonTerminalClause <IN> )
            {
                innerResult = ParseNonTerminal(tokens, innerClause as NonTerminalClause <IN>, currentPosition);
            }
            else
            {
                throw new InvalidOperationException("unable to apply repeater to " + innerClause.GetType().Name);
            }


            if (innerResult.IsError)
            {
                if (innerClause is TerminalClause <IN> )
                {
                    result                = new SyntaxParseResult <IN>();
                    result.IsError        = true;
                    result.Root           = new SyntaxLeaf <IN>(Token <IN> .Empty());
                    result.EndingPosition = position;
                }
                else
                {
                    result         = new SyntaxParseResult <IN>();
                    result.IsError = true;
                    var children = new List <ISyntaxNode <IN> > {
                        innerResult.Root
                    };
                    if (innerResult.IsError)
                    {
                        children.Clear();
                    }
                    result.Root = new OptionSyntaxNode <IN>(rule.NonTerminalName + "__" + rule.Key, children,
                                                            rule.GetVisitor());
                    result.EndingPosition = position;
                }
            }
            else
            {
                var node = innerResult.Root;

                var children = new List <ISyntaxNode <IN> > {
                    innerResult.Root
                };
                result.Root =
                    new OptionSyntaxNode <IN>(rule.NonTerminalName + "__" + rule.Key, children, rule.GetVisitor());
                result.EndingPosition = innerResult.EndingPosition;
            }

            return(result);
        }