OLD | NEW |
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. | 7 * all tokens in a linked list. |
8 */ | 8 */ |
9 class Parser { | 9 class Parser { |
10 final Listener listener; | 10 final Listener listener; |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 token = parseType(token.next); | 229 token = parseType(token.next); |
230 ++interfacesCount; | 230 ++interfacesCount; |
231 } while (optional(',', token)); | 231 } while (optional(',', token)); |
232 } | 232 } |
233 token = parseClassBody(token); | 233 token = parseClassBody(token); |
234 listener.endClassDeclaration(interfacesCount, begin, extendsKeyword, | 234 listener.endClassDeclaration(interfacesCount, begin, extendsKeyword, |
235 implementsKeyword, token); | 235 implementsKeyword, token); |
236 return token.next; | 236 return token.next; |
237 } | 237 } |
238 | 238 |
239 | |
240 Token parseStringPart(Token token) { | 239 Token parseStringPart(Token token) { |
241 if (token.kind === STRING_TOKEN) { | 240 if (token.kind === STRING_TOKEN) { |
242 listener.handleStringPart(token); | 241 listener.handleStringPart(token); |
243 return token.next; | 242 return token.next; |
244 } else { | 243 } else { |
245 return listener.expected('string', token); | 244 return listener.expected('string', token); |
246 } | 245 } |
247 } | 246 } |
248 | 247 |
249 Token parseIdentifier(Token token) { | 248 Token parseIdentifier(Token token) { |
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
849 } | 848 } |
850 | 849 |
851 Token parseExpressionStatement(Token token) { | 850 Token parseExpressionStatement(Token token) { |
852 listener.beginExpressionStatement(token); | 851 listener.beginExpressionStatement(token); |
853 token = parseExpression(token); | 852 token = parseExpression(token); |
854 listener.endExpressionStatement(token); | 853 listener.endExpressionStatement(token); |
855 return expectSemicolon(token); | 854 return expectSemicolon(token); |
856 } | 855 } |
857 | 856 |
858 Token parseExpression(Token token) { | 857 Token parseExpression(Token token) { |
859 return parsePrecedenceExpression(token, 2); | 858 return parsePrecedenceExpression(token, CASCADE_PRECEDENCE); |
860 } | 859 } |
861 | 860 |
862 Token parseConditionalExpressionRest(Token token) { | 861 Token parseConditionalExpressionRest(Token token) { |
863 assert(optional('?', token)); | 862 assert(optional('?', token)); |
864 Token question = token; | 863 Token question = token; |
865 token = parseExpression(token.next); | 864 token = parseExpression(token.next); |
866 Token colon = token; | 865 Token colon = token; |
867 token = expect(':', token); | 866 token = expect(':', token); |
868 token = parseExpression(token); | 867 token = parseExpression(token); |
869 listener.handleConditionalExpression(question, colon); | 868 listener.handleConditionalExpression(question, colon); |
870 return token; | 869 return token; |
871 } | 870 } |
872 | 871 |
873 Token parsePrecedenceExpression(Token token, int precedence) { | 872 Token parsePrecedenceExpression(Token token, int precedence) { |
874 assert(precedence >= 2); | 873 assert(precedence >= 1); |
875 assert(precedence <= POSTFIX_PRECEDENCE); | 874 assert(precedence <= POSTFIX_PRECEDENCE); |
876 token = parseUnaryExpression(token); | 875 token = parseUnaryExpression(token); |
877 PrecedenceInfo info = token.info; | 876 PrecedenceInfo info = token.info; |
878 int tokenLevel = info.precedence; | 877 int tokenLevel = info.precedence; |
879 for (int level = tokenLevel; level >= precedence; --level) { | 878 for (int level = tokenLevel; level >= precedence; --level) { |
880 while (tokenLevel === level) { | 879 while (tokenLevel === level) { |
881 Token operator = token; | 880 Token operator = token; |
882 if (tokenLevel === ASSIGNMENT_PRECEDENCE) { | 881 if (tokenLevel === CASCADE_PRECEDENCE) { |
| 882 token = parseCascadeExpression(token); |
| 883 } else if (tokenLevel === ASSIGNMENT_PRECEDENCE) { |
883 // Right associative, so we recurse at the same precedence | 884 // Right associative, so we recurse at the same precedence |
884 // level. | 885 // level. |
885 token = parsePrecedenceExpression(token.next, level); | 886 token = parsePrecedenceExpression(token.next, level); |
886 listener.handleAssignmentExpression(operator); | 887 listener.handleAssignmentExpression(operator); |
887 } else if (tokenLevel === POSTFIX_PRECEDENCE) { | 888 } else if (tokenLevel === POSTFIX_PRECEDENCE) { |
888 if (info === PERIOD_INFO) { | 889 if (info === PERIOD_INFO) { |
889 // Left associative, so we recurse at the next higher | 890 // Left associative, so we recurse at the next higher |
890 // precedence level. However, POSTFIX_PRECEDENCE is the | 891 // precedence level. However, POSTFIX_PRECEDENCE is the |
891 // highest level, so we just call parseUnaryExpression | 892 // highest level, so we just call parseUnaryExpression |
892 // directly. | 893 // directly. |
(...skipping 19 matching lines...) Expand all Loading... |
912 token = parsePrecedenceExpression(token.next, level + 1); | 913 token = parsePrecedenceExpression(token.next, level + 1); |
913 listener.handleBinaryExpression(operator); | 914 listener.handleBinaryExpression(operator); |
914 } | 915 } |
915 info = token.info; | 916 info = token.info; |
916 tokenLevel = info.precedence; | 917 tokenLevel = info.precedence; |
917 } | 918 } |
918 } | 919 } |
919 return token; | 920 return token; |
920 } | 921 } |
921 | 922 |
| 923 Token parseCascadeExpression(Token token) { |
| 924 listener.beginCascade(token); |
| 925 assert(optional('..', token)); |
| 926 Token cascadeOperator = token; |
| 927 if (optional('[', token)) { |
| 928 token = parseArgumentOrIndexStar(token); |
| 929 } else if (isIdentifier(token)) { |
| 930 token = parseSend(token); |
| 931 listener.handleBinaryExpression(cascadeOperator); |
| 932 } else { |
| 933 return listener.unexpected(token); |
| 934 } |
| 935 Token mark; |
| 936 do { |
| 937 mark = token; |
| 938 if (optional('.', token)) { |
| 939 Token period = token; |
| 940 token = parseSend(period.token); |
| 941 listener.handleBinaryExpression(period); |
| 942 } |
| 943 token = parseArgumentOrIndexStar(token); |
| 944 } while (mark !== token); |
| 945 |
| 946 if (token.info.precedence === ASSIGNMENT_PRECEDENCE) { |
| 947 Token assignment = token; |
| 948 token = parsePrecedenceExpression(token.token, CASCADE_PRECEDENCE + 1); |
| 949 listener.handleAssignmentExpression(assignment); |
| 950 } |
| 951 listener.endCascade(); |
| 952 return token; |
| 953 } |
| 954 |
922 Token parseUnaryExpression(Token token) { | 955 Token parseUnaryExpression(Token token) { |
923 String value = token.stringValue; | 956 String value = token.stringValue; |
924 // Prefix: | 957 // Prefix: |
925 if (value === '+') { | 958 if (value === '+') { |
926 // Dart only allows "prefix plus" as an initial part of a | 959 // Dart only allows "prefix plus" as an initial part of a |
927 // decimal literal. We scan it as a separate token and let | 960 // decimal literal. We scan it as a separate token and let |
928 // the parser listener combine it with the digits. | 961 // the parser listener combine it with the digits. |
929 Token next = token.next; | 962 Token next = token.next; |
930 if (next.charOffset === token.charOffset + 1) { | 963 if (next.charOffset === token.charOffset + 1) { |
931 if (next.kind === INT_TOKEN) { | 964 if (next.kind === INT_TOKEN) { |
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1583 } | 1616 } |
1584 listener.handleContinueStatement(hasTarget, continueKeyword, token); | 1617 listener.handleContinueStatement(hasTarget, continueKeyword, token); |
1585 return expectSemicolon(token); | 1618 return expectSemicolon(token); |
1586 } | 1619 } |
1587 | 1620 |
1588 Token parseEmptyStatement(Token token) { | 1621 Token parseEmptyStatement(Token token) { |
1589 listener.handleEmptyStatement(token); | 1622 listener.handleEmptyStatement(token); |
1590 return expectSemicolon(token); | 1623 return expectSemicolon(token); |
1591 } | 1624 } |
1592 } | 1625 } |
OLD | NEW |