diff --git a/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java b/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java index ee45d570f61..d7cf7ac9c31 100644 --- a/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java +++ b/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java @@ -26,6 +26,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.k2js.analyze.AnalyzerFacadeForJS; import org.jetbrains.k2js.config.Config; +import org.jetbrains.k2js.facade.exceptions.TranslationException; import org.jetbrains.k2js.generate.CodeGenerator; import org.jetbrains.k2js.translate.general.Translation; import org.jetbrains.k2js.utils.JetFileUtils; @@ -75,7 +76,7 @@ public final class K2JSTranslator { //TODO: web demo related method @SuppressWarnings("UnusedDeclaration") @NotNull - public String translateStringWithCallToMain(@NotNull String programText, @NotNull String argumentsString) { + public String translateStringWithCallToMain(@NotNull String programText, @NotNull String argumentsString) throws TranslationException { JetFile file = JetFileUtils.createPsiFile("test", programText, getProject()); String programCode = generateProgramCode(file, MainCallParameters.mainWithArguments(parseString(argumentsString))) + "\n"; String flushOutput = "Kotlin.System.flush();\n"; @@ -84,21 +85,23 @@ public final class K2JSTranslator { } @NotNull - public String generateProgramCode(@NotNull JetFile file, @NotNull MainCallParameters mainCallParameters) { + public String generateProgramCode(@NotNull JetFile file, @NotNull MainCallParameters mainCallParameters) throws TranslationException { JsProgram program = generateProgram(Arrays.asList(file), mainCallParameters); CodeGenerator generator = new CodeGenerator(); return generator.generateToString(program); } @NotNull - public String generateProgramCode(@NotNull List files, @NotNull MainCallParameters mainCallParameters) { + public String generateProgramCode(@NotNull List files, @NotNull MainCallParameters mainCallParameters) + throws TranslationException { JsProgram program = generateProgram(files, mainCallParameters); CodeGenerator generator = new CodeGenerator(); return generator.generateToString(program); } @NotNull - public JsProgram generateProgram(@NotNull List filesToTranslate, @NotNull MainCallParameters mainCallParameters) { + public JsProgram generateProgram(@NotNull List filesToTranslate, @NotNull MainCallParameters mainCallParameters) + throws TranslationException { JetStandardLibrary.initialize(config.getProject()); BindingContext bindingContext = AnalyzerFacadeForJS.analyzeFilesAndCheckErrors(filesToTranslate, config); Collection files = AnalyzerFacadeForJS.withJsLibAdded(filesToTranslate, config); diff --git a/js/js.translator/src/org/jetbrains/k2js/facade/exceptions/TranslationException.java b/js/js.translator/src/org/jetbrains/k2js/facade/exceptions/TranslationException.java new file mode 100644 index 00000000000..54483d8f5ec --- /dev/null +++ b/js/js.translator/src/org/jetbrains/k2js/facade/exceptions/TranslationException.java @@ -0,0 +1,33 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.k2js.facade.exceptions; + +import org.jetbrains.annotations.NotNull; + +/** + * @author Pavel Talanov + */ +public class TranslationException extends Exception { + + public TranslationException(@NotNull Throwable cause) { + super(cause); + } + + public TranslationException(@NotNull String message, @NotNull Exception cause) { + super(message, cause); + } +} diff --git a/js/js.translator/src/org/jetbrains/k2js/facade/exceptions/TranslationInternalException.java b/js/js.translator/src/org/jetbrains/k2js/facade/exceptions/TranslationInternalException.java new file mode 100644 index 00000000000..c2cf42247cd --- /dev/null +++ b/js/js.translator/src/org/jetbrains/k2js/facade/exceptions/TranslationInternalException.java @@ -0,0 +1,29 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.k2js.facade.exceptions; + +import org.jetbrains.annotations.NotNull; + +/** + * @author Pavel Talanov + */ +public class TranslationInternalException extends TranslationException { + + public TranslationInternalException(@NotNull Throwable cause) { + super(cause); + } +} diff --git a/js/js.translator/src/org/jetbrains/k2js/facade/exceptions/UnsupportedFeatureException.java b/js/js.translator/src/org/jetbrains/k2js/facade/exceptions/UnsupportedFeatureException.java new file mode 100644 index 00000000000..3806043ba5a --- /dev/null +++ b/js/js.translator/src/org/jetbrains/k2js/facade/exceptions/UnsupportedFeatureException.java @@ -0,0 +1,29 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.k2js.facade.exceptions; + +import org.jetbrains.annotations.NotNull; + +/** + * @author Pavel Talanov + */ +public class UnsupportedFeatureException extends TranslationException { + + public UnsupportedFeatureException(@NotNull String message, @NotNull Exception cause) { + super(message, cause); + } +} diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java b/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java index 54a65a1cc77..94595f723c8 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/general/Translation.java @@ -26,6 +26,9 @@ import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.k2js.facade.MainCallParameters; +import org.jetbrains.k2js.facade.exceptions.TranslationInternalException; +import org.jetbrains.k2js.facade.exceptions.TranslationException; +import org.jetbrains.k2js.facade.exceptions.UnsupportedFeatureException; import org.jetbrains.k2js.translate.context.StaticContext; import org.jetbrains.k2js.translate.context.TranslationContext; import org.jetbrains.k2js.translate.declaration.ClassTranslator; @@ -138,7 +141,21 @@ public final class Translation { @NotNull public static JsProgram generateAst(@NotNull BindingContext bindingContext, - @NotNull List files, @NotNull MainCallParameters mainCallParameters) { + @NotNull List files, @NotNull MainCallParameters mainCallParameters) throws TranslationException { + try { + return doGenerateAst(bindingContext, files, mainCallParameters); + } + catch (UnsupportedOperationException e) { + throw new UnsupportedFeatureException("Unsupported feature used.", e); + } + catch (Throwable e) { + throw new TranslationInternalException(e); + } + } + + @NotNull + private static JsProgram doGenerateAst(@NotNull BindingContext bindingContext, @NotNull List files, + @NotNull MainCallParameters mainCallParameters) { //TODO: move some of the code somewhere JetStandardLibrary standardLibrary = JetStandardLibrary.getInstance(); StaticContext staticContext = StaticContext.generateStaticContext(standardLibrary, bindingContext);