Package com.google.dart.engine.scanner

Examples of com.google.dart.engine.scanner.Token


      expression = new SuperExpression(getAndAdvance());
    } else {
      expression = parseBitwiseAndExpression();
    }
    while (matches(TokenType.CARET)) {
      Token operator = getAndAdvance();
      expression = new BinaryExpression(expression, operator, parseBitwiseAndExpression());
    }
    return expression;
  }
View Full Code Here


   * </pre>
   *
   * @return the break statement that was parsed
   */
  private Statement parseBreakStatement() {
    Token breakKeyword = expectKeyword(Keyword.BREAK);
    SimpleIdentifier label = null;
    if (matchesIdentifier()) {
      label = parseSimpleIdentifier();
    }
    if (!inLoop && !inSwitch && label == null) {
      reportErrorForToken(ParserErrorCode.BREAK_OUTSIDE_OF_LOOP, breakKeyword);
    }
    Token semicolon = expect(TokenType.SEMICOLON);
    return new BreakStatement(breakKeyword, label, semicolon);
  }
View Full Code Here

   * </pre>
   *
   * @return the expression representing the cascaded method invocation
   */
  private Expression parseCascadeSection() {
    Token period = expect(TokenType.PERIOD_PERIOD);
    Expression expression = null;
    SimpleIdentifier functionName = null;
    if (matchesIdentifier()) {
      functionName = parseSimpleIdentifier();
    } else if (currentToken.getType() == TokenType.OPEN_SQUARE_BRACKET) {
      Token leftBracket = getAndAdvance();
      boolean wasInInitializer = inInitializer;
      inInitializer = false;
      try {
        Expression index = parseExpression();
        Token rightBracket = expect(TokenType.CLOSE_SQUARE_BRACKET);
        expression = new IndexExpression(period, leftBracket, index, rightBracket);
        period = null;
      } finally {
        inInitializer = wasInInitializer;
      }
    } else {
      reportErrorForToken(
          ParserErrorCode.MISSING_IDENTIFIER,
          currentToken,
          currentToken.getLexeme());
      functionName = createSyntheticIdentifier();
    }
    assert (expression == null && functionName != null)
        || (expression != null && functionName == null);
    if (currentToken.getType() == TokenType.OPEN_PAREN) {
      while (currentToken.getType() == TokenType.OPEN_PAREN) {
        if (functionName != null) {
          expression = new MethodInvocation(expression, period, functionName, parseArgumentList());
          period = null;
          functionName = null;
        } else if (expression == null) {
          // It should not be possible to get here.
          expression = new MethodInvocation(
              expression,
              period,
              createSyntheticIdentifier(),
              parseArgumentList());
        } else {
          expression = new FunctionExpressionInvocation(expression, parseArgumentList());
        }
      }
    } else if (functionName != null) {
      expression = new PropertyAccess(expression, period, functionName);
      period = null;
    }
    assert expression != null;
    boolean progress = true;
    while (progress) {
      progress = false;
      Expression selector = parseAssignableSelector(expression, true);
      if (selector != expression) {
        expression = selector;
        progress = true;
        while (currentToken.getType() == TokenType.OPEN_PAREN) {
          if (expression instanceof PropertyAccess) {
            PropertyAccess propertyAccess = (PropertyAccess) expression;
            expression = new MethodInvocation(
                propertyAccess.getTarget(),
                propertyAccess.getOperator(),
                propertyAccess.getPropertyName(),
                parseArgumentList());
          } else {
            expression = new FunctionExpressionInvocation(expression, parseArgumentList());
          }
        }
      }
    }
    if (currentToken.getType().isAssignmentOperator()) {
      Token operator = getAndAdvance();
      ensureAssignable(expression);
      expression = new AssignmentExpression(expression, operator, parseExpressionWithoutCascade());
    }
    return expression;
  }
