Exemplo n.º 1
0
            public StatefullStackNode StartRecursionRuleCall(ParserNode.RecursiveParserNode source)
            {
                if (source == null)
                {
                    throw new ArgumentNullException();
                }

                return(new StatefullStackNode(this, -1, source));
            }
Exemplo n.º 2
0
        /*
         *
         * private void SkipOmitPattern()
         * {
         *  if (_omitRoot != null)
         *  {
         *      string spaces;
         *      _currState.TryRunFsm(((ParserNode.FsmParserNode)((ParserNode.Sequence)_omitRoot).Childs[0]).Fsm, out spaces);
         *  }
         * }
         *
         * void IParserNodeVisitor.VisitFsm(ParserNode.FsmParserNode fsmParserNode)
         * {
         *  if (_currState.PrevNode == fsmParserNode.Parent)
         *  {
         *      this.SkipOmitPattern();
         *
         *      string text;
         *      var from = _currState.Location;
         *      if (_currState.TryRunFsm(fsmParserNode.Fsm, out text))
         *      {
         *          var to = _currState.Location;
         *          _currState = _currState.TerminalParsed(from, to, text).ExitNode();
         *
         *          this.SkipOmitPattern();
         *      }
         *      else
         *      {
         *          _currState = _currState.TerminalFailed().ExitNode();
         *      }
         *  }
         *  else
         *  {
         *      throw new InvalidOperationException();
         *  }
         * }
         *
         */

        void IParserNodeVisitor.VisitRecursive(ParserNode.RecursiveParserNode recursiveParserNode)
        {
            if (_currState.PrevNode == recursiveParserNode.Parent)
            {
                _currState = _currState.EnterNode(recursiveParserNode.Target);
            }
            else if (_currState.PrevNode == recursiveParserNode.Target)
            {
                _currState = _currState.ExitNode();
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
Exemplo n.º 3
0
 private StatefullStackNode(StatefullStackNode prev, int childsCount, ParserNode.RecursiveParserNode source)
 {
     this.Prev            = prev;
     this.ChildsCount     = childsCount;
     this.RecursionSource = source;
 }
Exemplo n.º 4
0
 public void VisitRecursive(ParserNode.RecursiveParserNode recursiveParserNode)
 {
     this.LogLine("{0}", recursiveParserNode); _v.VisitRecursive(recursiveParserNode);
 }
Exemplo n.º 5
0
        void IRuleExpressionVisitor.VisitRuleUsage(RuleExpression.RuleUsage ruleUsage)
        {
            if (_currContext.invocationCount == 0)
            {
                // _log.WriteLine("entering from [{0}] to [{1}]", _currContext.rule == null ? "<NULL>" : _currContext.rule.Name, ruleUsage.RuleName).Push();

                RuleExpression expr;
                if (_nav.TryEnterParameterContext(ruleUsage.RuleName, out expr))
                {
                    _currContext = _currContext.ForChildExpression(expr, _nav.CurrRuleInfo.Rule);
                }
                else
                {
                    GrammarNavigator.RuleInfo ruleInfo;
                    //if (!_nav.TryResolveRule(ruleUsage.RuleName, out ruleInfo))
                    if (!_nav.TryEnterRule(ruleUsage.RuleName, out ruleInfo))
                    {
                        throw new InvalidOperationException(string.Format("Referenced rule [{0}] not found!", ruleUsage.RuleName));
                    }

                    var            rule = ruleInfo.IsExplicitRule ? ruleInfo.Rule : this.ExpandExtensibleRule(ruleInfo);
                    RuleExpression expression;
                    if (this.TryRewriteRecursionCalls(rule, out expression))
                    {
                        ParserNode recursiveTargetNode;
                        if (_cachedMaterializedRewritedRuleEnterByPath.TryGetValue(ruleInfo.NamePath, out recursiveTargetNode))
                        {
                            _nav.Exit();
                            var node = new ParserNode.RecursiveParserNode(rule, ruleUsage);
                            node.SetTarget(recursiveTargetNode);
                            _currContext = _currContext.ForParentExpression(node);
                            return;
                        }
                    }

                    this.SetRuleParamsInfo(rule, ruleUsage);

                    var entryContext = this.FindRuleRecursiveInvocationNode(rule, ruleUsage);


                    if (entryContext != null)
                    {
                        //_log.WriteLine("recursively exiting from [{0}] to [{1}]", ruleUsage.RuleName, _currContext.rule.Name).Pop();
                        _nav.Exit();

                        var node = new ParserNode.RecursiveParserNode(rule, ruleUsage);
                        _recursiveNodes.Add(node);
                        _currContext = _currContext.ForParentExpression(node);
                    }
                    else
                    {
                        _currContext = _currContext.ForChildExpression(expression, rule);
                    }

                    // _nav.Enter(ruleUsage.RuleName);
                }
            }
            else if (_currContext.invocationCount == 1)
            {
                if (_currContext.childEntries.node == null ||
                    _currContext.childEntries.prev != null)
                {
                    throw new InvalidOperationException();
                }

                var ruleNamePath = _nav.CurrRuleInfo.NamePath;

                //_log.WriteLine("exiting from [{0}] to [{1}]", ruleUsage.RuleName, _currContext.rule.Name).Pop();
                _nav.Exit();

                var node = new ParserNode.RuleCall(
                    _currContext.rule, _currContext.expression, _currContext.childEntries.node
                    );

                if (!_cachedMaterializedRewritedRuleEnterByPath.ContainsKey(ruleNamePath))
                {
                    _cachedMaterializedRewritedRuleEnterByPath.Add(ruleNamePath, node);
                }

                _currContext = _currContext.ForParentExpression(node);
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
Exemplo n.º 6
0
 void IParserNodeVisitor.VisitRecursive(ParserNode.RecursiveParserNode recursiveParserNode)
 {
     this.PrintNode(recursiveParserNode);
 }