Package com.google.dart.engine.internal.parser

Examples of com.google.dart.engine.internal.parser.Modifiers


   * @return the class member that was parsed, or {@code null} if what was found was not a valid
   *         class member
   */
  protected ClassMember parseClassMember(String className) {
    CommentAndMetadata commentAndMetadata = parseCommentAndMetadata();
    Modifiers modifiers = parseModifiers();
    if (matchesKeyword(Keyword.VOID)) {
      TypeName returnType = parseReturnType();
      if (matchesKeyword(Keyword.GET) && tokenMatchesIdentifier(peek())) {
        validateModifiersForGetterOrSetterOrMethod(modifiers);
        return parseGetter(
            commentAndMetadata,
            modifiers.getExternalKeyword(),
            modifiers.getStaticKeyword(),
            returnType);
      } else if (matchesKeyword(Keyword.SET) && tokenMatchesIdentifier(peek())) {
        validateModifiersForGetterOrSetterOrMethod(modifiers);
        return parseSetter(
            commentAndMetadata,
            modifiers.getExternalKeyword(),
            modifiers.getStaticKeyword(),
            returnType);
      } else if (matchesKeyword(Keyword.OPERATOR) && isOperator(peek())) {
        validateModifiersForOperator(modifiers);
        return parseOperator(commentAndMetadata, modifiers.getExternalKeyword(), returnType);
      } else if (matchesIdentifier()
          && peek().matchesAny(
              TokenType.OPEN_PAREN,
              TokenType.OPEN_CURLY_BRACKET,
              TokenType.FUNCTION)) {
        validateModifiersForGetterOrSetterOrMethod(modifiers);
        return parseMethodDeclarationAfterReturnType(
            commentAndMetadata,
            modifiers.getExternalKeyword(),
            modifiers.getStaticKeyword(),
            returnType);
      } else {
        //
        // We have found an error of some kind. Try to recover.
        //
        if (matchesIdentifier()) {
          if (peek().matchesAny(TokenType.EQ, TokenType.COMMA, TokenType.SEMICOLON)) {
            //
            // We appear to have a variable declaration with a type of "void".
            //
            reportErrorForNode(ParserErrorCode.VOID_VARIABLE, returnType);
            return parseInitializedIdentifierList(
                commentAndMetadata,
                modifiers.getStaticKeyword(),
                validateModifiersForField(modifiers),
                returnType);
          }
        }
        if (isOperator(currentToken)) {
          //
          // We appear to have found an operator declaration without the 'operator' keyword.
          //
          validateModifiersForOperator(modifiers);
          return parseOperator(commentAndMetadata, modifiers.getExternalKeyword(), returnType);
        }
        reportErrorForToken(ParserErrorCode.EXPECTED_EXECUTABLE, currentToken);
        return null;
      }
    } else if (matchesKeyword(Keyword.GET) && tokenMatchesIdentifier(peek())) {
      validateModifiersForGetterOrSetterOrMethod(modifiers);
      return parseGetter(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          modifiers.getStaticKeyword(),
          null);
    } else if (matchesKeyword(Keyword.SET) && tokenMatchesIdentifier(peek())) {
      validateModifiersForGetterOrSetterOrMethod(modifiers);
      return parseSetter(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          modifiers.getStaticKeyword(),
          null);
    } else if (matchesKeyword(Keyword.OPERATOR) && isOperator(peek())) {
      validateModifiersForOperator(modifiers);
      return parseOperator(commentAndMetadata, modifiers.getExternalKeyword(), null);
    } else if (!matchesIdentifier()) {
      if (isOperator(currentToken)) {
        //
        // We appear to have found an operator declaration without the 'operator' keyword.
        //
        validateModifiersForOperator(modifiers);
        return parseOperator(commentAndMetadata, modifiers.getExternalKeyword(), null);
      }
      Token keyword = modifiers.getVarKeyword();
      if (keyword == null) {
        keyword = modifiers.getFinalKeyword();
      }
      if (keyword == null) {
        keyword = modifiers.getConstKeyword();
      }
      if (keyword != null) {
        //
        // We appear to have found an incomplete field declaration.
        //
        reportErrorForCurrentToken(ParserErrorCode.MISSING_IDENTIFIER);
        ArrayList<VariableDeclaration> variables = new ArrayList<VariableDeclaration>();
        variables.add(new VariableDeclaration(null, null, createSyntheticIdentifier(), null, null));
        return new FieldDeclaration(
            commentAndMetadata.getComment(),
            commentAndMetadata.getMetadata(),
            null,
            new VariableDeclarationList(null, null, keyword, null, variables),
            expectSemicolon());
      }
      reportErrorForToken(ParserErrorCode.EXPECTED_CLASS_MEMBER, currentToken);
      if (commentAndMetadata.getComment() != null || !commentAndMetadata.getMetadata().isEmpty()) {
        //
        // We appear to have found an incomplete declaration at the end of the class. At this point
        // it consists of a metadata, which we don't want to loose, so we'll treat it as a method
        // declaration with a missing name, parameters and empty body.
        //
        return new MethodDeclaration(
            commentAndMetadata.getComment(),
            commentAndMetadata.getMetadata(),
            null,
            null,
            null,
            null,
            null,
            createSyntheticIdentifier(),
            new FormalParameterList(null, new ArrayList<FormalParameter>(), null, null, null),
            new EmptyFunctionBody(createSyntheticToken(TokenType.SEMICOLON)));
      }
      return null;
    } else if (tokenMatches(peek(), TokenType.PERIOD) && tokenMatchesIdentifier(peekAt(2))
        && tokenMatches(peekAt(3), TokenType.OPEN_PAREN)) {
      return parseConstructor(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          validateModifiersForConstructor(modifiers),
          modifiers.getFactoryKeyword(),
          parseSimpleIdentifier(),
          getAndAdvance(),
          parseSimpleIdentifier(),
          parseFormalParameterList());
    } else if (tokenMatches(peek(), TokenType.OPEN_PAREN)) {
      SimpleIdentifier methodName = parseSimpleIdentifier();
      FormalParameterList parameters = parseFormalParameterList();
      if (matches(TokenType.COLON) || modifiers.getFactoryKeyword() != null
          || methodName.getName().equals(className)) {
        return parseConstructor(
            commentAndMetadata,
            modifiers.getExternalKeyword(),
            validateModifiersForConstructor(modifiers),
            modifiers.getFactoryKeyword(),
            methodName,
            null,
            null,
            parameters);
      }
      validateModifiersForGetterOrSetterOrMethod(modifiers);
      validateFormalParameterList(parameters);
      return parseMethodDeclarationAfterParameters(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          modifiers.getStaticKeyword(),
          null,
          methodName,
          parameters);
    } else if (peek().matchesAny(TokenType.EQ, TokenType.COMMA, TokenType.SEMICOLON)) {
      if (modifiers.getConstKeyword() == null && modifiers.getFinalKeyword() == null
          && modifiers.getVarKeyword() == null) {
        reportErrorForCurrentToken(ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE);
      }
      return parseInitializedIdentifierList(
          commentAndMetadata,
          modifiers.getStaticKeyword(),
          validateModifiersForField(modifiers),
          null);
    }
    TypeName type = parseTypeName();
    if (matchesKeyword(Keyword.GET) && tokenMatchesIdentifier(peek())) {
      validateModifiersForGetterOrSetterOrMethod(modifiers);
      return parseGetter(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          modifiers.getStaticKeyword(),
          type);
    } else if (matchesKeyword(Keyword.SET) && tokenMatchesIdentifier(peek())) {
      validateModifiersForGetterOrSetterOrMethod(modifiers);
      return parseSetter(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          modifiers.getStaticKeyword(),
          type);
    } else if (matchesKeyword(Keyword.OPERATOR) && isOperator(peek())) {
      validateModifiersForOperator(modifiers);
      return parseOperator(commentAndMetadata, modifiers.getExternalKeyword(), type);
    } else if (!matchesIdentifier()) {
      if (matches(TokenType.CLOSE_CURLY_BRACKET)) {
        //
        // We appear to have found an incomplete declaration at the end of the class. At this point
        // it consists of a type name, so we'll treat it as a field declaration with a missing
        // field name and semicolon.
        //
        return parseInitializedIdentifierList(
            commentAndMetadata,
            modifiers.getStaticKeyword(),
            validateModifiersForField(modifiers),
            type);
      }
      if (isOperator(currentToken)) {
        //
        // We appear to have found an operator declaration without the 'operator' keyword.
        //
        validateModifiersForOperator(modifiers);
        return parseOperator(commentAndMetadata, modifiers.getExternalKeyword(), type);
      }
      //
      // We appear to have found an incomplete declaration before another declaration.
      // At this point it consists of a type name, so we'll treat it as a field declaration
      // with a missing field name and semicolon.
      //
      reportErrorForToken(ParserErrorCode.EXPECTED_CLASS_MEMBER, currentToken);
      try {
        lockErrorListener();
        return parseInitializedIdentifierList(
            commentAndMetadata,
            modifiers.getStaticKeyword(),
            validateModifiersForField(modifiers),
            type);
      } finally {
        unlockErrorListener();
      }
    } else if (tokenMatches(peek(), TokenType.OPEN_PAREN)) {
      SimpleIdentifier methodName = parseSimpleIdentifier();
      FormalParameterList parameters = parseFormalParameterList();
      if (methodName.getName().equals(className)) {
        reportErrorForNode(ParserErrorCode.CONSTRUCTOR_WITH_RETURN_TYPE, type);
        return parseConstructor(
            commentAndMetadata,
            modifiers.getExternalKeyword(),
            validateModifiersForConstructor(modifiers),
            modifiers.getFactoryKeyword(),
            methodName,
            null,
            null,
            parameters);
      }
      validateModifiersForGetterOrSetterOrMethod(modifiers);
      validateFormalParameterList(parameters);
      return parseMethodDeclarationAfterParameters(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          modifiers.getStaticKeyword(),
          type,
          methodName,
          parameters);
    } else if (tokenMatches(peek(), TokenType.OPEN_CURLY_BRACKET)) {
      // We have found "TypeName identifier {", and are guessing that this is a getter without the
      // keyword 'get'.
      validateModifiersForGetterOrSetterOrMethod(modifiers);
      reportErrorForCurrentToken(ParserErrorCode.MISSING_GET);
      currentToken = injectToken(new SyntheticKeywordToken(Keyword.GET, currentToken.getOffset()));
      return parseGetter(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          modifiers.getStaticKeyword(),
          type);
    }
    return parseInitializedIdentifierList(
        commentAndMetadata,
        modifiers.getStaticKeyword(),
        validateModifiersForField(modifiers),
        type);
  }
