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