use of com.google.javascript.jscomp.parsing.parser.trees.MemberVariableTree in project closure-compiler by google.
the class Parser method parseIndexSignature.
private IndexSignatureTree parseIndexSignature() {
SourcePosition start = getTreeStartLocation();
eat(TokenType.OPEN_SQUARE);
IdentifierToken name = eatIdOrKeywordAsId();
eat(TokenType.COLON);
// must be 'string' or 'number'
ParseTree indexType = parseTypeName();
eat(TokenType.CLOSE_SQUARE);
eat(TokenType.COLON);
ParseTree declaredType = parseType();
ParseTree nameTree = new MemberVariableTree(getTreeLocation(start), name, false, false, null, indexType);
return new IndexSignatureTree(getTreeLocation(start), nameTree, declaredType);
}
use of com.google.javascript.jscomp.parsing.parser.trees.MemberVariableTree 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.MemberVariableTree 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);
}
}
}
Aggregations