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

Side by Side Diff: lib/compiler/implementation/scanner/parser.dart

Issue 10957060: First stab at parsing redirecting constructors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. Created 8 years, 2 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 /** 5 /**
6 * An event generating parser of Dart programs. This parser expects 6 * An event generating parser of Dart programs. This parser expects
7 * all tokens in a linked list (aka a token stream). 7 * all tokens in a linked list (aka a token stream).
8 * 8 *
9 * The class [Scanner] is used to generate a token stream. See the 9 * The class [Scanner] is used to generate a token stream. See the
10 * file scanner.dart. 10 * file scanner.dart.
(...skipping 823 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 } 834 }
835 listener.endClassBody(count, begin, token); 835 listener.endClassBody(count, begin, token);
836 return token; 836 return token;
837 } 837 }
838 838
839 bool isGetOrSet(Token token) { 839 bool isGetOrSet(Token token) {
840 final String value = token.stringValue; 840 final String value = token.stringValue;
841 return (value === 'get') || (value === 'set'); 841 return (value === 'get') || (value === 'set');
842 } 842 }
843 843
844 bool isFactoryDeclaration(Token token) {
845 if (optional('external', token)) token = token.next;
846 if (optional('const', token)) token = token.next;
847 return optional('factory', token);
848 }
849
844 Token parseMember(Token token) { 850 Token parseMember(Token token) {
845 token = parseMetadataStar(token); 851 token = parseMetadataStar(token);
846 String value = token.stringValue; 852 String value = token.stringValue;
847 if (value === 'factory' || 853 if (isFactoryDeclaration(token)) {
848 (value === 'external' && optional('factory', token.next))) {
849 return parseFactoryMethod(token); 854 return parseFactoryMethod(token);
850 } 855 }
851 Token start = token; 856 Token start = token;
852 listener.beginMember(token); 857 listener.beginMember(token);
853 858
854 Link<Token> identifiers = findMemberName(token); 859 Link<Token> identifiers = findMemberName(token);
855 if (identifiers.isEmpty()) { 860 if (identifiers.isEmpty()) {
856 return listener.unexpected(start); 861 return listener.unexpected(start);
857 } 862 }
858 Token name = identifiers.head; 863 Token name = identifiers.head;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 token = parseQualifiedRestOpt(token); 943 token = parseQualifiedRestOpt(token);
939 token = parseFormalParametersOpt(token); 944 token = parseFormalParametersOpt(token);
940 token = parseInitializersOpt(token); 945 token = parseInitializersOpt(token);
941 token = parseFunctionBody(token, false); 946 token = parseFunctionBody(token, false);
942 listener.endMethod(getOrSet, start, token); 947 listener.endMethod(getOrSet, start, token);
943 } 948 }
944 return token.next; 949 return token.next;
945 } 950 }
946 951
947 Token parseFactoryMethod(Token token) { 952 Token parseFactoryMethod(Token token) {
948 assert((optional('external', token) && optional('factory', token.next)) || 953 assert(isFactoryDeclaration(token));
949 optional('factory', token));
950 Token start = token; 954 Token start = token;
951 if (token.stringValue === 'external') token = token.next; 955 if (token.stringValue === 'external') token = token.next;
956 Token constKeyword = null;
957 if (optional('const', token)) {
958 constKeyword = token;
959 token = token.next;
960 }
952 Token factoryKeyword = token; 961 Token factoryKeyword = token;
953 listener.beginFactoryMethod(factoryKeyword); 962 listener.beginFactoryMethod(factoryKeyword);
954 token = token.next; // Skip 'factory'. 963 token = token.next; // Skip 'factory'.
955 token = parseIdentifier(token); 964 token = parseIdentifier(token);
956 token = parseQualifiedRestOpt(token); 965 token = parseQualifiedRestOpt(token);
957 token = parseTypeVariablesOpt(token); 966 token = parseTypeVariablesOpt(token);
958 Token period = null; 967 Token period = null;
959 if (optional('.', token)) { 968 if (optional('.', token)) {
960 period = token; 969 period = token;
961 token = parseIdentifier(token.next); 970 token = parseIdentifier(token.next);
962 } 971 }
963 token = parseFormalParameters(token); 972 token = parseFormalParameters(token);
964 token = parseFunctionBody(token, false); 973 if (optional('=', token)) {
974 token = parseRedirectingFactoryBody(token);
975 } else {
976 token = parseFunctionBody(token, false);
977 }
965 listener.endFactoryMethod(start, period, token); 978 listener.endFactoryMethod(start, period, token);
966 return token.next; 979 return token.next;
967 } 980 }
968 981
969 Token parseOperatorName(Token token) { 982 Token parseOperatorName(Token token) {
970 assert(optional('operator', token)); 983 assert(optional('operator', token));
971 if (isUserDefinableOperator(token.next.stringValue)) { 984 if (isUserDefinableOperator(token.next.stringValue)) {
972 Token operator = token; 985 Token operator = token;
973 token = token.next; 986 token = token.next;
974 listener.handleOperatorName(operator, token); 987 listener.handleOperatorName(operator, token);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1029 token = parseIdentifier(token); 1042 token = parseIdentifier(token);
1030 listener.endFunctionName(token); 1043 listener.endFunctionName(token);
1031 token = parseFormalParameters(token); 1044 token = parseFormalParameters(token);
1032 listener.handleNoInitializers(); 1045 listener.handleNoInitializers();
1033 bool isBlock = optional('{', token); 1046 bool isBlock = optional('{', token);
1034 token = parseFunctionBody(token, true); 1047 token = parseFunctionBody(token, true);
1035 listener.endFunction(null, token); 1048 listener.endFunction(null, token);
1036 return isBlock ? token.next : token; 1049 return isBlock ? token.next : token;
1037 } 1050 }
1038 1051
1052 Token parseQualifiedList(Token token) {
1053 listener.beginQualifiedList(token);
1054 Token parseQualifiedPart(Token token) {
ahe 2012/10/09 10:51:37 Why is this a nested function?
Lasse Reichstein Nielsen 2012/10/09 11:51:26 because I didn't want to duplicate the code, but i
ahe 2012/10/09 12:16:51 There is no reason to create extra overhead by cre
1055 Token start = token;
1056 token = parseIdentifier(token);
1057 token = parseTypeVariablesOpt(token);
1058 listener.endType(start, null);
1059 return token;
1060 }
1061 Token beginToken = token;
1062 token = parseQualifiedPart(token);
1063 int count = 1;
1064 while (optional('.', token)) {
1065 token = parseQualifiedPart(token.next);
1066 count++;
1067 }
1068 listener.endQualifiedList(count);
1069 return token;
1070 }
1071
1072 Token parseRedirectingFactoryBody(Token token) {
1073 assert(optional('=', token));
1074 Token equals = token;
1075 listener.beginRedirectingFactoryBody(token);
ahe 2012/10/09 10:51:37 Move this before the assert.
Lasse Reichstein Nielsen 2012/10/09 11:51:26 Done.
1076 token = parseQualifiedList(token.next);
1077 Token semicolon = token;
1078 expectSemicolon(token);
1079 listener.endRedirectingFactoryBody(equals, semicolon);
1080 return token;
1081 }
1082
1039 Token parseFunctionBody(Token token, bool isExpression) { 1083 Token parseFunctionBody(Token token, bool isExpression) {
1040 if (optional(';', token)) { 1084 if (optional(';', token)) {
1041 listener.endFunctionBody(0, null, token); 1085 listener.endFunctionBody(0, null, token);
1042 return token; 1086 return token;
1043 } else if (optional('=>', token)) { 1087 } else if (optional('=>', token)) {
1044 Token begin = token; 1088 Token begin = token;
1045 token = parseExpression(token.next); 1089 token = parseExpression(token.next);
1046 if (!isExpression) { 1090 if (!isExpression) {
1047 expectSemicolon(token); 1091 expectSemicolon(token);
1048 listener.endReturnStatement(true, begin, token); 1092 listener.endReturnStatement(true, begin, token);
(...skipping 1041 matching lines...) Expand 10 before | Expand all | Expand 10 after
2090 } 2134 }
2091 listener.handleContinueStatement(hasTarget, continueKeyword, token); 2135 listener.handleContinueStatement(hasTarget, continueKeyword, token);
2092 return expectSemicolon(token); 2136 return expectSemicolon(token);
2093 } 2137 }
2094 2138
2095 Token parseEmptyStatement(Token token) { 2139 Token parseEmptyStatement(Token token) {
2096 listener.handleEmptyStatement(token); 2140 listener.handleEmptyStatement(token);
2097 return expectSemicolon(token); 2141 return expectSemicolon(token);
2098 } 2142 }
2099 } 2143 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698