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