From f68d9a2e7e6ea54d3c8c3dde34a37d6cb52876db Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Fri, 15 Mar 2024 10:00:47 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Add=20scaffold=20for=20pars?= =?UTF-8?q?e()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/src/lib.rs | 11 ++ .../java/com/caoccao/javet/swc4j/Swc4j.java | 30 ++++ .../com/caoccao/javet/swc4j/Swc4jNative.java | 2 + .../swc4j/options/Swc4jParseOptions.java | 163 ++++++++++++++++++ .../javet/swc4j/outputs/Swc4jParseOutput.java | 83 +++++++++ 5 files changed, 289 insertions(+) create mode 100644 src/main/java/com/caoccao/javet/swc4j/options/Swc4jParseOptions.java create mode 100644 src/main/java/com/caoccao/javet/swc4j/outputs/Swc4jParseOutput.java diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 242d4960..e15cfd2d 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -51,6 +51,17 @@ pub extern "system" fn Java_com_caoccao_javet_swc4j_Swc4jNative_coreGetVersion<' converter::string_to_jstring(&env, core::get_version()).as_raw() } +#[no_mangle] +pub extern "system" fn Java_com_caoccao_javet_swc4j_Swc4jNative_coreParse<'local>( + _: JNIEnv<'local>, + _: JClass<'local>, + _: jstring, + _: jobject, +) -> jobject { + // TODO + null_mut() +} + #[no_mangle] pub extern "system" fn Java_com_caoccao_javet_swc4j_Swc4jNative_coreTranspile<'local>( mut env: JNIEnv<'local>, diff --git a/src/main/java/com/caoccao/javet/swc4j/Swc4j.java b/src/main/java/com/caoccao/javet/swc4j/Swc4j.java index 6654a9c8..bc2648e4 100644 --- a/src/main/java/com/caoccao/javet/swc4j/Swc4j.java +++ b/src/main/java/com/caoccao/javet/swc4j/Swc4j.java @@ -17,7 +17,9 @@ package com.caoccao.javet.swc4j; import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException; +import com.caoccao.javet.swc4j.options.Swc4jParseOptions; import com.caoccao.javet.swc4j.options.Swc4jTranspileOptions; +import com.caoccao.javet.swc4j.outputs.Swc4jParseOutput; import com.caoccao.javet.swc4j.outputs.Swc4jTranspileOutput; import com.caoccao.javet.swc4j.utils.AssertionUtils; @@ -50,6 +52,34 @@ public String getVersion() { return Swc4jNative.coreGetVersion(); } + /** + * Parse. + * + * @param code the code + * @return the swc4j parse output + * @throws Swc4jCoreException the swc4j core exception + * @since 0.1.0 + */ + public Swc4jParseOutput parse(String code) throws Swc4jCoreException { + return parse(code, new Swc4jParseOptions()); + } + + /** + * Parse. + * + * @param code the code + * @param options the options + * @return the swc4j parse output + * @throws Swc4jCoreException the swc4j core exception + * @since 0.1.0 + */ + @SuppressWarnings("RedundantThrows") + public Swc4jParseOutput parse(String code, Swc4jParseOptions options) throws Swc4jCoreException { + return (Swc4jParseOutput) Swc4jNative.coreParse( + code, + AssertionUtils.notNull(options, "Options")); + } + /** * Transpile. * diff --git a/src/main/java/com/caoccao/javet/swc4j/Swc4jNative.java b/src/main/java/com/caoccao/javet/swc4j/Swc4jNative.java index bb6432d6..01c98932 100644 --- a/src/main/java/com/caoccao/javet/swc4j/Swc4jNative.java +++ b/src/main/java/com/caoccao/javet/swc4j/Swc4jNative.java @@ -23,5 +23,7 @@ private Swc4jNative() { static native String coreGetVersion(); + static native Object coreParse(String code, Object options); + static native Object coreTranspile(String code, Object options); } diff --git a/src/main/java/com/caoccao/javet/swc4j/options/Swc4jParseOptions.java b/src/main/java/com/caoccao/javet/swc4j/options/Swc4jParseOptions.java new file mode 100644 index 00000000..568f5c1d --- /dev/null +++ b/src/main/java/com/caoccao/javet/swc4j/options/Swc4jParseOptions.java @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2024. caoccao.com Sam Cao + * + * 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 com.caoccao.javet.swc4j.options; + +import com.caoccao.javet.swc4j.enums.Swc4jMediaType; +import com.caoccao.javet.swc4j.enums.Swc4jParseMode; +import com.caoccao.javet.swc4j.utils.AssertionUtils; + +/** + * The type Swc4j parse options. + * + * @since 0.2.0 + */ +public final class Swc4jParseOptions { + /** + * The constant DEFAULT_SPECIFIER. + * + * @since 0.2.0 + */ + public static final String DEFAULT_SPECIFIER = "file:///main.js"; + private boolean captureTokens; + private Swc4jMediaType mediaType; + private Swc4jParseMode parseMode; + private boolean scopeAnalysis; + private String specifier; + + /** + * Instantiates a new Swc4j parse options. + * + * @since 0.2.0 + */ + public Swc4jParseOptions() { + setCaptureTokens(false); + setMediaType(Swc4jMediaType.JavaScript); + setParseMode(Swc4jParseMode.Module); + setScopeAnalysis(false); + setSpecifier(DEFAULT_SPECIFIER); + } + + /** + * Gets Media type of the source text. + * + * @return the media type + * @since 0.2.0 + */ + public Swc4jMediaType getMediaType() { + return mediaType; + } + + /** + * Gets parse mode. + * + * @return the parse mode + * @since 0.2.0 + */ + public Swc4jParseMode getParseMode() { + return parseMode; + } + + /** + * Gets Specifier of the source text. + * + * @return the specifier + * @since 0.2.0 + */ + public String getSpecifier() { + return specifier; + } + + /** + * Whether to capture tokens or not. + * + * @return true : capture tokens, false : not capture tokens + * @since 0.2.0 + */ + public boolean isCaptureTokens() { + return captureTokens; + } + + /** + * Whether to apply swc's scope analysis. + * + * @return true : scope analysis, false : not scope analysis + * @since 0.2.0 + */ + public boolean isScopeAnalysis() { + return scopeAnalysis; + } + + /** + * Sets capture tokens. + * + * @param captureTokens the capture tokens + * @return the self + * @since 0.2.0 + */ + public Swc4jParseOptions setCaptureTokens(boolean captureTokens) { + this.captureTokens = captureTokens; + return this; + } + + /** + * Sets Media type of the source text. + * + * @param mediaType the Media type of the source text + * @return the self + * @since 0.2.0 + */ + public Swc4jParseOptions setMediaType(Swc4jMediaType mediaType) { + this.mediaType = AssertionUtils.notNull(mediaType, "Media type"); + return this; + } + + /** + * Sets parse mode. + * + * @param parseMode the parse mode + * @return the self + * @since 0.2.0 + */ + public Swc4jParseOptions setParseMode(Swc4jParseMode parseMode) { + this.parseMode = AssertionUtils.notNull(parseMode, "Parse mode"); + return this; + } + + /** + * Sets scope analysis. + * + * @param scopeAnalysis the scope analysis + * @return the self + * @since 0.2.0 + */ + public Swc4jParseOptions setScopeAnalysis(boolean scopeAnalysis) { + this.scopeAnalysis = scopeAnalysis; + return this; + } + + /** + * Sets Specifier of the source text. + * + * @param specifier the Specifier of the source text + * @return the self + * @since 0.2.0 + */ + public Swc4jParseOptions setSpecifier(String specifier) { + this.specifier = AssertionUtils.notNull(specifier, "Specifier"); + return this; + } +} diff --git a/src/main/java/com/caoccao/javet/swc4j/outputs/Swc4jParseOutput.java b/src/main/java/com/caoccao/javet/swc4j/outputs/Swc4jParseOutput.java new file mode 100644 index 00000000..cc0a0081 --- /dev/null +++ b/src/main/java/com/caoccao/javet/swc4j/outputs/Swc4jParseOutput.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2024-2024. caoccao.com Sam Cao + * + * 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 com.caoccao.javet.swc4j.outputs; + +/** + * The type Swc4j parse output. + * + * @since 0.2.0 + */ +public final class Swc4jParseOutput { + private boolean module; + private boolean script; + + /** + * Instantiates a new Swc4j parse output. + * + * @param module the module + * @param script the script + * @since 0.2.0 + */ + public Swc4jParseOutput(boolean module, boolean script) { + setModule(module); + setScript(script); + } + + /** + * Gets if this source is a module. + * + * @return true : module, false : not module + * @since 0.2.0 + */ + public boolean isModule() { + return module; + } + + /** + * Gets if this source is a script. + * + * @return true : script, false : not script + * @since 0.2.0 + */ + public boolean isScript() { + return script; + } + + /** + * Sets module. + * + * @param module the module + * @return the self + * @since 0.2.0 + */ + public Swc4jParseOutput setModule(boolean module) { + this.module = module; + return this; + } + + /** + * Sets script. + * + * @param script the script + * @return the self + * @since 0.2.0 + */ + public Swc4jParseOutput setScript(boolean script) { + this.script = script; + return this; + } +}