예제 #1
0
        public static ParserState <Result> ForStart(ParserNode root, ISourceTextReader reader, bool enableLog)
        {
            IndentedWriter w;

            if (enableLog)
            {
                w = new IndentedWriter("  ");
                w.Push().WriteLine("Start @{0} {1}", reader.GetPosition(), root).Push();
            }
            else
            {
                w = null;
            }

            if (root.Parent != null)
            {
                throw new ArgumentException();
            }

            return(new LinearParserState(
                       w,
                       root,
                       null,
                       new ParserStep.EnterNode(null, root, reader.Location),
                       StatefullStackNode.ForRoot(),
                       null,
                       reader
                       ));
        }
예제 #2
0
            protected override bool TryRunImpl(ISourceTextReader reader, out string content)
            {
                bool result;
                var  text = reader.GetText();
                var  pos  = reader.GetPosition();

                var match = _regex.Match(text, pos);

                if (match.Success && match.Index == pos)
                {
                    content = match.Value;
                    result  = reader.Move(match.Length);
                }
                else
                {
                    content = null;
                    result  = false;
                }

                return(result);
            }
예제 #3
0
        //public static ParserTreeState ForStart(ParserNode root, ISourceTextReader reader, bool enableLog)
        //{
        //    IndentedWriter w;
        //    if (enableLog)
        //    {
        //        w = new IndentedWriter("  ");
        //        w.Push().WriteLine("Start @{0} {1}", reader.GetPosition(), root).Push();
        //    }
        //    else
        //    {
        //        w = null;
        //    }

        //    if (root.Parent != null)
        //        throw new ArgumentException();

        //    return new ParserTreeState(
        //        w,
        //        null,
        //        ParsingTreeNode.CreateRootGroup(root, reader.Location),
        //        null,
        //        reader
        //    );
        //}

        public static ParserState <Result> ForStart(ParserNode grammarRoot, ParserNode omitRoot, ISourceTextReader reader, IParsingTreeNode oldRoot, Location limit, bool enableLog)
        {
            IndentedWriter w;

            if (enableLog)
            {
                w = new IndentedWriter("  ");

                if (oldRoot != null)
                {
                    w.WriteLine("Incrementally");
                }

                w.Push().WriteLine("Start @{0} {1}", reader.GetPosition(), grammarRoot).Push();
            }
            else
            {
                w = null;
            }

            if (oldRoot != null)
            {
                ParserNode prevGrammarNode = null;
                bool       insideOmittedFragment;
                var        treeNode = ParsingTreeNode.CreateRootGroup(oldRoot, omitRoot, reader.Location, limit, out prevGrammarNode, out insideOmittedFragment);
                if (!reader.MoveTo(treeNode.Location))
                {
                    throw new NotImplementedException("");
                }

                if (w != null)
                {
                    w.WriteLine("Reconstructing log indentation...");

                    var t    = treeNode;
                    var path = new List <ParsingTreeNode.TemporaryGroup>();
                    while (t != null)
                    {
                        path.Add(t);
                        t = t.Parent;
                    }

                    path.Reverse();

                    for (int i = 0; i < path.Count - 1; i++)
                    {
                        w.Push().WriteLine("EnterNode @{2} {0} --> {1}", path[i].GrammarNode, path[i + 1].GrammarNode, path[i].Location).Push();

                        if (path[i].GrammarNode is ParserNode.RecursiveParserNode)
                        {
                            w.WriteLine("Enter to recursion");
                        }
                    }

                    w.WriteLine("...Identation reconstructed.");
                }

                ParserState <Result> state = new ParserTreeState(
                    w,
                    prevGrammarNode,
                    treeNode,
                    null,
                    reader
                    );

                state.InsideOmittedFragment = insideOmittedFragment;

                if (state.LastTerminalFailed == false)
                {
                    state = state.ExitNode();
                }
                else
                {
                    System.Diagnostics.Debug.Print(string.Empty);
                }

                return(state);
            }
            else
            {
                return(new ParserTreeState(
                           w,
                           null,
                           ParsingTreeNode.CreateRootGroup(grammarRoot, reader.Location),
                           null,
                           reader
                           ));
            }
        }