SSLR Toolkit中next()方法的案例

时间:2019-07-11 12:40:34

标签: java sonarqube grammar

我不明白在这种情况下为什么要使用next()方法:

b.rule(SUITE).is(b.firstOf(
      b.sequence(STMT_LIST, b.firstOf(NEWLINE, b.next(EOF), /* (Godin): no newline at the end of file */ b.next(DEDENT))),
      b.sequence(NEWLINE, INDENT, b.oneOrMore(STATEMENT), DEDENT)));

对我来说,它是一样的:

b.rule(SUITE).is(b.firstOf(
      b.sequence(STMT_LIST, b.firstOf(NEWLINE, EOF, /* (Godin): no newline at the end of file */ DEDENT)),
      b.sequence(NEWLINE, INDENT, b.oneOrMore(STATEMENT), DEDENT)));

我想念什么?

PS:这是next(),firstOf()和sequence()文档

 /**
   * Creates parsing expression - "next".
   * During execution of this expression parser will execute sub-expression once.
   * This expression succeeds only if sub-expression succeeds, but never consumes any input.
   *
   * @param e  sub-expression
   * @throws IllegalArgumentException if given argument is not a parsing expression
   */
  public final Object next(Object e) {
    return new NextExpression(convertToExpression(e));
  }

/**
   * Creates parsing expression - "first of".
   * During the execution of this expression parser execute sub-expressions in order until one succeeds.
   * This expressions succeeds if any sub-expression succeeds.
   * <p>
   * Be aware that in expression {@code firstOf("foo", sequence("foo", "bar"))} second sub-expression will never be executed.
   *
   * @param e1  first sub-expression
   * @param e2  second sub-expression
   * @throws IllegalArgumentException if any of given arguments is not a parsing expression
   */
  public final Object firstOf(Object e1, Object e2) {
    return new FirstOfExpression(convertToExpression(e1), convertToExpression(e2));
  }

 /**
   * Creates parsing expression - "sequence".
   * During execution of this expression parser will sequentially execute all sub-expressions.
   * This expression succeeds only if all sub-expressions succeed.
   *
   * @param e1  first sub-expression
   * @param e2  second sub-expression
   * @throws IllegalArgumentException if any of given arguments is not a parsing expression
   */
  public final Object sequence(Object e1, Object e2) {
    return new SequenceExpression(convertToExpression(e1), convertToExpression(e2));
  }

希望您能提供帮助:)

0 个答案:

没有答案