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 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 if (errorCollector.hasErrors) throw errorCollector.group; | 46 if (errorCollector.hasErrors) throw errorCollector.group; |
47 | 47 |
48 return unit; | 48 return unit; |
49 } | 49 } |
50 | 50 |
51 /// Parses a string of Dart code into an AST. | 51 /// Parses a string of Dart code into an AST. |
52 /// | 52 /// |
53 /// 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 |
54 /// being parsed. | 54 /// being parsed. |
| 55 /// |
| 56 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless |
| 57 /// [suppressErrors] is `true`, in which case any errors are discarded. |
55 CompilationUnit parseCompilationUnit(String contents, | 58 CompilationUnit parseCompilationUnit(String contents, |
56 {String name, bool suppressErrors: false}) { | 59 {String name, bool suppressErrors: false}) { |
57 if (name == null) name = '<unknown source>'; | 60 if (name == null) name = '<unknown source>'; |
58 var source = new StringSource(contents, name); | 61 var source = new StringSource(contents, name); |
59 var errorCollector = new _ErrorCollector(); | 62 var errorCollector = new _ErrorCollector(); |
60 var reader = new CharSequenceReader(contents); | 63 var reader = new CharSequenceReader(contents); |
61 var scanner = new Scanner(source, reader, errorCollector); | 64 var scanner = new Scanner(source, reader, errorCollector); |
62 var token = scanner.tokenize(); | 65 var token = scanner.tokenize(); |
63 var parser = new Parser(source, errorCollector); | 66 var parser = new Parser(source, errorCollector); |
64 var unit = parser.parseCompilationUnit(token); | 67 var unit = parser.parseCompilationUnit(token); |
65 unit.lineInfo = new LineInfo(scanner.lineStarts); | 68 unit.lineInfo = new LineInfo(scanner.lineStarts); |
66 | 69 |
67 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; | 70 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; |
68 | 71 |
69 return unit; | 72 return unit; |
70 } | 73 } |
71 | 74 |
| 75 /// Parses the script tag and directives in a string of Dart code into an AST. |
| 76 /// |
| 77 /// Stops parsing when the first non-directive is encountered. The rest of the |
| 78 /// string will not be parsed. |
| 79 /// |
| 80 /// If [name] is passed, it's used in error messages as the name of the code |
| 81 /// being parsed. |
| 82 /// |
| 83 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless |
| 84 /// [suppressErrors] is `true`, in which case any errors are discarded. |
| 85 CompilationUnit parseDirectives(String contents, |
| 86 {String name, bool suppressErrors: false}) { |
| 87 if (name == null) name = '<unknown source>'; |
| 88 var source = new StringSource(contents, name); |
| 89 var errorCollector = new _ErrorCollector(); |
| 90 var reader = new CharSequenceReader(contents); |
| 91 var scanner = new Scanner(source, reader, errorCollector); |
| 92 var token = scanner.tokenize(); |
| 93 var parser = new Parser(source, errorCollector); |
| 94 var unit = parser.parseDirectives(token); |
| 95 unit.lineInfo = new LineInfo(scanner.lineStarts); |
| 96 |
| 97 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; |
| 98 |
| 99 return unit; |
| 100 } |
| 101 |
72 /// Converts an AST node representing a string literal into a [String]. | 102 /// Converts an AST node representing a string literal into a [String]. |
73 String stringLiteralToString(StringLiteral literal) { | 103 String stringLiteralToString(StringLiteral literal) { |
74 return literal.stringValue; | 104 return literal.stringValue; |
75 } | 105 } |
76 | 106 |
77 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. | 107 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. |
78 class _ErrorCollector extends AnalysisErrorListener { | 108 class _ErrorCollector extends AnalysisErrorListener { |
79 final _errors = <AnalysisError>[]; | 109 final _errors = <AnalysisError>[]; |
80 | 110 |
81 /// Whether any errors where collected. | 111 /// Whether any errors where collected. |
82 bool get hasErrors => !_errors.isEmpty; | 112 bool get hasErrors => !_errors.isEmpty; |
83 | 113 |
84 /// The group of errors collected. | 114 /// The group of errors collected. |
85 AnalyzerErrorGroup get group => | 115 AnalyzerErrorGroup get group => |
86 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); | 116 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); |
87 | 117 |
88 _ErrorCollector(); | 118 _ErrorCollector(); |
89 | 119 |
90 void onError(AnalysisError error) => _errors.add(error); | 120 void onError(AnalysisError error) => _errors.add(error); |
91 } | 121 } |
OLD | NEW |