| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, 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 analyzer; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 | |
| 9 import 'package:path/path.dart' as pathos; | |
| 10 | |
| 11 import 'src/generated/java_core.dart' show CharSequence; | |
| 12 import 'src/error.dart'; | |
| 13 import 'src/generated/ast.dart'; | |
| 14 import 'src/generated/error.dart'; | |
| 15 import 'src/generated/parser.dart'; | |
| 16 import 'src/generated/scanner.dart'; | |
| 17 import 'src/generated/source_io.dart'; | |
| 18 import 'src/string_source.dart'; | |
| 19 | |
| 20 export 'src/error.dart'; | |
| 21 export 'src/generated/ast.dart'; | |
| 22 export 'src/generated/error.dart'; | |
| 23 export 'src/generated/utilities_dart.dart'; | |
| 24 | |
| 25 /// Parses a Dart file into an AST. | |
| 26 CompilationUnit parseDartFile(String path) { | |
| 27 String contents = new File(path).readAsStringSync(); | |
| 28 var errorCollector = new _ErrorCollector(); | |
| 29 var sourceFactory = new SourceFactory.con2([new FileUriResolver()]); | |
| 30 | |
| 31 var absolutePath = pathos.absolute(path); | |
| 32 var source = sourceFactory.forUri(pathos.toUri(absolutePath).toString()); | |
| 33 if (source == null) { | |
| 34 throw new ArgumentError("Can't get source for path $path"); | |
| 35 } | |
| 36 if (!source.exists()) { | |
| 37 throw new ArgumentError("Source $source doesn't exist"); | |
| 38 } | |
| 39 | |
| 40 var reader = new CharSequenceReader(new CharSequence(contents)); | |
| 41 var scanner = new Scanner(source, reader, errorCollector); | |
| 42 var token = scanner.tokenize(); | |
| 43 var parser = new Parser(source, errorCollector); | |
| 44 var unit = parser.parseCompilationUnit(token); | |
| 45 unit.lineInfo = new LineInfo(scanner.lineStarts); | |
| 46 | |
| 47 if (errorCollector.hasErrors) throw errorCollector.group; | |
| 48 | |
| 49 return unit; | |
| 50 } | |
| 51 | |
| 52 /// Parses a string of Dart code into an AST. | |
| 53 /// | |
| 54 /// If [name] is passed, it's used in error messages as the name of the code | |
| 55 /// being parsed. | |
| 56 CompilationUnit parseCompilationUnit(String contents, {String name}) { | |
| 57 if (name == null) name = '<unknown source>'; | |
| 58 var source = new StringSource(contents, name); | |
| 59 var errorCollector = new _ErrorCollector(); | |
| 60 var reader = new CharSequenceReader(new CharSequence(contents)); | |
| 61 var scanner = new Scanner(source, reader, errorCollector); | |
| 62 var token = scanner.tokenize(); | |
| 63 var parser = new Parser(source, errorCollector); | |
| 64 var unit = parser.parseCompilationUnit(token); | |
| 65 unit.lineInfo = new LineInfo(scanner.lineStarts); | |
| 66 | |
| 67 if (errorCollector.hasErrors) throw errorCollector.group; | |
| 68 | |
| 69 return unit; | |
| 70 } | |
| 71 | |
| 72 /// Converts an AST node representing a string literal into a [String]. | |
| 73 String stringLiteralToString(StringLiteral literal) { | |
| 74 return literal.stringValue; | |
| 75 } | |
| 76 | |
| 77 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. | |
| 78 class _ErrorCollector extends AnalysisErrorListener { | |
| 79 final _errors = <AnalysisError>[]; | |
| 80 | |
| 81 /// Whether any errors where collected. | |
| 82 bool get hasErrors => !_errors.isEmpty; | |
| 83 | |
| 84 /// The group of errors collected. | |
| 85 AnalyzerErrorGroup get group => | |
| 86 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); | |
| 87 | |
| 88 _ErrorCollector(); | |
| 89 | |
| 90 void onError(AnalysisError error) => _errors.add(error); | |
| 91 } | |
| OLD | NEW |