/** * Consumes the next token. If the consumed token is not of the expected type then * report an error and return null. Otherwise return the consumed token. * * @param expectedTokenType * @return The consumed token, or null if the next token is not of the expected type. */ private Token eat(TokenType expectedTokenType) { Token token = nextToken(); if (token.type != expectedTokenType) { reportExpectedError(token, expectedTokenType); return null; } return token; }
/** * Consumes the next token. If the consumed token is not of the expected type then * report an error and return null. Otherwise return the consumed token. * * @param expectedTokenType * @return The consumed token, or null if the next token is not of the expected type. */ private Token eat(TokenType expectedTokenType) { Token token = nextToken(); if (token.type != expectedTokenType) { reportExpectedError(token, expectedTokenType); return null; } return token; }
private Token eatPredefinedString(String string) { Token token = eatId(); if (token == null || !token.asIdentifier().value.equals(string)) { reportExpectedError(token, string); return null; } return token; }
private Token eatPredefinedString(String string) { Token token = eatId(); if (token == null || !token.asIdentifier().value.equals(string)) { reportExpectedError(token, string); return null; } return token; }
/** * Consumes an identifier token that is not a reserved word. * @see "http://www.ecma-international.org/ecma-262/5.1/#sec-7.6" */ private IdentifierToken eatId() { if (peekId()) { return eatIdOrKeywordAsId(); } else { reportExpectedError(peekToken(), TokenType.IDENTIFIER); return null; } }
/** * Consumes an identifier token that is not a reserved word. * @see "http://www.ecma-international.org/ecma-262/5.1/#sec-7.6" */ private IdentifierToken eatId() { if (peekId()) { return eatIdOrKeywordAsId(); } else { reportExpectedError(peekToken(), TokenType.IDENTIFIER); if (peekIdOrKeyword()) { return eatIdOrKeywordAsId(); } else { return null; } } }
/** * Consumes an identifier token that may be a reserved word, i.e. * an IdentifierName, not necessarily an Identifier. * @see "http://www.ecma-international.org/ecma-262/5.1/#sec-7.6" */ private IdentifierToken eatIdOrKeywordAsId() { Token token = nextToken(); if (token.type == TokenType.IDENTIFIER) { return (IdentifierToken) token; } else if (Keywords.isKeyword(token.type)) { return new IdentifierToken( token.location, Keywords.get(token.type).toString()); } else { reportExpectedError(token, TokenType.IDENTIFIER); } return null; }
/** * Consumes an identifier token that may be a reserved word, i.e. * an IdentifierName, not necessarily an Identifier. * @see "http://www.ecma-international.org/ecma-262/5.1/#sec-7.6" */ private IdentifierToken eatIdOrKeywordAsId() { Token token = nextToken(); if (token.type == TokenType.IDENTIFIER) { return (IdentifierToken) token; } else if (Keywords.isKeyword(token.type)) { return new IdentifierToken( token.location, Keywords.get(token.type).toString()); } else { reportExpectedError(token, TokenType.IDENTIFIER); } return null; }
private ParseTree parseImportSpecifier() { SourcePosition start = getTreeStartLocation(); IdentifierToken importedName = eatIdOrKeywordAsId(); IdentifierToken destinationName = null; if (peekPredefinedString(PredefinedName.AS)) { eatPredefinedString(PredefinedName.AS); destinationName = eatId(); } else if (isKeyword(importedName.value)) { reportExpectedError(null, PredefinedName.AS); } return new ImportSpecifierTree( getTreeLocation(start), importedName, destinationName); }
private ParseTree parseImportSpecifier() { SourcePosition start = getTreeStartLocation(); IdentifierToken importedName = eatIdOrKeywordAsId(); IdentifierToken destinationName = null; if (peekPredefinedString(PredefinedName.AS)) { eatPredefinedString(PredefinedName.AS); destinationName = eatId(); } else if (Keywords.isKeyword(importedName.value)) { reportExpectedError(null, PredefinedName.AS); } return new ImportSpecifierTree( getTreeLocation(start), importedName, destinationName); }
private ParseTree parsePropertyNameAssignment() { SourcePosition start = getTreeStartLocation(); Token name = eatObjectLiteralPropertyName(); Token colon = eatOpt(TokenType.COLON); if (colon == null) { if (name.type != TokenType.IDENTIFIER) { reportExpectedError(peekToken(), TokenType.COLON); } else if (Keywords.isKeyword( name.asIdentifier().value, /* includeTypeScriptKeywords= */ false)) { reportError(name, "Cannot use keyword in short object literal"); } else if (peek(TokenType.EQUAL)) { IdentifierExpressionTree idTree = new IdentifierExpressionTree( getTreeLocation(start), (IdentifierToken) name); eat(TokenType.EQUAL); ParseTree defaultValue = parseAssignmentExpression(); return new DefaultParameterTree(getTreeLocation(start), idTree, defaultValue); } } ParseTree value = colon == null ? null : parseAssignmentExpression(); return new PropertyNameAssignmentTree(getTreeLocation(start), name, value); }
private ParseTree parsePropertyNameAssignment() { SourcePosition start = getTreeStartLocation(); Token name = eatObjectLiteralPropertyName(); Token colon = eatOpt(TokenType.COLON); if (colon == null) { if (name.type != TokenType.IDENTIFIER) { reportExpectedError(peekToken(), TokenType.COLON); } else if (Keywords.isKeyword(name.asIdentifier().value) && !Keywords.isTypeScriptSpecificKeyword(name.asIdentifier().value)) { reportError(name, "Cannot use keyword in short object literal"); } else if (peek(TokenType.EQUAL)) { IdentifierExpressionTree idTree = new IdentifierExpressionTree( getTreeLocation(start), (IdentifierToken) name); eat(TokenType.EQUAL); ParseTree defaultValue = parseAssignmentExpression(); return new DefaultParameterTree(getTreeLocation(start), idTree, defaultValue); } } ParseTree value = colon == null ? null : parseAssignmentExpression(); return new PropertyNameAssignmentTree(getTreeLocation(start), name, value); }