| 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 (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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 Token parseInterfaceBody(Token token) { | 62 Token parseInterfaceBody(Token token) { |
| 63 return parseClassBody(token); | 63 return parseClassBody(token); |
| 64 } | 64 } |
| 65 | 65 |
| 66 Token parseNamedFunctionAlias(Token token) { | 66 Token parseNamedFunctionAlias(Token token) { |
| 67 Token typedefKeyword = token; | 67 Token typedefKeyword = token; |
| 68 listener.beginFunctionTypeAlias(token); | 68 listener.beginFunctionTypeAlias(token); |
| 69 token = parseReturnTypeOpt(token.next); | 69 token = parseReturnTypeOpt(token.next); |
| 70 token = parseIdentifier(token); | 70 token = parseIdentifier(token); |
| 71 token = parseTypeVariablesOpt(token); | 71 token = parseTypeVariablesOpt(token); |
| 72 token = parseFormalParameters(token); | 72 token = parseFormalParameters(token, DISALLOW_VALUES_FOR_NAMED_OPTIONAL_PARA
MS); |
| 73 listener.endFunctionTypeAlias(typedefKeyword, token); | 73 listener.endFunctionTypeAlias(typedefKeyword, token); |
| 74 return expect(';', token); | 74 return expect(';', token); |
| 75 } | 75 } |
| 76 | 76 |
| 77 Token parseReturnTypeOpt(Token token) { | 77 Token parseReturnTypeOpt(Token token) { |
| 78 if (token.stringValue === 'void') { | 78 if (token.stringValue === 'void') { |
| 79 listener.handleVoidKeyword(token); | 79 listener.handleVoidKeyword(token); |
| 80 return token.next; | 80 return token.next; |
| 81 } else { | 81 } else { |
| 82 return parseTypeOpt(token); | 82 return parseTypeOpt(token); |
| 83 } | 83 } |
| 84 } | 84 } |
| 85 | 85 |
| 86 Token parseFormalParameters(Token token) { | 86 final bool DISALLOW_VALUES_FOR_NAMED_OPTIONAL_PARAMS = false; |
| 87 Token parseFormalParameters(Token token, [bool allowValuesForNamedOptionalPara
ms = true]) { |
| 87 Token begin = token; | 88 Token begin = token; |
| 88 listener.beginFormalParameters(begin); | 89 listener.beginFormalParameters(begin); |
| 89 expect('(', token); | 90 expect('(', token); |
| 90 int parameterCount = 0; | 91 int parameterCount = 0; |
| 91 if (optional(')', token.next)) { | 92 if (optional(')', token.next)) { |
| 92 listener.endFormalParameters(parameterCount, begin, token.next); | 93 listener.endFormalParameters(parameterCount, begin, token.next); |
| 93 return token.next.next; | 94 return token.next.next; |
| 94 } | 95 } |
| 95 do { | 96 do { |
| 96 ++parameterCount; | 97 ++parameterCount; |
| 97 token = token.next; | 98 token = token.next; |
| 98 if (optional('[', token)) { | 99 if (optional('[', token)) { |
| 99 token = parseOptionalFormalParameters(token); | 100 token = parseOptionalFormalParameters(token, allowValuesForNamedOptional
Params); |
| 100 break; | 101 break; |
| 101 } | 102 } |
| 102 token = parseFormalParameter(token); | 103 token = parseFormalParameter(token, allowValuesForNamedOptionalParams); |
| 103 } while (optional(',', token)); | 104 } while (optional(',', token)); |
| 104 listener.endFormalParameters(parameterCount, begin, token); | 105 listener.endFormalParameters(parameterCount, begin, token); |
| 105 return expect(')', token); | 106 return expect(')', token); |
| 106 } | 107 } |
| 107 | 108 |
| 108 Token parseFormalParameter(Token token) { | 109 Token parseFormalParameter(Token token, bool allowValuesForNamedOptionalParams
) { |
| 109 listener.beginFormalParameter(token); | 110 listener.beginFormalParameter(token); |
| 110 token = parseModifiers(token); | 111 token = parseModifiers(token); |
| 111 // TODO(ahe): Validate that there are formal parameters if void. | 112 // TODO(ahe): Validate that there are formal parameters if void. |
| 112 token = parseReturnTypeOpt(token); | 113 token = parseReturnTypeOpt(token); |
| 113 Token thisKeyword = null; | 114 Token thisKeyword = null; |
| 114 if (optional('this', token)) { | 115 if (optional('this', token)) { |
| 115 thisKeyword = token; | 116 thisKeyword = token; |
| 116 // TODO(ahe): Validate field initializers are only used in | 117 // TODO(ahe): Validate field initializers are only used in |
| 117 // constructors, and not for function-typed arguments. | 118 // constructors, and not for function-typed arguments. |
| 118 token = expect('.', token.next); | 119 token = expect('.', token.next); |
| 119 } | 120 } |
| 120 token = parseIdentifier(token); | 121 token = parseIdentifier(token); |
| 121 if (optional('(', token)) { | 122 if (optional('(', token)) { |
| 122 token = parseFormalParameters(token); | 123 token = parseFormalParameters(token, DISALLOW_VALUES_FOR_NAMED_OPTIONAL_PA
RAMS); |
| 123 listener.handleFunctionTypedFormalParameter(token); | 124 listener.handleFunctionTypedFormalParameter(token); |
| 124 } | 125 } |
| 125 if (optional('=', token)) { | 126 if (optional('=', token)) { |
| 127 if (!allowValuesForNamedOptionalParams) { |
| 128 listener.recoverableError("parameter must not specify a default value",
token: token); |
| 129 } |
| 130 |
| 126 // TODO(ahe): Validate that these are only used for optional parameters. | 131 // TODO(ahe): Validate that these are only used for optional parameters. |
| 127 Token equal = token; | 132 Token equal = token; |
| 128 token = parseExpression(token.next); | 133 token = parseExpression(token.next); |
| 129 listener.handleValuedFormalParameter(equal, token); | 134 listener.handleValuedFormalParameter(equal, token); |
| 130 } | 135 } |
| 131 listener.endFormalParameter(token, thisKeyword); | 136 listener.endFormalParameter(token, thisKeyword); |
| 132 return token; | 137 return token; |
| 133 } | 138 } |
| 134 | 139 |
| 135 Token parseOptionalFormalParameters(Token token) { | 140 Token parseOptionalFormalParameters(Token token, bool allowValuesForNamedOptio
nalParams) { |
| 136 Token begin = token; | 141 Token begin = token; |
| 137 listener.beginOptionalFormalParameters(begin); | 142 listener.beginOptionalFormalParameters(begin); |
| 138 assert(optional('[', token)); | 143 assert(optional('[', token)); |
| 139 int parameterCount = 0; | 144 int parameterCount = 0; |
| 140 do { | 145 do { |
| 141 token = token.next; | 146 token = token.next; |
| 142 token = parseFormalParameter(token); | 147 token = parseFormalParameter(token, allowValuesForNamedOptionalParams); |
| 143 ++parameterCount; | 148 ++parameterCount; |
| 144 } while (optional(',', token)); | 149 } while (optional(',', token)); |
| 145 listener.endOptionalFormalParameters(parameterCount, begin, token); | 150 listener.endOptionalFormalParameters(parameterCount, begin, token); |
| 146 return expect(']', token); | 151 return expect(']', token); |
| 147 } | 152 } |
| 148 | 153 |
| 149 Token parseTypeOpt(Token token) { | 154 Token parseTypeOpt(Token token) { |
| 150 String value = token.stringValue; | 155 String value = token.stringValue; |
| 151 if (value === 'var') return parseType(token); | 156 if (value === 'var') return parseType(token); |
| 152 if (value !== 'this') { | 157 if (value !== 'this') { |
| (...skipping 1561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1714 } | 1719 } |
| 1715 listener.handleContinueStatement(hasTarget, continueKeyword, token); | 1720 listener.handleContinueStatement(hasTarget, continueKeyword, token); |
| 1716 return expectSemicolon(token); | 1721 return expectSemicolon(token); |
| 1717 } | 1722 } |
| 1718 | 1723 |
| 1719 Token parseEmptyStatement(Token token) { | 1724 Token parseEmptyStatement(Token token) { |
| 1720 listener.handleEmptyStatement(token); | 1725 listener.handleEmptyStatement(token); |
| 1721 return expectSemicolon(token); | 1726 return expectSemicolon(token); |
| 1722 } | 1727 } |
| 1723 } | 1728 } |
| OLD | NEW |