Exemplo n.º 1
0
        protected override ParserState <Result> EnterNodeImpl(ParserNode nextNode)
        {
            var recursionCallNode = this.CurrNode as ParserNode.RecursiveParserNode;

            if (nextNode is ParserNode.Number)
            {
                if (this.CurrNode == nextNode.Parent)
                {
                    this.PushChildIndex();
                }
                else if (this.CurrNode.Parent == nextNode)
                {
                    this.IncChildIndex();
                }
            }

            return(new LinearParserState(
                       _log,
                       nextNode,
                       this.CurrNode,
                       this.LastStep.CreateEnterNode(nextNode, this.Location),
                       recursionCallNode == null ? this.Stack : this.Stack.StartRecursionRuleCall(recursionCallNode),
                       null,
                       this.Reader
                       ));
        }
Exemplo n.º 2
0
        private ParserState <Result> MayBeReconstruct(ParserNode nextNode)
        {
            ParsingTreeNode.TemporaryGroup reconstructedTree;
            if (this.TreeNode.TryReconstruct(nextNode, this.Location, out reconstructedTree))
            {
                if (_log != null)
                {
                    while (_log.Level > 2)
                    {
                        _log.Pop();
                    }

                    _log.WriteLine("Tree reconstruction completed!");
                }

                return(new ParserTreeState(
                           _log,
                           this.CurrNode,
                           reconstructedTree,
                           null,
                           this.Reader
                           )
                {
                    ResultReconstructed = true,
                    InsideOmittedFragment = false
                });
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 3
0
        protected override ParserState <Result> ExitNodeImpl(bool?terminalFailed, ParserNode nextNode)
        {
            if (this.CurrNode is ParserNode.Number && nextNode == this.CurrNode.Parent)
            {
                this.PopChildIndex();
            }

            // fallback
            if (terminalFailed == true)
            {
                var cloc = this.Reader.Location;
                var oloc = this.LastStep.Location;
                if (cloc.Line != oloc.Line)
                {
                    throw new NotImplementedException();
                }

                if (cloc.Column != oloc.Column)
                {
                    this.Reader.Move(oloc.Column - cloc.Column);
                }
            }

            return(new LinearParserState(
                       _log,
                       nextNode,
                       this.CurrNode,
                       this.LastStep is ParserStep.EnterNode && this.LastStep.Node == this.CurrNode && this.LastStep.PrevStep.Node == nextNode
                    ? this.LastStep.PrevStep
                    : this.LastStep.CreateExitNode(this.CurrNode, this.Location),
                       nextNode is ParserNode.RecursiveParserNode ? this.Stack.EndRecursionRuleCall() : this.Stack,
                       terminalFailed,
                       this.Reader
                       ));
        }
Exemplo n.º 4
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
                       ));
        }
Exemplo n.º 5
0
 public Terminal(ParserNode node, Location loc, ParsedNode next, Location from, Location to, string content)
     : base(node, loc, next)
 {
     this.From    = from;
     this.To      = to;
     this.Content = content;
 }
Exemplo n.º 6
0
 public TemporaryTerminal(ParserNode node, Location loc, TemporaryGroup parent, Location from, Location to, string content)
     : base(node, loc, parent)
 {
     this.From    = from;
     this.To      = to;
     this.Content = content;
 }
Exemplo n.º 7
0
 public TemporaryGroupChildTerminal(ParserNode grammarNode, Location loc, TemporaryChildNode prev, Location from, Location to, string content)
     : base(grammarNode, loc, prev)
 {
     this.From    = from;
     this.To      = to;
     this.Content = content;
 }
Exemplo n.º 8
0
 public Terminal(ParserStep prev, ParserNode node, Location from, Location to, string text)
     : base(prev, node, from)
 {
     this.From = from;
     this.To   = to;
     this.Text = text;
 }
Exemplo n.º 9
0
        private ParserNode BuildGraphInternal(RuleExpression rootExpression)
        {
            _currContext = GraphContext.ForRoot(rootExpression);

            _recursiveNodes.Clear();
            do
            {
                var ctx = _currContext;

                if (ctx.expression != null)
                {
                    ctx.expression.Visit(_expressionsVisitor);
                }
                else
                {
                    throw new NotImplementedException(); // should never happen
                }

                if (_currContext == ctx)
                {
                    throw new InvalidOperationException(string.Format("Missing transition for expression [{0}] of rule [{1}] was not created!", _currContext.expression, _currContext.rule));
                }
                if (_currContext == null)
                {
                    throw new InvalidOperationException(string.Format("Invalid handler for expression [{0}] of rule [{1}]!", _currContext.expression, _currContext.rule));
                }
                //if (_currContext.prevContext != ctx)
                //    throw new InvalidOperationException(string.Format("Invalid previos context was set from handler for expression [{0}] of rule [{1}] was not created!", _currContext.expression, _currContext.rule));
            } while (_currContext.parentContext != null);

            if (_currContext.childEntries == null ||
                _currContext.childEntries.prev != null)
            {
                throw new InvalidOperationException();
            }

            foreach (var item in _recursiveNodes)
            {
                ParserNode target = null;
                for (var n = item.Parent; n != null; n = n.Parent)
                {
                    if (n.Rule == item.Rule && n.Parent.Rule != item.Rule && n.Parent is ParserNode.RuleCall)
                    // && item.TargetCallExpr.ToString() && )
                    {
                        target = n.Parent;
                        break;
                    }
                }

                if (target == null)
                {
                    throw new InvalidOperationException(string.Format("Filed to find recursive rule call target for rule [{0}] from rule [{1}]!", item.Rule, item.Parent.Rule));
                }

                item.SetTarget(target);
            }

            return(_currContext.childEntries.node);
        }
