Skip to content

Commit

Permalink
🦄 refactor: Simplify token factory
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 25, 2024
1 parent 690f629 commit 2f357a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 89 deletions.
110 changes: 29 additions & 81 deletions rust/src/token_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ impl JavaAstTokenFactory {
.get_static_method_id(
&class,
"createAssignOperator",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jTokenType;IIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;",
"(IIIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;",
)
.expect("Couldn't find method Swc4jTokenFactory.createAssignOperator");
let method_create_binary_operator = env
.get_static_method_id(
&class,
"createBinaryOperator",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jTokenType;IIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;",
"(IIIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;",
)
.expect("Couldn't find method Swc4jTokenFactory.createBinaryOperator");
let method_create_bigint = env
Expand Down Expand Up @@ -107,7 +107,7 @@ impl JavaAstTokenFactory {
.get_static_method_id(
&class,
"createGenericOperator",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jTokenType;IIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;",
"(IIIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;",
)
.expect("Couldn't find method Swc4jTokenFactory.createGenericOperator");
let method_create_ident_known = env
Expand Down Expand Up @@ -135,15 +135,11 @@ impl JavaAstTokenFactory {
.get_static_method_id(
&class,
"createKeyword",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jTokenType;IIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;",
"(IIIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;",
)
.expect("Couldn't find method Swc4jTokenFactory.createKeyword");
let method_create_null = env
.get_static_method_id(
&class,
"createNull",
"(IIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;",
)
.get_static_method_id(&class, "createNull", "(IIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;")
.expect("Couldn't find method Swc4jTokenFactory.createNull");
let method_create_ident_other = env
.get_static_method_id(
Expand Down Expand Up @@ -188,11 +184,7 @@ impl JavaAstTokenFactory {
)
.expect("Couldn't find method Swc4jTokenFactory.createTemplate");
let method_create_true = env
.get_static_method_id(
&class,
"createTrue",
"(IIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;",
)
.get_static_method_id(&class, "createTrue", "(IIZ)Lcom/caoccao/javet/swc4j/tokens/Swc4jToken;")
.expect("Couldn't find method Swc4jTokenFactory.createTrue");
let method_create_unknown = env
.get_static_method_id(
Expand Down Expand Up @@ -235,32 +227,24 @@ impl JavaAstTokenFactory {
where
'local: 'a,
{
let java_token_type = unsafe { JAVA_TOKEN_TYPE.as_ref().unwrap() };
let java_token_type = java_token_type.parse(env, token_type.get_id());
let token_type = jvalue {
l: java_token_type.as_raw(),
};
let token_type = jvalue { i: token_type.get_id() };
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 {
unsafe {
env
.call_static_method_unchecked(
&self.class,
self.method_create_assign_operator,
ReturnType::Object,
&[token_type, start_position, end_position, line_break_ahead],
)
.expect("Couldn't create Swc4jTokenAssignOperator")
.expect("Couldn't call create_assign_operator")
.l()
.expect("Couldn't convert Swc4jTokenAssignOperator")
};
env
.delete_local_ref(java_token_type)
.expect("Couldn't delete local ast token type");
token
.expect("Couldn't convert create_assign_operator")
}
}

pub fn create_binary_operator<'local, 'a>(
Expand All @@ -273,17 +257,13 @@ impl JavaAstTokenFactory {
where
'local: 'a,
{
let java_token_type = unsafe { JAVA_TOKEN_TYPE.as_ref().unwrap() };
let java_token_type = java_token_type.parse(env, token_type.get_id());
let token_type = jvalue {
l: java_token_type.as_raw(),
};
let token_type = jvalue { i: token_type.get_id() };
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 {
unsafe {
env
.call_static_method_unchecked(
&self.class,
Expand All @@ -294,11 +274,7 @@ impl JavaAstTokenFactory {
.expect("Couldn't create Swc4jTokenBinaryOperator")
.l()
.expect("Couldn't convert Swc4jTokenBinaryOperator")
};
env
.delete_local_ref(java_token_type)
.expect("Couldn't delete local ast token type");
token
}
}

pub fn create_bigint<'local, 'a>(
Expand Down Expand Up @@ -416,17 +392,13 @@ impl JavaAstTokenFactory {
where
'local: 'a,
{
let java_token_type = unsafe { JAVA_TOKEN_TYPE.as_ref().unwrap() };
let java_token_type = java_token_type.parse(env, token_type.get_id());
let token_type = jvalue {
l: java_token_type.as_raw(),
};
let token_type = jvalue { i: token_type.get_id() };
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 {
unsafe {
env
.call_static_method_unchecked(
&self.class,
Expand All @@ -437,11 +409,7 @@ impl JavaAstTokenFactory {
.expect("Couldn't create Swc4jTokenGenericOperator")
.l()
.expect("Couldn't convert Swc4jTokenGenericOperator")
};
env
.delete_local_ref(java_token_type)
.expect("Couldn't delete local ast token type");
token
}
}

pub fn create_ident_known<'local, 'a>(
Expand Down Expand Up @@ -559,17 +527,13 @@ impl JavaAstTokenFactory {
where
'local: 'a,
{
let java_token_type = unsafe { JAVA_TOKEN_TYPE.as_ref().unwrap() };
let java_token_type = java_token_type.parse(env, token_type.get_id());
let token_type = jvalue {
l: java_token_type.as_raw(),
};
let token_type = jvalue { i: token_type.get_id() };
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 {
unsafe {
env
.call_static_method_unchecked(
&self.class,
Expand All @@ -580,11 +544,7 @@ impl JavaAstTokenFactory {
.expect("Couldn't create Swc4jTokenKeyword")
.l()
.expect("Couldn't convert Swc4jTokenKeyword")
};
env
.delete_local_ref(java_token_type)
.expect("Couldn't delete local ast token type");
token
}
}

pub fn create_null<'local, 'a>(
Expand Down Expand Up @@ -981,12 +941,9 @@ pub fn token_and_spans_to_java_list<'local>(
Word::True => java_token_factory.create_true(env, index_range, line_break_ahead),
Word::False => java_token_factory.create_false(env, index_range, line_break_ahead),
Word::Ident(ident) => match ident {
IdentLike::Known(known_ident) => java_token_factory.create_ident_known(
env,
&Atom::from(*known_ident),
index_range,
line_break_ahead,
),
IdentLike::Known(known_ident) => {
java_token_factory.create_ident_known(env, &Atom::from(*known_ident), index_range, line_break_ahead)
}
IdentLike::Other(js_word) => {
java_token_factory.create_ident_other(env, &js_word, index_range, line_break_ahead)
}
Expand Down Expand Up @@ -1026,32 +983,23 @@ pub fn token_and_spans_to_java_list<'local>(
Token::Shebang(shebang) => {
java_token_factory.create_shebang(env, &text, &shebang, index_range, line_break_ahead)
}
Token::Error(error) => {
java_token_factory.create_error(env, &text, &error, index_range, line_break_ahead)
}
Token::Error(error) => java_token_factory.create_error(env, &text, &error, index_range, line_break_ahead),
Token::JSXName { name } => {
java_token_factory.create_jsx_tag_name(env, &name, index_range, line_break_ahead)
}
Token::JSXText { raw } => {
java_token_factory.create_jsx_tag_text(env, &raw, index_range, line_break_ahead)
}
Token::JSXText { raw } => java_token_factory.create_jsx_tag_text(env, &raw, index_range, line_break_ahead),
token => match &TokenType::parse_by_generic_operator(token) {
TokenType::Unknown => {
eprintln!("Unknown {:?}", token);
java_token_factory.create_unknown(env, &text, index_range, line_break_ahead)
}
generic_operator_type => java_token_factory.create_generic_operator(
env,
*generic_operator_type,
index_range,
line_break_ahead,
),
generic_operator_type => {
java_token_factory.create_generic_operator(env, *generic_operator_type, index_range, line_break_ahead)
}
},
};
java_array_list.add(env, &list, &token);
env
.delete_local_ref(token)
.expect("Couldn't delete local ast token");
env.delete_local_ref(token).expect("Couldn't delete local ast token");
});
list.as_raw()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ private Swc4jTokenFactory() {
/**
* Create token assign operator.
*
* @param type the type
* @param typeId the type id
* @param startPosition the start position
* @param endPosition the end position
* @param lineBreakAhead the line break ahead
* @return the token assign operator
* @since 0.2.0
*/
public static Swc4jToken createAssignOperator(
Swc4jTokenType type, int startPosition, int endPosition, boolean lineBreakAhead) {
int typeId, int startPosition, int endPosition, boolean lineBreakAhead) {
Swc4jTokenType type = Swc4jTokenType.parse(typeId);
AssertionUtils.notTrue(type.getSubType().isAssignOperator(), "Assign operator is expected");
return new Swc4jToken(type, startPosition, endPosition, lineBreakAhead);
}
Expand Down Expand Up @@ -70,15 +71,16 @@ public static Swc4jTokenTextValue<BigInteger> createBigInt(
/**
* Create token binary operator.
*
* @param type the type
* @param typeId the type id
* @param startPosition the start position
* @param endPosition the end position
* @param lineBreakAhead the line break ahead
* @return the token binary operator
* @since 0.2.0
*/
public static Swc4jToken createBinaryOperator(
Swc4jTokenType type, int startPosition, int endPosition, boolean lineBreakAhead) {
int typeId, int startPosition, int endPosition, boolean lineBreakAhead) {
Swc4jTokenType type = Swc4jTokenType.parse(typeId);
AssertionUtils.notTrue(type.getSubType().isBinaryOperator(), "Binary operator is expected");
return new Swc4jToken(type, startPosition, endPosition, lineBreakAhead);
}
Expand Down Expand Up @@ -122,15 +124,16 @@ public static Swc4jToken createFalse(
/**
* Create token generic operator.
*
* @param type the type
* @param typeId the type id
* @param startPosition the start position
* @param endPosition the end position
* @param lineBreakAhead the line break ahead
* @return the token generic operator
* @since 0.2.0
*/
public static Swc4jToken createGenericOperator(
Swc4jTokenType type, int startPosition, int endPosition, boolean lineBreakAhead) {
int typeId, int startPosition, int endPosition, boolean lineBreakAhead) {
Swc4jTokenType type = Swc4jTokenType.parse(typeId);
AssertionUtils.notTrue(type.getSubType().isGenericOperator(), "Generic operator is expected");
return new Swc4jToken(type, startPosition, endPosition, lineBreakAhead);
}
Expand Down Expand Up @@ -198,15 +201,16 @@ public static Swc4jTokenText createJsxTagText(
/**
* Create token keyword.
*
* @param type the type
* @param typeId the type id
* @param startPosition the start position
* @param endPosition the end position
* @param lineBreakAhead the line break ahead
* @return the token keyword
* @since 0.2.0
*/
public static Swc4jToken createKeyword(
Swc4jTokenType type, int startPosition, int endPosition, boolean lineBreakAhead) {
int typeId, int startPosition, int endPosition, boolean lineBreakAhead) {
Swc4jTokenType type = Swc4jTokenType.parse(typeId);
AssertionUtils.notTrue(type.getSubType().isKeyword(), "Keyword is expected");
return new Swc4jToken(type, startPosition, endPosition, lineBreakAhead);
}
Expand Down

0 comments on commit 2f357a1

Please sign in to comment.