View Full Code Here


   * @param commentAndMetadata the metadata to be associated with the member
   * @return the compilation unit member that was parsed, or {@code null} if what was parsed could
   *         not be represented as a compilation unit member
   */
  private CompilationUnitMember parseCompilationUnitMember(CommentAndMetadata commentAndMetadata) {
    Modifiers modifiers = parseModifiers();
    if (matchesKeyword(Keyword.CLASS)) {
      return parseClassDeclaration(commentAndMetadata, validateModifiersForClass(modifiers));
    } else if (matchesKeyword(Keyword.TYPEDEF) && !tokenMatches(peek(), TokenType.PERIOD)
        && !tokenMatches(peek(), TokenType.LT) && !tokenMatches(peek(), TokenType.OPEN_PAREN)) {
      validateModifiersForTypedef(modifiers);
      return parseTypeAlias(commentAndMetadata);
    } else if (parseEnum && matchesKeyword(Keyword.ENUM)) {
      validateModifiersForEnum(modifiers);
      return parseEnumDeclaration(commentAndMetadata);
    }
    if (matchesKeyword(Keyword.VOID)) {
      TypeName returnType = parseReturnType();
      if ((matchesKeyword(Keyword.GET) || matchesKeyword(Keyword.SET))
          && tokenMatchesIdentifier(peek())) {
        validateModifiersForTopLevelFunction(modifiers);
        return parseFunctionDeclaration(
            commentAndMetadata,
            modifiers.getExternalKeyword(),
            returnType);
      } else if (matchesKeyword(Keyword.OPERATOR) && isOperator(peek())) {
        reportErrorForToken(ParserErrorCode.TOP_LEVEL_OPERATOR, currentToken);
        return convertToFunctionDeclaration(parseOperator(
            commentAndMetadata,
            modifiers.getExternalKeyword(),
            returnType));
      } else if (matchesIdentifier()
          && peek().matchesAny(
              TokenType.OPEN_PAREN,
              TokenType.OPEN_CURLY_BRACKET,
              TokenType.FUNCTION)) {
        validateModifiersForTopLevelFunction(modifiers);
        return parseFunctionDeclaration(
            commentAndMetadata,
            modifiers.getExternalKeyword(),
            returnType);
      } else {
        //
        // We have found an error of some kind. Try to recover.
        //
        if (matchesIdentifier()) {
          if (peek().matchesAny(TokenType.EQ, TokenType.COMMA, TokenType.SEMICOLON)) {
            //
            // We appear to have a variable declaration with a type of "void".
            //
            reportErrorForNode(ParserErrorCode.VOID_VARIABLE, returnType);
            return new TopLevelVariableDeclaration(
                commentAndMetadata.getComment(),
                commentAndMetadata.getMetadata(),
                parseVariableDeclarationListAfterType(
                    null,
                    validateModifiersForTopLevelVariable(modifiers),
                    null), expect(TokenType.SEMICOLON));
          }
        }
        reportErrorForToken(ParserErrorCode.EXPECTED_EXECUTABLE, currentToken);
        return null;
      }
    } else if ((matchesKeyword(Keyword.GET) || matchesKeyword(Keyword.SET))
        && tokenMatchesIdentifier(peek())) {
      validateModifiersForTopLevelFunction(modifiers);
      return parseFunctionDeclaration(commentAndMetadata, modifiers.getExternalKeyword(), null);
    } else if (matchesKeyword(Keyword.OPERATOR) && isOperator(peek())) {
      reportErrorForToken(ParserErrorCode.TOP_LEVEL_OPERATOR, currentToken);
      return convertToFunctionDeclaration(parseOperator(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          null));
    } else if (!matchesIdentifier()) {
      Token keyword = modifiers.getVarKeyword();
      if (keyword == null) {
        keyword = modifiers.getFinalKeyword();
      }
      if (keyword == null) {
        keyword = modifiers.getConstKeyword();
      }
      if (keyword != null) {
        //
        // We appear to have found an incomplete top-level variable declaration.
        //
        reportErrorForCurrentToken(ParserErrorCode.MISSING_IDENTIFIER);
        ArrayList<VariableDeclaration> variables = new ArrayList<VariableDeclaration>();
        variables.add(new VariableDeclaration(null, null, createSyntheticIdentifier(), null, null));
        return new TopLevelVariableDeclaration(
            commentAndMetadata.getComment(),
            commentAndMetadata.getMetadata(),
            new VariableDeclarationList(null, null, keyword, null, variables),
            expectSemicolon());
      }
      reportErrorForToken(ParserErrorCode.EXPECTED_EXECUTABLE, currentToken);
      return null;
    } else if (tokenMatches(peek(), TokenType.OPEN_PAREN)) {
      validateModifiersForTopLevelFunction(modifiers);
      return parseFunctionDeclaration(commentAndMetadata, modifiers.getExternalKeyword(), null);
    } else if (peek().matchesAny(TokenType.EQ, TokenType.COMMA, TokenType.SEMICOLON)) {
      if (modifiers.getConstKeyword() == null && modifiers.getFinalKeyword() == null
          && modifiers.getVarKeyword() == null) {
        reportErrorForCurrentToken(ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE);
      }
      return new TopLevelVariableDeclaration(
          commentAndMetadata.getComment(),
          commentAndMetadata.getMetadata(),
          parseVariableDeclarationListAfterType(
              null,
              validateModifiersForTopLevelVariable(modifiers),
              null), expect(TokenType.SEMICOLON));
    }
    TypeName returnType = parseReturnType();
    if ((matchesKeyword(Keyword.GET) || matchesKeyword(Keyword.SET))
        && tokenMatchesIdentifier(peek())) {
      validateModifiersForTopLevelFunction(modifiers);
      return parseFunctionDeclaration(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          returnType);
    } else if (matchesKeyword(Keyword.OPERATOR) && isOperator(peek())) {
      reportErrorForToken(ParserErrorCode.TOP_LEVEL_OPERATOR, currentToken);
      return convertToFunctionDeclaration(parseOperator(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          returnType));
    } else if (matches(TokenType.AT)) {
      return new TopLevelVariableDeclaration(
          commentAndMetadata.getComment(),
          commentAndMetadata.getMetadata(),
          parseVariableDeclarationListAfterType(
              null,
              validateModifiersForTopLevelVariable(modifiers),
              returnType), expect(TokenType.SEMICOLON));
    } else if (!matchesIdentifier()) {
      // TODO(brianwilkerson) Generalize this error. We could also be parsing a top-level variable at this point.
      reportErrorForToken(ParserErrorCode.EXPECTED_EXECUTABLE, currentToken);
      Token semicolon;
      if (matches(TokenType.SEMICOLON)) {
        semicolon = getAndAdvance();
      } else {
        semicolon = createSyntheticToken(TokenType.SEMICOLON);
      }
      List<VariableDeclaration> variables = new ArrayList<VariableDeclaration>();
      variables.add(new VariableDeclaration(null, null, createSyntheticIdentifier(), null, null));
      return new TopLevelVariableDeclaration(
          commentAndMetadata.getComment(),
          commentAndMetadata.getMetadata(),
          new VariableDeclarationList(null, null, null, returnType, variables),
          semicolon);
    }
    if (peek().matchesAny(TokenType.OPEN_PAREN, TokenType.FUNCTION, TokenType.OPEN_CURLY_BRACKET)) {
      validateModifiersForTopLevelFunction(modifiers);
      return parseFunctionDeclaration(
          commentAndMetadata,
          modifiers.getExternalKeyword(),
          returnType);
    }
    return new TopLevelVariableDeclaration(
        commentAndMetadata.getComment(),
        commentAndMetadata.getMetadata(),
View Full Code Here

   * </pre>
   *
   * @return the function declaration statement that was parsed
   */
  private Statement parseFunctionDeclarationStatement() {
    Modifiers modifiers = parseModifiers();
    validateModifiersForFunctionDeclarationStatement(modifiers);
    return parseFunctionDeclarationStatementAfterReturnType(
        parseCommentAndMetadata(),
        parseOptionalReturnType());
  }
View Full Code Here

   * </pre>
   *
   * @return the modifiers that were parsed
   */
  private Modifiers parseModifiers() {
    Modifiers modifiers = new Modifiers();
    boolean progress = true;
    while (progress) {
      if (tokenMatches(peek(), TokenType.PERIOD) || tokenMatches(peek(), TokenType.LT)
          || tokenMatches(peek(), TokenType.OPEN_PAREN)) {
        return modifiers;
      }
      if (matchesKeyword(Keyword.ABSTRACT)) {
        if (modifiers.getAbstractKeyword() != null) {
          reportErrorForCurrentToken(ParserErrorCode.DUPLICATED_MODIFIER, currentToken.getLexeme());
          advance();
        } else {
          modifiers.setAbstractKeyword(getAndAdvance());
        }
      } else if (matchesKeyword(Keyword.CONST)) {
        if (modifiers.getConstKeyword() != null) {
          reportErrorForCurrentToken(ParserErrorCode.DUPLICATED_MODIFIER, currentToken.getLexeme());
          advance();
        } else {
          modifiers.setConstKeyword(getAndAdvance());
        }
      } else if (matchesKeyword(Keyword.EXTERNAL) && !tokenMatches(peek(), TokenType.PERIOD)
          && !tokenMatches(peek(), TokenType.LT)) {
        if (modifiers.getExternalKeyword() != null) {
          reportErrorForCurrentToken(ParserErrorCode.DUPLICATED_MODIFIER, currentToken.getLexeme());
          advance();
        } else {
          modifiers.setExternalKeyword(getAndAdvance());
        }
      } else if (matchesKeyword(Keyword.FACTORY) && !tokenMatches(peek(), TokenType.PERIOD)
          && !tokenMatches(peek(), TokenType.LT)) {
        if (modifiers.getFactoryKeyword() != null) {
          reportErrorForCurrentToken(ParserErrorCode.DUPLICATED_MODIFIER, currentToken.getLexeme());
          advance();
        } else {
          modifiers.setFactoryKeyword(getAndAdvance());
        }
      } else if (matchesKeyword(Keyword.FINAL)) {
        if (modifiers.getFinalKeyword() != null) {
          reportErrorForCurrentToken(ParserErrorCode.DUPLICATED_MODIFIER, currentToken.getLexeme());
          advance();
        } else {
          modifiers.setFinalKeyword(getAndAdvance());
        }
      } else if (matchesKeyword(Keyword.STATIC) && !tokenMatches(peek(), TokenType.PERIOD)
          && !tokenMatches(peek(), TokenType.LT)) {
        if (modifiers.getStaticKeyword() != null) {
          reportErrorForCurrentToken(ParserErrorCode.DUPLICATED_MODIFIER, currentToken.getLexeme());
          advance();
        } else {
          modifiers.setStaticKeyword(getAndAdvance());
        }
      } else if (matchesKeyword(Keyword.VAR)) {
        if (modifiers.getVarKeyword() != null) {
          reportErrorForCurrentToken(ParserErrorCode.DUPLICATED_MODIFIER, currentToken.getLexeme());
          advance();
        } else {
          modifiers.setVarKeyword(getAndAdvance());
        }
      } else {
        progress = false;
      }
    }
View Full Code Here

TOP

Related Classes of com.google.dart.engine.internal.parser.Modifiers

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.