Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Unified Diff: compiler/java/com/google/dart/compiler/parser/DartParser.java

Issue 10019010: Adds more parser recovery to '#' directives (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | compiler/java/com/google/dart/compiler/parser/ParserErrorCode.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: compiler/java/com/google/dart/compiler/parser/DartParser.java
diff --git a/compiler/java/com/google/dart/compiler/parser/DartParser.java b/compiler/java/com/google/dart/compiler/parser/DartParser.java
index 3a5f1a42c5604ef109070690ef52415d59365e9a..b325b9b1914cd1cb01c2346ae1cf1f6219c14899 100644
--- a/compiler/java/com/google/dart/compiler/parser/DartParser.java
+++ b/compiler/java/com/google/dart/compiler/parser/DartParser.java
@@ -28,6 +28,7 @@ import com.google.dart.compiler.ast.DartConditional;
import com.google.dart.compiler.ast.DartContinueStatement;
import com.google.dart.compiler.ast.DartDeclaration;
import com.google.dart.compiler.ast.DartDefault;
+import com.google.dart.compiler.ast.DartDirective;
import com.google.dart.compiler.ast.DartDoWhileStatement;
import com.google.dart.compiler.ast.DartDoubleLiteral;
import com.google.dart.compiler.ast.DartEmptyStatement;
@@ -253,7 +254,8 @@ public class DartParser extends CompletionHooksParserBase {
* ;
* </pre>
*/
- @Terminals(tokens={Token.EOS, Token.CLASS})
+ @Terminals(tokens={Token.EOS, Token.CLASS, Token.LIBRARY, Token.IMPORT, Token.SOURCE,
+ Token.RESOURCE, Token.NATIVE})
public DartUnit parseUnit(DartSource source) {
beginCompilationUnit();
ctx.unitAboutToCompile(source, isDietParse);
@@ -287,6 +289,12 @@ public class DartParser extends CompletionHooksParserBase {
&& (peek(1).equals(Token.IDENTIFIER) || peek(1).equals(Token.VOID))) {
consume(Token.IDENTIFIER);
node = done(parseFunctionTypeAlias());
+ } else if (looksLikeDirective()) {
+ ctx.begin();
+ next();
+ reportError(position(), ParserErrorCode.DIRECTIVE_OUT_OF_ORDER);
+ ctx.rollback();
+ parseDirectives(unit);
} else {
node = done(parseFieldOrMethod(false));
}
@@ -320,6 +328,18 @@ public class DartParser extends CompletionHooksParserBase {
return done(unit);
}
+ private boolean looksLikeDirective() {
+ switch(peek(0)) {
+ case LIBRARY:
+ case IMPORT:
+ case SOURCE:
+ case RESOURCE:
+ case NATIVE:
+ return true;
+ }
+ return false;
+ }
+
/**
* @return <code>true</code> if token at given position is keyword which can be used only on top
* level.
@@ -411,7 +431,15 @@ public class DartParser extends CompletionHooksParserBase {
private void parseDirectives(DartUnit unit) {
if (peek(0) == Token.LIBRARY) {
beginLibraryDirective();
- unit.getDirectives().add(done(parseLibraryDirective()));
+ DartLibraryDirective libraryDirective = parseLibraryDirective();
+ for (DartDirective directive : unit.getDirectives()) {
+ if (directive instanceof DartLibraryDirective) {
+ reportError(position(), ParserErrorCode.ONLY_ONE_LIBRARY_DIRECTIVE);
+ break;
+ }
+ }
+ unit.getDirectives().add(libraryDirective);
+ done(libraryDirective);
}
while (peek(0) == Token.IMPORT) {
beginImportDirective();
« no previous file with comments | « no previous file | compiler/java/com/google/dart/compiler/parser/ParserErrorCode.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698