예제 #1
0
        // simulates rule entry for left-recursive rules
        public virtual void UnrollRecursionContexts(ParserRuleContext _parentctx)
        {
            _precedenceStack.RemoveAt(_precedenceStack.Count - 1);
            _ctx.stop = _input.Lt(-1);
            ParserRuleContext retctx = _ctx;

            // save current ctx (return value)
            // unroll so _ctx is as it was before call to recursive method
            if (_parseListeners != null)
            {
                while (_ctx != _parentctx)
                {
                    TriggerExitRuleEvent();
                    _ctx = (ParserRuleContext)_ctx.parent;
                }
            }
            else
            {
                _ctx = _parentctx;
            }
            // hook into tree
            retctx.parent = _parentctx;
            if (_buildParseTrees && _parentctx != null)
            {
                // add return ctx into invoking rule's tree
                _parentctx.AddChild(retctx);
            }
        }
예제 #2
0
 /// <summary>
 /// Consume and return the
 /// <linkplain>
 /// #getCurrentToken
 /// current symbol
 /// </linkplain>
 /// .
 /// <p>E.g., given the following input with
 /// <code>A</code>
 /// being the current
 /// lookahead symbol, this function moves the cursor to
 /// <code>B</code>
 /// and returns
 /// <code>A</code>
 /// .</p>
 /// <pre>
 /// A B
 /// ^
 /// </pre>
 /// If the parser is not in error recovery mode, the consumed symbol is added
 /// to the parse tree using
 /// <see cref="ParserRuleContext.AddChild(IToken)">ParserRuleContext.AddChild(IToken)</see>
 /// , and
 /// <see cref="Antlr4.Runtime.Tree.IParseTreeListener.VisitTerminal(Antlr4.Runtime.Tree.ITerminalNode)">Antlr4.Runtime.Tree.IParseTreeListener.VisitTerminal(Antlr4.Runtime.Tree.ITerminalNode)</see>
 /// is called on any parse listeners.
 /// If the parser <em>is</em> in error recovery mode, the consumed symbol is
 /// added to the parse tree using
 /// <see cref="ParserRuleContext.AddErrorNode(IToken)">ParserRuleContext.AddErrorNode(IToken)</see>
 /// , and
 /// <see cref="Antlr4.Runtime.Tree.IParseTreeListener.VisitErrorNode(Antlr4.Runtime.Tree.IErrorNode)">Antlr4.Runtime.Tree.IParseTreeListener.VisitErrorNode(Antlr4.Runtime.Tree.IErrorNode)</see>
 /// is called on any parse
 /// listeners.
 /// </summary>
 public virtual IToken Consume()
 {
     IToken o = CurrentToken;
     if (o.Type != Eof)
     {
         ((ITokenStream)InputStream).Consume();
     }
     bool hasListener = _parseListeners != null && _parseListeners.Count != 0;
     if (_buildParseTrees || hasListener)
     {
         if (_errHandler.InErrorRecoveryMode(this))
         {
             IErrorNode node = _ctx.AddErrorNode(o);
             if (_parseListeners != null)
             {
                 foreach (IParseTreeListener listener in _parseListeners)
                 {
                     listener.VisitErrorNode(node);
                 }
             }
         }
         else
         {
             ITerminalNode node = _ctx.AddChild(o);
             if (_parseListeners != null)
             {
                 foreach (IParseTreeListener listener in _parseListeners)
                 {
                     listener.VisitTerminal(node);
                 }
             }
         }
     }
     return o;
 }
예제 #3
0
 protected internal virtual void AddContextToParseTree()
 {
     ParserRuleContext parent = (ParserRuleContext)_ctx.parent;
     // add current context to parent if we have a parent
     if (parent != null)
     {
         parent.AddChild(_ctx);
     }
 }
예제 #4
0
 public virtual void EnterOuterAlt(ParserRuleContext localctx, int altNum)
 {
     // if we have new localctx, make sure we replace existing ctx
     // that is previous child of parse tree
     if (_buildParseTrees && _ctx != localctx)
     {
         ParserRuleContext parent = (ParserRuleContext)_ctx.parent;
         if (parent != null)
         {
             parent.RemoveLastChild();
             parent.AddChild(localctx);
         }
     }
     _ctx = localctx;
 }
예제 #5
0
 // simulates rule entry for left-recursive rules
 /// <summary>
 /// Like
 /// <see cref="EnterRule(ParserRuleContext, int, int)">EnterRule(ParserRuleContext, int, int)</see>
 /// but for recursive rules.
 /// </summary>
 public virtual void PushNewRecursionContext(ParserRuleContext localctx, int state, int ruleIndex)
 {
     ParserRuleContext previous = _ctx;
     previous.parent = localctx;
     previous.invokingState = state;
     previous.stop = _input.Lt(-1);
     _ctx = localctx;
     _ctx.start = previous.start;
     if (_buildParseTrees)
     {
         _ctx.AddChild(previous);
     }
     if (_parseListeners != null)
     {
         TriggerEnterRuleEvent();
     }
 }
예제 #6
0
 public virtual void EnterLeftFactoredRule(ParserRuleContext localctx, int state, int ruleIndex)
 {
     State = state;
     if (_buildParseTrees)
     {
         ParserRuleContext factoredContext = (ParserRuleContext)_ctx.GetChild(_ctx.ChildCount - 1);
         _ctx.RemoveLastChild();
         factoredContext.parent = localctx;
         localctx.AddChild(factoredContext);
     }
     _ctx       = localctx;
     _ctx.start = _input.Lt(1);
     if (_buildParseTrees)
     {
         AddContextToParseTree();
     }
     if (_parseListeners != null)
     {
         TriggerEnterRuleEvent();
     }
 }