Exemplo n.º 1
0
        public static string CollectTree(IParsingTreeNode node)
        {
            var v = new RulesParsingTreePrinter();

            node.Visit(v);
            return(v._sb.ToString());
        }
Exemplo n.º 2
0
            public static IParsingTreeTerminal FindLastTerminal(IParsingTreeNode node)
            {
                var visitor = new LastTerminalSearcher();

                node.Visit(visitor);
                return(visitor.Result);
            }
Exemplo n.º 3
0
        public ParsingTreeNodeInfo(IParsingTreeNode node, ISourceTextReader textReader, Func <IParsingTreeGroup, ParsingTreeNodeInfo, IEnumerable <ParsingTreeNodeInfo> > childsAccessor, bool fullInfo, ParsingTreeNodeInfo parent = null)
        {
            this.Node   = node;
            this.Parent = parent;

            var terminal = node as IParsingTreeTerminal;

            if (terminal != null)
            {
                this.Text = "'" + textReader.GetText(terminal.From, terminal.To) + "'";

                if (fullInfo)
                {
                    this.Text = (node.Rule == null ? "<NULL>" : node.Rule.Name) + ": " +
                                (node.Expression == null ? "<NULL>" : node.Expression.ToString()) + "; " + this.Text;
                }
            }

            var group = node as IParsingTreeGroup;

            if (group != null)
            {
                this.Childs = childsAccessor(group, this);

                this.Text = node.Rule == null ? "<NULL>" : node.Rule.Name;
                if (fullInfo)
                {
                    this.Text += ": " + (node.Expression == null ? "<NULL>" : node.Expression.ToString()) + ";";
                }
            }
        }
Exemplo n.º 4
0
        public MappingContext <TContext> Translate(IParsingTreeNode node, TContext contextObj)
        {
            var context = new MappingContext <TContext>(this);

            context.Context = contextObj;
            context.Result  = _maps[node.Rule.TokenId].action.DynamicInvoke(node, context);
            return(context);
        }
Exemplo n.º 5
0
        public void TraverseFragment(IParsingTreeNode root, IParsingTreeWalkerHandler handler, Location locFrom, Location locTo)
        {
            if (root == null)
            {
                throw new ArgumentNullException("Argument 'root' cannot be null!");
            }

            _stack.Clear();
            _stack.Push((ParsingTreeNode.Group)root);

            while (_stack.Count > 0)
            {
                var n        = _stack.Count;
                var currNode = _stack.Pop();

                var g = currNode as ParsingTreeNode.Group;
                var t = currNode as ParsingTreeNode.Terminal;

                if (g != null)
                {
                    if (g.Location > locTo)
                    {
                        continue;
                    }

                    _stack.Push(null);
                    handler.EnterGroup(g);

                    foreach (ParsingTreeNode.ParsedNode item in g.GetRuleChilds(true))
                    {
                        _stack.Push(item);
                    }
                }
                else if (t != null)
                {
                    if (t.From > locTo)
                    {
                        continue;
                    }

                    handler.Terminal(t);

                    if (t.To < locFrom)
                    {
                        return;
                    }
                }
                else if (currNode == null)
                {
                    handler.ExitGroup();
                }
                else
                {
                    throw new NotImplementedException("");
                }
            }
        }
Exemplo n.º 6
0
        private void PrintNode(IParsingTreeNode node)
        {
            for (int i = 0; i < _i; i++)
            {
                _sb.Append(_s);
            }

            _sb.Append(node.GetType().Name);
            _sb.AppendFormat("  @{0} {1} ", (node as ParsingTreeNode).Location, node.Rule);
            _sb.AppendLine(((ParsingTreeNode)node).GrammarNode.ToString());
        }
Exemplo n.º 7
0
        static IEnumerable <IParsingTreeNode <T> > GetChildren <T>(this IParsingTreeNode <T> node)
        {
            var child = node.Child;

            while (child != null)
            {
                yield return(child);

                child = child.NextChild;
            }
        }
Exemplo n.º 8
0
        private void SetTrees(IParsingTreeNode root, ISourceTextReader textReader)
        {
            Func <IParsingTreeGroup, ParsingTreeNodeInfo, IEnumerable <ParsingTreeNodeInfo> > childsAccessor = null;

            childsAccessor = (n, p) => n.Childs.Select(nc => new ParsingTreeNodeInfo(nc, textReader, childsAccessor, true, p)).ToArray();
            this.FullTree  = new ParsingTreeNodeInfo(root, textReader, childsAccessor, true);

            Func <IParsingTreeGroup, ParsingTreeNodeInfo, IEnumerable <ParsingTreeNodeInfo> > filteredChildsAccessor = null;

            filteredChildsAccessor = (n, p) => n.GetRuleChilds().Select(nc => new ParsingTreeNodeInfo(nc, textReader, filteredChildsAccessor, false, p)).ToArray();
            this.FilteredTree      = new ParsingTreeNodeInfo(root, textReader, filteredChildsAccessor, false);
        }
