예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="history"></param>
        /// <param name="node"></param>
        /// <returns>Number of false pushes to history</returns>
        private bool CreateHistoryRecursevely(DebugStack history, SyntaxParsingNode node)
        {
            //Look at GrammarEntry
            GrammarEntry current = node.Entry;

            //Try to match Tokens or Entries store original history
            int  h         = history.Count;
            int  t         = node.Span.StartToken;
            bool lastToken = false;

            for (int k = 0; k < current.Blocks.Length; k++)
            {
                string b = current.Blocks[k];
                //Look at next token if last one was
                if (lastToken)
                {
                    t++;
                    lastToken = false;
                }

                //Consider Grammar Block
                if (b.StartsWith('!'))
                {
                    bool          found = false;
                    GrammarScheme next  = BaseGrammar.GetNamed(b.Substring(1));
                    for (int i = 0; i < next.Overloads.Length; i++)
                    {
                        //Store Route
                        history.Push(i);

                        //Create Child and continue
                        SyntaxParsingNode c = node.ProduceChild();
                        c.Span     = new TokenSpan(t);
                        c.Overload = i;
                        c.Scheme   = next;
                        if (CreateHistoryRecursevely(history, c))
                        {
                            t         = c.Span.EndToken;
                            lastToken = true;
                            found     = true;
                            break;
                        }
                        else
                        {
                            history.Pop();
                        }
                    }

                    if (!found)
                    {
                        Console.WriteLine($"Considered Element: {node.Scheme.Name} [{node.Overload}]");
                        Console.WriteLine($" - Could not be created because of block: {b}");
                        Console.WriteLine($" - Error: No fitting overload found.\n---");
                        //Reset History
                        while (h < history.Count)
                        {
                            history.Pop();
                        }

                        //Return
                        return(false);
                    }
                }
                //Consider Token Block
                else
                {
                    //Match Token
                    if (t < node.Collection.Count && b == node.Collection[t].Name)
                    {
                        lastToken = true;
                    }
                    else
                    {
                        Console.WriteLine($"Considered Element: {node.Scheme.Name} [{node.Overload}]");
                        Console.WriteLine($" - Could not be created because of block: {b}");
                        Console.WriteLine($" - Error: { (t < node.Collection.Count ? $"Token did not match {node.Collection[t].Name}" : "The definition exceeds the assigned tokens.") }.\n---");
                        //Reset History
                        while (history.Count > h)
                        {
                            history.Pop();
                        }

                        //Return
                        return(false);
                    }
                }
            }

            Console.WriteLine($"Considered Element: {node.Scheme.Name} [{node.Overload}]");
            Console.WriteLine($" - Succesfully created!");
            Console.WriteLine($" - Token Span: {node.Span.StartToken} - {t}\n---");

            //Node was a match
            node.Span.EndToken = t;
            return(true);
        }
        public void SaveGrammarEntry(GrammarEntry entry)
        {
            if (entry.ObjectId == null)
            {
                entry.ObjectId = BsonObjectId.GenerateNewId();
            }

            grammarEntries[entry.Text] = entry;
        }