View Full Code Here

   *          not given
   * @return the class declaration that was parsed
   */
  private CompilationUnitMember parseClassDeclaration(CommentAndMetadata commentAndMetadata,
      Token abstractKeyword) {
    Token keyword = expectKeyword(Keyword.CLASS);

    if (matchesIdentifier()) {
      Token next = peek();
      if (tokenMatches(next, TokenType.LT)) {
        next = skipTypeParameterList(next);
        if (next != null && tokenMatches(next, TokenType.EQ)) {
          return parseClassTypeAlias(commentAndMetadata, abstractKeyword, keyword);
        }
      } else if (tokenMatches(next, TokenType.EQ)) {
        return parseClassTypeAlias(commentAndMetadata, abstractKeyword, keyword);
      }
    }

    SimpleIdentifier name = parseSimpleIdentifier();
    String className = name.getName();

    TypeParameterList typeParameters = null;
    if (matches(TokenType.LT)) {
      typeParameters = parseTypeParameterList();
    }
    //
    // Parse the clauses. The parser accepts clauses in any order, but will generate errors if they
    // are not in the order required by the specification.
    //
    ExtendsClause extendsClause = null;
    WithClause withClause = null;
    ImplementsClause implementsClause = null;
    boolean foundClause = true;
    while (foundClause) {
      if (matchesKeyword(Keyword.EXTENDS)) {
        if (extendsClause == null) {
          extendsClause = parseExtendsClause();
          if (withClause != null) {
            reportErrorForToken(ParserErrorCode.WITH_BEFORE_EXTENDS, withClause.getWithKeyword());
          } else if (implementsClause != null) {
            reportErrorForToken(
                ParserErrorCode.IMPLEMENTS_BEFORE_EXTENDS,
                implementsClause.getKeyword());
          }
        } else {
          reportErrorForToken(ParserErrorCode.MULTIPLE_EXTENDS_CLAUSES, extendsClause.getKeyword());
          parseExtendsClause();
        }
      } else if (matchesKeyword(Keyword.WITH)) {
        if (withClause == null) {
          withClause = parseWithClause();
          if (implementsClause != null) {
            reportErrorForToken(
                ParserErrorCode.IMPLEMENTS_BEFORE_WITH,
                implementsClause.getKeyword());
          }
        } else {
          reportErrorForToken(ParserErrorCode.MULTIPLE_WITH_CLAUSES, withClause.getWithKeyword());
          parseWithClause();
          // TODO(brianwilkerson) Should we merge the list of applied mixins into a single list?
        }
      } else if (matchesKeyword(Keyword.IMPLEMENTS)) {
        if (implementsClause == null) {
          implementsClause = parseImplementsClause();
        } else {
          reportErrorForToken(
              ParserErrorCode.MULTIPLE_IMPLEMENTS_CLAUSES,
              implementsClause.getKeyword());
          parseImplementsClause();
          // TODO(brianwilkerson) Should we merge the list of implemented classes into a single list?
        }
      } else {
        foundClause = false;
      }
    }
    if (withClause != null && extendsClause == null) {
      reportErrorForToken(ParserErrorCode.WITH_WITHOUT_EXTENDS, withClause.getWithKeyword());
    }
    //
    // Look for and skip over the extra-lingual 'native' specification.
    //
    NativeClause nativeClause = null;
    if (matchesString(NATIVE) && tokenMatches(peek(), TokenType.STRING)) {
      nativeClause = parseNativeClause();
    }
    //
    // Parse the body of the class.
    //
    Token leftBracket = null;
    List<ClassMember> members = null;
    Token rightBracket = null;
    if (matches(TokenType.OPEN_CURLY_BRACKET)) {
      leftBracket = expect(TokenType.OPEN_CURLY_BRACKET);
      members = parseClassMembers(className, getEndToken(leftBracket));
      rightBracket = expect(TokenType.CLOSE_CURLY_BRACKET);
    } else {
View Full Code Here

   *          is missing
   * @return the list of class members that were parsed
   */
  private List<ClassMember> parseClassMembers(String className, Token closingBracket) {
    List<ClassMember> members = new ArrayList<ClassMember>();
    Token memberStart = currentToken;
    while (!matches(TokenType.EOF)
        && !matches(TokenType.CLOSE_CURLY_BRACKET)
        && (closingBracket != null || (!matchesKeyword(Keyword.CLASS) && !matchesKeyword(Keyword.TYPEDEF)))) {
      if (matches(TokenType.SEMICOLON)) {
        reportErrorForToken(
View Full Code Here

    SimpleIdentifier className = parseSimpleIdentifier();
    TypeParameterList typeParameters = null;
    if (matches(TokenType.LT)) {
      typeParameters = parseTypeParameterList();
    }
    Token equals = expect(TokenType.EQ);
    TypeName superclass = parseTypeName();
    WithClause withClause = null;
    if (matchesKeyword(Keyword.WITH)) {
      withClause = parseWithClause();
    } else {
      reportErrorForCurrentToken(ParserErrorCode.EXPECTED_TOKEN, Keyword.WITH.getSyntax());
    }
    ImplementsClause implementsClause = null;
    if (matchesKeyword(Keyword.IMPLEMENTS)) {
      implementsClause = parseImplementsClause();
    }
    Token semicolon;
    if (matches(TokenType.SEMICOLON)) {
      semicolon = getAndAdvance();
    } else {
      if (matches(TokenType.OPEN_CURLY_BRACKET)) {
        reportErrorForCurrentToken(ParserErrorCode.EXPECTED_TOKEN, TokenType.SEMICOLON.getLexeme());
        Token leftBracket = getAndAdvance();
        parseClassMembers(className.getName(), getEndToken(leftBracket));
        expect(TokenType.CLOSE_CURLY_BRACKET);
      } else {
        reportErrorForToken(
            ParserErrorCode.EXPECTED_TOKEN,
View Full Code Here

   * @return the combinators that were parsed
   */
  private List<Combinator> parseCombinators() {
    List<Combinator> combinators = new ArrayList<Combinator>();
    while (matchesString(SHOW) || matchesString(HIDE)) {
      Token keyword = expect(TokenType.IDENTIFIER);
      if (keyword.getLexeme().equals(SHOW)) {
        List<SimpleIdentifier> shownNames = parseIdentifierList();
        combinators.add(new ShowCombinator(keyword, shownNames));
      } else {
        List<SimpleIdentifier> hiddenNames = parseIdentifierList();
        combinators.add(new HideCombinator(keyword, hiddenNames));
View Full Code Here

   * @return the comment reference that was parsed, or {@code null} if no reference could be found
   */
  private CommentReference parseCommentReference(String referenceSource, int sourceOffset) {
    // TODO(brianwilkerson) The errors are not getting the right offset/length and are being duplicated.
    if (referenceSource.length() == 0) {
      Token syntheticToken = new SyntheticStringToken(TokenType.IDENTIFIER, "", sourceOffset);
      return new CommentReference(null, new SimpleIdentifier(syntheticToken));
    }
    try {
      BooleanErrorListener listener = new BooleanErrorListener();
      Scanner scanner = new Scanner(
          null,
          new SubSequenceReader(referenceSource, sourceOffset),
          listener);
      scanner.setSourceStart(1, 1);
      Token firstToken = scanner.tokenize();
      if (listener.getErrorReported()) {
        return null;
      }
      Token newKeyword = null;
      if (tokenMatchesKeyword(firstToken, Keyword.NEW)) {
        newKeyword = firstToken;
        firstToken = firstToken.getNext();
      }
      if (tokenMatchesIdentifier(firstToken)) {
        Token secondToken = firstToken.getNext();
        Token thirdToken = secondToken.getNext();
        Token nextToken;
        Identifier identifier;
        if (tokenMatches(secondToken, TokenType.PERIOD) && tokenMatchesIdentifier(thirdToken)) {
          identifier = new PrefixedIdentifier(
              new SimpleIdentifier(firstToken),
              secondToken,
              new SimpleIdentifier(thirdToken));
          nextToken = thirdToken.getNext();
        } else {
          identifier = new SimpleIdentifier(firstToken);
          nextToken = firstToken.getNext();
        }
        if (nextToken.getType() != TokenType.EOF) {
          return null;
        }
        return new CommentReference(newKeyword, identifier);
      } else if (tokenMatchesKeyword(firstToken, Keyword.THIS)
          || tokenMatchesKeyword(firstToken, Keyword.NULL)
View Full Code Here

            // terminating ']' is not typed yet
            char charAfterLeft = comment.charAt(leftIndex + 1);
            if (Character.isLetterOrDigit(charAfterLeft)) {
              int nameEnd = StringUtilities.indexOfFirstNotLetterDigit(comment, leftIndex + 1);
              String name = comment.substring(leftIndex + 1, nameEnd);
              Token nameToken = new StringToken(TokenType.IDENTIFIER, name, nameOffset);
              references.add(new CommentReference(null, new SimpleIdentifier(nameToken)));
            } else {
              Token nameToken = new SyntheticStringToken(TokenType.IDENTIFIER, "", nameOffset);
              references.add(new CommentReference(null, new SimpleIdentifier(nameToken)));
            }
            // next character
            rightIndex = leftIndex + 1;
          }
View Full Code Here

      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);
      }
View Full Code Here

TOP

Related Classes of com.google.dart.engine.scanner.Token

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.