Use 'int' instead of Position.
By doing so we get 5-7% of performance in running JUnit tests. R=brianwilkerson@google.com BUG= TEST= Review URL: https://chromiumcodereview.appspot.com//10704180 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@9596 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
@@ -7,7 +7,6 @@ package com.google.dart.compiler;
|
||||
import com.google.dart.compiler.common.HasSourceInfo;
|
||||
import com.google.dart.compiler.common.SourceInfo;
|
||||
import com.google.dart.compiler.parser.DartScanner.Location;
|
||||
import com.google.dart.compiler.parser.DartScanner.Position;
|
||||
|
||||
/**
|
||||
* Information about a compilation error.
|
||||
@@ -109,19 +108,11 @@ public class DartCompilationError {
|
||||
this.errorCode = errorCode;
|
||||
this.message = String.format(errorCode.getMessage(), arguments);
|
||||
if (location != null) {
|
||||
Position begin = location.getBegin();
|
||||
if (begin != null) {
|
||||
offset = begin.getPos();
|
||||
lineNumber = begin.getLine();
|
||||
columnNumber = begin.getCol();
|
||||
}
|
||||
Position end = location.getEnd();
|
||||
if (end != null) {
|
||||
length = end.getPos() - offset;
|
||||
if (length < 0) {
|
||||
length = 0;
|
||||
}
|
||||
}
|
||||
offset = location.getBegin();
|
||||
SourceInfo sourceInfo = new SourceInfo(source, offset, 0);
|
||||
lineNumber = sourceInfo.getLine();
|
||||
columnNumber = sourceInfo.getColumn();
|
||||
length = location.getEnd() - offset;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -161,6 +161,9 @@ public final class SourceInfo implements Serializable {
|
||||
}
|
||||
|
||||
int getLineOffset(int line) {
|
||||
if (line < 0) {
|
||||
return 0;
|
||||
}
|
||||
return lineOffsets.get(line);
|
||||
}
|
||||
|
||||
|
||||
@@ -190,25 +190,24 @@ abstract class AbstractParser {
|
||||
&& keyword.equals(getPeekTokenValue(n));
|
||||
}
|
||||
|
||||
protected DartScanner.Position position() {
|
||||
protected int position() {
|
||||
DartScanner.Location tokenLocation = ctx.getTokenLocation();
|
||||
return tokenLocation != null ? tokenLocation.getBegin()
|
||||
: new DartScanner.Position(0, 1, 1);
|
||||
return tokenLocation != null ? tokenLocation.getBegin() : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Report a syntax error, unless an error has already been reported at the
|
||||
* given or a later position.
|
||||
*/
|
||||
protected void reportError(DartScanner.Position position,
|
||||
protected void reportError(int position,
|
||||
ErrorCode errorCode, Object... arguments) {
|
||||
DartScanner.Location location = ctx.getTokenLocation();
|
||||
if (location.getBegin().getPos() <= lastErrorPosition) {
|
||||
if (location.getBegin() <= lastErrorPosition) {
|
||||
return;
|
||||
}
|
||||
DartCompilationError dartError = new DartCompilationError(ctx.getSource(),
|
||||
location, errorCode, arguments);
|
||||
lastErrorPosition = position.getPos();
|
||||
lastErrorPosition = position;
|
||||
ctx.error(dartError);
|
||||
}
|
||||
|
||||
@@ -217,11 +216,11 @@ abstract class AbstractParser {
|
||||
* uses that to prevent logging more than one error at that position. This
|
||||
* method actually uses the passed position to create the error event.
|
||||
*/
|
||||
protected void reportErrorAtPosition(DartScanner.Position startPosition,
|
||||
DartScanner.Position endPosition,
|
||||
protected void reportErrorAtPosition(int startPosition,
|
||||
int endPosition,
|
||||
ErrorCode errorCode, Object... arguments) {
|
||||
DartScanner.Location location = ctx.getTokenLocation();
|
||||
if (location.getBegin().getPos() <= lastErrorPosition) {
|
||||
if (location.getBegin() <= lastErrorPosition) {
|
||||
return;
|
||||
}
|
||||
DartCompilationError dartError = new DartCompilationError(ctx.getSource(),
|
||||
@@ -229,7 +228,7 @@ abstract class AbstractParser {
|
||||
ctx.error(dartError);
|
||||
}
|
||||
|
||||
protected void reportUnexpectedToken(DartScanner.Position position,
|
||||
protected void reportUnexpectedToken(int position,
|
||||
Token expected, Token actual) {
|
||||
if (expected == Token.EOS) {
|
||||
reportError(position, ParserErrorCode.EXPECTED_EOS, actual);
|
||||
|
||||
@@ -55,7 +55,7 @@ public abstract class CompletionHooksParserBase extends AbstractParser {
|
||||
* productions at fault. Called from begin()
|
||||
*/
|
||||
public boolean assertProgress() {
|
||||
int currentPosition = position().getPos();
|
||||
int currentPosition = position();
|
||||
if (currentPosition > maxPositionRange) {
|
||||
minPositionRange = maxPositionRange;
|
||||
maxPositionRange = currentPosition;
|
||||
|
||||
@@ -96,7 +96,6 @@ import com.google.dart.compiler.ast.LibraryUnit;
|
||||
import com.google.dart.compiler.ast.Modifiers;
|
||||
import com.google.dart.compiler.metrics.CompilerMetrics;
|
||||
import com.google.dart.compiler.parser.DartScanner.Location;
|
||||
import com.google.dart.compiler.parser.DartScanner.Position;
|
||||
import com.google.dart.compiler.util.Lists;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -122,7 +121,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
private final Set<Integer> errorHistory = new HashSet<Integer>();
|
||||
private boolean isParsingInterface;
|
||||
private boolean isTopLevelAbstract;
|
||||
private DartScanner.Position topLevelAbstractModifierPosition;
|
||||
private int topLevelAbstractModifierPosition;
|
||||
private boolean isParsingClass;
|
||||
private int errorCount = 0;
|
||||
|
||||
@@ -304,7 +303,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
isParsingClass = isParsingInterface = false;
|
||||
// Check for ABSTRACT_KEYWORD.
|
||||
isTopLevelAbstract = false;
|
||||
topLevelAbstractModifierPosition = null;
|
||||
topLevelAbstractModifierPosition = 0;
|
||||
if (optionalPseudoKeyword(ABSTRACT_KEYWORD)) {
|
||||
isTopLevelAbstract = true;
|
||||
topLevelAbstractModifierPosition = position();
|
||||
@@ -332,7 +331,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
unit.getTopLevelNodes().add(node);
|
||||
// Only "class" can be top-level abstract element.
|
||||
if (isTopLevelAbstract && !isParsingClass) {
|
||||
Position abstractPositionEnd = topLevelAbstractModifierPosition.getAdvancedColumns(ABSTRACT_KEYWORD.length());
|
||||
int abstractPositionEnd = topLevelAbstractModifierPosition + ABSTRACT_KEYWORD.length();
|
||||
Location location = new Location(topLevelAbstractModifierPosition, abstractPositionEnd);
|
||||
reportError(new DartCompilationError(source, location,
|
||||
ParserErrorCode.ABSTRACT_TOP_LEVEL_ELEMENT));
|
||||
@@ -1927,7 +1926,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
} else if (token.isAssignmentOperator()) {
|
||||
ensureAssignable(result);
|
||||
consume(token);
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin().getPos();
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin();
|
||||
result = done(new DartBinaryExpression(token, tokenOffset, result, parseExpression()));
|
||||
} else {
|
||||
done(null);
|
||||
@@ -1959,7 +1958,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
if (token.isAssignmentOperator()) {
|
||||
ensureAssignable(result);
|
||||
consume(token);
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin().getPos();
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin();
|
||||
result = done(new DartBinaryExpression(token, tokenOffset, result, parseExpressionWithoutCascade()));
|
||||
} else {
|
||||
done(null);
|
||||
@@ -2026,7 +2025,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
if (token.isAssignmentOperator()) {
|
||||
ensureAssignable(result);
|
||||
consume(token);
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin().getPos();
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin();
|
||||
result = doneWithoutConsuming(new DartBinaryExpression(token, tokenOffset, result, parseExpressionWithoutCascade()));
|
||||
}
|
||||
return result;
|
||||
@@ -2043,7 +2042,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
DartExpression result = parseExpression();
|
||||
// Must keep in sync with @Terminals above
|
||||
while (optional(Token.COMMA)) {
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin().getPos();
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin();
|
||||
result = new DartBinaryExpression(Token.COMMA, tokenOffset, result, parseExpression());
|
||||
if (match(Token.COMMA)) {
|
||||
result = doneWithoutConsuming(result);
|
||||
@@ -2107,10 +2106,10 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
DartExpression result = lastResult;
|
||||
for (int level = peek(0).getPrecedence(); level >= precedence; level--) {
|
||||
while (peek(0).getPrecedence() == level) {
|
||||
Position prevPositionStart = ctx.getTokenLocation().getBegin();
|
||||
Position prevPositionEnd = ctx.getTokenLocation().getEnd();
|
||||
int prevPositionStart = ctx.getTokenLocation().getBegin();
|
||||
int prevPositionEnd = ctx.getTokenLocation().getEnd();
|
||||
Token token = next();
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin().getPos();
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin();
|
||||
if (lastResult instanceof DartSuperExpression
|
||||
&& (token == Token.AND || token == Token.OR)) {
|
||||
reportErrorAtPosition(prevPositionStart, prevPositionEnd,
|
||||
@@ -2120,7 +2119,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
if (token == Token.IS) {
|
||||
beginTypeExpression();
|
||||
if (optional(Token.NOT)) {
|
||||
int notOffset = ctx.getTokenLocation().getBegin().getPos();
|
||||
int notOffset = ctx.getTokenLocation().getBegin();
|
||||
beginTypeExpression();
|
||||
DartTypeExpression typeExpression = done(new DartTypeExpression(parseTypeAnnotation()));
|
||||
right = done(new DartUnaryExpression(Token.NOT, notOffset, typeExpression, true));
|
||||
@@ -2621,7 +2620,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
if (token.isCountOperator()) {
|
||||
ensureAssignable(result);
|
||||
consume(token);
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin().getPos();
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin();
|
||||
result = doneWithoutConsuming(new DartUnaryExpression(token, tokenOffset, result, false));
|
||||
}
|
||||
|
||||
@@ -3286,12 +3285,12 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
reportErrorWithoutAdvancing(ParserErrorCode.UNEXPECTED_TOKEN);
|
||||
break;
|
||||
}
|
||||
int startPosition = position().getPos();
|
||||
int startPosition = position();
|
||||
DartStatement newStatement = parseStatement();
|
||||
if (newStatement == null) {
|
||||
break;
|
||||
}
|
||||
if (startPosition == position().getPos()) {
|
||||
if (startPosition == position()) {
|
||||
// The parser is not making progress.
|
||||
Set<Token> terminals = this.collectTerminalAnnotations();
|
||||
if (terminals.contains(peek(0))) {
|
||||
@@ -4390,7 +4389,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
if (optional(Token.ADD)) {
|
||||
if (peek(0) != Token.INTEGER_LITERAL && peek(0) != Token.DOUBLE_LITERAL) {
|
||||
reportError(position(), ParserErrorCode.NO_UNARY_PLUS_OPERATOR);
|
||||
} else if (position().getPos() + 1 != peekTokenLocation(0).getBegin().getPos()) {
|
||||
} else if (position() + 1 != peekTokenLocation(0).getBegin()) {
|
||||
reportError(position(), ParserErrorCode.NO_SPACE_AFTER_PLUS);
|
||||
}
|
||||
}
|
||||
@@ -4401,14 +4400,14 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
beginUnaryExpression();
|
||||
beginUnaryExpression();
|
||||
consume(token);
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin().getPos();
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin();
|
||||
DartExpression unary = parseUnaryExpression();
|
||||
DartUnaryExpression unary2 = new DartUnaryExpression(Token.SUB, tokenOffset, unary, true);
|
||||
return done(new DartUnaryExpression(Token.SUB, tokenOffset, done(unary2), true));
|
||||
} else {
|
||||
beginUnaryExpression();
|
||||
consume(token);
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin().getPos();
|
||||
int tokenOffset = ctx.getTokenLocation().getBegin();
|
||||
DartExpression unary = parseUnaryExpression();
|
||||
if (token.isCountOperator()) {
|
||||
ensureAssignable(unary);
|
||||
@@ -4611,7 +4610,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reportError(Position position, ErrorCode errorCode, Object... arguments) {
|
||||
protected void reportError(int position, ErrorCode errorCode, Object... arguments) {
|
||||
// TODO(devoncarew): we're not correctly identifying dart:html as a core library
|
||||
if (incErrorCount()) {
|
||||
super.reportError(position, errorCode, arguments);
|
||||
@@ -4619,7 +4618,7 @@ public class DartParser extends CompletionHooksParserBase {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reportErrorAtPosition(Position startPosition, Position endPosition,
|
||||
protected void reportErrorAtPosition(int startPosition, int endPosition,
|
||||
ErrorCode errorCode, Object... arguments) {
|
||||
if (incErrorCount()) {
|
||||
super.reportErrorAtPosition(startPosition, endPosition, errorCode, arguments);
|
||||
|
||||
@@ -53,7 +53,7 @@ public class DartParserCommentsHelper {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void recordCommentLocation(int start, int stop, int line, int col) {
|
||||
protected void recordCommentLocation(int start, int stop) {
|
||||
int size = commentLocs.size();
|
||||
if (size > 0) {
|
||||
// the parser may re-scan lookahead tokens
|
||||
|
||||
@@ -17,88 +17,34 @@ import java.util.Stack;
|
||||
*/
|
||||
public class DartScanner {
|
||||
|
||||
/**
|
||||
* Represents a position in a source file, including absolute character position,
|
||||
* line, and column.
|
||||
*/
|
||||
public static class Position {
|
||||
private int pos;
|
||||
private int line;
|
||||
private int col;
|
||||
|
||||
public Position(int pos, int line, int col) {
|
||||
this.pos = pos;
|
||||
this.line = line;
|
||||
this.col = col;
|
||||
}
|
||||
|
||||
public Position copy() {
|
||||
return new Position(pos, line, col);
|
||||
}
|
||||
|
||||
public int getPos() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
public int getCol() {
|
||||
return col;
|
||||
}
|
||||
|
||||
public void advance(boolean isNewline) {
|
||||
++pos;
|
||||
if (isNewline) {
|
||||
col = 1;
|
||||
++line;
|
||||
} else {
|
||||
++col;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link Position} which is advanced on the given number of columns, on the same
|
||||
* line.
|
||||
*/
|
||||
public Position getAdvancedColumns(int cols) {
|
||||
return new Position(pos + cols, line, col + cols);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return line + "," + col + "@" + pos;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a span of characters in a source file.
|
||||
*/
|
||||
public static class Location {
|
||||
public static final Location NONE = null;
|
||||
private Position begin, end;
|
||||
private int begin;
|
||||
private int end;
|
||||
|
||||
public Location(Position begin, Position end) {
|
||||
public Location(int begin, int end) {
|
||||
this.begin = begin;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public Location(Position begin) {
|
||||
public Location(int begin) {
|
||||
this.begin = this.end = begin;
|
||||
}
|
||||
|
||||
public Position getBegin() {
|
||||
public int getBegin() {
|
||||
return begin;
|
||||
}
|
||||
|
||||
public Position getEnd() {
|
||||
public int getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return begin.toString() + "::" + end.toString();
|
||||
return begin + "::" + end;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,8 +198,8 @@ public class DartScanner {
|
||||
}
|
||||
|
||||
private int lookahead[] = new int[NUM_LOOKAHEAD];
|
||||
private Position lookaheadPos[] = new Position[NUM_LOOKAHEAD];
|
||||
private Position nextLookaheadPos;
|
||||
private int lookaheadPos[] = new int[NUM_LOOKAHEAD];
|
||||
private int nextLookaheadPos;
|
||||
private ArrayList<TokenData> tokens;
|
||||
private TokenData lastToken;
|
||||
|
||||
@@ -435,7 +381,6 @@ public class DartScanner {
|
||||
return c == ' ' || c == '\t';
|
||||
}
|
||||
|
||||
private int commentLineCount;
|
||||
private int commentCharCount;
|
||||
private int lastCommentStart;
|
||||
private int lastCommentStop;
|
||||
@@ -455,9 +400,9 @@ public class DartScanner {
|
||||
|
||||
// Initialize lookahead positions.
|
||||
// TODO Determine if line & column should be relative to 0 or 'start'
|
||||
internalState.nextLookaheadPos = new Position(start, 1, 1);
|
||||
internalState.nextLookaheadPos = start;
|
||||
for (int i = 0; i < internalState.lookaheadPos.length; ++i) {
|
||||
internalState.lookaheadPos[i] = new Position(start, 1, 1);
|
||||
internalState.lookaheadPos[i] = start;
|
||||
}
|
||||
|
||||
// Fill all the characters in the look-ahead and all the peek
|
||||
@@ -473,31 +418,11 @@ public class DartScanner {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of lines of source that were scanned, excluding the number of lines
|
||||
* consumed by comments.
|
||||
*/
|
||||
public int getNonCommentLineCount() {
|
||||
return getLineCount() - commentLineCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of lines of source that were scanned.
|
||||
*/
|
||||
public int getLineCount() {
|
||||
int lineCount = internalState.nextLookaheadPos.line;
|
||||
if (isEos()) {
|
||||
// At the end of the file the next line has advanced one past the end
|
||||
lineCount -= 1;
|
||||
}
|
||||
return lineCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of characters of source code that were scanned.
|
||||
*/
|
||||
public int getCharCount() {
|
||||
return internalState.nextLookaheadPos.pos;
|
||||
return internalState.nextLookaheadPos;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -647,32 +572,28 @@ public class DartScanner {
|
||||
* invocation.
|
||||
* @param start the character position of the second character in the comment
|
||||
* @param stop the character position of the final character in the comment
|
||||
* @param line the line number at <code>start</code>
|
||||
* @param col the column number at <code>start</code>
|
||||
*/
|
||||
protected void recordCommentLocation(int start, int stop, int line, int col) {
|
||||
protected void recordCommentLocation(int start, int stop) {
|
||||
}
|
||||
|
||||
private void advance() {
|
||||
for (int i = 0; i < NUM_LOOKAHEAD - 1; ++i) {
|
||||
internalState.lookahead[i] = internalState.lookahead[i + 1];
|
||||
internalState.lookaheadPos[i] = internalState.lookaheadPos[i + 1].copy();
|
||||
internalState.lookaheadPos[i] = internalState.lookaheadPos[i + 1];
|
||||
}
|
||||
if (internalState.nextLookaheadPos.pos < source.length()) {
|
||||
int ch = source.codePointAt(internalState.nextLookaheadPos.pos);
|
||||
if (internalState.nextLookaheadPos < source.length()) {
|
||||
int ch = source.codePointAt(internalState.nextLookaheadPos);
|
||||
internalState.lookahead[NUM_LOOKAHEAD - 1] = ch;
|
||||
internalState.lookaheadPos[NUM_LOOKAHEAD - 1] = internalState.nextLookaheadPos.copy();
|
||||
internalState.nextLookaheadPos.advance(ch == '\n');
|
||||
internalState.lookaheadPos[NUM_LOOKAHEAD - 1] = internalState.nextLookaheadPos;
|
||||
internalState.nextLookaheadPos++;
|
||||
} else {
|
||||
// Let the last look-ahead position be past the source. This makes
|
||||
// the position information for the last token correct.
|
||||
internalState.lookahead[NUM_LOOKAHEAD - 1] = -1;
|
||||
internalState.lookaheadPos[NUM_LOOKAHEAD - 1] = new Position(source.length(),
|
||||
internalState.nextLookaheadPos.line, internalState.nextLookaheadPos.col);
|
||||
internalState.lookaheadPos[NUM_LOOKAHEAD - 1] = source.length();
|
||||
|
||||
// Leave the nextLookahead position pointing to the line after the last line
|
||||
internalState.nextLookaheadPos = new Position(source.length(),
|
||||
internalState.nextLookaheadPos.line + 1, 1);
|
||||
internalState.nextLookaheadPos = source.length();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -688,17 +609,16 @@ public class DartScanner {
|
||||
* @param endLine the line number of the last line of the comment
|
||||
* @param col the column number at <code>start</code>
|
||||
*/
|
||||
private void commentLocation(int start, int stop, int startLine, int endLine, int col) {
|
||||
private void commentLocation(int start, int stop) {
|
||||
if (start <= lastCommentStart && stop <= lastCommentStop) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastCommentStart = start;
|
||||
lastCommentStop = stop;
|
||||
commentLineCount += endLine - startLine + 1;
|
||||
commentCharCount += stop - start + 1;
|
||||
|
||||
recordCommentLocation(start, stop, startLine, col);
|
||||
recordCommentLocation(start, stop);
|
||||
}
|
||||
|
||||
private boolean is(int c) {
|
||||
@@ -715,7 +635,7 @@ public class DartScanner {
|
||||
}
|
||||
|
||||
// Get the current source code position.
|
||||
private Position position() {
|
||||
private int position() {
|
||||
return internalState.lookaheadPos[0];
|
||||
}
|
||||
|
||||
@@ -727,7 +647,7 @@ public class DartScanner {
|
||||
while (true) {
|
||||
internalState.lastToken = new TokenData();
|
||||
Token token;
|
||||
Position begin, end;
|
||||
int begin, end;
|
||||
do {
|
||||
skipWhiteSpace();
|
||||
begin = position();
|
||||
@@ -757,7 +677,7 @@ public class DartScanner {
|
||||
|
||||
private Token scanIdentifier(boolean allowDollars) {
|
||||
assert (isIdentifierStart(lookahead(0)));
|
||||
Position begin = position();
|
||||
int begin = position();
|
||||
while (true) {
|
||||
int nextChar = lookahead(0);
|
||||
if (!isIdentifierPart(nextChar) || (!allowDollars && nextChar == '$')) {
|
||||
@@ -765,11 +685,11 @@ public class DartScanner {
|
||||
}
|
||||
advance();
|
||||
}
|
||||
int size = position().pos - begin.pos;
|
||||
int size = position() - begin;
|
||||
|
||||
// Use a substring of the source string instead of copying all the
|
||||
// characters to the token value buffer.
|
||||
String result = source.substring(begin.pos, begin.pos + size);
|
||||
String result = source.substring(begin, begin + size);
|
||||
internalState.lastToken.value = result;
|
||||
return Token.lookup(result);
|
||||
}
|
||||
@@ -777,7 +697,7 @@ public class DartScanner {
|
||||
private Token scanNumber() {
|
||||
boolean isDouble = false;
|
||||
assert (isDecimalDigit(lookahead(0)) || is('.'));
|
||||
Position begin = position();
|
||||
int begin = position();
|
||||
while (isDecimalDigit(lookahead(0)))
|
||||
advance();
|
||||
if (is('.') && isDecimalDigit(lookahead(1))) {
|
||||
@@ -801,8 +721,8 @@ public class DartScanner {
|
||||
// Number literals must not be followed directly by an identifier.
|
||||
return Token.ILLEGAL;
|
||||
}
|
||||
int size = position().pos - begin.pos;
|
||||
internalState.lastToken.value = source.substring(begin.pos, begin.pos + size);
|
||||
int size = position() - begin;
|
||||
internalState.lastToken.value = source.substring(begin, begin + size);
|
||||
return isDouble ? Token.DOUBLE_LITERAL : Token.INTEGER_LITERAL;
|
||||
}
|
||||
|
||||
@@ -816,7 +736,7 @@ public class DartScanner {
|
||||
advance();
|
||||
advance();
|
||||
|
||||
Position begin = position();
|
||||
int begin = position();
|
||||
if (!isHexDigit(lookahead(0))) {
|
||||
return Token.ILLEGAL;
|
||||
}
|
||||
@@ -827,7 +747,7 @@ public class DartScanner {
|
||||
if (isIdentifierStart(lookahead(0))) {
|
||||
return Token.ILLEGAL;
|
||||
}
|
||||
internalState.lastToken.value = source.substring(begin.pos, position().pos);
|
||||
internalState.lastToken.value = source.substring(begin, position());
|
||||
return Token.HEX_LITERAL;
|
||||
}
|
||||
|
||||
@@ -1316,18 +1236,16 @@ public class DartScanner {
|
||||
*/
|
||||
private Token scanDirective() {
|
||||
assert (is('#'));
|
||||
Position currPos = position();
|
||||
int start = currPos.pos;
|
||||
int line = currPos.line;
|
||||
int col = currPos.col;
|
||||
int currPos = position();
|
||||
int start = currPos;
|
||||
|
||||
// Skip over the #! if it exists and consider it a comment
|
||||
if (start == 0) {
|
||||
if (lookahead(1) == '!') {
|
||||
while (!isEos() && !isLineTerminator(lookahead(0)))
|
||||
advance();
|
||||
int stop = internalState.lookaheadPos[0].pos;
|
||||
commentLocation(start, stop, line, internalState.lookaheadPos[0].line, col);
|
||||
int stop = internalState.lookaheadPos[0];
|
||||
commentLocation(start, stop);
|
||||
return Token.COMMENT;
|
||||
}
|
||||
}
|
||||
@@ -1345,7 +1263,7 @@ public class DartScanner {
|
||||
}
|
||||
advance();
|
||||
}
|
||||
String syntax = source.substring(start, position().pos);
|
||||
String syntax = source.substring(start, position());
|
||||
Token token = Token.lookup(syntax);
|
||||
return token == Token.IDENTIFIER ? Token.ILLEGAL : token;
|
||||
}
|
||||
@@ -1365,10 +1283,8 @@ public class DartScanner {
|
||||
|
||||
private Token skipMultiLineComment() {
|
||||
assert (is('*'));
|
||||
Position currPos = internalState.lookaheadPos[0];
|
||||
int start = currPos.pos - 1;
|
||||
int line = currPos.line;
|
||||
int col = currPos.col;
|
||||
int currPos = internalState.lookaheadPos[0];
|
||||
int start = currPos - 1;
|
||||
int commentDepth = 1;
|
||||
advance();
|
||||
while (!isEos()) {
|
||||
@@ -1377,8 +1293,8 @@ public class DartScanner {
|
||||
if (first == '*' && is('/')) {
|
||||
if(--commentDepth == 0) {
|
||||
Token result = select(Token.COMMENT);
|
||||
int stop = internalState.lookaheadPos[0].pos;
|
||||
commentLocation(start, stop, line, internalState.lookaheadPos[0].line, col);
|
||||
int stop = internalState.lookaheadPos[0];
|
||||
commentLocation(start, stop);
|
||||
return result;
|
||||
}
|
||||
advance();
|
||||
@@ -1387,23 +1303,21 @@ public class DartScanner {
|
||||
advance();
|
||||
}
|
||||
}
|
||||
int stop = internalState.lookaheadPos[0].pos;
|
||||
commentLocation(start, stop, line, internalState.lookaheadPos[0].line, col);
|
||||
int stop = internalState.lookaheadPos[0];
|
||||
commentLocation(start, stop);
|
||||
// Unterminated multi-line comment.
|
||||
return Token.ILLEGAL;
|
||||
}
|
||||
|
||||
private Token skipSingleLineComment() {
|
||||
assert (is('/'));
|
||||
Position currPos = internalState.lookaheadPos[0];
|
||||
int start = currPos.pos - 1;
|
||||
int line = currPos.line;
|
||||
int col = currPos.col;
|
||||
int currPos = internalState.lookaheadPos[0];
|
||||
int start = currPos - 1;
|
||||
advance();
|
||||
while (!isEos() && !isLineTerminator(lookahead(0)))
|
||||
advance();
|
||||
int stop = internalState.lookaheadPos[0].pos;
|
||||
commentLocation(start, stop, line, internalState.lookaheadPos[0].line, col);
|
||||
int stop = internalState.lookaheadPos[0];
|
||||
commentLocation(start, stop);
|
||||
return Token.COMMENT;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ import java.util.Stack;
|
||||
class DartScannerParserContext implements ParserContext {
|
||||
private DartScanner scanner;
|
||||
private Deque<DartScanner.State> stateStack = new ArrayDeque<DartScanner.State>();
|
||||
private Deque<DartScanner.Position> positionStack = new ArrayDeque<DartScanner.Position>();
|
||||
private Deque<Integer> positionStack = new ArrayDeque<Integer>();
|
||||
private Source source;
|
||||
private DartCompilerListener listener;
|
||||
private final CompilerMetrics compilerMetrics;
|
||||
@@ -50,14 +50,14 @@ class DartScannerParserContext implements ParserContext {
|
||||
positionStack.push(getBeginLocation(0));
|
||||
}
|
||||
|
||||
private DartScanner.Position getBeginLocation(int n) {
|
||||
private int getBeginLocation(int n) {
|
||||
DartScanner.Location tokenLocation = scanner.peekTokenLocation(n);
|
||||
return tokenLocation != null ? tokenLocation.getBegin() : new DartScanner.Position(0, 1, 1);
|
||||
return tokenLocation != null ? tokenLocation.getBegin() : 0;
|
||||
}
|
||||
|
||||
private DartScanner.Position getEndLocation() {
|
||||
private int getEndLocation() {
|
||||
DartScanner.Location tokenLocation = scanner.getTokenLocation();
|
||||
return tokenLocation != null ? tokenLocation.getEnd() : new DartScanner.Position(0, 1, 1);
|
||||
return tokenLocation != null ? tokenLocation.getEnd() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -80,7 +80,7 @@ class DartScannerParserContext implements ParserContext {
|
||||
if (result instanceof DartUnit) {
|
||||
if (compilerMetrics != null) {
|
||||
compilerMetrics.unitParsed(scanner.getCharCount(), scanner.getNonCommentCharCount(),
|
||||
scanner.getLineCount(), scanner.getNonCommentLineCount());
|
||||
0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,11 +95,11 @@ class DartScannerParserContext implements ParserContext {
|
||||
* @param result
|
||||
* @param startPos
|
||||
*/
|
||||
private <T> void setSourcePosition(T result, DartScanner.Position startPos) {
|
||||
private <T> void setSourcePosition(T result, int startPos) {
|
||||
if (result instanceof HasSourceInfoSetter) {
|
||||
HasSourceInfoSetter hasSourceInfoSetter = (HasSourceInfoSetter) result;
|
||||
int start = startPos.getPos();
|
||||
int end = getEndLocation().getPos();
|
||||
int start = startPos;
|
||||
int end = getEndLocation();
|
||||
if (start != -1 && end < start) {
|
||||
// handle 0-length tokens, including where there is trailing whitespace
|
||||
end = start;
|
||||
|
||||
Reference in New Issue
Block a user