OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library dart2js.parser.partial; |
| 6 |
| 7 import '../common.dart'; |
| 8 import '../options.dart' show ParserOptions; |
| 9 import '../tokens/token.dart' show BeginGroupToken, ErrorToken, Token; |
| 10 import '../tokens/token_constants.dart' as Tokens show EOF_TOKEN; |
| 11 import '../util/characters.dart' as Characters show $CLOSE_CURLY_BRACKET; |
| 12 import 'listener.dart' show Listener; |
| 13 import 'parser.dart' show Parser; |
| 14 |
| 15 class PartialParser extends Parser { |
| 16 PartialParser(Listener listener, ParserOptions options) |
| 17 : super(listener, options); |
| 18 |
| 19 Token parseClassBody(Token token) => skipClassBody(token); |
| 20 |
| 21 Token fullParseClassBody(Token token) => super.parseClassBody(token); |
| 22 |
| 23 Token parseExpression(Token token) => skipExpression(token); |
| 24 |
| 25 Token parseArgumentsOpt(Token token) { |
| 26 // This method is overridden for two reasons: |
| 27 // 1. Avoid generating events for arguments. |
| 28 // 2. Avoid calling skip expression for each argument (which doesn't work). |
| 29 listener.handleNoArguments(token); |
| 30 if (optional('(', token)) { |
| 31 BeginGroupToken begin = token; |
| 32 return begin.endGroup.next; |
| 33 } else { |
| 34 return token; |
| 35 } |
| 36 } |
| 37 |
| 38 Token skipExpression(Token token) { |
| 39 while (true) { |
| 40 final kind = token.kind; |
| 41 final value = token.stringValue; |
| 42 if ((identical(kind, Tokens.EOF_TOKEN)) || |
| 43 (identical(value, ';')) || |
| 44 (identical(value, ',')) || |
| 45 (identical(value, '}')) || |
| 46 (identical(value, ')')) || |
| 47 (identical(value, ']'))) { |
| 48 break; |
| 49 } |
| 50 if (identical(value, '=') || |
| 51 identical(value, '?') || |
| 52 identical(value, ':') || |
| 53 identical(value, '??')) { |
| 54 var nextValue = token.next.stringValue; |
| 55 if (identical(nextValue, 'const')) { |
| 56 token = token.next; |
| 57 nextValue = token.next.stringValue; |
| 58 } |
| 59 if (identical(nextValue, '{')) { |
| 60 // Handle cases like this: |
| 61 // class Foo { |
| 62 // var map; |
| 63 // Foo() : map = {}; |
| 64 // Foo.x() : map = true ? {} : {}; |
| 65 // } |
| 66 BeginGroupToken begin = token.next; |
| 67 token = (begin.endGroup != null) ? begin.endGroup : token; |
| 68 token = token.next; |
| 69 continue; |
| 70 } |
| 71 if (identical(nextValue, '<')) { |
| 72 // Handle cases like this: |
| 73 // class Foo { |
| 74 // var map; |
| 75 // Foo() : map = <String, Foo>{}; |
| 76 // Foo.x() : map = true ? <String, Foo>{} : <String, Foo>{}; |
| 77 // } |
| 78 BeginGroupToken begin = token.next; |
| 79 token = (begin.endGroup != null) ? begin.endGroup : token; |
| 80 token = token.next; |
| 81 if (identical(token.stringValue, '{')) { |
| 82 begin = token; |
| 83 token = (begin.endGroup != null) ? begin.endGroup : token; |
| 84 token = token.next; |
| 85 } |
| 86 continue; |
| 87 } |
| 88 } |
| 89 if (!mayParseFunctionExpressions && identical(value, '{')) { |
| 90 break; |
| 91 } |
| 92 if (token is BeginGroupToken) { |
| 93 BeginGroupToken begin = token; |
| 94 token = (begin.endGroup != null) ? begin.endGroup : token; |
| 95 } else if (token is ErrorToken) { |
| 96 listener.reportErrorToken(token); |
| 97 } |
| 98 token = token.next; |
| 99 } |
| 100 return token; |
| 101 } |
| 102 |
| 103 Token skipClassBody(Token token) { |
| 104 if (!optional('{', token)) { |
| 105 return listener.expectedClassBodyToSkip(token); |
| 106 } |
| 107 BeginGroupToken beginGroupToken = token; |
| 108 Token endGroup = beginGroupToken.endGroup; |
| 109 if (endGroup == null) { |
| 110 return listener.unmatched(beginGroupToken); |
| 111 } else if (!identical(endGroup.kind, Characters.$CLOSE_CURLY_BRACKET)) { |
| 112 return listener.unmatched(beginGroupToken); |
| 113 } |
| 114 return endGroup; |
| 115 } |
| 116 |
| 117 Token skipAsyncModifier(Token token) { |
| 118 String value = token.stringValue; |
| 119 if (identical(value, 'async')) { |
| 120 token = token.next; |
| 121 value = token.stringValue; |
| 122 |
| 123 if (identical(value, '*')) { |
| 124 token = token.next; |
| 125 } |
| 126 } else if (identical(value, 'sync')) { |
| 127 token = token.next; |
| 128 value = token.stringValue; |
| 129 |
| 130 if (identical(value, '*')) { |
| 131 token = token.next; |
| 132 } |
| 133 } |
| 134 return token; |
| 135 } |
| 136 |
| 137 Token parseFunctionBody(Token token, bool isExpression, bool allowAbstract) { |
| 138 assert(!isExpression); |
| 139 token = skipAsyncModifier(token); |
| 140 String value = token.stringValue; |
| 141 if (identical(value, ';')) { |
| 142 if (!allowAbstract) { |
| 143 listener.reportError(token, MessageKind.BODY_EXPECTED); |
| 144 } |
| 145 listener.handleNoFunctionBody(token); |
| 146 } else { |
| 147 if (identical(value, '=>')) { |
| 148 token = parseExpression(token.next); |
| 149 expectSemicolon(token); |
| 150 } else if (value == '=') { |
| 151 token = parseRedirectingFactoryBody(token); |
| 152 expectSemicolon(token); |
| 153 } else { |
| 154 token = skipBlock(token); |
| 155 } |
| 156 listener.skippedFunctionBody(token); |
| 157 } |
| 158 return token; |
| 159 } |
| 160 |
| 161 Token parseFormalParameters(Token token) => skipFormals(token); |
| 162 |
| 163 Token skipFormals(Token token) { |
| 164 listener.beginOptionalFormalParameters(token); |
| 165 if (!optional('(', token)) { |
| 166 if (optional(';', token)) { |
| 167 listener.recoverableError(token, "expected '('"); |
| 168 return token; |
| 169 } |
| 170 return listener.unexpected(token); |
| 171 } |
| 172 BeginGroupToken beginGroupToken = token; |
| 173 Token endToken = beginGroupToken.endGroup; |
| 174 listener.endFormalParameters(0, token, endToken); |
| 175 return endToken.next; |
| 176 } |
| 177 } |
OLD | NEW |