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/error.dart'; |
| 12 import 'src/generated/ast.dart'; |
| 13 import 'src/generated/error.dart'; |
| 14 import 'src/generated/parser.dart'; |
| 15 import 'src/generated/scanner.dart'; |
| 16 import 'src/generated/source_io.dart'; |
| 17 import 'src/string_source.dart'; |
| 18 |
| 19 export 'src/error.dart'; |
| 20 export 'src/generated/ast.dart'; |
| 21 export 'src/generated/error.dart'; |
| 22 export 'src/generated/utilities_dart.dart'; |
| 23 |
| 24 /// Parses a Dart file into an AST. |
| 25 CompilationUnit parseDartFile(String path) { |
| 26 String contents = new File(path).readAsStringSync(); |
| 27 var errorCollector = new _ErrorCollector(); |
| 28 var sourceFactory = new SourceFactory([new FileUriResolver()]); |
| 29 |
| 30 var absolutePath = pathos.absolute(path); |
| 31 var source = sourceFactory.forUri(pathos.toUri(absolutePath).toString()); |
| 32 if (source == null) { |
| 33 throw new ArgumentError("Can't get source for path $path"); |
| 34 } |
| 35 if (!source.exists()) { |
| 36 throw new ArgumentError("Source $source doesn't exist"); |
| 37 } |
| 38 |
| 39 var reader = new CharSequenceReader(contents); |
| 40 var scanner = new Scanner(source, reader, errorCollector); |
| 41 var token = scanner.tokenize(); |
| 42 var parser = new Parser(source, errorCollector); |
| 43 var unit = parser.parseCompilationUnit(token); |
| 44 unit.lineInfo = new LineInfo(scanner.lineStarts); |
| 45 |
| 46 if (errorCollector.hasErrors) throw errorCollector.group; |
| 47 |
| 48 return unit; |
| 49 } |
| 50 |
| 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. |
| 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 |
| 102 /// Converts an AST node representing a string literal into a [String]. |
| 103 String stringLiteralToString(StringLiteral literal) { |
| 104 return literal.stringValue; |
| 105 } |
| 106 |
| 107 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. |
| 108 class _ErrorCollector extends AnalysisErrorListener { |
| 109 final _errors = <AnalysisError>[]; |
| 110 |
| 111 /// Whether any errors where collected. |
| 112 bool get hasErrors => !_errors.isEmpty; |
| 113 |
| 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); |
| 121 } |
OLD | NEW |