Skip to content

Commit

Permalink
Replace boolean with enum to indicate type of function in context.
Browse files Browse the repository at this point in the history
No functional change here.
This is the first step toward properly detecting async function context.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=148662862
  • Loading branch information
brad4d authored and Tyler Breisacher committed Feb 27, 2017
1 parent 96ae3cb commit fb314b3
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/com/google/javascript/jscomp/parsing/parser/Parser.java
Expand Up @@ -173,12 +173,20 @@
* </pre>
*/
public class Parser {

/**
* Indicates the type of function currently being parsed.
*/
private enum FunctionFlavor {
NORMAL, GENERATOR, ASYNC;
}

private final Scanner scanner;
private final ErrorReporter errorReporter;
private final Config config;
private final boolean parseInlineSourceMaps;
private final CommentRecorder commentRecorder = new CommentRecorder();
private final ArrayDeque<Boolean> inGeneratorContext = new ArrayDeque<>();
private final ArrayDeque<FunctionFlavor> functionContextStack = new ArrayDeque<>();
private FeatureSet features = FeatureSet.ES3;
private SourcePosition lastSourcePosition;
@Nullable
Expand All @@ -190,7 +198,8 @@ public Parser(Config config, ErrorReporter errorReporter, SourceFile source, int
this.errorReporter = errorReporter;
this.parseInlineSourceMaps = parseInlineSourceMaps;
this.scanner = new Scanner(errorReporter, commentRecorder, source, offset);
this.inGeneratorContext.add(initialGeneratorContext);
this.functionContextStack.addLast(
initialGeneratorContext ? FunctionFlavor.GENERATOR : FunctionFlavor.NORMAL);
lastSourcePosition = scanner.getPosition();
}

Expand Down Expand Up @@ -1043,14 +1052,15 @@ private void parseGeneratorFunctionTail(FunctionDeclarationTree.Builder builder)
}

private void parseFunctionTail(FunctionDeclarationTree.Builder builder, boolean isGenerator) {
inGeneratorContext.addLast(isGenerator);
FunctionFlavor functionFlavor = isGenerator ? FunctionFlavor.GENERATOR : FunctionFlavor.NORMAL;
functionContextStack.addLast(functionFlavor);
builder
.setGenerator(isGenerator)
.setGenerics(maybeParseGenericTypes())
.setFormalParameterList(parseFormalParameterList(ParamContext.IMPLEMENTATION))
.setReturnType(maybeParseColonType())
.setFunctionBody(parseFunctionBody());
inGeneratorContext.removeLast();
functionContextStack.removeLast();
}

private NamespaceDeclarationTree parseNamespaceDeclaration(boolean isAmbient) {
Expand Down Expand Up @@ -2960,14 +2970,14 @@ private ParseTree parseAsyncArrowFunction(Expression expressionIn) {
}

private ParseTree parseArrowFunctionBody(Expression expressionIn) {
inGeneratorContext.addLast(false);
functionContextStack.addLast(FunctionFlavor.NORMAL);
ParseTree arrowFunctionBody;
if (peek(TokenType.OPEN_CURLY)) {
arrowFunctionBody = parseFunctionBody();
} else {
arrowFunctionBody = parseAssignment(expressionIn);
}
inGeneratorContext.removeLast();
functionContextStack.removeLast();
return arrowFunctionBody;
}

Expand Down Expand Up @@ -3035,7 +3045,7 @@ private boolean peekAssignmentOperator() {

private boolean inGeneratorContext() {
// disallow yield outside of generators
return inGeneratorContext.peekLast();
return functionContextStack.peekLast() == FunctionFlavor.GENERATOR;
}

// yield [no line terminator] (*)? AssignExpression
Expand Down

0 comments on commit fb314b3

Please sign in to comment.