Remove "rawStatements" from APIs and refactor CodeGenerator

This commit is contained in:
Pavel V. Talanov
2012-07-04 13:01:11 +04:00
parent 0cd1097dd0
commit c3972d9c58
4 changed files with 16 additions and 44 deletions
@@ -103,7 +103,7 @@ public abstract class BasicTest extends TestWithEnvironment {
@NotNull MainCallParameters mainCallParameters, @NotNull Iterable<EcmaVersion> ecmaVersions) @NotNull MainCallParameters mainCallParameters, @NotNull Iterable<EcmaVersion> ecmaVersions)
throws Exception { throws Exception {
for (EcmaVersion version : ecmaVersions) { for (EcmaVersion version : ecmaVersions) {
TranslationUtils.translateFiles(getProject(), files, getOutputFilePath(testName, version), mainCallParameters, version, null); TranslationUtils.translateFiles(getProject(), files, getOutputFilePath(testName, version), mainCallParameters, version);
} }
} }
@@ -109,13 +109,12 @@ public final class TranslationUtils {
public static void translateFiles(@NotNull Project project, @NotNull List<String> inputFiles, public static void translateFiles(@NotNull Project project, @NotNull List<String> inputFiles,
@NotNull String outputFile, @NotNull String outputFile,
@NotNull MainCallParameters mainCallParameters, @NotNull MainCallParameters mainCallParameters,
@NotNull EcmaVersion version, @NotNull EcmaVersion version) throws Exception {
@Nullable List<String> rawStatements) throws Exception {
List<JetFile> psiFiles = createPsiFileList(inputFiles, project); List<JetFile> psiFiles = createPsiFileList(inputFiles, project);
JsProgram program = getTranslator(project, version).generateProgram(psiFiles, mainCallParameters, rawStatements); JsProgram program = getTranslator(project, version).generateProgram(psiFiles, mainCallParameters);
FileWriter writer = new FileWriter(new File(outputFile)); FileWriter writer = new FileWriter(new File(outputFile));
try { try {
writer.write(CodeGenerator.generateProgramToString(program, null)); writer.write(CodeGenerator.generateProgramToString(program));
} }
finally { finally {
writer.close(); writer.close();
@@ -16,11 +16,9 @@
package org.jetbrains.k2js.facade; package org.jetbrains.k2js.facade;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.JsProgram; import com.google.dart.compiler.backend.js.ast.JsProgram;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
@@ -71,33 +69,30 @@ public final class K2JSTranslator {
@NotNull @NotNull
public String translateStringWithCallToMain(@NotNull String programText, @NotNull String argumentsString) throws TranslationException { public String translateStringWithCallToMain(@NotNull String programText, @NotNull String argumentsString) throws TranslationException {
JetFile file = JetFileUtils.createPsiFile("test", programText, getProject()); JetFile file = JetFileUtils.createPsiFile("test", programText, getProject());
String programCode = generateProgramCode(file, MainCallParameters.mainWithArguments(parseString(argumentsString)), null) + "\n"; String programCode = generateProgramCode(file, MainCallParameters.mainWithArguments(parseString(argumentsString))) + "\n";
return FLUSH_SYSTEM_OUT + programCode + GET_SYSTEM_OUT; return FLUSH_SYSTEM_OUT + programCode + GET_SYSTEM_OUT;
} }
@NotNull @NotNull
public String generateProgramCode(@NotNull JetFile file, @NotNull MainCallParameters mainCallParameters, public String generateProgramCode(@NotNull JetFile file, @NotNull MainCallParameters mainCallParameters) throws TranslationException {
@Nullable List<String> rawStatements) throws TranslationException { JsProgram program = generateProgram(Arrays.asList(file), mainCallParameters);
JsProgram program = generateProgram(Arrays.asList(file), mainCallParameters, rawStatements); return generateProgramToString(program);
return generateProgramToString(program, rawStatements);
} }
@NotNull @NotNull
public String generateProgramCode(@NotNull List<JetFile> files, @NotNull MainCallParameters mainCallParameters) public String generateProgramCode(@NotNull List<JetFile> files, @NotNull MainCallParameters mainCallParameters)
throws TranslationException { throws TranslationException {
List<String> rawStatements = Lists.newArrayList(); JsProgram program = generateProgram(files, mainCallParameters);
JsProgram program = generateProgram(files, mainCallParameters, rawStatements); return generateProgramToString(program);
return generateProgramToString(program, rawStatements);
} }
@NotNull @NotNull
public JsProgram generateProgram(@NotNull List<JetFile> filesToTranslate, public JsProgram generateProgram(@NotNull List<JetFile> filesToTranslate,
@NotNull MainCallParameters mainCallParameters, @NotNull MainCallParameters mainCallParameters)
@Nullable List<String> rawStatements)
throws TranslationException { throws TranslationException {
JetStandardLibrary.initialize(config.getProject()); JetStandardLibrary.initialize(config.getProject());
BindingContext bindingContext = AnalyzerFacadeForJS.analyzeFilesAndCheckErrors(filesToTranslate, config); BindingContext bindingContext = AnalyzerFacadeForJS.analyzeFilesAndCheckErrors(filesToTranslate, config);
return Translation.generateAst(bindingContext, withJsLibAdded(filesToTranslate, config), mainCallParameters, config, rawStatements); return Translation.generateAst(bindingContext, withJsLibAdded(filesToTranslate, config), mainCallParameters, config);
} }
@NotNull @NotNull
@@ -19,43 +19,21 @@ package org.jetbrains.k2js.generate;
import com.google.dart.compiler.backend.js.JsSourceGenerationVisitor; import com.google.dart.compiler.backend.js.JsSourceGenerationVisitor;
import com.google.dart.compiler.backend.js.ast.JsProgram; import com.google.dart.compiler.backend.js.ast.JsProgram;
import com.google.dart.compiler.util.DefaultTextOutput; import com.google.dart.compiler.util.DefaultTextOutput;
import com.google.dart.compiler.util.TextOutput;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
/** /**
* @author Pavel.Talanov * @author Pavel.Talanov
*/ */
public final class CodeGenerator { public final class CodeGenerator {
@NotNull
private final TextOutput output = new DefaultTextOutput(false);
private CodeGenerator() { private CodeGenerator() {
} }
@NotNull @NotNull
public static String generateProgramToString(@NotNull JsProgram program, @Nullable List<String> rawStatements) { public static String generateProgramToString(@NotNull JsProgram program) {
return (new CodeGenerator()).generateToString(program, rawStatements); DefaultTextOutput output = new DefaultTextOutput(false);
} JsSourceGenerationVisitor sourceGenerator = new JsSourceGenerationVisitor(output);
program.traverse(sourceGenerator, null);
@NotNull
private String generateToString(@NotNull JsProgram program, List<String> rawStatements) {
generateCode(program);
if (rawStatements != null) {
for (String statement : rawStatements) {
output.print(statement);
output.newline();
}
}
return output.toString(); return output.toString();
} }
private void generateCode(@NotNull JsProgram program) {
JsSourceGenerationVisitor sourceGenerator =
new JsSourceGenerationVisitor(output);
program.traverse(sourceGenerator, null);
}
} }