| 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 import 'src/string_source.dart'; |
| 18 | 18 |
| 19 export 'src/error.dart'; | 19 export 'src/error.dart'; |
| 20 export 'src/generated/ast.dart'; | 20 export 'src/generated/ast.dart'; |
| 21 export 'src/generated/error.dart'; | 21 export 'src/generated/error.dart'; |
| 22 export 'src/generated/utilities_dart.dart'; | 22 export 'src/generated/utilities_dart.dart'; |
| 23 | 23 |
| 24 /// Parses a string of Dart code into an AST. |
| 25 /// |
| 26 /// If [name] is passed, it's used in error messages as the name of the code |
| 27 /// being parsed. |
| 28 /// |
| 29 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless |
| 30 /// [suppressErrors] is `true`, in which case any errors are discarded. |
| 31 CompilationUnit parseCompilationUnit(String contents, {String name, |
| 32 bool suppressErrors: false}) { |
| 33 if (name == null) name = '<unknown source>'; |
| 34 var source = new StringSource(contents, name); |
| 35 var errorCollector = new _ErrorCollector(); |
| 36 var reader = new CharSequenceReader(contents); |
| 37 var scanner = new Scanner(source, reader, errorCollector); |
| 38 var token = scanner.tokenize(); |
| 39 var parser = new Parser(source, errorCollector); |
| 40 var unit = parser.parseCompilationUnit(token); |
| 41 unit.lineInfo = new LineInfo(scanner.lineStarts); |
| 42 |
| 43 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; |
| 44 |
| 45 return unit; |
| 46 } |
| 47 |
| 24 /// Parses a Dart file into an AST. | 48 /// Parses a Dart file into an AST. |
| 25 CompilationUnit parseDartFile(String path) { | 49 CompilationUnit parseDartFile(String path) { |
| 26 String contents = new File(path).readAsStringSync(); | 50 String contents = new File(path).readAsStringSync(); |
| 27 var errorCollector = new _ErrorCollector(); | 51 var errorCollector = new _ErrorCollector(); |
| 28 var sourceFactory = new SourceFactory([new FileUriResolver()]); | 52 var sourceFactory = new SourceFactory([new FileUriResolver()]); |
| 29 | 53 |
| 30 var absolutePath = pathos.absolute(path); | 54 var absolutePath = pathos.absolute(path); |
| 31 var source = sourceFactory.forUri(pathos.toUri(absolutePath).toString()); | 55 var source = sourceFactory.forUri(pathos.toUri(absolutePath).toString()); |
| 32 if (source == null) { | 56 if (source == null) { |
| 33 throw new ArgumentError("Can't get source for path $path"); | 57 throw new ArgumentError("Can't get source for path $path"); |
| 34 } | 58 } |
| 35 if (!source.exists()) { | 59 if (!source.exists()) { |
| 36 throw new ArgumentError("Source $source doesn't exist"); | 60 throw new ArgumentError("Source $source doesn't exist"); |
| 37 } | 61 } |
| 38 | 62 |
| 39 var reader = new CharSequenceReader(contents); | 63 var reader = new CharSequenceReader(contents); |
| 40 var scanner = new Scanner(source, reader, errorCollector); | 64 var scanner = new Scanner(source, reader, errorCollector); |
| 41 var token = scanner.tokenize(); | 65 var token = scanner.tokenize(); |
| 42 var parser = new Parser(source, errorCollector); | 66 var parser = new Parser(source, errorCollector); |
| 43 var unit = parser.parseCompilationUnit(token); | 67 var unit = parser.parseCompilationUnit(token); |
| 44 unit.lineInfo = new LineInfo(scanner.lineStarts); | 68 unit.lineInfo = new LineInfo(scanner.lineStarts); |
| 45 | 69 |
| 46 if (errorCollector.hasErrors) throw errorCollector.group; | 70 if (errorCollector.hasErrors) throw errorCollector.group; |
| 47 | 71 |
| 48 return unit; | 72 return unit; |
| 49 } | 73 } |
| 50 | 74 |
| 51 /// Parses a string of Dart code into an AST. | |
| 52 /// | |
| 53 /// If [name] is passed, it's used in error messages as the name of the code | |
| 54 /// being parsed. | |
| 55 /// | |
| 56 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless | |
| 57 /// [suppressErrors] is `true`, in which case any errors are discarded. | |
| 58 CompilationUnit parseCompilationUnit(String contents, | |
| 59 {String name, bool suppressErrors: false}) { | |
| 60 if (name == null) name = '<unknown source>'; | |
| 61 var source = new StringSource(contents, name); | |
| 62 var errorCollector = new _ErrorCollector(); | |
| 63 var reader = new CharSequenceReader(contents); | |
| 64 var scanner = new Scanner(source, reader, errorCollector); | |
| 65 var token = scanner.tokenize(); | |
| 66 var parser = new Parser(source, errorCollector); | |
| 67 var unit = parser.parseCompilationUnit(token); | |
| 68 unit.lineInfo = new LineInfo(scanner.lineStarts); | |
| 69 | |
| 70 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; | |
| 71 | |
| 72 return unit; | |
| 73 } | |
| 74 | |
| 75 /// Parses the script tag and directives in a string of Dart code into an AST. | 75 /// Parses the script tag and directives in a string of Dart code into an AST. |
| 76 /// | 76 /// |
| 77 /// Stops parsing when the first non-directive is encountered. The rest of the | 77 /// Stops parsing when the first non-directive is encountered. The rest of the |
| 78 /// string will not be parsed. | 78 /// string will not be parsed. |
| 79 /// | 79 /// |
| 80 /// If [name] is passed, it's used in error messages as the name of the code | 80 /// If [name] is passed, it's used in error messages as the name of the code |
| 81 /// being parsed. | 81 /// being parsed. |
| 82 /// | 82 /// |
| 83 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless | 83 /// Throws an [AnalyzerErrorGroup] if any errors occurred, unless |
| 84 /// [suppressErrors] is `true`, in which case any errors are discarded. | 84 /// [suppressErrors] is `true`, in which case any errors are discarded. |
| 85 CompilationUnit parseDirectives(String contents, | 85 CompilationUnit parseDirectives(String contents, {String name, |
| 86 {String name, bool suppressErrors: false}) { | 86 bool suppressErrors: false}) { |
| 87 if (name == null) name = '<unknown source>'; | 87 if (name == null) name = '<unknown source>'; |
| 88 var source = new StringSource(contents, name); | 88 var source = new StringSource(contents, name); |
| 89 var errorCollector = new _ErrorCollector(); | 89 var errorCollector = new _ErrorCollector(); |
| 90 var reader = new CharSequenceReader(contents); | 90 var reader = new CharSequenceReader(contents); |
| 91 var scanner = new Scanner(source, reader, errorCollector); | 91 var scanner = new Scanner(source, reader, errorCollector); |
| 92 var token = scanner.tokenize(); | 92 var token = scanner.tokenize(); |
| 93 var parser = new Parser(source, errorCollector); | 93 var parser = new Parser(source, errorCollector); |
| 94 var unit = parser.parseDirectives(token); | 94 var unit = parser.parseDirectives(token); |
| 95 unit.lineInfo = new LineInfo(scanner.lineStarts); | 95 unit.lineInfo = new LineInfo(scanner.lineStarts); |
| 96 | 96 |
| 97 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; | 97 if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group; |
| 98 | 98 |
| 99 return unit; | 99 return unit; |
| 100 } | 100 } |
| 101 | 101 |
| 102 /// Converts an AST node representing a string literal into a [String]. | 102 /// Converts an AST node representing a string literal into a [String]. |
| 103 String stringLiteralToString(StringLiteral literal) { | 103 String stringLiteralToString(StringLiteral literal) { |
| 104 return literal.stringValue; | 104 return literal.stringValue; |
| 105 } | 105 } |
| 106 | 106 |
| 107 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. | 107 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. |
| 108 class _ErrorCollector extends AnalysisErrorListener { | 108 class _ErrorCollector extends AnalysisErrorListener { |
| 109 final _errors = <AnalysisError>[]; | 109 final _errors = <AnalysisError>[]; |
| 110 | 110 |
| 111 _ErrorCollector(); |
| 112 |
| 113 /// The group of errors collected. |
| 114 AnalyzerErrorGroup get group => |
| 115 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); |
| 116 |
| 111 /// Whether any errors where collected. | 117 /// Whether any errors where collected. |
| 112 bool get hasErrors => !_errors.isEmpty; | 118 bool get hasErrors => !_errors.isEmpty; |
| 113 | 119 |
| 114 /// The group of errors collected. | |
| 115 AnalyzerErrorGroup get group => | |
| 116 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); | |
| 117 | |
| 118 _ErrorCollector(); | |
| 119 | |
| 120 void onError(AnalysisError error) => _errors.add(error); | 120 void onError(AnalysisError error) => _errors.add(error); |
| 121 } | 121 } |
| OLD | NEW |