Exemplo n.º 10
0
 public GraphContext ForParentExpression(ParserNode node)
 {
     return(new GraphContext(
                this.parentContext.rule,
                this.parentContext.expression,
                this.parentContext.parentContext,
                this.parentContext.invocationCount + 1,
                new ChildParserNodeEntry(this.parentContext.childEntries, node)
                ));
 }
Exemplo n.º 11
0
        void SetParent(ParserNode parent, int index)
        {
            if (this.Parent != null)
            {
                throw new InvalidOperationException();
            }

            this.Parent            = parent;
            this.IndexInParentList = index;
        }
Exemplo n.º 12
0
        private LinearParserState(IndentedWriter w, ParserNode currNode, ParserNode prevNode, ParserStep lastStep, StatefullStackNode stack, bool?lastTerminalFailed, ISourceTextReader reader, ISourceTextReaderHolder holder = null)
            : base(w, currNode, prevNode, lastTerminalFailed, reader, holder)
        {
            this.LastStep = lastStep;
            this.Stack    = stack;

            if (_log != null)
            {
                _log.WriteLine(this.Stack.ToString());
            }
        }
Exemplo n.º 13
0
            internal SingleChildNode(Rule rule, RuleExpression expression, ParserNode child)
                : base(rule, expression)
            {
                if (child == null)
                {
                    throw new ArgumentNullException("Argument 'child' cannot be null!");
                }

                this.Child = child;

                child.SetParent(this, 0);
            }
Exemplo n.º 14
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.º 15
0
 protected override ParserState <Result> EnterNodeImpl(ParserNode nextNode)
 {
     return(this.MayBeReconstruct(nextNode) ?? new ParserTreeState(
                _log,
                this.CurrNode,
                this.TreeNode.CreateChildGroup(nextNode, this.Reader.Location),
                null,
                this.Reader
                )
     {
         InsideOmittedFragment = this.InsideOmittedFragment
     });
 }
Exemplo n.º 16
0
        public string CollectTree(ParserNode node)
        {
            _w = new IndentedWriter();

            this.Push(_finishIndent);
            node.Visit(this);

            var result = _w.GetContentAsString();

            _w = null;

            return(result);
        }
Exemplo n.º 17
0
        internal ParserState(IndentedWriter log, ParserNode curr, ParserNode prev, bool?lastTerminalFailed, ISourceTextReader reader, ISourceTextReaderHolder holder = null)
        {
            this.CurrNode            = curr;
            this.PrevNode            = prev;
            this.ResultReconstructed = false;

            _log = log;

            this.LastTerminalFailed = lastTerminalFailed;

            _reader       = reader;
            _readerHolder = holder;
        }
Exemplo n.º 18
0
            public void SetTarget(ParserNode target)
            {
                if (target == null)
                {
                    throw new ArgumentNullException("Argument 'target' cannot be null!");
                }
                if (this.Target != null)
                {
                    throw new InvalidOperationException();
                }

                this.Target = target;
            }
Exemplo n.º 19
0
        public ParserContext(Parser <TResult> owner, AnalyzerGraphInfo analyzerInfo, ISourceTextReader source, ParserInitialStateFabric <TResult> initialStateFabric, TResult oldResult, Location limit)
        {
            _owner              = owner;
            _grammarRoot        = analyzerInfo.AnalyzerGraph;
            _omitRoot           = analyzerInfo.OmitGraph;
            _source             = source;
            _initialStateFabric = initialStateFabric;

            _currState = null;
            _oldResult = oldResult;
            _limit     = limit;
            _materializeOmittedFragments = owner.MaterializeOmittedFragments;
            _useDelayedStates            = owner.UseDelayedStates;
            _parserVisitor = this; // new ParserNodeLoggingVisitor(this);
        }
