44
55part of code_builder;
66
7+ CompilationUnit _emptyCompilationUnit () => new CompilationUnit (
8+ null ,
9+ null ,
10+ null ,
11+ null ,
12+ null ,
13+ );
14+
715/// Builds files of Dart source code.
816///
9- /// Files may either be a standalone library or `part of` another library.
10- class FileBuilder extends _AbstractCodeBuilder <CompilationUnit > {
11- static Token _library = new KeywordToken (Keyword .LIBRARY , 0 );
12- static Token _part = new KeywordToken (Keyword .PART , 0 );
13- static Token _of = new StringToken (TokenType .KEYWORD , 'of' , 0 );
17+ /// See [LibraryBuilder] and [PartBuilder] for concrete implementations.
18+ abstract class FileBuilder extends _AbstractCodeBuilder <CompilationUnit > {
19+ FileBuilder ._(CompilationUnit astNode) : super ._(astNode);
20+
21+ /// Adds [declaration] 's resulting AST to the source.
22+ void addDeclaration (CodeBuilder <Declaration > declaration) {
23+ _astNode.declarations.add (declaration.toAst ());
24+ }
25+ }
26+
27+ /// Builds a standalone Dart library [CompilationUnit] AST.
28+ class LibraryBuilder extends FileBuilder {
29+ static final Token _library = new KeywordToken (Keyword .LIBRARY , 0 );
1430
1531 /// Create a new standalone Dart library, optionally with a [name] .
16- factory FileBuilder ([String name]) {
32+ factory LibraryBuilder ([String name]) {
1733 var astNode = _emptyCompilationUnit ();
1834 if (name != null ) {
1935 astNode.directives.add (new LibraryDirective (
20- null ,
21- null ,
22- _library,
23- new LibraryIdentifier ([_stringId (name)]),
24- null ,
25- ));
36+ null ,
37+ null ,
38+ _library,
39+ new LibraryIdentifier ([_stringId (name)]),
40+ null ,));
2641 }
27- return new FileBuilder ._(astNode);
42+ return new LibraryBuilder ._(astNode);
2843 }
2944
30- /// Create a new `part of` the dart library with [name] .
31- factory FileBuilder .partOf (String name) {
32- var astNode = _emptyCompilationUnit ();
33- astNode.directives.add (new PartOfDirective (
34- null ,
35- null ,
36- _part,
37- _of,
38- new LibraryIdentifier ([_stringId (name)]),
39- null ,
40- ));
41- return new FileBuilder ._(astNode);
42- }
43-
44- FileBuilder ._(CompilationUnit astNode) : super ._(astNode);
45+ LibraryBuilder ._(CompilationUnit astNode) : super ._(astNode);
4546
46- /// Add a copy of [clazz] as a declaration in this file.
47- void addClass (ClassBuilder clazz) {
48- _astNode.declarations.add (clazz.toAst ());
49- }
50-
51- /// Adds an `import` or `export` [directive] .
47+ /// Adds [directive] 's resulting AST to the source.
5248 void addDirective (CodeBuilder <Directive > directive) {
53- if (isPartOf) {
54- throw const DartPartFileException ._();
55- }
5649 _astNode.directives.add (directive.toAst ());
5750 }
51+ }
5852
59- static CompilationUnit _emptyCompilationUnit () => new CompilationUnit (
60- null ,
61- null ,
53+ /// Builds a `part of` [CompilationUnit] AST for an existing Dart library.
54+ class PartBuilder extends FileBuilder {
55+ static final Token _part = new KeywordToken (Keyword .PART , 0 );
56+ static final Token _of = new StringToken (TokenType .KEYWORD , 'of' , 0 );
57+
58+ /// Create a new `part of` source file.
59+ factory PartBuilder (String name) {
60+ var astNode = _emptyCompilationUnit ();
61+ astNode.directives.add (new PartOfDirective (
6262 null ,
6363 null ,
64+ _part,
65+ _of,
66+ new LibraryIdentifier ([_stringId (name)]),
6467 null ,
65- );
66-
67- static bool _isPartOf ( Directive d) => d is PartOfDirective ;
68+ ) );
69+ return new PartBuilder ._(astNode);
70+ }
6871
69- /// Whether the file is `part of` another.
70- bool get isPartOf => _astNode.directives.contains (_isPartOf);
72+ PartBuilder ._(CompilationUnit astNode) : super ._(astNode);
7173}
72-
73- /// An `export` directive in a [FileBuilder] .
74+ /// An `export` directive in a [LibraryBuilder] .
7475class ExportBuilder extends _AbstractCodeBuilder <ExportDirective > {
7576 /// Create a new `export` directive exporting [uri] .
7677 factory ExportBuilder (String uri) {
@@ -81,13 +82,13 @@ class ExportBuilder extends _AbstractCodeBuilder<ExportDirective> {
8182 ExportBuilder ._(ExportDirective astNode) : super ._(astNode);
8283
8384 static ExportDirective _createExportDirective () => new ExportDirective (
84- null ,
85- null ,
86- null ,
87- null ,
88- null ,
89- null ,
90- null ,
85+ null ,
86+ null ,
87+ null ,
88+ null ,
89+ null ,
90+ null ,
91+ null ,
9192 );
9293}
9394
@@ -111,23 +112,15 @@ class ImportBuilder extends _AbstractCodeBuilder<ImportDirective> {
111112 ImportBuilder ._(ImportDirective astNode) : super ._(astNode);
112113
113114 static ImportDirective _createImportDirective () => new ImportDirective (
114- null ,
115- null ,
116- null ,
117- null ,
118- null ,
119- null ,
120- null ,
121- null ,
122- null ,
123- null ,
115+ null ,
116+ null ,
117+ null ,
118+ null ,
119+ null ,
120+ null ,
121+ null ,
122+ null ,
123+ null ,
124+ null ,
124125 );
125126}
126-
127- /// Thrown when an invalid operation is attempted on a [FileBuilder] instance.
128- class DartPartFileException implements Exception {
129- const DartPartFileException ._();
130-
131- @override
132- String toString () => 'Not a valid operation for a `part of` file.' ;
133- }
0 commit comments