OLD | NEW |
1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
2 | 2 |
3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
4 // for details. All rights reserved. Use of this source code is governed by a | 4 // for details. All rights reserved. Use of this source code is governed by a |
5 // BSD-style license that can be found in the LICENSE file. | 5 // BSD-style license that can be found in the LICENSE file. |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:analyzer/src/generated/java_core.dart' show CharSequence; | |
10 import 'package:analyzer/src/generated/ast.dart'; | 9 import 'package:analyzer/src/generated/ast.dart'; |
11 import 'package:analyzer/src/generated/error.dart'; | 10 import 'package:analyzer/src/generated/error.dart'; |
12 import 'package:analyzer/src/generated/parser.dart'; | 11 import 'package:analyzer/src/generated/parser.dart'; |
13 import 'package:analyzer/src/generated/scanner.dart'; | 12 import 'package:analyzer/src/generated/scanner.dart'; |
14 | 13 |
15 | 14 |
16 main(List<String> args) { | 15 main(List<String> args) { |
17 | 16 |
18 print('working dir ${new File('.').resolveSymbolicLinksSync()}'); | 17 print('working dir ${new File('.').resolveSymbolicLinksSync()}'); |
19 | 18 |
20 if (args.length == 0) { | 19 if (args.length == 0) { |
21 print('Usage: parser_driver [files_to_parse]'); | 20 print('Usage: parser_driver [files_to_parse]'); |
22 exit(0); | 21 exit(0); |
23 } | 22 } |
24 | 23 |
25 for (var arg in args) { | 24 for (var arg in args) { |
26 _parse(new File(arg)); | 25 _parse(new File(arg)); |
27 } | 26 } |
28 | 27 |
29 } | 28 } |
30 | 29 |
31 _parse(File file) { | 30 _parse(File file) { |
32 var src = file.readAsStringSync(); | 31 var src = file.readAsStringSync(); |
33 var errorListener = new _ErrorCollector(); | 32 var errorListener = new _ErrorCollector(); |
34 var reader = new CharSequenceReader(new CharSequence(src)); | 33 var reader = new CharSequenceReader(src); |
35 var scanner = new Scanner(null, reader, errorListener); | 34 var scanner = new Scanner(null, reader, errorListener); |
36 var token = scanner.tokenize(); | 35 var token = scanner.tokenize(); |
37 var parser = new Parser(null, errorListener); | 36 var parser = new Parser(null, errorListener); |
38 var unit = parser.parseCompilationUnit(token); | 37 var unit = parser.parseCompilationUnit(token); |
39 | 38 |
40 var visitor = new _ASTVisitor(); | 39 var visitor = new _ASTVisitor(); |
41 unit.accept(visitor); | 40 unit.accept(visitor); |
42 | 41 |
43 for (var error in errorListener.errors) { | 42 for (var error in errorListener.errors) { |
44 print(error); | 43 print(error); |
45 } | 44 } |
46 } | 45 } |
47 | 46 |
48 class _ErrorCollector extends AnalysisErrorListener { | 47 class _ErrorCollector extends AnalysisErrorListener { |
49 List<AnalysisError> errors; | 48 List<AnalysisError> errors; |
50 _ErrorCollector() : errors = new List<AnalysisError>(); | 49 _ErrorCollector() : errors = new List<AnalysisError>(); |
51 onError(error) => errors.add(error); | 50 onError(error) => errors.add(error); |
52 } | 51 } |
53 | 52 |
54 class _ASTVisitor extends GeneralizingASTVisitor { | 53 class _ASTVisitor extends GeneralizingASTVisitor { |
55 visitNode(ASTNode node) { | 54 visitNode(ASTNode node) { |
56 print('${node.runtimeType} : <"${node.toString()}">'); | 55 print('${node.runtimeType} : <"${node.toString()}">'); |
57 return super.visitNode(node); | 56 return super.visitNode(node); |
58 } | 57 } |
59 } | 58 } |
60 | 59 |
OLD | NEW |