Chromium Code Reviews| 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/error.dart'; | 11 import 'src/error.dart'; |
| 12 import 'src/generated/ast.dart'; | 12 import 'src/generated/ast.dart'; |
| 13 import 'src/generated/error.dart'; | 13 import 'src/generated/error.dart'; |
| 14 import 'src/generated/parser.dart'; | 14 import 'src/generated/parser.dart'; |
| 15 import 'src/generated/scanner.dart'; | 15 import 'src/generated/scanner.dart'; |
| 16 import 'src/generated/source_io.dart'; | 16 import 'src/generated/source_io.dart'; |
| 17 import 'src/string_source.dart'; | |
| 17 | 18 |
| 18 export 'src/error.dart'; | 19 export 'src/error.dart'; |
| 19 export 'src/generated/ast.dart'; | 20 export 'src/generated/ast.dart'; |
| 20 export 'src/generated/error.dart'; | 21 export 'src/generated/error.dart'; |
| 21 export 'src/generated/utilities_dart.dart'; | 22 export 'src/generated/utilities_dart.dart'; |
| 22 | 23 |
| 23 /// Parses a Dart file into an AST. | 24 /// Parses a Dart file into an AST. |
| 24 CompilationUnit parseDartFile(String path) { | 25 CompilationUnit parseDartFile(String path) { |
| 25 var contents = new File(path).readAsStringSync(); | 26 var contents = new File(path).readAsStringSync(); |
| 26 var errorCollector = new _ErrorCollector(); | 27 var errorCollector = new _ErrorCollector(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 39 var token = scanner.tokenize(); | 40 var token = scanner.tokenize(); |
| 40 var parser = new Parser(source, errorCollector); | 41 var parser = new Parser(source, errorCollector); |
| 41 var unit = parser.parseCompilationUnit(token); | 42 var unit = parser.parseCompilationUnit(token); |
| 42 unit.lineInfo = new LineInfo(scanner.lineStarts); | 43 unit.lineInfo = new LineInfo(scanner.lineStarts); |
| 43 | 44 |
| 44 if (errorCollector.hasErrors) throw errorCollector.group; | 45 if (errorCollector.hasErrors) throw errorCollector.group; |
| 45 | 46 |
| 46 return unit; | 47 return unit; |
| 47 } | 48 } |
| 48 | 49 |
| 50 /// Parses a string of Dart code into an AST. | |
| 51 /// | |
| 52 /// If [name] is passed, it's used in error messages as the name of the code | |
| 53 /// being parsed. | |
| 54 CompilationUnit parseDartString(String contents, {String name}) { | |
|
Brian Wilkerson
2013/08/23 14:19:29
It might be better to name this something like par
nweiz
2013/08/26 17:03:39
Done.
| |
| 55 var source = name == null ? null : new StringSource(contents, name); | |
|
Brian Wilkerson
2013/08/23 14:19:29
It would be good to have some tests for this. In p
nweiz
2013/08/26 17:03:39
The error message does default to "<unknown source
| |
| 56 var errorCollector = new _ErrorCollector(); | |
| 57 var scanner = new StringScanner(source, contents, errorCollector); | |
| 58 var token = scanner.tokenize(); | |
| 59 var parser = new Parser(source, errorCollector); | |
| 60 var unit = parser.parseCompilationUnit(token); | |
| 61 unit.lineInfo = new LineInfo(scanner.lineStarts); | |
| 62 | |
| 63 if (errorCollector.hasErrors) throw errorCollector.group; | |
| 64 | |
| 65 return unit; | |
| 66 } | |
| 67 | |
| 49 /// Converts an AST node representing a string literal into a [String]. | 68 /// Converts an AST node representing a string literal into a [String]. |
| 50 String stringLiteralToString(StringLiteral literal) { | 69 String stringLiteralToString(StringLiteral literal) { |
| 51 return literal.stringValue; | 70 return literal.stringValue; |
| 52 } | 71 } |
| 53 | 72 |
| 54 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. | 73 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. |
| 55 class _ErrorCollector extends AnalysisErrorListener { | 74 class _ErrorCollector extends AnalysisErrorListener { |
| 56 final _errors = <AnalysisError>[]; | 75 final _errors = <AnalysisError>[]; |
| 57 | 76 |
| 58 /// Whether any errors where collected. | 77 /// Whether any errors where collected. |
| 59 bool get hasErrors => !_errors.isEmpty; | 78 bool get hasErrors => !_errors.isEmpty; |
| 60 | 79 |
| 61 /// The group of errors collected. | 80 /// The group of errors collected. |
| 62 AnalyzerErrorGroup get group => | 81 AnalyzerErrorGroup get group => |
| 63 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); | 82 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); |
| 64 | 83 |
| 65 _ErrorCollector(); | 84 _ErrorCollector(); |
| 66 | 85 |
| 67 void onError(AnalysisError error) => _errors.add(error); | 86 void onError(AnalysisError error) => _errors.add(error); |
| 68 } | 87 } |
| OLD | NEW |