Skip to content

Commit

Permalink
✨ feat: Add str to tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 23, 2024
1 parent d96e735 commit 186b314
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 16 deletions.
59 changes: 56 additions & 3 deletions rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct JavaAstTokenFactory {
method_create_ident_other: JStaticMethodID,
method_create_keyword: JStaticMethodID,
method_create_null: JStaticMethodID,
method_create_string: JStaticMethodID,
method_create_true: JStaticMethodID,
method_create_unknown: JStaticMethodID,
}
Expand Down Expand Up @@ -115,6 +116,13 @@ impl JavaAstTokenFactory {
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/words/Swc4jAstTokenIdentOther;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createIdentOther");
let method_create_string = env
.get_static_method_id(
&class,
"createString",
"(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_true = env
.get_static_method_id(
&class,
Expand All @@ -126,7 +134,7 @@ impl JavaAstTokenFactory {
.get_static_method_id(
&class,
"createUnknown",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenUnknown;",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/atom/uni/Swc4jAstTokenUnknown;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createUnknown");
JavaAstTokenFactory {
Expand All @@ -139,6 +147,7 @@ impl JavaAstTokenFactory {
method_create_ident_other,
method_create_keyword,
method_create_null,
method_create_string,
method_create_true,
method_create_unknown,
}
Expand Down Expand Up @@ -422,6 +431,47 @@ impl JavaAstTokenFactory {
token
}

pub fn create_string<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
text: &str,
value: &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 = converter::string_to_jstring(env, &value);
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_string,
ReturnType::Object,
&[text, value, start_position, end_position, line_break_ahead],
)
.expect("Couldn't create Swc4jAstTokenString")
.l()
.expect("Couldn't convert Swc4jAstTokenString")
};
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 @@ -560,12 +610,12 @@ pub fn token_and_spans_to_java_list<'local>(
Word::Ident(ident) => match ident {
IdentLike::Known(known_ident) => java_ast_token_factory.create_ident_known(
env,
&Atom::from(*known_ident).as_str(),
&Atom::from(*known_ident),
index_range,
line_break_ahead,
),
IdentLike::Other(js_word) => {
java_ast_token_factory.create_ident_other(env, &js_word.as_str(), index_range, line_break_ahead)
java_ast_token_factory.create_ident_other(env, &js_word, index_range, line_break_ahead)
}
},
},
Expand All @@ -581,6 +631,9 @@ pub fn token_and_spans_to_java_list<'local>(
index_range,
line_break_ahead,
),
Token::Str { value, raw } => {
java_ast_token_factory.create_string(env, &raw, &value, index_range, line_break_ahead)
}
token => match &AstTokenType::parse_by_generic_operator(token) {
AstTokenType::Unknown => {
eprintln!("Unknown {:?}", token);
Expand Down
4 changes: 4 additions & 0 deletions rust/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ pub enum AstTokenType {
AndAssign, // 98
OrAssign, // 99
NullishAssign, // 100
// Atom - 2
Str, // 101
}

impl IdentifiableEnum<AstTokenType> for AstTokenType {
Expand Down Expand Up @@ -244,6 +246,7 @@ impl IdentifiableEnum<AstTokenType> for AstTokenType {
AstTokenType::AndAssign => 98,
AstTokenType::OrAssign => 99,
AstTokenType::NullishAssign => 100,
AstTokenType::Str => 101,
_ => 0,
}
}
Expand Down Expand Up @@ -349,6 +352,7 @@ impl IdentifiableEnum<AstTokenType> for AstTokenType {
98 => AstTokenType::AndAssign,
99 => AstTokenType::OrAssign,
100 => AstTokenType::NullishAssign,
101 => AstTokenType::Str,
_ => AstTokenType::Unknown,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.caoccao.javet.swc4j.ast;

import com.caoccao.javet.swc4j.ast.atom.bi.Swc4jAstTokenString;
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;
import com.caoccao.javet.swc4j.ast.operators.Swc4jAstTokenGenericOperator;
Expand Down Expand Up @@ -149,6 +151,22 @@ public static Swc4jAstTokenNull createNull(
return new Swc4jAstTokenNull(startPosition, endPosition, lineBreakAhead);
}

/**
* Create ast token string.
*
* @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 string
* @since 0.2.0
*/
public static Swc4jAstTokenString createString(
String text, String value, int startPosition, int endPosition, boolean lineBreakAhead) {
return new Swc4jAstTokenString(text, value, startPosition, endPosition, lineBreakAhead);
}

/**
* Create ast token true.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
* 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.
Expand All @@ -14,14 +14,16 @@
* limitations under the License.
*/

package com.caoccao.javet.swc4j.ast;
package com.caoccao.javet.swc4j.ast.atom;

import com.caoccao.javet.swc4j.ast.BaseSwc4jAstToken;

/**
* The type Base swc4j ast token text.
* The type Base swc4j ast token atom.
*
* @since 0.2.0
*/
public abstract class BaseSwc4jAstTokenText extends BaseSwc4jAstToken {
public abstract class BaseSwc4jAstTokenAtom extends BaseSwc4jAstToken {
/**
* The Text.
*
Expand All @@ -30,15 +32,15 @@ public abstract class BaseSwc4jAstTokenText extends BaseSwc4jAstToken {
protected final String text;

/**
* Instantiates a new Base swc4j ast token text.
* Instantiates a new Base swc4j ast token atom.
*
* @param text the text
* @param startPosition the start position
* @param endPosition the end position
* @param lineBreakAhead the line break ahead
* @since 0.2.0
*/
public BaseSwc4jAstTokenText(String text, int startPosition, int endPosition, boolean lineBreakAhead) {
public BaseSwc4jAstTokenAtom(String text, int startPosition, int endPosition, boolean lineBreakAhead) {
super(startPosition, endPosition, lineBreakAhead);
this.text = text;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.ast.atom.BaseSwc4jAstTokenAtom;

/**
* The type Base swc4j ast token bi atom.
*
* @param <T> the type parameter
* @since 0.2.0
*/
public abstract class BaseSwc4jAstTokenBiAtom<T> extends BaseSwc4jAstTokenAtom {
/**
* The Text.
*
* @since 0.2.0
*/
protected final T value;

/**
* Instantiates a new Base swc4j ast token bi atom.
*
* @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 BaseSwc4jAstTokenBiAtom(
String text, T value, int startPosition, int endPosition, boolean lineBreakAhead) {
super(text, startPosition, endPosition, lineBreakAhead);
this.value = value;
}

/**
* Gets value.
*
* @return the value
* @since 0.2.0
*/
public T getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 string.
*
* @since 0.2.0
*/
public class Swc4jAstTokenString extends BaseSwc4jAstTokenBiAtom<String> {
/**
* Instantiates a new Swc4j ast token string.
*
* @param text the text
* @param startPosition the start position
* @param endPosition the end position
* @param lineBreakAhead the line break ahead
* @since 0.2.0
*/
public Swc4jAstTokenString(String text, String value, int startPosition, int endPosition, boolean lineBreakAhead) {
super(text, value, startPosition, endPosition, lineBreakAhead);
}

@Override
public Swc4jAstTokenType getType() {
return Swc4jAstTokenType.Str;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
* 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.
Expand All @@ -14,8 +14,9 @@
* limitations under the License.
*/

package com.caoccao.javet.swc4j.ast;
package com.caoccao.javet.swc4j.ast.atom.uni;

import com.caoccao.javet.swc4j.ast.atom.BaseSwc4jAstTokenAtom;
import com.caoccao.javet.swc4j.enums.Swc4jAstTokenType;

/**
Expand All @@ -24,7 +25,7 @@
*
* @since 0.2.0
*/
public class Swc4jAstTokenUnknown extends BaseSwc4jAstTokenText {
public class Swc4jAstTokenUnknown extends BaseSwc4jAstTokenAtom {
/**
* Instantiates a new Swc4j ast token unknown.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package com.caoccao.javet.swc4j.ast.words;

import com.caoccao.javet.swc4j.ast.BaseSwc4jAstTokenText;
import com.caoccao.javet.swc4j.ast.atom.BaseSwc4jAstTokenAtom;
import com.caoccao.javet.swc4j.enums.Swc4jAstTokenType;

/**
* The type Swc4j ast token ident known.
*
* @since 0.2.0
*/
public class Swc4jAstTokenIdentKnown extends BaseSwc4jAstTokenText {
public class Swc4jAstTokenIdentKnown extends BaseSwc4jAstTokenAtom {
/**
* Instantiates a new Swc4j ast token ident known.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package com.caoccao.javet.swc4j.ast.words;

import com.caoccao.javet.swc4j.ast.BaseSwc4jAstTokenText;
import com.caoccao.javet.swc4j.ast.atom.BaseSwc4jAstTokenAtom;
import com.caoccao.javet.swc4j.enums.Swc4jAstTokenType;

/**
* The type Swc4j ast token ident other.
*
* @since 0.2.0
*/
public class Swc4jAstTokenIdentOther extends BaseSwc4jAstTokenText {
public class Swc4jAstTokenIdentOther extends BaseSwc4jAstTokenAtom {
/**
* Instantiates a new Swc4j ast token ident other.
*
Expand Down
Loading

0 comments on commit 186b314

Please sign in to comment.