Exemplo n.º 9
0
            public TemporaryReparsingGroup(IParsingTreeNode oldNode, ParserNode node, Location fromLoc, Location toLoc, TemporaryGroup parent, TemporaryChildNode child)
                : base(node, oldNode.Location, parent, child)
            {
                this.OldGroup      = oldNode as IParsingTreeGroup;
                this.ReparsingFrom = fromLoc;
                this.ReparsingTo   = toLoc;

                if (this.OldGroup == null)
                {
                    throw new ReparsingFailException();
                }
            }
Exemplo n.º 10
0
        public static string GetContent(this IParsingTreeNode node, ISourceTextReader reader)
        {
            var from = node.GetFromLocation();

            if (from.HasValue)
            {
                var to = node.GetToLocation();
                return(reader.GetText(from.Value, to.Value));
            }
            else
            {
                return(string.Empty);
            }
        }
Exemplo n.º 11
0
        internal static IParsingTreeNode[] GetRuleChildsArray(this IParsingTreeNode node)
        {
            if (node is IParsingTreeTerminal)
            {
                throw new InvalidOperationException();
            }

            var g = node as IParsingTreeGroup;

            if (g == null)
            {
                throw new InvalidOperationException();
            }

            return(g.GetRuleChilds().ToArray());
        }
Exemplo n.º 12
0
        public static IEnumerable <IParsingTreeNode> EnumerateRuleChilds(this IParsingTreeNode node, bool disableExpanding = false, bool skipOmitFragments = true)
        {
            if (node is IParsingTreeTerminal)
            {
                throw new InvalidOperationException();
            }

            var g = node as IParsingTreeGroup;

            if (g == null)
            {
                throw new InvalidOperationException();
            }

            return(g.GetRuleChilds(disableExpanding, skipOmitFragments));
        }
Exemplo n.º 13
0
        public static TemporaryGroup CreateRootGroup(IParsingTreeNode oldNode, ParserNode omitPatternRoot, Location locFrom, Location locTo, out ParserNode prevGrammarNode, out bool insideOmittedFragment)
        {
            insideOmittedFragment = false;
            var oldGroup = oldNode as IParsingTreeGroup;

            if (oldGroup == null || oldGroup.Location > locFrom)
            {
                throw new InvalidOperationException();
            }

            prevGrammarNode = null;
            var newGroup      = new TemporaryReparsingGroup(oldNode, (oldNode as ParsingTreeNode).GrammarNode, locFrom, locTo, null, null);
            var newGroupChild = CreateItemToReparse(oldGroup, newGroup, locFrom, locTo);
            int depth         = 0;

            while (newGroupChild != null)
            {
                if (newGroupChild.GrammarNode == omitPatternRoot)
                {
                    insideOmittedFragment = true;
                }

                prevGrammarNode = newGroup.GrammarNode;
                oldGroup        = newGroupChild.OldGroup;
                newGroup        = newGroupChild;
                newGroupChild   = CreateItemToReparse(oldGroup, newGroup, locFrom, locTo);
                depth++;
            }

            // TODO: [Portable.Parser.Impl.ParsingTreeNode.CreateRootGroup] to think about possible parsing directions when reparsing
            //var lastTakenChild = newGroup.Childs.LastOrDefault();
            //if (lastTakenChild != null)
            //    prevGrammarNode = ((ParsingTreeNode)lastTakenChild).GrammarNode;

            prevGrammarNode = newGroup.Parent.GrammarNode;
            return(newGroup);
        }
Exemplo n.º 14
0
        static Location?GetFromLocation(this IParsingTreeNode node)
        {
            var t = FirstTerminalSearcher.FindFirstTerminal(node);

            return(t == null ? null : (Location?)t.From);
        }
Exemplo n.º 15
0
 public FinalNode(T info, FinalNode child, FinalNode nextChild)
     : base(info)
 {
     this.Child     = child;
     this.NextChild = nextChild;
 }
Exemplo n.º 16
0
        static Location?GetToLocation(this IParsingTreeNode node)
        {
            var t = LastTerminalSearcher.FindLastTerminal(node);

            return(t == null ? null : (Location?)t.To);
        }
Exemplo n.º 17
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
                           ));
            }
        }
Exemplo n.º 18
0
 public T Map <T>(IParsingTreeNode node)
 {
     return(_mapping.GetMap <T>(node.Rule)(node, this));
 }