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