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

Side by Side Diff: compiler/java/com/google/dart/compiler/parser/DartParser.java

Issue 10696056: Fix for issues 3729 and 3523 (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 5 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 package com.google.dart.compiler.parser; 5 package com.google.dart.compiler.parser;
6 6
7 import com.google.common.annotations.VisibleForTesting; 7 import com.google.common.annotations.VisibleForTesting;
8 import com.google.common.collect.ImmutableSet; 8 import com.google.common.collect.ImmutableSet;
9 import com.google.common.io.CharStreams; 9 import com.google.common.io.CharStreams;
10 import com.google.dart.compiler.DartCompilationError; 10 import com.google.dart.compiler.DartCompilationError;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 import com.google.dart.compiler.ast.DartUnaryExpression; 87 import com.google.dart.compiler.ast.DartUnaryExpression;
88 import com.google.dart.compiler.ast.DartUnit; 88 import com.google.dart.compiler.ast.DartUnit;
89 import com.google.dart.compiler.ast.DartUnqualifiedInvocation; 89 import com.google.dart.compiler.ast.DartUnqualifiedInvocation;
90 import com.google.dart.compiler.ast.DartVariable; 90 import com.google.dart.compiler.ast.DartVariable;
91 import com.google.dart.compiler.ast.DartVariableStatement; 91 import com.google.dart.compiler.ast.DartVariableStatement;
92 import com.google.dart.compiler.ast.DartWhileStatement; 92 import com.google.dart.compiler.ast.DartWhileStatement;
93 import com.google.dart.compiler.ast.ImportCombinator; 93 import com.google.dart.compiler.ast.ImportCombinator;
94 import com.google.dart.compiler.ast.LibraryNode; 94 import com.google.dart.compiler.ast.LibraryNode;
95 import com.google.dart.compiler.ast.LibraryUnit; 95 import com.google.dart.compiler.ast.LibraryUnit;
96 import com.google.dart.compiler.ast.Modifiers; 96 import com.google.dart.compiler.ast.Modifiers;
97 import com.google.dart.compiler.common.HasSourceInfo;
97 import com.google.dart.compiler.metrics.CompilerMetrics; 98 import com.google.dart.compiler.metrics.CompilerMetrics;
98 import com.google.dart.compiler.parser.DartScanner.Location; 99 import com.google.dart.compiler.parser.DartScanner.Location;
99 import com.google.dart.compiler.parser.DartScanner.Position; 100 import com.google.dart.compiler.parser.DartScanner.Position;
100 import com.google.dart.compiler.util.Lists; 101 import com.google.dart.compiler.util.Lists;
101 102
102 import java.io.IOException; 103 import java.io.IOException;
103 import java.io.Reader; 104 import java.io.Reader;
104 import java.math.BigInteger; 105 import java.math.BigInteger;
105 import java.util.ArrayList; 106 import java.util.ArrayList;
106 import java.util.Collections; 107 import java.util.Collections;
(...skipping 1769 matching lines...) Expand 10 before | Expand all | Expand 10 after
1876 } 1877 }
1877 } 1878 }
1878 } 1879 }
1879 1880
1880 /** 1881 /**
1881 * Parse an expression. 1882 * Parse an expression.
1882 * 1883 *
1883 * <pre> 1884 * <pre>
1884 * expression 1885 * expression
1885 * : assignableExpression assignmentOperator expression 1886 * : assignableExpression assignmentOperator expression
1886 * | conditionalExpression 1887 * | conditionalExpression cascadeSection*
1887 * ; 1888 * ;
1888 * 1889 *
1889 * assignableExpression 1890 * assignableExpression
1890 * : primary (arguments* assignableSelector)+ 1891 * : primary (arguments* assignableSelector)+
1891 * | SUPER assignableSelector 1892 * | SUPER assignableSelector
1892 * | identifier 1893 * | identifier
1893 * ; 1894 * ;
1894 * </pre> 1895 * </pre>
1895 * 1896 *
1896 * @return an expression matching the {@code expression} production above 1897 * @return an expression matching the {@code expression} production above
1897 */ 1898 */
1898 @VisibleForTesting 1899 @VisibleForTesting
1899 public DartExpression parseExpression() { 1900 public DartExpression parseExpression() {
1900 beginExpression(); 1901 beginExpression();
1901 if (looksLikeTopLevelKeyword() || peek(0).equals(Token.RBRACE)) { 1902 if (looksLikeTopLevelKeyword() || peek(0).equals(Token.RBRACE)) {
1902 // Allow recovery back to the top level. 1903 // Allow recovery back to the top level.
1903 reportErrorWithoutAdvancing(ParserErrorCode.UNEXPECTED_TOKEN); 1904 reportErrorWithoutAdvancing(ParserErrorCode.UNEXPECTED_TOKEN);
1904 return done(null); 1905 return done(null);
1905 } 1906 }
1906 DartExpression result = parseConditionalExpression(); 1907 DartExpression result = parseConditionalExpression();
1907 Token token = peek(0); 1908 Token token = peek(0);
1908 if (token.isAssignmentOperator()) { 1909 if (token == Token.CASCADE) {
1910 while (token == Token.CASCADE) {
1911 result = parseCascadeSection(result);
1912 token = peek(0);
1913 }
1914 done(result);
1915 } else if (token.isAssignmentOperator()) {
1909 ensureAssignable(result); 1916 ensureAssignable(result);
1910 consume(token); 1917 consume(token);
1911 int tokenOffset = ctx.getTokenLocation().getBegin().getPos(); 1918 int tokenOffset = ctx.getTokenLocation().getBegin().getPos();
1912 result = done(new DartBinaryExpression(token, tokenOffset, result, parseEx pression())); 1919 result = done(new DartBinaryExpression(token, tokenOffset, result, parseEx pression()));
1913 } else { 1920 } else {
1914 done(null); 1921 done(null);
1915 } 1922 }
1916 return result; 1923 return result;
1917 } 1924 }
1918 1925
1919 /** 1926 /**
1927 * Parse an expression without a cascade.
1928 *
1929 * <pre>
1930 * expressionWithoutCascade
1931 * : assignableExpression assignmentOperator expressionWithoutCascade
1932 * | conditionalExpression
1933 * ;
1934 * </pre>
1935 *
1936 * @return an expression matching the {@code expression} production above
1937 */
1938 private DartExpression parseExpressionWithoutCascade() {
1939 beginExpression();
1940 if (looksLikeTopLevelKeyword() || peek(0).equals(Token.RBRACE)) {
1941 // Allow recovery back to the top level.
1942 reportErrorWithoutAdvancing(ParserErrorCode.UNEXPECTED_TOKEN);
1943 return done(null);
1944 }
1945 DartExpression result = parseConditionalExpression();
1946 Token token = peek(0);
1947 if (token.isAssignmentOperator()) {
1948 ensureAssignable(result);
1949 consume(token);
1950 int tokenOffset = ctx.getTokenLocation().getBegin().getPos();
1951 result = done(new DartBinaryExpression(token, tokenOffset, result, parseEx pressionWithoutCascade()));
1952 } else {
1953 done(null);
1954 }
1955 return result;
1956 }
1957
1958 /**
1959 * Parse a cascade section.
1960 * <pre>
1961 * cascadeSection
1962 * : CASCADE (cascadeSelector arguments*) (assignableSelector arguments*)* (assignmentOperator
1963 * expressionWithoutCascade)?
1964 * ;
1965 *
1966 * cascadeSelector
1967 * : LBRACK expression RBRACK
1968 * | identifier
1969 * ;
1970 * </pre>
1971 *
1972 * @param target the target of the method invocation
1973 * @return the expression representing the cascaded method invocation
1974 */
1975 private DartExpression parseCascadeSection(DartExpression target) {
1976 expect(Token.CASCADE);
1977 DartExpression result = target;
1978 DartIdentifier functionName = null;
1979 if (peek(0) == Token.IDENTIFIER) {
1980 functionName = parseIdentifier();
1981 } else if (peek(0) == Token.LBRACK) {
1982 consume(Token.LBRACK);
1983 result = doneWithoutConsuming(new DartArrayAccess(result, parseExpression( )));
1984 expect(Token.RBRACK);
1985 } else {
1986 reportUnexpectedToken(position(), null, next());
1987 return result;
1988 }
1989 if (peek(0) == Token.LPAREN) {
1990 while (peek(0) == Token.LPAREN) {
1991 if (functionName != null) {
1992 result = doneWithoutConsuming(new DartMethodInvocation(result, true, f unctionName, parseArguments()));
1993 functionName = null;
1994 } else {
1995 result = doneWithoutConsuming(new DartFunctionObjectInvocation(result, parseArguments()));
1996 }
1997 }
1998 } else if (functionName != null) {
1999 result = doneWithoutConsuming(new DartPropertyAccess(result, true, functio nName));
2000 }
2001 boolean progress = true;
2002 while (progress) {
2003 progress = false;
2004 DartExpression selector = tryParseAssignableSelector(result);
2005 if (selector != null) {
2006 result = selector;
2007 progress = true;
2008 while (peek(0) == Token.LPAREN) {
2009 result = doneWithoutConsuming(new DartFunctionObjectInvocation(result, parseArguments()));
2010 }
2011 }
2012 }
2013 Token token = peek(0);
2014 if (token.isAssignmentOperator()) {
2015 ensureAssignable(result);
2016 consume(token);
2017 int tokenOffset = ctx.getTokenLocation().getBegin().getPos();
2018 result = doneWithoutConsuming(new DartBinaryExpression(token, tokenOffset, result, parseExpressionWithoutCascade()));
2019 }
2020 return result;
2021 }
2022
2023 /**
1920 * expressionList 2024 * expressionList
1921 * : expression (',' expression)* 2025 * : expression (',' expression)*
1922 * ; 2026 * ;
1923 */ 2027 */
1924 @Terminals(tokens={Token.COMMA}) 2028 @Terminals(tokens={Token.COMMA})
1925 private DartExpression parseExpressionList() { 2029 private DartExpression parseExpressionList() {
1926 beginExpressionList(); 2030 beginExpressionList();
1927 DartExpression result = parseExpression(); 2031 DartExpression result = parseExpression();
1928 // Must keep in sync with @Terminals above 2032 // Must keep in sync with @Terminals above
1929 while (optional(Token.COMMA)) { 2033 while (optional(Token.COMMA)) {
(...skipping 1164 matching lines...) Expand 10 before | Expand all | Expand 10 after
3094 switch (peek(0)) { 3198 switch (peek(0)) {
3095 case SEMICOLON: 3199 case SEMICOLON:
3096 case RBRACE: 3200 case RBRACE:
3097 reportError(position(), ParserErrorCode.EXPECTED_IDENTIFIER); 3201 reportError(position(), ParserErrorCode.EXPECTED_IDENTIFIER);
3098 DartIdentifier error = doneWithoutConsuming(new DartIdentifier("")); 3202 DartIdentifier error = doneWithoutConsuming(new DartIdentifier(""));
3099 return doneWithoutConsuming(new DartPropertyAccess(receiver, error)) ; 3203 return doneWithoutConsuming(new DartPropertyAccess(receiver, error)) ;
3100 } 3204 }
3101 DartIdentifier name = parseIdentifier(); 3205 DartIdentifier name = parseIdentifier();
3102 if (peek(0) == Token.LPAREN) { 3206 if (peek(0) == Token.LPAREN) {
3103 boolean save = setAllowFunctionExpression(true); 3207 boolean save = setAllowFunctionExpression(true);
3104 DartMethodInvocation expr = doneWithoutConsuming(new DartMethodInvocat ion(receiver, name, 3208 DartMethodInvocation expr = doneWithoutConsuming(new DartMethodInvocat ion(receiver, false,
3105 parseArguments())); 3209 name, parseArguments()));
3106 setAllowFunctionExpression(save); 3210 setAllowFunctionExpression(save);
3107 return expr; 3211 return expr;
3108 } else { 3212 } else {
3109 return doneWithoutConsuming(new DartPropertyAccess(receiver, name)); 3213 return doneWithoutConsuming(new DartPropertyAccess(receiver, name));
3110 } 3214 }
3111 3215
3112 case LBRACK: 3216 case LBRACK:
3113 consume(Token.LBRACK); 3217 consume(Token.LBRACK);
3114 DartExpression key = parseExpression(); 3218 DartExpression key = parseExpression();
3115 expect(Token.RBRACK); 3219 expect(Token.RBRACK);
(...skipping 1365 matching lines...) Expand 10 before | Expand all | Expand 10 after
4481 } 4585 }
4482 4586
4483 private void reportError(DartNode node, ErrorCode errorCode, Object... argumen ts) { 4587 private void reportError(DartNode node, ErrorCode errorCode, Object... argumen ts) {
4484 reportError(new DartCompilationError(node, errorCode, arguments)); 4588 reportError(new DartCompilationError(node, errorCode, arguments));
4485 } 4589 }
4486 4590
4487 private boolean currentlyParsingToplevel() { 4591 private boolean currentlyParsingToplevel() {
4488 return !(isParsingInterface || isTopLevelAbstract || isParsingClass); 4592 return !(isParsingInterface || isTopLevelAbstract || isParsingClass);
4489 } 4593 }
4490 } 4594 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698