@@ -24,6 +24,15 @@ public final class Swift2JavaTranslator {
2424
2525 package var log = Logger ( label: " translator " , logLevel: . info)
2626
27+ // ==== Input
28+
29+ struct Input {
30+ let filePath : String
31+ let syntax : Syntax
32+ }
33+
34+ var inputs : [ Input ] = [ ]
35+
2736 // ==== Output configuration
2837 let javaPackage : String
2938
@@ -39,7 +48,7 @@ public final class Swift2JavaTranslator {
3948 /// type representation.
4049 package var importedTypes : [ String : ImportedNominalType ] = [ : ]
4150
42- public var swiftStdlibTypes : SwiftStandardLibraryTypes
51+ package var swiftStdlibTypes : SwiftStandardLibraryTypes
4352
4453 let symbolTable : SwiftSymbolTable
4554 let nominalResolution : NominalTypeResolution = NominalTypeResolution ( )
@@ -78,43 +87,42 @@ extension Swift2JavaTranslator {
7887 /// a checked truncation operation at the Java/Swift board.
7988 var javaPrimitiveForSwiftInt : JavaType { . long }
8089
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(
8299 file: String ,
83- text: String ? = nil
100+ text: String
84101 ) 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 ( )
95104 }
96105
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 {
101108 prepareForTranslation ( )
102109
103110 let visitor = Swift2JavaVisitor (
104111 moduleName: self . swiftModuleName,
105112 targetJavaPackage: self . javaPackage,
106113 translator: self
107114 )
108- visitor. walk ( sourceFileSyntax)
109- }
110115
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+ }
113120 }
114121
115122 package func prepareForTranslation( ) {
116123 nominalResolution. bindExtensions ( )
117124
125+ // Prepare symbol table for nominal type names.
118126 for (_, node) in nominalResolution. topLevelNominalTypes {
119127 symbolTable. parsedModule. addNominalTypeDeclaration ( node, parent: nil )
120128 }
0 commit comments