Ejemplo n.º 1
0
 /*
  * propertyDelegate
  *   : "by" expression
  *   ;
  */
 private void parsePropertyDelegate() {
   assert _at(BY_KEYWORD);
   PsiBuilder.Marker delegate = mark();
   advance(); // BY_KEYWORD
   myExpressionParsing.parseExpression();
   delegate.done(PROPERTY_DELEGATE);
 }
Ejemplo n.º 2
0
  /*
   * initializer
   *   : attributes "this" valueArguments
   *   : attributes constructorInvocation // type parameters may (must?) be omitted
   *   ;
   */
  private void parseInitializer() {
    PsiBuilder.Marker initializer = mark();
    parseAnnotations(false);

    IElementType type;
    if (at(THIS_KEYWORD)) {
      PsiBuilder.Marker mark = mark();
      advance(); // THIS_KEYWORD
      mark.done(THIS_CONSTRUCTOR_REFERENCE);
      type = THIS_CALL;
    } else if (atSet(TYPE_REF_FIRST)) {
      PsiBuilder.Marker reference = mark();
      parseTypeRef();
      reference.done(CONSTRUCTOR_CALLEE);
      type = DELEGATOR_SUPER_CALL;
    } else {
      errorWithRecovery(
          "Expecting constructor call (this(...)) or supertype initializer",
          TokenSet.create(LBRACE, COMMA));
      initializer.drop();
      return;
    }
    myExpressionParsing.parseValueArgumentList();

    initializer.done(type);
  }
Ejemplo n.º 3
0
  /*
   * functionParameterRest
   *   : parameter ("=" element)?
   *   ;
   */
  private boolean parseFunctionParameterRest() {
    boolean noErrors = true;

    // Recovery for the case 'fun foo(Array<String>) {}'
    if (at(IDENTIFIER) && lookahead(1) == LT) {
      error("Parameter name expected");
      parseTypeRef();
      noErrors = false;
    } else {
      expect(IDENTIFIER, "Parameter name expected", PARAMETER_NAME_RECOVERY_SET);

      if (at(COLON)) {
        advance(); // COLON
        parseTypeRef();
      } else {
        errorWithRecovery("Parameters must have type annotation", PARAMETER_NAME_RECOVERY_SET);
        noErrors = false;
      }
    }

    if (at(EQ)) {
      advance(); // EQ
      myExpressionParsing.parseExpression();
    }

    return noErrors;
  }
Ejemplo n.º 4
0
  void parseExpressionCodeFragment() {
    PsiBuilder.Marker marker = mark();
    myExpressionParsing.parseExpression();

    while (!eof()) {
      error("unexpected symbol");
      advance();
    }

    marker.done(EXPRESSION_CODE_FRAGMENT);
  }
Ejemplo n.º 5
0
 /*
  * functionBody
  *   : block
  *   : "=" element
  *   ;
  */
 private void parseFunctionBody() {
   if (at(LBRACE)) {
     parseBlock();
   } else if (at(EQ)) {
     advance(); // EQ
     myExpressionParsing.parseExpression();
     consumeIf(SEMICOLON);
   } else {
     errorAndAdvance("Expecting function body");
   }
 }
Ejemplo n.º 6
0
  /*
   * block
   *   : "{" (expressions)* "}"
   *   ;
   */
  void parseBlock() {
    PsiBuilder.Marker block = mark();

    myBuilder.enableNewlines();
    expect(LBRACE, "Expecting '{' to open a block");

    myExpressionParsing.parseStatements();

    expect(RBRACE, "Expecting '}");
    myBuilder.restoreNewlinesState();

    block.done(BLOCK);
  }
Ejemplo n.º 7
0
  void parseScript() {
    PsiBuilder.Marker fileMarker = mark();

    parsePreamble(false);

    PsiBuilder.Marker scriptMarker = mark();
    parseImportDirectives();

    PsiBuilder.Marker blockMarker = mark();

    myExpressionParsing.parseStatements();

    blockMarker.done(BLOCK);
    scriptMarker.done(SCRIPT);
    fileMarker.done(JET_FILE);
  }
Ejemplo n.º 8
0
  /*
   * annotationEntry
   *   : SimpleName{"."} typeArguments? valueArguments?
   *   ;
   */
  private void parseAnnotationEntry() {
    assert _at(IDENTIFIER);

    PsiBuilder.Marker attribute = mark();

    PsiBuilder.Marker reference = mark();
    PsiBuilder.Marker typeReference = mark();
    parseUserType();
    typeReference.done(TYPE_REFERENCE);
    reference.done(CONSTRUCTOR_CALLEE);

    parseTypeArgumentList();

    if (at(LPAR)) {
      myExpressionParsing.parseValueArgumentList();
    }
    attribute.done(ANNOTATION_ENTRY);
  }