Exemplo n.º 20
0
        private ParserNode BuildGraphImpl()
        {
            if (!_nav.TryEnter(_rootRuleSetName))
            {
                throw new InvalidOperationException(string.Format("Rule set [{0}] not found!", _rootRuleSetName));
            }

            var omitExpression = _nav.GetRuleSetAttributeArgument("OmitPattern");

            this.OmitGraph = omitExpression == null ? null : this.BuildGraphInternal(omitExpression);

            var rootExpression = _nav.GetRuleSetAttributeArgument("RootRule");
            var parserGraph    = this.BuildGraphInternal(rootExpression);

            return(parserGraph);
        }
Exemplo n.º 21
0
        protected override ParserState <Result> ExitNodeImpl(bool?terminalFailed, ParserNode nextNode)
        {
            // var nextNode = this.TreeNode.Parent.GrammarNode;

            // fallback
            if (terminalFailed == true)
            {
                var cloc = this.Reader.Location;
                var oloc = this.TreeNode.Location;

                if (cloc != oloc)
                {
                    if (!this.Reader.MoveTo(oloc))
                    {
                        throw new InvalidOperationException();
                    }
                }
            }

            // var removeCurr = this.TreeNode.ChildsCount == 0 || terminalFailed.Value;
            var removeCurr = terminalFailed.Value;

            if (!this.TreeNode.HasChilds && this.InsideOmittedFragment)
            {
                removeCurr = true;
            }

            //var treeNode = removeCurr ? this.TreeNode.RemoveCurrent() : (
            //    this.CurrNode is ParserNode.RecursiveParserNode
            //            ? this.TreeNode.TakeOffCurrent()
            //            : this.TreeNode.ExitChild()
            //);

            var treeNode = removeCurr ? this.TreeNode.RemoveCurrent() : this.TreeNode.ExitChild();

            return(new ParserTreeState(
                       _log,
                       this.CurrNode,
                       treeNode,
                       terminalFailed,
                       this.Reader
                       )
            {
                InsideOmittedFragment = this.InsideOmittedFragment
            });
        }
Exemplo n.º 22
0
        public ParserState <TResult> EnterNode(ParserNode nextNode, bool offTree = false)
        {
            if (nextNode.Parent != this.CurrNode && !(this.CurrNode is ParserNode.RecursiveParserNode) && !offTree)
            {
                throw new InvalidOperationException();
            }

            if (_log != null)
            {
                _log.Push().WriteLine("EnterNode @{2} {0} --> {1}", this.CurrNode, nextNode, this.Reader.Location).Push();

                if (this.CurrNode is ParserNode.RecursiveParserNode)
                {
                    _log.WriteLine("Enter to recursion");
                }
            }

            return(this.EnterNodeImpl(nextNode));
        }
Exemplo n.º 23
0
            //private ParsedNode OldRecreateChilds()
            //{
            //    ParsedNode node = null;

            //    for (var n = this.Child; n != null; n = n.Prev)
            //        node = n.Recreate(node);

            //    var loc = node.Location;
            //    var prepends = new List<ParsedNode>();
            //    foreach (ParsedNode item in this.OldGroup.Childs)
            //    {
            //        if (item.Location < loc && item.Location < this.ReparsingFrom)
            //            prepends.Add(item);
            //        else
            //            break;
            //    }

            //    for (int i = prepends.Count - 1; i >= 0; i--)
            //        node = prepends[i].UpdateNext(node);

            //    return node;
            //}

            protected override bool TryReconstructImpl(ParserNode node, Location loc, out TemporaryGroup reconstructedRoot)
            {
                if (loc >= this.ReparsingTo) // reuse right
                {
                    var it = this.OldGroup.Childs.GetEnumerator();
                    while (it.MoveNext())
                    {
                        var oldChild = it.Current;
                        if (oldChild.Location == loc && (oldChild as ParsingTreeNode).GrammarNode == node)
                        {
                            // reconstruct all the rest of the tree
                            reconstructedRoot = this.ReconstructTree();
                            return(true);
                        }
                    }
                }

                reconstructedRoot = null;
                return(false);
            }
Exemplo n.º 24
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.º 25
0
 public EnterNode(ParserStep prev, ParserNode node, Location location)
     : base(prev, node, location)
 {
 }
Exemplo n.º 26
0
 public ParserStep CreateExitNode(ParserNode node, Location location)
 {
     return(new ExitNode(this, node, location));
 }
Exemplo n.º 27
0
 public ParserStep CreateTerminal(ParserNode node, Location from, Location to, string text)
 {
     return(new Terminal(this, node, from, to, text));
 }
Exemplo n.º 28
0
 internal ParserStep(ParserStep prev, ParserNode Node, Location location)
 {
     this.PrevStep = prev;
     this.Node     = Node;
     this.Location = location;
 }
Exemplo n.º 29
0
 protected abstract ParserState <TResult> EnterNodeImpl(ParserNode nextNode);
Exemplo n.º 30
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
                           ));
            }
        }