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

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

Issue 9950045: Implements error recovery for cases where 'var' or 'final' prefix a method def'n (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Adds additional unit test Created 8 years, 9 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 75459e805df70630d2b9c0eeb231458be8485537..5472afd6bd02858c8e5e4d9fac656cab450ce4cc 100644
--- a/compiler/java/com/google/dart/compiler/parser/DartParser.java
+++ b/compiler/java/com/google/dart/compiler/parser/DartParser.java
@@ -5,6 +5,7 @@
package com.google.dart.compiler.parser;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableSet;
import com.google.common.io.CharStreams;
import com.google.dart.compiler.DartCompilationError;
import com.google.dart.compiler.DartCompilerListener;
@@ -161,7 +162,7 @@ public class DartParser extends CompletionHooksParserBase {
STATIC_KEYWORD,
TYPEDEF_KEYWORD
};
-
+
public DartParser(Source source,
String sourceCode,
DartCompilerListener listener) {
@@ -922,6 +923,13 @@ public class DartParser extends CompletionHooksParserBase {
switch (peek(0)) {
case VAR: {
consume(Token.VAR);
+ // Check for malformed method starting with 'var' : var ^ foo() { }
+ if (peek(0).equals(Token.IDENTIFIER) && looksLikeMethodOrAccessorDefinition()) {
+ reportError(position(), ParserErrorCode.VAR_IS_NOT_ALLOWED_ON_A_METHOD_DEFINITION);
+ member = parseMethodOrAccessor(modifiers, null);
+ break;
+ }
+
member = parseFieldDeclaration(modifiers, null);
expectStatmentTerminator();
break;
@@ -941,11 +949,25 @@ public class DartParser extends CompletionHooksParserBase {
case FINAL: {
consume(Token.FINAL);
modifiers = modifiers.makeFinal();
+
+ // Check for malformed method starting with 'final': final ^ foo() { }
+ if (peek(0).equals(Token.IDENTIFIER) && looksLikeMethodOrAccessorDefinition()) {
+ reportError(position(), ParserErrorCode.FINAL_IS_NOT_ALLOWED_ON_A_METHOD_DEFINITION);
+ member = parseMethodOrAccessor(modifiers, null);
+ break;
+ }
DartTypeNode type = null;
if (peek(1) != Token.COMMA
&& peek(1) != Token.ASSIGN
&& peek(1) != Token.SEMICOLON) {
type = parseTypeAnnotation();
+
+ // Check again for malformed method starting with 'final': final String ^ foo() { }
+ if (peek(0).equals(Token.IDENTIFIER) && looksLikeMethodOrAccessorDefinition()) {
+ reportError(position(), ParserErrorCode.FINAL_IS_NOT_ALLOWED_ON_A_METHOD_DEFINITION);
+ member = parseMethodOrAccessor(modifiers, null);
+ break;
+ }
}
member = parseFieldDeclaration(modifiers, type);
expectStatmentTerminator();
@@ -1004,12 +1026,15 @@ public class DartParser extends CompletionHooksParserBase {
*
* The following constructs will match:
*
- * : get
- * | set
- * | operator
- * | identifier ( // Case 1
- * | identifier DOT identifier ( // Case 2
- * | identifier DOT identifier DOT identifier ( // Case 3
+ * : get (
+ * | get identifier (
+ * | set (
+ * | set identifier (
+ * | operator (
+ * | operator <op> (
+ * | identifier (
+ * | identifier DOT identifier (
+ * | identifier DOT identifier DOT identifier (
*
* @return <code>true</code> if the signature of a method has been found. No tokens are consumed.
*/
@@ -1017,11 +1042,55 @@ public class DartParser extends CompletionHooksParserBase {
assert (peek(0).equals(Token.IDENTIFIER));
beginMethodName(); // begin() equivalent
try {
- // Simple checks
+ if (peekPseudoKeyword(0, OPERATOR_KEYWORD)) {
+ next();
+ // Using 'operator' as a field name is valid
+ if (peek(0).equals(Token.SEMICOLON) || peek(0).equals(Token.ASSIGN)) {
+ return false;
+ }
+ // Using 'operator' as a method name is valid (but discouraged)
+ if (peek(0).equals(Token.LPAREN)) {
+ return true;
+ }
+ // operator negate (
+ if (peekPseudoKeyword(0, NEGATE_KEYWORD) && peek(1).equals(Token.LPAREN)) {
+ return true;
+ }
+ // TODO(zundel): Look for valid operator overload tokens. For now just assuming
+ // non-idents are good enough
+ // operator ??? (
+ if (!(peek(0).equals(Token.IDENTIFIER) && peek(1).equals(Token.LPAREN))) {
+ return true;
+ }
+ if (peek(0).equals(Token.LBRACK) && peek(1).equals(Token.RBRACK)) {
+ // operator [] (
+ if (peek(2).equals(Token.LPAREN)) {
+ return true;
+ }
+ // operator []= (
+ if (peek(2).equals(Token.ASSIGN) && peek(3).equals(Token.LPAREN)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
if (peekPseudoKeyword(0, GETTER_KEYWORD)
- || peekPseudoKeyword(0, SETTER_KEYWORD)
- || peekPseudoKeyword(0, OPERATOR_KEYWORD)) {
- return true;
+ || peekPseudoKeyword(0, SETTER_KEYWORD)) {
+ next();
+ // Using 'get' or 'set' as a field name is valid
+ if (peek(0).equals(Token.SEMICOLON) || peek(0).equals(Token.ASSIGN)) {
+ return false;
+ }
+ // Using 'get' or 'set' as a method name is valid (but discouraged)
+ if (peek(0).equals(Token.LPAREN)) {
+ return true;
+ }
+ // normal case: get foo (
+ if (peek(0).equals(Token.IDENTIFIER) && peek(1).equals(Token.LPAREN)) {
+ return true;
+ }
+ return false;
}
consume(Token.IDENTIFIER);
« 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