@@ -24,6 +24,15 @@ public final class Swift2JavaTranslator {
24
24
25
25
package var log = Logger ( label: " translator " , logLevel: . info)
26
26
27
+ // ==== Input
28
+
29
+ struct Input {
30
+ let filePath : String
31
+ let syntax : Syntax
32
+ }
33
+
34
+ var inputs : [ Input ] = [ ]
35
+
27
36
// ==== Output configuration
28
37
let javaPackage : String
29
38
@@ -39,7 +48,7 @@ public final class Swift2JavaTranslator {
39
48
/// type representation.
40
49
package var importedTypes : [ String : ImportedNominalType ] = [ : ]
41
50
42
- public var swiftStdlibTypes : SwiftStandardLibraryTypes
51
+ package var swiftStdlibTypes : SwiftStandardLibraryTypes
43
52
44
53
let symbolTable : SwiftSymbolTable
45
54
let nominalResolution : NominalTypeResolution = NominalTypeResolution ( )
@@ -78,43 +87,42 @@ extension Swift2JavaTranslator {
78
87
/// a checked truncation operation at the Java/Swift board.
79
88
var javaPrimitiveForSwiftInt : JavaType { . long }
80
89
81
- public func analyze(
90
+ package func add( filePath: String , text: String ) {
91
+ log. trace ( " Adding: \( filePath) " )
92
+ let sourceFileSyntax = Parser . parse ( source: text)
93
+ self . nominalResolution. addSourceFile ( sourceFileSyntax)
94
+ self . inputs. append ( Input ( filePath: filePath, syntax: Syntax ( sourceFileSyntax) ) )
95
+ }
96
+
97
+ /// Convenient method for analyzing single file.
98
+ package func analyze(
82
99
file: String ,
83
- text: String ? = nil
100
+ text: String
84
101
) throws {
85
- guard text != nil || FileManager . default. fileExists ( atPath: file) else {
86
- throw Swift2JavaTranslatorError ( message: " Missing input file: \( file) " )
87
- }
88
-
89
- log. trace ( " Analyze: \( file) " )
90
- let text = try text ?? String ( contentsOfFile: file)
91
-
92
- try analyzeSwiftInterface ( interfaceFilePath: file, text: text)
93
-
94
- log. debug ( " Done processing: \( file) " )
102
+ self . add ( filePath: file, text: text)
103
+ try self . analyze ( )
95
104
}
96
105
97
- package func analyzeSwiftInterface( interfaceFilePath: String , text: String ) throws {
98
- let sourceFileSyntax = Parser . parse ( source: text)
99
-
100
- addSourceFile ( sourceFileSyntax)
106
+ /// Analyze registered inputs.
107
+ func analyze( ) throws {
101
108
prepareForTranslation ( )
102
109
103
110
let visitor = Swift2JavaVisitor (
104
111
moduleName: self . swiftModuleName,
105
112
targetJavaPackage: self . javaPackage,
106
113
translator: self
107
114
)
108
- visitor. walk ( sourceFileSyntax)
109
- }
110
115
111
- package func addSourceFile( _ sourceFile: SourceFileSyntax ) {
112
- nominalResolution. addSourceFile ( sourceFile)
116
+ for input in self . inputs {
117
+ log. trace ( " Analyzing \( input. filePath) " )
118
+ visitor. walk ( input. syntax)
119
+ }
113
120
}
114
121
115
122
package func prepareForTranslation( ) {
116
123
nominalResolution. bindExtensions ( )
117
124
125
+ // Prepare symbol table for nominal type names.
118
126
for (_, node) in nominalResolution. topLevelNominalTypes {
119
127
symbolTable. parsedModule. addNominalTypeDeclaration ( node, parent: nil )
120
128
}
0 commit comments