org.antlr.v4.runtime.Parser.consume()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(216)

本文整理了Java中org.antlr.v4.runtime.Parser.consume()方法的一些代码示例,展示了Parser.consume()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Parser.consume()方法的具体详情如下:
包路径:org.antlr.v4.runtime.Parser
类名称:Parser
方法名:consume

Parser.consume介绍

[英]Consume and return the #getCurrentToken.

E.g., given the following input with A being the current lookahead symbol, this function moves the cursor to B and returns A.

A B 
^

If the parser is not in error recovery mode, the consumed symbol is added to the parse tree using ParserRuleContext#addChild(Token), and ParseTreeListener#visitTerminal is called on any parse listeners. If the parser is in error recovery mode, the consumed symbol is added to the parse tree using ParserRuleContext#addErrorNode(Token), and ParseTreeListener#visitErrorNode is called on any parse listeners.
[中]使用并返回#getCurrentToken。
例如,给定以下输入,A是当前的先行符号,此函数将光标移动到B并返回A。

A B 
^

如果解析器未处于错误恢复模式,则使用ParserRunContext#addChild(令牌)将使用的符号添加到解析树中,并在任何解析侦听器上调用ParserTreeListener#VisiterMinal。如果解析器*处于错误恢复模式,则使用ParserRuleContext#addErrorNode(令牌)将消耗的符号添加到解析树中,并在任何解析侦听器上调用ParserTreeListener#visitErrorNode。

代码示例

代码示例来源:origin: apache/incubator-shardingsphere

/**
   * Get matched token by token type.
   *
   * @param tokenType token type
   * @return matched token
   * @throws RecognitionException mismatch throw exception
   */
  public Token getMatchedToken(final int tokenType) throws RecognitionException {
    Token result = parser.getCurrentToken();
    boolean isIdentifierCompatible = false;
    if (identifierTokenIndex == tokenType && identifierTokenIndex > result.getType()) {
      isIdentifierCompatible = true;
    }
    if (result.getType() == tokenType || isIdentifierCompatible) {
      if (Token.EOF != tokenType && isIdentifierCompatible && result instanceof CommonToken) {
        ((CommonToken) result).setType(identifierTokenIndex);
      }
      parser.getErrorHandler().reportMatch(parser);
      parser.consume();
    } else {
      result = parser.getErrorHandler().recoverInline(parser);
      if (parser.getBuildParseTree() && -1 == result.getTokenIndex()) {
        parser.getContext().addErrorNode(parser.createErrorNode(parser.getContext(), result));
      }
    }
    return result;
  }
}

代码示例来源:origin: org.antlr/antlr4-runtime

/** Consume tokens until one matches the given token set. */
  protected void consumeUntil(Parser recognizer, IntervalSet set) {
//        System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
    int ttype = recognizer.getInputStream().LA(1);
    while (ttype != Token.EOF && !set.contains(ttype) ) {
      //System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
//            recognizer.getInputStream().consume();
      recognizer.consume();
      ttype = recognizer.getInputStream().LA(1);
    }
  }
}

代码示例来源:origin: org.antlr/antlr4-runtime

if (t.getType() > 0) {
  _errHandler.reportMatch(this);
  consume();

代码示例来源:origin: org.antlr/antlr4-runtime

recognizer.consume(); // simply delete extra token

代码示例来源:origin: org.antlr/antlr4-runtime

consume();

代码示例来源:origin: org.antlr/antlr4-runtime

recognizer.consume();
return matchedSymbol;

代码示例来源:origin: org.antlr/antlr4-runtime

recognizer.consume();

代码示例来源:origin: net.sf.cssbox/jstyleparser

/**
 * Consumes token until lexer state is balanced and
 * token from follow is matched. Matched token is also consumed
 */
protected void consumeUntilGreedy(Parser recognizer, IntervalSet follow) {
  logger.trace("CONSUME UNTIL GREEDY {}", follow.toString());
  for (int ttype = recognizer.getInputStream().LA(1); ttype != -1 && !follow.contains(ttype); ttype = recognizer.getInputStream().LA(1)) {
    Token t = recognizer.consume();
    logger.trace("Skipped greedy: {}", t.getText());
  }
  Token t = recognizer.consume();
  logger.trace("Skipped greedy: {} follow: {}", t.getText(), follow);
}

代码示例来源:origin: antlr/intellij-plugin-v4

@Override
  protected void consumeUntil(Parser recognizer, IntervalSet set) {
//        System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
    int ttype = recognizer.getInputStream().LA(1);
    while (ttype != Token.EOF ) {
      //System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
//            recognizer.getInputStream().consume();
      recognizer.consume();
      ttype = recognizer.getInputStream().LA(1);
    }
  }
}

代码示例来源:origin: radkovo/jStyleParser

/**
 * Consumes token until lexer state is balanced and
 * token from follow is matched. Matched token is also consumed
 */
protected void consumeUntilGreedy(Parser recognizer, IntervalSet follow) {
  logger.trace("CONSUME UNTIL GREEDY {}", follow.toString());
  for (int ttype = recognizer.getInputStream().LA(1); ttype != -1 && !follow.contains(ttype); ttype = recognizer.getInputStream().LA(1)) {
    Token t = recognizer.consume();
    logger.trace("Skipped greedy: {}", t.getText());
  }
  Token t = recognizer.consume();
  logger.trace("Skipped greedy: {} follow: {}", t.getText(), follow);
}

代码示例来源:origin: uk.co.nichesolutions/antlr4-runtime

