Skip to content

Commit

Permalink
Correct and simplify parser's tracking of the last source position read.
Browse files Browse the repository at this point in the history
1. Storing a token is overkill. We only ever need its position.
2. resetScanner() should reset last source position.
   It doesn't matter for the current use-case, but it will for
   an upcoming change to handle async arrow functions.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=125061835
  • Loading branch information
brad4d authored and blickly committed Jun 20, 2016
1 parent bbe7ab4 commit 709799f
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/com/google/javascript/jscomp/parsing/parser/Parser.java
Expand Up @@ -168,11 +168,11 @@
public class Parser { public class Parser {
private final Scanner scanner; private final Scanner scanner;
private final ErrorReporter errorReporter; private final ErrorReporter errorReporter;
private Token lastToken;
private final Config config; private final Config config;
private final CommentRecorder commentRecorder = new CommentRecorder(); private final CommentRecorder commentRecorder = new CommentRecorder();
private final ArrayDeque<Boolean> inGeneratorContext = new ArrayDeque<>(); private final ArrayDeque<Boolean> inGeneratorContext = new ArrayDeque<>();
private FeatureSet features = FeatureSet.ES3; private FeatureSet features = FeatureSet.ES3;
private SourcePosition lastSourcePosition;


public Parser( public Parser(
Config config, ErrorReporter errorReporter, Config config, ErrorReporter errorReporter,
Expand All @@ -181,6 +181,7 @@ public Parser(
this.errorReporter = errorReporter; this.errorReporter = errorReporter;
this.scanner = new Scanner(errorReporter, commentRecorder, source, offset); this.scanner = new Scanner(errorReporter, commentRecorder, source, offset);
this.inGeneratorContext.add(initialGeneratorContext); this.inGeneratorContext.add(initialGeneratorContext);
lastSourcePosition = scanner.getPosition();
} }


public Parser( public Parser(
Expand Down Expand Up @@ -2770,7 +2771,11 @@ private ParseTree parseLeftHandSidePattern() {
} }


private void resetScanner(ParseTree tree) { private void resetScanner(ParseTree tree) {
scanner.setOffset(tree.location.start.offset); // TODO(bradfordcsmith): lastSourcePosition should really point to the end of the last token
// before the tree to correctly detect implicit semicolons, but it doesn't matter for the
// current use case.
lastSourcePosition = tree.location.start;
scanner.setOffset(lastSourcePosition.offset);
} }


private boolean peekAssignmentOperator() { private boolean peekAssignmentOperator() {
Expand Down Expand Up @@ -3522,7 +3527,7 @@ private boolean peekImplicitSemiColon(int index) {
* Returns the line number of the most recently consumed token. * Returns the line number of the most recently consumed token.
*/ */
private int getLastLine() { private int getLastLine() {
return lastToken.location.end.line; return lastSourcePosition.line;
} }


/** /**
Expand Down Expand Up @@ -3675,7 +3680,7 @@ private SourcePosition getTreeStartLocation() {
* Returns a SourcePosition for the end of a parse tree that ends at the current location. * Returns a SourcePosition for the end of a parse tree that ends at the current location.
*/ */
private SourcePosition getTreeEndLocation() { private SourcePosition getTreeEndLocation() {
return lastToken.location.end; return lastSourcePosition;
} }


/** /**
Expand All @@ -3694,26 +3699,27 @@ private SourceRange getTreeLocation(SourcePosition start) {
* <p>Tokenizing is contextual. nextToken() will never return a regular expression literal. * <p>Tokenizing is contextual. nextToken() will never return a regular expression literal.
*/ */
private Token nextToken() { private Token nextToken() {
lastToken = scanner.nextToken(); Token token = scanner.nextToken();
return lastToken; lastSourcePosition = token.location.end;
return token;
} }


/** /**
* Consumes a regular expression literal token and returns it. * Consumes a regular expression literal token and returns it.
*/ */
private LiteralToken nextRegularExpressionLiteralToken() { private LiteralToken nextRegularExpressionLiteralToken() {
LiteralToken lastToken = scanner.nextRegularExpressionLiteralToken(); LiteralToken token = scanner.nextRegularExpressionLiteralToken();
this.lastToken = lastToken; lastSourcePosition = token.location.end;
return lastToken; return token;
} }


/** /**
* Consumes a template literal token and returns it. * Consumes a template literal token and returns it.
*/ */
private LiteralToken nextTemplateLiteralToken() { private LiteralToken nextTemplateLiteralToken() {
LiteralToken lastToken = scanner.nextTemplateLiteralToken(); LiteralToken token = scanner.nextTemplateLiteralToken();
this.lastToken = lastToken; lastSourcePosition = token.location.end;
return lastToken; return token;
} }


/** /**
Expand Down

0 comments on commit 709799f

Please sign in to comment.