Example #1
0
 protected void addContextToParseTree() {
   ParserRuleContext parent = (ParserRuleContext) _ctx.parent;
   // add current context to parent if we have a parent
   if (parent != null) {
     parent.addChild(_ctx);
   }
 }
Example #2
0
 /**
  * Consume and return the {@linkplain #getCurrentToken current symbol}.
  *
  * <p>E.g., given the following input with {@code A} being the current lookahead symbol, this
  * function moves the cursor to {@code B} and returns {@code A}.
  *
  * <pre>
  *  A B
  *  ^
  * </pre>
  *
  * If the parser is not in error recovery mode, the consumed symbol is added to the parse tree
  * using {@link ParserRuleContext#addChild(Token)}, and {@link ParseTreeListener#visitTerminal} 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 {@link ParserRuleContext#addErrorNode(Token)}, and
  * {@link ParseTreeListener#visitErrorNode} is called on any parse listeners.
  */
 public Token consume() {
   Token o = getCurrentToken();
   if (o.getType() != EOF) {
     getInputStream().consume();
   }
   boolean hasListener = _parseListeners != null && !_parseListeners.isEmpty();
   if (_buildParseTrees || hasListener) {
     if (_errHandler.inErrorRecoveryMode(this)) {
       ErrorNode node = _ctx.addErrorNode(o);
       if (_parseListeners != null) {
         for (ParseTreeListener listener : _parseListeners) {
           listener.visitErrorNode(node);
         }
       }
     } else {
       TerminalNode node = _ctx.addChild(o);
       if (_parseListeners != null) {
         for (ParseTreeListener listener : _parseListeners) {
           listener.visitTerminal(node);
         }
       }
     }
   }
   return o;
 }
Example #3
0
 public void enterOuterAlt(ParserRuleContext localctx, int altNum) {
   localctx.setAltNumber(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;
 }
Example #4
0
  /**
   * Like {@link #enterRule} but for recursive rules. Make the current context the child of the
   * incoming localctx.
   */
  public 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(); // simulates rule entry for left-recursive rules
    }
  }
Example #5
0
  public void unrollRecursionContexts(ParserRuleContext _parentctx) {
    _precedenceStack.pop();
    _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);
    }
  }
Example #6
0
 public ErrorNode addErrorNode(Token badToken) {
   ErrorNodeImpl t = new ErrorNodeImpl(badToken);
   addChild(t);
   t.parent = this;
   return t;
 }
Example #7
0
 public TerminalNode addChild(Token matchedToken) {
   TerminalNodeImpl t = new TerminalNodeImpl(matchedToken);
   addChild(t);
   t.parent = this;
   return t;
 }