Skip to content

Commit

Permalink
✨ feat: Add scaffold for parse()
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 15, 2024
1 parent f997060 commit f68d9a2
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 0 deletions.
11 changes: 11 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/caoccao/javet/swc4j/Swc4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/caoccao/javet/swc4j/Swc4jNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
163 changes: 163 additions & 0 deletions src/main/java/com/caoccao/javet/swc4j/options/Swc4jParseOptions.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit f68d9a2

Please sign in to comment.