Skip to content

Commit

Permalink
🦄 refactor: Simplify tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 24, 2024
1 parent b4b4099 commit 88dd04c
Show file tree
Hide file tree
Showing 30 changed files with 212 additions and 1,120 deletions.
42 changes: 21 additions & 21 deletions rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,133 +73,133 @@ impl JavaAstTokenFactory {
.get_static_method_id(
&class,
"createAssignOperator",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jAstTokenType;IIZ)Lcom/caoccao/javet/swc4j/ast/operators/Swc4jAstTokenAssignOperator;",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jAstTokenType;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstToken;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createAssignOperator");
let method_create_binary_operator = env
.get_static_method_id(
&class,
"createBinaryOperator",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jAstTokenType;IIZ)Lcom/caoccao/javet/swc4j/ast/operators/Swc4jAstTokenBinaryOperator;",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jAstTokenType;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstToken;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createBinaryOperator");
let method_create_bigint = env
.get_static_method_id(
&class,
"createBigInt",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/atom/bi/Swc4jAstTokenBigInt;",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenTextValue;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createBigInt");
let method_create_error = env
.get_static_method_id(
&class,
"createError",
"(Ljava/lang/String;Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/atom/uni/Swc4jAstTokenError;",
"(Ljava/lang/String;Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenTextValue;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createError");
let method_create_false = env
.get_static_method_id(
&class,
"createFalse",
"(IIZ)Lcom/caoccao/javet/swc4j/ast/words/Swc4jAstTokenFalse;",
"(IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstToken;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createFalse");
let method_create_generic_operator = env
.get_static_method_id(
&class,
"createGenericOperator",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jAstTokenType;IIZ)Lcom/caoccao/javet/swc4j/ast/operators/Swc4jAstTokenGenericOperator;",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jAstTokenType;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstToken;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createGenericOperator");
let method_create_ident_known = env
.get_static_method_id(
&class,
"createIdentKnown",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/words/Swc4jAstTokenIdentKnown;",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenText;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createIdentKnown");
let method_create_jsx_tag_name = env
.get_static_method_id(
&class,
"createJsxName",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/atom/uni/Swc4jAstTokenJsxTagName;",
"createJsxTagName",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenText;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createJsxTagName");
let method_create_jsx_tag_text = env
.get_static_method_id(
&class,
"createJsxText",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/atom/uni/Swc4jAstTokenJsxTagText;",
"createJsxTagText",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenText;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createJsxTagText");
let method_create_keyword = env
.get_static_method_id(
&class,
"createKeyword",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jAstTokenType;IIZ)Lcom/caoccao/javet/swc4j/ast/words/Swc4jAstTokenKeyword;",
"(Lcom/caoccao/javet/swc4j/enums/Swc4jAstTokenType;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstToken;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createKeyword");
let method_create_null = env
.get_static_method_id(
&class,
"createNull",
"(IIZ)Lcom/caoccao/javet/swc4j/ast/words/Swc4jAstTokenNull;",
"(IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstToken;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createNull");
let method_create_ident_other = env
.get_static_method_id(
&class,
"createIdentOther",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/words/Swc4jAstTokenIdentOther;",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenText;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createIdentOther");
let method_create_number = env
.get_static_method_id(
&class,
"createNumber",
"(Ljava/lang/String;DIIZ)Lcom/caoccao/javet/swc4j/ast/atom/bi/Swc4jAstTokenNumber;",
"(Ljava/lang/String;DIIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenTextValue;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createNumber");
let method_create_regex = env
.get_static_method_id(
&class,
"createRegex",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/atom/tri/Swc4jAstTokenRegex;",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenTextValueFlags;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createRegex");
let method_create_shebang = env
.get_static_method_id(
&class,
"createShebang",
"(Ljava/lang/String;Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/atom/uni/Swc4jAstTokenShebang;",
"(Ljava/lang/String;Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenTextValue;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createShebang");
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;",
"(Ljava/lang/String;Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenTextValue;",
)
.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;",
"(Ljava/lang/String;Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenTextValue;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createTemplate");
let method_create_true = env
.get_static_method_id(
&class,
"createTrue",
"(IIZ)Lcom/caoccao/javet/swc4j/ast/words/Swc4jAstTokenTrue;",
"(IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstToken;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createTrue");
let method_create_unknown = env
.get_static_method_id(
&class,
"createUnknown",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/atom/uni/Swc4jAstTokenUnknown;",
"(Ljava/lang/String;IIZ)Lcom/caoccao/javet/swc4j/ast/Swc4jAstTokenText;",
)
.expect("Couldn't find method Swc4jAstTokenFactory.createUnknown");
JavaAstTokenFactory {
Expand Down
5 changes: 2 additions & 3 deletions rust/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,14 @@ pub enum AstTokenType {
AndAssign, // 98
OrAssign, // 99
NullishAssign, // 100
// Atom - Uni
// TextValue
Shebang, // 101
Error, // 102
// Atom - Bi
Str, // 103
Num, // 104
BigInt, // 105
Template, // 106
// Atom - Tri
// TextValueFlags
Regex, // 107
// Jsx
JSXTagStart, // 108
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,56 @@
package com.caoccao.javet.swc4j.ast;

import com.caoccao.javet.swc4j.enums.Swc4jAstTokenType;
import com.caoccao.javet.swc4j.utils.AssertionUtils;
import com.caoccao.javet.swc4j.utils.JsonUtils;

/**
* The type Base swc4j ast token.
* The type swc4j ast token.
*
* @since 0.2.0
*/
public abstract class BaseSwc4jAstToken {
public class Swc4jAstToken {
/**
* The End position of the token.
* It is zero-based.
*
* @since 0.2.0
*/
protected int endPosition;
protected final int endPosition;
/**
* The Line break ahead.
*
* @since 0.2.0
*/
protected boolean lineBreakAhead;
protected final boolean lineBreakAhead;
/**
* The Start position of the token.
* It is zero-based.
*
* @since 0.2.0
*/
protected int startPosition;
protected final int startPosition;
/**
* The token Type.
*
* @since 0.2.0
*/
protected final Swc4jAstTokenType type;

/**
* Instantiates a new Base swc4j ast token.
* Instantiates a new swc4j ast token.
*
* @param type the type
* @param startPosition the start position
* @param endPosition the end position
* @param lineBreakAhead the line break ahead
* @since 0.2.0
*/
public BaseSwc4jAstToken(int startPosition, int endPosition, boolean lineBreakAhead) {
public Swc4jAstToken(Swc4jAstTokenType type, int startPosition, int endPosition, boolean lineBreakAhead) {
this.endPosition = endPosition;
this.lineBreakAhead = lineBreakAhead;
this.startPosition = startPosition;
this.type = AssertionUtils.notNull(type, "Ast token type");
}

/**
Expand Down Expand Up @@ -86,15 +95,19 @@ public int getStartPosition() {
* @return the text
* @since 0.2.0
*/
public abstract String getText();
public String getText() {
return getType().getName();
}

/**
* Gets type.
*
* @return the type
* @since 0.2.0
*/
public abstract Swc4jAstTokenType getType();
public Swc4jAstTokenType getType() {
return type;
}

/**
* Is line break ahead.
Expand All @@ -106,43 +119,13 @@ public boolean isLineBreakAhead() {
return lineBreakAhead;
}

/**
* Sets end position.
*
* @param endPosition the end position
* @since 0.2.0
*/
public void setEndPosition(int endPosition) {
this.endPosition = endPosition;
}

/**
* Sets line break ahead.
*
* @param lineBreakAhead the line break ahead
* @since 0.2.0
*/
public void setLineBreakAhead(boolean lineBreakAhead) {
this.lineBreakAhead = lineBreakAhead;
}

/**
* Sets start position.
*
* @param startPosition the start position
* @since 0.2.0
*/
public void setStartPosition(int startPosition) {
this.startPosition = startPosition;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{ \"lineBreakAhead\": ").append(lineBreakAhead);
sb.append(", \"start\": ").append(startPosition);
sb.append(", \"end\": ").append(endPosition);
sb.append(", \"type\": \"").append(getType().name()).append("\"");
sb.append(", \"type\": \"").append(type.name()).append("\"");
sb.append(", \"text\": \"").append(JsonUtils.escape(getText())).append("\"");
sb.append(" }");
return sb.toString();
Expand Down
Loading

0 comments on commit 88dd04c

Please sign in to comment.