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 library dart2js.parser; | 5 library dart2js.parser; |
6 | 6 |
7 import '../common.dart'; | 7 import '../common.dart'; |
8 import '../tokens/keyword.dart' show | 8 import '../tokens/keyword.dart' show |
9 Keyword; | 9 Keyword; |
10 import '../tokens/precedence.dart' show | 10 import '../tokens/precedence.dart' show |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 * | 91 * |
92 * Parse methods are generally named parseGrammarProductionSuffix. The | 92 * Parse methods are generally named parseGrammarProductionSuffix. The |
93 * suffix can be one of "opt", or "star". "opt" means zero or one | 93 * suffix can be one of "opt", or "star". "opt" means zero or one |
94 * matches, "star" means zero or more matches. For example, | 94 * matches, "star" means zero or more matches. For example, |
95 * [parseMetadataStar] corresponds to this grammar snippet: [: | 95 * [parseMetadataStar] corresponds to this grammar snippet: [: |
96 * metadata* :], and [parseTypeOpt] corresponds to: [: type? :]. | 96 * metadata* :], and [parseTypeOpt] corresponds to: [: type? :]. |
97 */ | 97 */ |
98 class Parser { | 98 class Parser { |
99 final Listener listener; | 99 final Listener listener; |
100 bool mayParseFunctionExpressions = true; | 100 bool mayParseFunctionExpressions = true; |
101 final bool enableConditionalDirectives; | |
102 bool asyncAwaitKeywordsEnabled; | 101 bool asyncAwaitKeywordsEnabled; |
103 | 102 |
104 Parser(this.listener, | 103 Parser(this.listener, |
105 {this.enableConditionalDirectives: false, | 104 {this.asyncAwaitKeywordsEnabled: false}); |
106 this.asyncAwaitKeywordsEnabled: false}); | |
107 | 105 |
108 Token parseUnit(Token token) { | 106 Token parseUnit(Token token) { |
109 listener.beginCompilationUnit(token); | 107 listener.beginCompilationUnit(token); |
110 int count = 0; | 108 int count = 0; |
111 while (!identical(token.kind, EOF_TOKEN)) { | 109 while (!identical(token.kind, EOF_TOKEN)) { |
112 token = parseTopLevelDeclaration(token); | 110 token = parseTopLevelDeclaration(token); |
113 listener.endTopLevelDeclaration(token); | 111 listener.endTopLevelDeclaration(token); |
114 count++; | 112 count++; |
115 } | 113 } |
116 listener.endCompilationUnit(count, token); | 114 listener.endCompilationUnit(count, token); |
(...skipping 28 matching lines...) Expand all Loading... |
145 Token libraryKeyword = token; | 143 Token libraryKeyword = token; |
146 listener.beginLibraryName(libraryKeyword); | 144 listener.beginLibraryName(libraryKeyword); |
147 assert(optional('library', token)); | 145 assert(optional('library', token)); |
148 token = parseQualified(token.next); | 146 token = parseQualified(token.next); |
149 Token semicolon = token; | 147 Token semicolon = token; |
150 token = expect(';', token); | 148 token = expect(';', token); |
151 listener.endLibraryName(libraryKeyword, semicolon); | 149 listener.endLibraryName(libraryKeyword, semicolon); |
152 return token; | 150 return token; |
153 } | 151 } |
154 | 152 |
155 /// import uri (if (test) uri)* (as identifier)? combinator* ';' | 153 /// import uri (as identifier)? combinator* ';' |
156 Token parseImport(Token token) { | 154 Token parseImport(Token token) { |
157 Token importKeyword = token; | 155 Token importKeyword = token; |
158 listener.beginImport(importKeyword); | 156 listener.beginImport(importKeyword); |
159 assert(optional('import', token)); | 157 assert(optional('import', token)); |
160 token = parseLiteralStringOrRecoverExpression(token.next); | 158 token = parseLiteralStringOrRecoverExpression(token.next); |
161 token = parseConditionalUris(token); | |
162 Token deferredKeyword; | 159 Token deferredKeyword; |
163 if (optional('deferred', token)) { | 160 if (optional('deferred', token)) { |
164 deferredKeyword = token; | 161 deferredKeyword = token; |
165 token = token.next; | 162 token = token.next; |
166 } | 163 } |
167 Token asKeyword; | 164 Token asKeyword; |
168 if (optional('as', token)) { | 165 if (optional('as', token)) { |
169 asKeyword = token; | 166 asKeyword = token; |
170 token = parseIdentifier(token.next); | 167 token = parseIdentifier(token.next); |
171 } | 168 } |
172 token = parseCombinators(token); | 169 token = parseCombinators(token); |
173 Token semicolon = token; | 170 Token semicolon = token; |
174 token = expect(';', token); | 171 token = expect(';', token); |
175 listener.endImport(importKeyword, deferredKeyword, asKeyword, semicolon); | 172 listener.endImport(importKeyword, deferredKeyword, asKeyword, semicolon); |
176 return token; | 173 return token; |
177 } | 174 } |
178 | 175 |
179 /// if (test) uri | 176 /// export uri combinator* ';' |
180 Token parseConditionalUris(Token token) { | |
181 listener.beginConditionalUris(token); | |
182 int count = 0; | |
183 if (enableConditionalDirectives) { | |
184 while (optional('if', token)) { | |
185 count++; | |
186 token = parseConditionalUri(token); | |
187 } | |
188 } | |
189 listener.endConditionalUris(count); | |
190 return token; | |
191 } | |
192 | |
193 Token parseConditionalUri(Token token) { | |
194 listener.beginConditionalUri(token); | |
195 Token ifKeyword = token; | |
196 token = expect('if', token); | |
197 token = expect('(', token); | |
198 token = parseDottedName(token); | |
199 Token equalitySign; | |
200 if (optional('==', token)) { | |
201 equalitySign = token; | |
202 token = parseLiteralStringOrRecoverExpression(token.next); | |
203 } | |
204 token = expect(')', token); | |
205 token = parseLiteralStringOrRecoverExpression(token); | |
206 listener.endConditionalUri(ifKeyword, equalitySign); | |
207 return token; | |
208 } | |
209 | |
210 Token parseDottedName(Token token) { | |
211 listener.beginDottedName(token); | |
212 Token firstIdentifier = token; | |
213 token = parseIdentifier(token); | |
214 int count = 1; | |
215 while (optional('.', token)) { | |
216 token = parseIdentifier(token.next); | |
217 count++; | |
218 } | |
219 listener.endDottedName(count, firstIdentifier); | |
220 return token; | |
221 } | |
222 | |
223 /// export uri conditional-uris* combinator* ';' | |
224 Token parseExport(Token token) { | 177 Token parseExport(Token token) { |
225 Token exportKeyword = token; | 178 Token exportKeyword = token; |
226 listener.beginExport(exportKeyword); | 179 listener.beginExport(exportKeyword); |
227 assert(optional('export', token)); | 180 assert(optional('export', token)); |
228 token = parseLiteralStringOrRecoverExpression(token.next); | 181 token = parseLiteralStringOrRecoverExpression(token.next); |
229 token = parseConditionalUris(token); | |
230 token = parseCombinators(token); | 182 token = parseCombinators(token); |
231 Token semicolon = token; | 183 Token semicolon = token; |
232 token = expect(';', token); | 184 token = expect(';', token); |
233 listener.endExport(exportKeyword, semicolon); | 185 listener.endExport(exportKeyword, semicolon); |
234 return token; | 186 return token; |
235 } | 187 } |
236 | 188 |
237 Token parseCombinators(Token token) { | 189 Token parseCombinators(Token token) { |
238 listener.beginCombinators(token); | 190 listener.beginCombinators(token); |
239 int count = 0; | 191 int count = 0; |
(...skipping 2542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2782 } | 2734 } |
2783 listener.handleContinueStatement(hasTarget, continueKeyword, token); | 2735 listener.handleContinueStatement(hasTarget, continueKeyword, token); |
2784 return expectSemicolon(token); | 2736 return expectSemicolon(token); |
2785 } | 2737 } |
2786 | 2738 |
2787 Token parseEmptyStatement(Token token) { | 2739 Token parseEmptyStatement(Token token) { |
2788 listener.handleEmptyStatement(token); | 2740 listener.handleEmptyStatement(token); |
2789 return expectSemicolon(token); | 2741 return expectSemicolon(token); |
2790 } | 2742 } |
2791 } | 2743 } |
OLD | NEW |