Ejemplo n.º 9
0
  /*
   * attributes delegationSpecifier
   *
   * delegationSpecifier
   *   : constructorInvocation // type and constructor arguments
   *   : userType
   *   : explicitDelegation
   *   ;
   *
   * explicitDelegation
   *   : userType "by" element
   *   ;
   */
  private void parseDelegationSpecifier() {
    PsiBuilder.Marker delegator = mark();
    parseAnnotations(false);

    PsiBuilder.Marker reference = mark();
    parseTypeRef();

    if (at(BY_KEYWORD)) {
      reference.drop();
      advance(); // BY_KEYWORD
      createForByClause(myBuilder).myExpressionParsing.parseExpression();
      delegator.done(DELEGATOR_BY);
    } else if (at(LPAR)) {
      reference.done(CONSTRUCTOR_CALLEE);
      myExpressionParsing.parseValueArgumentList();
      delegator.done(DELEGATOR_SUPER_CALL);
    } else {
      reference.drop();
      delegator.done(DELEGATOR_SUPER_CLASS);
    }
  }
Ejemplo n.º 10
0
  public IElementType parseProperty(boolean local) {
    if (at(VAL_KEYWORD) || at(VAR_KEYWORD)) {
      advance(); // VAL_KEYWORD or VAR_KEYWORD
    } else {
      errorAndAdvance("Expecting 'val' or 'var'");
    }

    boolean typeParametersDeclared =
        at(LT) && parseTypeParameterList(TokenSet.create(IDENTIFIER, EQ, COLON, SEMICOLON));

    TokenSet propertyNameFollow =
        TokenSet.create(
            COLON,
            EQ,
            LBRACE,
            RBRACE,
            SEMICOLON,
            VAL_KEYWORD,
            VAR_KEYWORD,
            FUN_KEYWORD,
            CLASS_KEYWORD);

    myBuilder.disableJoiningComplexTokens();

    // TODO: extract constant
    int lastDot =
        matchTokenStreamPredicate(
            new LastBefore(
                new AtSet(DOT, SAFE_ACCESS),
                new AbstractTokenStreamPredicate() {
                  @Override
                  public boolean matching(boolean topLevel) {
                    if (topLevel && (at(EQ) || at(COLON))) return true;
                    if (topLevel && at(IDENTIFIER)) {
                      IElementType lookahead = lookahead(1);
                      return lookahead != LT
                          && lookahead != DOT
                          && lookahead != SAFE_ACCESS
                          && lookahead != QUEST;
                    }
                    return false;
                  }
                }));

    PsiBuilder.Marker receiver = mark();
    parseReceiverType("property", propertyNameFollow, lastDot);

    boolean multiDeclaration = at(LPAR);
    boolean receiverTypeDeclared = lastDot != -1;

    errorIf(
        receiver,
        multiDeclaration && receiverTypeDeclared,
        "Receiver type is not allowed on a multi-declaration");

    if (multiDeclaration) {
      PsiBuilder.Marker multiDecl = mark();
      parseMultiDeclarationName(propertyNameFollow);
      errorIf(multiDecl, !local, "Multi-declarations are only allowed for local variables/values");
    } else {
      parseFunctionOrPropertyName(receiverTypeDeclared, "property", propertyNameFollow);
    }

    myBuilder.restoreJoiningComplexTokensState();

    if (at(COLON)) {
      PsiBuilder.Marker type = mark();
      advance(); // COLON
      if (!parseIdeTemplate()) {
        parseTypeRef();
      }
      errorIf(type, multiDeclaration, "Type annotations are not allowed on multi-declarations");
    }

    parseTypeConstraintsGuarded(typeParametersDeclared);

    if (local) {
      if (at(BY_KEYWORD)) {
        parsePropertyDelegate();
      } else if (at(EQ)) {
        advance(); // EQ
        myExpressionParsing.parseExpression();
        // "val a = 1; b" must not be an infix call of b on "val ...;"
      }
    } else {
      if (at(BY_KEYWORD)) {
        parsePropertyDelegate();
        consumeIf(SEMICOLON);
      } else if (at(EQ)) {
        advance(); // EQ
        myExpressionParsing.parseExpression();
        consumeIf(SEMICOLON);
      }

      if (parsePropertyGetterOrSetter()) {
        parsePropertyGetterOrSetter();
      }
      if (!atSet(EOL_OR_SEMICOLON, RBRACE)) {
        if (getLastToken() != SEMICOLON) {
          errorUntil("Property getter or setter expected", TokenSet.create(EOL_OR_SEMICOLON));
        }
      } else {
        consumeIf(SEMICOLON);
      }
    }

    return multiDeclaration ? MULTI_VARIABLE_DECLARATION : PROPERTY;
  }