/** Consume tokens until one matches the given token set. */
  protected void consumeUntil(Parser recognizer, IntervalSet set) {
//        System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
    int ttype = recognizer.getInputStream().LA(1);
    while (ttype != Token.EOF && !set.contains(ttype) ) {
      //System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
//            recognizer.getInputStream().consume();
      recognizer.consume();
      ttype = recognizer.getInputStream().LA(1);
    }
  }
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

/** Consume tokens until one matches the given token set. */
  protected void consumeUntil(Parser recognizer, IntervalSet set) {
//        System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
    int ttype = recognizer.getInputStream().LA(1);
    while (ttype != Token.EOF && !set.contains(ttype) ) {
      //System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
//            recognizer.getInputStream().consume();
      recognizer.consume();
      ttype = recognizer.getInputStream().LA(1);
    }
  }
}

代码示例来源:origin: io.virtdata/virtdata-lib-realer

/** Consume tokens until one matches the given token set. */
  protected void consumeUntil(Parser recognizer, IntervalSet set) {
//        System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
    int ttype = recognizer.getInputStream().LA(1);
    while (ttype != Token.EOF && !set.contains(ttype) ) {
      //System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
//            recognizer.getInputStream().consume();
      recognizer.consume();
      ttype = recognizer.getInputStream().LA(1);
    }
  }
}

代码示例来源:origin: com.tunnelvisionlabs/antlr4-runtime

/** Consume tokens until one matches the given token set. */
  protected void consumeUntil(@NotNull Parser recognizer, @NotNull IntervalSet set) {
//        System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
    int ttype = recognizer.getInputStream().LA(1);
    while (ttype != Token.EOF && !set.contains(ttype) ) {
      //System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
//            recognizer.getInputStream().consume();
      recognizer.consume();
      ttype = recognizer.getInputStream().LA(1);
    }
  }
}

代码示例来源:origin: org.antlr/antlr4

@Override
    public void recover(Parser recognizer, RecognitionException e) {
      int errIndex = recognizer.getInputStream().index();
      if ( firstErrorTokenIndex == -1 ) {
        firstErrorTokenIndex = errIndex; // latch
      }
//            System.err.println("recover: error at " + errIndex);
      TokenStream input = recognizer.getInputStream();
      if ( input.index()<input.size()-1 ) { // don't consume() eof
        recognizer.consume(); // just kill this bad token and let it continue.
      }
    }

代码示例来源:origin: com.tunnelvisionlabs/antlr4

@Override
    public void recover(Parser recognizer, RecognitionException e) {
      int errIndex = recognizer.getInputStream().index();
      if ( firstErrorTokenIndex == -1 ) {
        firstErrorTokenIndex = errIndex; // latch
      }
//            System.err.println("recover: error at " + errIndex);
      TokenStream input = recognizer.getInputStream();
      if ( input.index()<input.size()-1 ) { // don't consume() eof
        recognizer.consume(); // just kill this bad token and let it continue.
      }
    }

代码示例来源:origin: uk.co.nichesolutions/antlr4

@Override
    public void recover(Parser recognizer, RecognitionException e) {
      int errIndex = recognizer.getInputStream().index();
      if ( firstErrorTokenIndex == -1 ) {
        firstErrorTokenIndex = errIndex; // latch
      }
//            System.err.println("recover: error at " + errIndex);
      TokenStream input = recognizer.getInputStream();
      if ( input.index()<input.size()-1 ) { // don't consume() eof
        recognizer.consume(); // just kill this bad token and let it continue.
      }
    }

代码示例来源:origin: antlr/intellij-plugin-v4

@Override
    public void recover(Parser recognizer, RecognitionException e) {
      int errIndex = recognizer.getInputStream().index();
      if ( firstErrorTokenIndex == -1 ) {
        firstErrorTokenIndex = errIndex; // latch
      }
//            System.err.println("recover: error at " + errIndex);
      TokenStream input = recognizer.getInputStream();
      if ( input.index()<input.size()-1 ) { // don't consume() eof
        recognizer.consume(); // just kill this bad token and let it continue.
      }
    }

代码示例来源:origin: io.virtdata/virtdata-lib-realer

@Override
    public void recover(Parser recognizer, RecognitionException e) {
      int errIndex = recognizer.getInputStream().index();
      if ( firstErrorTokenIndex == -1 ) {
        firstErrorTokenIndex = errIndex; // latch
      }
//            System.err.println("recover: error at " + errIndex);
      TokenStream input = recognizer.getInputStream();
      if ( input.index()<input.size()-1 ) { // don't consume() eof
        recognizer.consume(); // just kill this bad token and let it continue.
      }
    }

代码示例来源:origin: org.ballerinalang/language-server-core

private void removePendingTokensAfterThisToken(Parser recognizer, Token token,
                        TokenRemovalStrategy currentStrategy) {
  int currentTokenIndex = recognizer.getCurrentToken().getTokenIndex();
  if (token != null && (isInLastTermination(recognizer) ||
      TokenRemovalStrategy.SYNC.equals(currentStrategy) && currentTokenIndex < token.getTokenIndex()
      || TokenRemovalStrategy.MATCH.equals(currentStrategy) && currentTokenIndex <= token.getTokenIndex())) {
    return;
  }
  while (removeTokenCount > 0) {
    forceConsumedTokens.push(recognizer.consume());
    removeTokenCount--;
  }
  this.context.put(CompletionKeys.FORCE_CONSUMED_TOKENS_KEY, forceConsumedTokens);
}

相关文章