use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition 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.util.SourcePosition in project closure-compiler by google.
the class Parser method parseNamespaceName.
private NamespaceNameTree parseNamespaceName() {
SourcePosition start = getTreeStartLocation();
IdentifierToken token = eatId();
return new NamespaceNameTree(getTreeLocation(start), buildIdentifierPath(token));
}
use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition 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.util.SourcePosition 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);
}
use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition in project closure-compiler by google.
the class CodeGenerator method addNonJsDoc_nonTrailing.
private void addNonJsDoc_nonTrailing(Node node, NonJSDocComment nonJSDocComment) {
String content = nonJSDocComment.getCommentString();
SourcePosition commentEndPosition = nonJSDocComment.getEndPosition();
// source lines are 1-indexed
int nodeLineNumber = node.getLineno() - 1;
if (nonJSDocComment.isEndingAsLineComment()) {
// Non trailing line comments can not be on the same line as the node.
checkState(commentEndPosition.line < nodeLineNumber, "Non trailing line comments can not be on the same line as the node.");
add(content + "\n");
} else {
if (nodeLineNumber == commentEndPosition.line) {
// e.g. ``` /* comment */ let x; ```
add(content + " ");
} else {
// e.g.
// ```
// /* comment */
// let x;
// ```
add(content + "\n");
}
}
}
Aggregations