| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 library dart_parser.class_member_parser; | 5 library dart_parser.class_member_parser; |
| 6 | 6 |
| 7 import 'package:dart_scanner/src/token.dart' show | 7 import 'package:dart_scanner/src/token.dart' show |
| 8 BeginGroupToken, | |
| 9 ErrorToken, | |
| 10 Token; | 8 Token; |
| 11 | 9 |
| 12 import 'package:dart_scanner/src/token_constants.dart' show | |
| 13 EOF_TOKEN; | |
| 14 | |
| 15 import 'listener.dart' show | 10 import 'listener.dart' show |
| 16 Listener; | 11 Listener; |
| 17 | 12 |
| 18 import 'error_kind.dart' show | |
| 19 ErrorKind; | |
| 20 | |
| 21 import 'parser.dart' show | 13 import 'parser.dart' show |
| 22 Parser, | 14 Parser; |
| 23 optional; | |
| 24 | 15 |
| 25 /// Parser similar to [TopLevelParser] but also parses class members (excluding | 16 /// Parser similar to [TopLevelParser] but also parses class members (excluding |
| 26 /// their bodies). | 17 /// their bodies). |
| 27 class ClassMemberParser extends Parser { | 18 class ClassMemberParser extends Parser { |
| 28 ClassMemberParser(Listener listener, | 19 ClassMemberParser(Listener listener, |
| 29 {bool asyncAwaitKeywordsEnabled: false, | 20 {bool asyncAwaitKeywordsEnabled: false, |
| 30 bool enableGenericMethodSyntax: false}) | 21 bool enableGenericMethodSyntax: false}) |
| 31 : super(listener, asyncAwaitKeywordsEnabled: asyncAwaitKeywordsEnabled, | 22 : super(listener, asyncAwaitKeywordsEnabled: asyncAwaitKeywordsEnabled, |
| 32 enableGenericMethodSyntax: enableGenericMethodSyntax); | 23 enableGenericMethodSyntax: enableGenericMethodSyntax); |
| 33 | 24 |
| 34 Token parseExpression(Token token) => skipExpression(token); | 25 Token parseExpression(Token token) => skipExpression(token); |
| 35 | 26 |
| 36 Token parseArgumentsOpt(Token token) { | 27 // This method is overridden for two reasons: |
| 37 // This method is overridden for two reasons: | 28 // 1. Avoid generating events for arguments. |
| 38 // 1. Avoid generating events for arguments. | 29 // 2. Avoid calling skip expression for each argument (which doesn't work). |
| 39 // 2. Avoid calling skip expression for each argument (which doesn't work). | 30 Token parseArgumentsOpt(Token token) => skipArgumentsOpt(token); |
| 40 listener.handleNoArguments(token); | |
| 41 if (optional('(', token)) { | |
| 42 BeginGroupToken begin = token; | |
| 43 return begin.endGroup.next; | |
| 44 } else { | |
| 45 return token; | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 Token skipExpression(Token token) { | |
| 50 while (true) { | |
| 51 final kind = token.kind; | |
| 52 final value = token.stringValue; | |
| 53 if ((identical(kind, EOF_TOKEN)) || | |
| 54 (identical(value, ';')) || | |
| 55 (identical(value, ',')) || | |
| 56 (identical(value, '}')) || | |
| 57 (identical(value, ')')) || | |
| 58 (identical(value, ']'))) { | |
| 59 break; | |
| 60 } | |
| 61 if (identical(value, '=') || | |
| 62 identical(value, '?') || | |
| 63 identical(value, ':') || | |
| 64 identical(value, '??')) { | |
| 65 var nextValue = token.next.stringValue; | |
| 66 if (identical(nextValue, 'const')) { | |
| 67 token = token.next; | |
| 68 nextValue = token.next.stringValue; | |
| 69 } | |
| 70 if (identical(nextValue, '{')) { | |
| 71 // Handle cases like this: | |
| 72 // class Foo { | |
| 73 // var map; | |
| 74 // Foo() : map = {}; | |
| 75 // Foo.x() : map = true ? {} : {}; | |
| 76 // } | |
| 77 BeginGroupToken begin = token.next; | |
| 78 token = (begin.endGroup != null) ? begin.endGroup : token; | |
| 79 token = token.next; | |
| 80 continue; | |
| 81 } | |
| 82 if (identical(nextValue, '<')) { | |
| 83 // Handle cases like this: | |
| 84 // class Foo { | |
| 85 // var map; | |
| 86 // Foo() : map = <String, Foo>{}; | |
| 87 // Foo.x() : map = true ? <String, Foo>{} : <String, Foo>{}; | |
| 88 // } | |
| 89 BeginGroupToken begin = token.next; | |
| 90 token = (begin.endGroup != null) ? begin.endGroup : token; | |
| 91 token = token.next; | |
| 92 if (identical(token.stringValue, '{')) { | |
| 93 begin = token; | |
| 94 token = (begin.endGroup != null) ? begin.endGroup : token; | |
| 95 token = token.next; | |
| 96 } | |
| 97 continue; | |
| 98 } | |
| 99 } | |
| 100 if (!mayParseFunctionExpressions && identical(value, '{')) { | |
| 101 break; | |
| 102 } | |
| 103 if (token is BeginGroupToken) { | |
| 104 BeginGroupToken begin = token; | |
| 105 token = (begin.endGroup != null) ? begin.endGroup : token; | |
| 106 } else if (token is ErrorToken) { | |
| 107 listener.reportErrorToken(token); | |
| 108 } | |
| 109 token = token.next; | |
| 110 } | |
| 111 return token; | |
| 112 } | |
| 113 | |
| 114 Token skipAsyncModifier(Token token) { | |
| 115 String value = token.stringValue; | |
| 116 if (identical(value, 'async')) { | |
| 117 token = token.next; | |
| 118 value = token.stringValue; | |
| 119 | |
| 120 if (identical(value, '*')) { | |
| 121 token = token.next; | |
| 122 } | |
| 123 } else if (identical(value, 'sync')) { | |
| 124 token = token.next; | |
| 125 value = token.stringValue; | |
| 126 | |
| 127 if (identical(value, '*')) { | |
| 128 token = token.next; | |
| 129 } | |
| 130 } | |
| 131 return token; | |
| 132 } | |
| 133 | 31 |
| 134 Token parseFunctionBody(Token token, bool isExpression, bool allowAbstract) { | 32 Token parseFunctionBody(Token token, bool isExpression, bool allowAbstract) { |
| 135 assert(!isExpression); | 33 return skipFunctionBody(token, isExpression, allowAbstract); |
| 136 token = skipAsyncModifier(token); | |
| 137 String value = token.stringValue; | |
| 138 if (identical(value, ';')) { | |
| 139 if (!allowAbstract) { | |
| 140 listener.reportError(token, ErrorKind.EXPECTED_BODY); | |
| 141 } | |
| 142 listener.handleNoFunctionBody(token); | |
| 143 } else { | |
| 144 if (identical(value, '=>')) { | |
| 145 token = parseExpression(token.next); | |
| 146 expectSemicolon(token); | |
| 147 } else if (value == '=') { | |
| 148 token = parseRedirectingFactoryBody(token); | |
| 149 expectSemicolon(token); | |
| 150 } else { | |
| 151 token = skipBlock(token); | |
| 152 } | |
| 153 listener.skippedFunctionBody(token); | |
| 154 } | |
| 155 return token; | |
| 156 } | 34 } |
| 157 } | 35 } |
| OLD | NEW |