Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: pkg/analyzer_experimental/lib/analyzer.dart

Issue 22865035: Add a parseDartString method to pkg/analyzer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analyzer_experimental/lib/src/string_source.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 parseCompilationUnit(String contents, {String name}) {
55 if (name == null) name = '<unknown source>';
56 var source = new StringSource(contents, name);
57 var errorCollector = new _ErrorCollector();
58 var scanner = new StringScanner(source, contents, errorCollector);
59 var token = scanner.tokenize();
60 var parser = new Parser(source, errorCollector);
61 var unit = parser.parseCompilationUnit(token);
62 unit.lineInfo = new LineInfo(scanner.lineStarts);
63
64 if (errorCollector.hasErrors) throw errorCollector.group;
65
66 return unit;
67 }
68
49 /// Converts an AST node representing a string literal into a [String]. 69 /// Converts an AST node representing a string literal into a [String].
50 String stringLiteralToString(StringLiteral literal) { 70 String stringLiteralToString(StringLiteral literal) {
51 return literal.stringValue; 71 return literal.stringValue;
52 } 72 }
53 73
54 /// A simple error listener that collects errors into an [AnalysisErrorGroup]. 74 /// A simple error listener that collects errors into an [AnalysisErrorGroup].
55 class _ErrorCollector extends AnalysisErrorListener { 75 class _ErrorCollector extends AnalysisErrorListener {
56 final _errors = <AnalysisError>[]; 76 final _errors = <AnalysisError>[];
57 77
58 /// Whether any errors where collected. 78 /// Whether any errors where collected.
59 bool get hasErrors => !_errors.isEmpty; 79 bool get hasErrors => !_errors.isEmpty;
60 80
61 /// The group of errors collected. 81 /// The group of errors collected.
62 AnalyzerErrorGroup get group => 82 AnalyzerErrorGroup get group =>
63 new AnalyzerErrorGroup.fromAnalysisErrors(_errors); 83 new AnalyzerErrorGroup.fromAnalysisErrors(_errors);
64 84
65 _ErrorCollector(); 85 _ErrorCollector();
66 86
67 void onError(AnalysisError error) => _errors.add(error); 87 void onError(AnalysisError error) => _errors.add(error);
68 } 88 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer_experimental/lib/src/string_source.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698