Skip to content

Commit

Permalink
✨ feat: Add template to tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 23, 2024
1 parent 3d7cea6 commit 4dc834f
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 6 deletions.
66 changes: 65 additions & 1 deletion rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct JavaAstTokenFactory {
method_create_number: JStaticMethodID,
method_create_regex: JStaticMethodID,
method_create_string: JStaticMethodID,
method_create_template: JStaticMethodID,
method_create_true: JStaticMethodID,
method_create_unknown: JStaticMethodID,
}
Expand Down Expand Up @@ -147,6 +148,13 @@ impl JavaAstTokenFactory {
"(Ljava/lang/String;Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/atom/bi/Swc4jAstTokenString;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createString");
let method_create_template = env
.get_static_method_id(
&class,
"createTemplate",
"(Ljava/lang/String;Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/atom/bi/Swc4jAstTokenTemplate;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createTemplate");
let method_create_true = env
.get_static_method_id(
&class,
Expand Down Expand Up @@ -174,6 +182,7 @@ impl JavaAstTokenFactory {
method_create_null,
method_create_number,
method_create_regex,
method_create_template,
method_create_string,
method_create_true,
method_create_unknown,
Expand Down Expand Up @@ -628,6 +637,54 @@ impl JavaAstTokenFactory {
token
}

pub fn create_template<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
text: &str,
value: Option<&str>,
range: Range<usize>,
line_break_ahead: bool,
) -> JObject<'a>
where
'local: 'a,
{
let java_string_text = converter::string_to_jstring(env, &text);
let java_string_value = match value {
Some(value) => converter::string_to_jstring(env, &value),
None => Default::default(),
};
let text = jvalue {
l: java_string_text.as_raw(),
};
let value = jvalue {
l: java_string_value.as_raw(),
};
let start_position = jvalue { i: range.start as i32 };
let end_position = jvalue { i: range.end as i32 };
let line_break_ahead = jvalue {
z: line_break_ahead as u8,
};
let token = unsafe {
env
.call_static_method_unchecked(
&self.class,
self.method_create_template,
ReturnType::Object,
&[text, value, start_position, end_position, line_break_ahead],
)
.expect("Couldn't create Swc4jAstTokenTemplate")
.l()
.expect("Couldn't convert Swc4jAstTokenTemplate")
};
env
.delete_local_ref(java_string_text)
.expect("Couldn't delete local text");
env
.delete_local_ref(java_string_value)
.expect("Couldn't delete local value");
token
}

pub fn create_true<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
Expand Down Expand Up @@ -796,9 +853,16 @@ pub fn token_and_spans_to_java_list<'local>(
Token::BigInt { value: _, raw } => {
java_ast_token_factory.create_bigint(env, &raw, index_range, line_break_ahead)
}
Token::Regex (value, flags) => {
Token::Regex(value, flags) => {
java_ast_token_factory.create_regex(env, &text, &value, &flags, index_range, line_break_ahead)
}
Token::Template { raw, cooked } => {
let cooked = match &cooked {
Ok(atom) => Some(atom.as_str()),
Err(_) => None,
};
java_ast_token_factory.create_template(env, &raw, cooked, index_range, line_break_ahead)
}
token => match &AstTokenType::parse_by_generic_operator(token) {
AstTokenType::Unknown => {
eprintln!("Unknown {:?}", token);
Expand Down
10 changes: 7 additions & 3 deletions rust/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ pub enum AstTokenType {
Str, // 101
Num, // 102
BigInt, // 103
Regex, // 104
Template, // 104
// Atom - Tri
Regex, // 105
}

impl IdentifiableEnum<AstTokenType> for AstTokenType {
Expand Down Expand Up @@ -252,7 +254,8 @@ impl IdentifiableEnum<AstTokenType> for AstTokenType {
AstTokenType::Str => 101,
AstTokenType::Num => 102,
AstTokenType::BigInt => 103,
AstTokenType::Regex => 104,
AstTokenType::Template => 104,
AstTokenType::Regex => 105,
_ => 0,
}
}
Expand Down Expand Up @@ -361,7 +364,8 @@ impl IdentifiableEnum<AstTokenType> for AstTokenType {
101 => AstTokenType::Str,
102 => AstTokenType::Num,
103 => AstTokenType::BigInt,
104 => AstTokenType::Regex,
104 => AstTokenType::Template,
105 => AstTokenType::Regex,
_ => AstTokenType::Unknown,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

import com.caoccao.javet.swc4j.ast.atom.bi.Swc4jAstTokenBigInt;
import com.caoccao.javet.swc4j.ast.atom.bi.Swc4jAstTokenNumber;
import com.caoccao.javet.swc4j.ast.atom.tri.Swc4jAstTokenRegex;
import com.caoccao.javet.swc4j.ast.atom.bi.Swc4jAstTokenString;
import com.caoccao.javet.swc4j.ast.atom.bi.Swc4jAstTokenTemplate;
import com.caoccao.javet.swc4j.ast.atom.tri.Swc4jAstTokenRegex;
import com.caoccao.javet.swc4j.ast.atom.uni.Swc4jAstTokenUnknown;
import com.caoccao.javet.swc4j.ast.operators.Swc4jAstTokenAssignOperator;
import com.caoccao.javet.swc4j.ast.operators.Swc4jAstTokenBinaryOperator;
Expand Down Expand Up @@ -218,6 +219,22 @@ public static Swc4jAstTokenString createString(
return new Swc4jAstTokenString(text, value, startPosition, endPosition, lineBreakAhead);
}

/**
* Create ast token template.
*
* @param text the text
* @param value the value
* @param startPosition the start position
* @param endPosition the end position
* @param lineBreakAhead the line break ahead
* @return the ast token template
* @since 0.2.0
*/
public static Swc4jAstTokenTemplate createTemplate(
String text, String value, int startPosition, int endPosition, boolean lineBreakAhead) {
return new Swc4jAstTokenTemplate(text, value, startPosition, endPosition, lineBreakAhead);
}

/**
* Create ast token true.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.ast.atom.bi;

import com.caoccao.javet.swc4j.enums.Swc4jAstTokenType;

/**
* The type Swc4j ast token template.
*
* @since 0.2.0
*/
public class Swc4jAstTokenTemplate extends BaseSwc4jAstTokenBiAtom<String> {
/**
* Instantiates a new Swc4j ast token template.
*
* @param text the text
* @param value the value
* @param startPosition the start position
* @param endPosition the end position
* @param lineBreakAhead the line break ahead
* @since 0.2.0
*/
public Swc4jAstTokenTemplate(String text, String value, int startPosition, int endPosition, boolean lineBreakAhead) {
super(text, value, startPosition, endPosition, lineBreakAhead);
}

@Override
public Swc4jAstTokenType getType() {
return Swc4jAstTokenType.Template;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ public enum Swc4jAstTokenType {
Str(101, "$Str", Swc4jAstTokenSubType.BiAtom),
Num(102, "$Num", Swc4jAstTokenSubType.BiAtom),
BigInt(103, "$BigInt", Swc4jAstTokenSubType.BiAtom),
Template(104, "$Template", Swc4jAstTokenSubType.BiAtom),
// Atom - Tri
Regex(104, "$Regex", Swc4jAstTokenSubType.TriAtom),
Regex(105, "$Regex", Swc4jAstTokenSubType.TriAtom),
;

private static final int LENGTH = values().length;
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/com/caoccao/javet/swc4j/TestSwc4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ public void testParseTypeScriptWithCaptureTokens() throws Swc4jCoreException {
Swc4jAstTokenRegex astTokenRegex = (Swc4jAstTokenRegex) parseAndAssert("a = /x/ig;", options, Swc4jAstTokenType.Regex, "x/ig", 5, 9, 3, 5);
assertEquals("x", astTokenRegex.getValue());
assertEquals("ig", astTokenRegex.getFlags());
assertTokenValue("a ", parseAndAssert("`a ${b} c`", options, Swc4jAstTokenType.Template, "a ", 1, 3, 1, 7));
parseAndAssert("`a ${b} c`", options, Swc4jAstTokenType.IdentOther, "b", 5, 6, 3, 7);
assertTokenValue(" c", parseAndAssert("`a ${b} c`", options, Swc4jAstTokenType.Template, " c", 7, 9, 5, 7));
}

@Test
Expand Down

0 comments on commit 4dc834f

Please sign in to comment.