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 package com.google.dart.compiler.parser; | 5 package com.google.dart.compiler.parser; |
6 | 6 |
7 import com.google.common.annotations.VisibleForTesting; | 7 import com.google.common.annotations.VisibleForTesting; |
8 import com.google.common.collect.ImmutableSet; | 8 import com.google.common.collect.ImmutableSet; |
9 import com.google.common.io.CharStreams; | 9 import com.google.common.io.CharStreams; |
10 import com.google.dart.compiler.DartCompilationError; | 10 import com.google.dart.compiler.DartCompilationError; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 import com.google.dart.compiler.ast.DartTypeParameter; | 86 import com.google.dart.compiler.ast.DartTypeParameter; |
87 import com.google.dart.compiler.ast.DartUnaryExpression; | 87 import com.google.dart.compiler.ast.DartUnaryExpression; |
88 import com.google.dart.compiler.ast.DartUnit; | 88 import com.google.dart.compiler.ast.DartUnit; |
89 import com.google.dart.compiler.ast.DartUnqualifiedInvocation; | 89 import com.google.dart.compiler.ast.DartUnqualifiedInvocation; |
90 import com.google.dart.compiler.ast.DartVariable; | 90 import com.google.dart.compiler.ast.DartVariable; |
91 import com.google.dart.compiler.ast.DartVariableStatement; | 91 import com.google.dart.compiler.ast.DartVariableStatement; |
92 import com.google.dart.compiler.ast.DartWhileStatement; | 92 import com.google.dart.compiler.ast.DartWhileStatement; |
93 import com.google.dart.compiler.ast.LibraryNode; | 93 import com.google.dart.compiler.ast.LibraryNode; |
94 import com.google.dart.compiler.ast.LibraryUnit; | 94 import com.google.dart.compiler.ast.LibraryUnit; |
95 import com.google.dart.compiler.ast.Modifiers; | 95 import com.google.dart.compiler.ast.Modifiers; |
96 import com.google.dart.compiler.metrics.CompilerMetrics; | |
96 import com.google.dart.compiler.parser.DartScanner.Location; | 97 import com.google.dart.compiler.parser.DartScanner.Location; |
97 import com.google.dart.compiler.parser.DartScanner.Position; | 98 import com.google.dart.compiler.parser.DartScanner.Position; |
98 import com.google.dart.compiler.util.Lists; | 99 import com.google.dart.compiler.util.Lists; |
99 | 100 |
100 import java.io.IOException; | 101 import java.io.IOException; |
101 import java.io.Reader; | 102 import java.io.Reader; |
102 import java.math.BigInteger; | 103 import java.math.BigInteger; |
103 import java.util.ArrayList; | 104 import java.util.ArrayList; |
104 import java.util.Collections; | 105 import java.util.Collections; |
105 import java.util.HashSet; | 106 import java.util.HashSet; |
106 import java.util.List; | 107 import java.util.List; |
107 import java.util.Set; | 108 import java.util.Set; |
108 | 109 |
109 /** | 110 /** |
110 * The Dart parser. Parses a single compilation unit and produces a {@link DartU nit}. | 111 * The Dart parser. Parses a single compilation unit and produces a {@link DartU nit}. |
111 * The grammar rules are taken from Dart.g revision 557. | 112 * The grammar rules are taken from Dart.g revision 557. |
112 */ | 113 */ |
113 public class DartParser extends CompletionHooksParserBase { | 114 public class DartParser extends CompletionHooksParserBase { |
114 | 115 |
116 private final Source source; | |
117 private final String sourceCode; | |
115 private final boolean isDietParse; | 118 private final boolean isDietParse; |
116 private final Set<String> prefixes; | 119 private final Set<String> prefixes; |
117 private final boolean corelibParse; | 120 private final boolean corelibParse; |
118 private final Set<Integer> errorHistory = new HashSet<Integer>(); | 121 private final Set<Integer> errorHistory = new HashSet<Integer>(); |
119 private boolean isParsingInterface; | 122 private boolean isParsingInterface; |
120 private boolean isTopLevelAbstract; | 123 private boolean isTopLevelAbstract; |
121 private DartScanner.Position topLevelAbstractModifierPosition; | 124 private DartScanner.Position topLevelAbstractModifierPosition; |
122 private boolean isParsingClass; | 125 private boolean isParsingClass; |
123 | 126 |
124 /** | 127 /** |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 "throw", | 197 "throw", |
195 "true", | 198 "true", |
196 "try", | 199 "try", |
197 "var", | 200 "var", |
198 "void", | 201 "void", |
199 "while"}; | 202 "while"}; |
200 public static final Set<String> RESERVED_WORDS_SET = ImmutableSet.copyOf(RESER VED_WORDS); | 203 public static final Set<String> RESERVED_WORDS_SET = ImmutableSet.copyOf(RESER VED_WORDS); |
201 | 204 |
202 public DartParser(Source source, | 205 public DartParser(Source source, |
203 String sourceCode, | 206 String sourceCode, |
204 DartCompilerListener listener) { | 207 boolean isDietParse, |
205 this(new DartScannerParserContext(source, sourceCode, listener)); | 208 Set<String> prefixes, |
209 DartCompilerListener listener, | |
210 CompilerMetrics compilerMetrics) { | |
211 super(new DartParserCommentsHelper.CommentParserContext(source, sourceCode, listener, compilerMetrics)); | |
212 this.source = source; | |
213 this.sourceCode = sourceCode; | |
214 this.isDietParse = isDietParse; | |
215 this.prefixes = prefixes; | |
216 this.corelibParse = source != null && SystemLibraryManager.isDartUri(source. getUri()); | |
206 } | 217 } |
207 | 218 |
208 public DartParser(ParserContext ctx) { | 219 // public DartParser(ParserContext ctx) { |
Brian Wilkerson
2012/06/25 14:24:38
Do we need this commented out code, or can we dele
scheglov
2012/06/26 19:46:34
Done.
| |
209 this(ctx, false); | 220 // this(ctx, false); |
221 // } | |
222 // | |
223 // public DartParser(ParserContext ctx, Set<String> prefixes, boolean isDietPar se) { | |
224 // this(ctx, isDietParse, prefixes); | |
225 // } | |
226 // | |
227 // public DartParser(ParserContext ctx, boolean isDietParse) { | |
228 // this(ctx, isDietParse, Collections.<String>emptySet()); | |
229 // } | |
230 // | |
231 // public DartParser(ParserContext ctx, boolean isDietParse, Set<String> prefix es) { | |
232 // super(ctx); | |
233 // this.isDietParse = isDietParse; | |
234 // this.prefixes = prefixes; | |
235 // { | |
236 // Source source = ctx.getSource(); | |
237 // this.corelibParse = source != null && SystemLibraryManager.isDartUri(sou rce.getUri()); | |
238 // } | |
239 // } | |
240 // | |
241 // private DartParser(Source source, DartCompilerListener listener) throws IOEx ception { | |
242 // this(source, source.getSourceReader(), listener); | |
243 // } | |
244 // | |
245 // private DartParser(Source source, | |
246 // Reader sourceReader, | |
247 // DartCompilerListener listener) throws IOException { | |
248 // this(new DartScannerParserContext(source, read(sourceReader), listener)); | |
249 // } | |
250 // | |
251 // public static DartParser getSourceParser(Source source, DartCompilerListener listener) | |
252 // throws IOException { | |
253 // return new DartParser(source, listener); | |
254 // } | |
255 | |
256 public static String read(Source source) throws IOException { | |
257 return read(source.getSourceReader()); | |
210 } | 258 } |
211 | 259 |
212 public DartParser(ParserContext ctx, Set<String> prefixes, boolean isDietParse ) { | 260 public static String read(Reader reader) throws IOException { |
213 this(ctx, isDietParse, prefixes); | |
214 } | |
215 | |
216 public DartParser(ParserContext ctx, boolean isDietParse) { | |
217 this(ctx, isDietParse, Collections.<String>emptySet()); | |
218 } | |
219 | |
220 public DartParser(ParserContext ctx, boolean isDietParse, Set<String> prefixes ) { | |
221 super(ctx); | |
222 this.isDietParse = isDietParse; | |
223 this.prefixes = prefixes; | |
224 { | |
225 Source source = ctx.getSource(); | |
226 this.corelibParse = source != null && SystemLibraryManager.isDartUri(sourc e.getUri()); | |
227 } | |
228 } | |
229 | |
230 private DartParser(Source source, DartCompilerListener listener) throws IOExce ption { | |
231 this(source, source.getSourceReader(), listener); | |
232 } | |
233 | |
234 private DartParser(Source source, | |
235 Reader sourceReader, | |
236 DartCompilerListener listener) throws IOException { | |
237 this(new DartScannerParserContext(source, read(sourceReader), listener)); | |
238 } | |
239 | |
240 public static DartParser getSourceParser(Source source, DartCompilerListener l istener) | |
241 throws IOException { | |
242 return new DartParser(source, listener); | |
243 } | |
244 | |
245 private static String read(Reader reader) throws IOException { | |
246 try { | 261 try { |
247 return CharStreams.toString(reader); | 262 return CharStreams.toString(reader); |
248 } finally { | 263 } finally { |
249 reader.close(); | 264 reader.close(); |
250 } | 265 } |
251 } | 266 } |
252 | 267 |
253 /** | 268 /** |
254 * A flag indicating whether function expressions are allowed. See | 269 * A flag indicating whether function expressions are allowed. See |
255 * {@link #setAllowFunctionExpression(boolean)}. | 270 * {@link #setAllowFunctionExpression(boolean)}. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 * | functionTypeAlias | 310 * | functionTypeAlias |
296 * | methodOrConstructorDeclaration functionStatementBody | 311 * | methodOrConstructorDeclaration functionStatementBody |
297 * | type? getOrSet identifier formalParameterList functionStatementBody | 312 * | type? getOrSet identifier formalParameterList functionStatementBody |
298 * | CONST type? staticConstDeclarationList ';' | 313 * | CONST type? staticConstDeclarationList ';' |
299 * | variableDeclaration ';' | 314 * | variableDeclaration ';' |
300 * ; | 315 * ; |
301 * </pre> | 316 * </pre> |
302 */ | 317 */ |
303 @Terminals(tokens={Token.EOS, Token.CLASS, Token.LIBRARY, Token.IMPORT, Token. SOURCE, | 318 @Terminals(tokens={Token.EOS, Token.CLASS, Token.LIBRARY, Token.IMPORT, Token. SOURCE, |
304 Token.RESOURCE, Token.NATIVE}) | 319 Token.RESOURCE, Token.NATIVE}) |
305 public DartUnit parseUnit(DartSource source) { | 320 public DartUnit parseUnit() { |
321 DartSource dartSource = (DartSource) source; | |
306 try { | 322 try { |
307 beginCompilationUnit(); | 323 beginCompilationUnit(); |
308 ctx.unitAboutToCompile(source, isDietParse); | 324 ctx.unitAboutToCompile(dartSource, isDietParse); |
309 DartUnit unit = new DartUnit(source, isDietParse); | 325 DartUnit unit = new DartUnit(dartSource, isDietParse); |
310 | 326 |
311 // parse any directives at the beginning of the source | 327 // parse any directives at the beginning of the source |
312 parseDirectives(unit); | 328 parseDirectives(unit); |
313 | 329 |
314 while (!EOS()) { | 330 while (!EOS()) { |
315 DartNode node = null; | 331 DartNode node = null; |
316 beginTopLevelElement(); | 332 beginTopLevelElement(); |
317 isParsingClass = isParsingInterface = false; | 333 isParsingClass = isParsingInterface = false; |
318 // Check for ABSTRACT_KEYWORD. | 334 // Check for ABSTRACT_KEYWORD. |
319 isTopLevelAbstract = false; | 335 isTopLevelAbstract = false; |
(...skipping 26 matching lines...) Expand all Loading... | |
346 // Only "class" can be top-level abstract element. | 362 // Only "class" can be top-level abstract element. |
347 if (isTopLevelAbstract && !isParsingClass) { | 363 if (isTopLevelAbstract && !isParsingClass) { |
348 Position abstractPositionEnd = topLevelAbstractModifierPosition.getA dvancedColumns(ABSTRACT_KEYWORD.length()); | 364 Position abstractPositionEnd = topLevelAbstractModifierPosition.getA dvancedColumns(ABSTRACT_KEYWORD.length()); |
349 Location location = new Location(topLevelAbstractModifierPosition, a bstractPositionEnd); | 365 Location location = new Location(topLevelAbstractModifierPosition, a bstractPositionEnd); |
350 reportError(new DartCompilationError(source, location, | 366 reportError(new DartCompilationError(source, location, |
351 ParserErrorCode.ABSTRACT_TOP_LEVEL_ELEMENT)); | 367 ParserErrorCode.ABSTRACT_TOP_LEVEL_ELEMENT)); |
352 } | 368 } |
353 } | 369 } |
354 } | 370 } |
355 expect(Token.EOS); | 371 expect(Token.EOS); |
372 // add comments | |
373 { | |
374 List<int[]> commentLocs = ((DartParserCommentsHelper.CommentParserContex t) ctx).getCommentLocs(); | |
375 DartParserCommentsHelper.addComments(unit, source, sourceCode, commentLo cs); | |
376 } | |
377 // done | |
356 return done(unit); | 378 return done(unit); |
357 } catch (StringInterpolationParseError exception) { | 379 } catch (StringInterpolationParseError exception) { |
358 throw new InternalCompilerException("Failed to parse " + source.getUri(), exception); | 380 throw new InternalCompilerException("Failed to parse " + source.getUri(), exception); |
359 } | 381 } |
360 } | 382 } |
361 | 383 |
362 private boolean looksLikeDirective() { | 384 private boolean looksLikeDirective() { |
363 switch(peek(0)) { | 385 switch(peek(0)) { |
364 case LIBRARY: | 386 case LIBRARY: |
365 case IMPORT: | 387 case IMPORT: |
(...skipping 4084 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4450 } | 4472 } |
4451 | 4473 |
4452 private void reportError(DartNode node, ErrorCode errorCode, Object... argumen ts) { | 4474 private void reportError(DartNode node, ErrorCode errorCode, Object... argumen ts) { |
4453 reportError(new DartCompilationError(node, errorCode, arguments)); | 4475 reportError(new DartCompilationError(node, errorCode, arguments)); |
4454 } | 4476 } |
4455 | 4477 |
4456 private boolean currentlyParsingToplevel() { | 4478 private boolean currentlyParsingToplevel() { |
4457 return !(isParsingInterface || isTopLevelAbstract || isParsingClass); | 4479 return !(isParsingInterface || isTopLevelAbstract || isParsingClass); |
4458 } | 4480 } |
4459 } | 4481 } |
OLD | NEW |