use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.
the class Parser method parseInterfaceElement.
private ParseTree parseInterfaceElement() {
SourcePosition start = getTreeStartLocation();
boolean isGenerator = eatOpt(TokenType.STAR) != null;
IdentifierToken name = null;
TokenType type = peekType();
if (type == TokenType.NEW) {
// ConstructSignature
return parseCallSignature(true);
} else if (type == TokenType.IDENTIFIER || Keywords.isKeyword(type)) {
name = eatIdOrKeywordAsId();
} else if (type == TokenType.OPEN_SQUARE) {
// IndexSignature
return parseIndexSignature();
} else if (type == TokenType.OPEN_ANGLE || type == TokenType.OPEN_PAREN) {
// CallSignature
return parseCallSignature(false);
}
boolean isOptional = false;
if (peek(TokenType.QUESTION)) {
eat(TokenType.QUESTION);
isOptional = true;
}
if (peek(TokenType.OPEN_PAREN) || peek(TokenType.OPEN_ANGLE)) {
// Method signature.
ParseTree function = parseMethodSignature(start, name, false, isGenerator, isOptional, null);
return function;
} else {
// Property signature.
ParseTree declaredType = maybeParseColonType();
return new MemberVariableTree(getTreeLocation(start), name, false, isOptional, null, declaredType);
}
}
use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.
the class Parser method parseAmbientDeclarationHelper.
private ParseTree parseAmbientDeclarationHelper() {
ParseTree declare;
switch(peekType()) {
case FUNCTION:
declare = parseAmbientFunctionDeclaration();
eatPossibleImplicitSemiColon();
break;
case CLASS:
declare = parseClassDeclaration(true);
break;
case ENUM:
declare = parseEnumDeclaration();
break;
case MODULE:
case NAMESPACE:
declare = parseNamespaceDeclaration(true);
break;
case VAR:
case LET:
case CONST:
default:
// unreachable, parse as a var decl to get a parse error.
declare = parseAmbientVariableDeclarationList();
eatPossibleImplicitSemiColon();
break;
}
return declare;
}
use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.
the class Parser method parseClassMemberDeclaration.
private ParseTree parseClassMemberDeclaration(PartialClassElement partial) {
boolean isGenerator = eatOpt(TokenType.STAR) != null;
ParseTree nameExpr;
IdentifierToken name;
if (peekPropertyName(0)) {
if (peekIdOrKeyword()) {
nameExpr = null;
name = eatIdOrKeywordAsId();
if (Keywords.isKeyword(name.value)) {
features = features.with(Feature.KEYWORDS_AS_PROPERTIES);
}
} else {
// { 'str'() {} }
// { 123() {} }
// Treat these as if they were computed properties.
name = null;
nameExpr = parseLiteralExpression();
}
} else {
if (config.parseTypeSyntax && peekIndexSignature()) {
ParseTree indexSignature = parseIndexSignature();
eatPossibleImplicitSemiColon();
return indexSignature;
}
nameExpr = parseComputedPropertyName();
name = null;
}
// Member variables are supported only when parsing type syntax
if (!config.parseTypeSyntax || peek(TokenType.OPEN_PAREN) || peek(TokenType.OPEN_ANGLE)) {
// Member function.
FunctionDeclarationTree.Kind kind;
TokenType accessOnFunction;
if (nameExpr == null) {
kind = FunctionDeclarationTree.Kind.MEMBER;
accessOnFunction = partial.accessModifier;
} else {
kind = FunctionDeclarationTree.Kind.EXPRESSION;
// Accessibility modifier goes on the ComputedPropertyMethodTree
accessOnFunction = null;
}
ParseTree function;
if (partial.isAmbient) {
function = parseMethodSignature(partial, name, isGenerator, /* isOptional */
false);
eatPossibleImplicitSemiColon();
} else {
FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(kind).setName(name).setStatic(partial.isStatic).setAccess(accessOnFunction);
parseFunctionTail(builder, isGenerator ? FunctionFlavor.GENERATOR : FunctionFlavor.NORMAL);
function = builder.build(getTreeLocation(partial.start));
}
if (kind == FunctionDeclarationTree.Kind.MEMBER) {
return function;
} else {
return new ComputedPropertyMethodTree(getTreeLocation(partial.start), partial.accessModifier, nameExpr, function);
}
} else {
// Member variable.
if (isGenerator) {
reportError("Member variable cannot be prefixed by '*' (generator function)");
}
ParseTree declaredType = maybeParseColonType();
if (peek(TokenType.EQUAL)) {
reportError("Member variable initializers ('=') are not supported");
}
eatPossibleImplicitSemiColon();
if (nameExpr == null) {
return new MemberVariableTree(getTreeLocation(partial.start), name, partial.isStatic, false, partial.accessModifier, declaredType);
} else {
return new ComputedPropertyMemberVariableTree(getTreeLocation(partial.start), nameExpr, partial.isStatic, partial.accessModifier, declaredType);
}
}
}
use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.
the class Parser method parseType.
private ParseTree parseType() {
SourcePosition start = getTreeStartLocation();
if (!peekId() && !EnumSet.of(TokenType.VOID, TokenType.OPEN_PAREN, TokenType.OPEN_CURLY, TokenType.TYPEOF).contains(peekType())) {
reportError("Unexpected token '%s' in type expression", peekType());
return new TypeNameTree(getTreeLocation(start), ImmutableList.of("error"));
}
ParseTree typeExpression = parseFunctionTypeExpression();
if (!peek(TokenType.BAR)) {
return typeExpression;
}
ImmutableList.Builder<ParseTree> unionType = ImmutableList.builder();
unionType.add(typeExpression);
do {
eat(TokenType.BAR);
unionType.add(parseArrayTypeExpression());
} while (peek(TokenType.BAR));
return new UnionTypeTree(getTreeLocation(start), unionType.build());
}
use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.
the class Parser method parseExportDeclaration.
/*
ExportDeclaration :
export * FromClause ;
export ExportClause [NoReference] FromClause ;
export ExportClause ;
export VariableStatement
export Declaration[Default]
export default AssignmentExpression ;
ExportClause [NoReference] :
{ }
{ ExportsList [?NoReference] }
{ ExportsList [?NoReference] , }
ExportsList [NoReference] :
ExportSpecifier [?NoReference]
ExportsList [?NoReference] , ExportSpecifier [?NoReference]
ExportSpecifier [NoReference] :
[~NoReference] IdentifierReference
[~NoReference] IdentifierReference as IdentifierName
[+NoReference] IdentifierName
[+NoReference] IdentifierName as IdentifierName
*/
private ParseTree parseExportDeclaration(boolean isAmbient) {
SourcePosition start = getTreeStartLocation();
boolean isDefault = false;
boolean isExportAll = false;
boolean isExportSpecifier = false;
boolean needsSemiColon = true;
eat(TokenType.EXPORT);
ParseTree export = null;
ImmutableList<ParseTree> exportSpecifierList = null;
switch(peekType()) {
case STAR:
isExportAll = true;
nextToken();
break;
case FUNCTION:
// TODO(bradfordcsmith): handle async functions here
export = isAmbient ? parseAmbientFunctionDeclaration() : parseFunctionDeclaration();
needsSemiColon = isAmbient;
break;
case CLASS:
export = parseClassDeclaration(isAmbient);
needsSemiColon = false;
break;
case INTERFACE:
export = parseInterfaceDeclaration();
needsSemiColon = false;
break;
case ENUM:
export = parseEnumDeclaration();
needsSemiColon = false;
break;
case MODULE:
case NAMESPACE:
export = parseNamespaceDeclaration(isAmbient);
needsSemiColon = false;
break;
case DECLARE:
export = parseAmbientDeclaration();
needsSemiColon = false;
break;
case DEFAULT:
isDefault = true;
nextToken();
export = parseExpression();
needsSemiColon = false;
break;
case OPEN_CURLY:
isExportSpecifier = true;
exportSpecifierList = parseExportSpecifierSet();
break;
case TYPE:
export = parseTypeAlias();
break;
case VAR:
case LET:
case CONST:
default:
// unreachable, parse as a var decl to get a parse error.
export = isAmbient ? parseAmbientVariableDeclarationList() : parseVariableDeclarationList();
break;
}
LiteralToken moduleSpecifier = null;
if (isExportAll || (isExportSpecifier && peekPredefinedString(PredefinedName.FROM))) {
eatPredefinedString(PredefinedName.FROM);
moduleSpecifier = (LiteralToken) eat(TokenType.STRING);
} else if (isExportSpecifier) {
for (ParseTree tree : exportSpecifierList) {
IdentifierToken importedName = tree.asExportSpecifier().importedName;
if (isKeyword(importedName.value)) {
reportError(importedName, "cannot use keyword '%s' here.", importedName.value);
}
}
}
if (needsSemiColon || peekImplicitSemiColon()) {
eatPossibleImplicitSemiColon();
}
return new ExportDeclarationTree(getTreeLocation(start), isDefault, isExportAll, export, exportSpecifierList, moduleSpecifier);
}
Aggregations