Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.Fusion</groupId>
<artifactId>ScriptBotX-FusionCompiler</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<includes>
<include>**/*.class</include>
</includes>
</filter>
</filters>
<transformers>
<transformer>
<mainClass>org.Fusion.Server.ServerMain</mainClass>
</transformer>
</transformers>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
55 changes: 54 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,57 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version> <!-- Use the version that fits your needs -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<includes>
<include>**/*.class</include>
</includes>
</filter>
</filters>
<transformers>
<!-- Ensure the Main-Class entry is added to the manifest -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.Fusion.Server.ServerMain</mainClass>
</transformer>
</transformers>
</configuration>
</plugin>
</plugins>
</build>
</project>
10 changes: 9 additions & 1 deletion src/main/java/org/Fusion/Compiler/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class Compiler {
private final List<List<Object>> tokens;

private String botToken = null;
public Compiler(List<List<Object>> tokens) {
this.tokens = tokens;
}
Expand All @@ -32,6 +32,7 @@ public void convertToTokens(List<Object> inputList) {
public String compileToPython() {
StringBuilder pythonCode = new StringBuilder();
pythonCode.append("import discord\n");
pythonCode.append("import tasks\n");
pythonCode.append("from discord.ext import commands\n\n");

pythonCode.append("intents = discord.Intents.default()\n" +
Expand Down Expand Up @@ -73,6 +74,7 @@ private String tokenToPython(Token token) {
return "botname = " + token.value();
}
case TOKEN -> {
setBotToken(token.value());
return "TOKEN = " + token.value();
}
case SET_COMMAND_PREFIX -> {
Expand All @@ -99,4 +101,10 @@ private String tokenToPython(Token token) {
}
}
}
public void setBotToken(String token) {
this.botToken = token;
}
public String getBotToken() {
return botToken;
}
}
35 changes: 22 additions & 13 deletions src/main/java/org/Fusion/Lexer/TokenGrouper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

public class TokenGrouper {
private final List<Token> tokens;
public ErrorHandler handler = new ErrorHandler();

public TokenGrouper(List<Token> tokens) {
this.tokens = tokens;
Expand All @@ -27,7 +28,7 @@ public List<List<Object>> group() {
groupedTokens.add(botGroup);
index += 3;
} else {
ErrorHandler.handleError("Invalid bot declaration.");
handler.handleError("Invalid bot declaration.");
break;
}
}
Expand All @@ -40,7 +41,7 @@ else if (currentToken.type() == TokenType.COMMAND) {
groupedTokens.add(commandGroup);
index = result.newIndex();
} else {
ErrorHandler.handleError("Invalid command declaration.");
handler.handleError("Invalid command declaration.");
break;
}
}
Expand All @@ -53,7 +54,7 @@ else if (currentToken.type() == TokenType.ON_READY) {
groupedTokens.add(onReadyGroup);
index = result.newIndex();
} else {
ErrorHandler.handleError("Invalid 'on_ready' declaration.");
handler.handleError("Invalid 'on_ready' declaration.");
break;
}
}
Expand All @@ -67,7 +68,7 @@ else if (currentToken.type() == TokenType.TOKEN) {
index += 2; // Move index ahead by 2 (one for 'token', one for the associated STRING value)
} else {
// Handle error or invalid syntax if next token isn't a STRING
ErrorHandler.handleError("Invalid token declaration. Expected a STRING value after 'token' keyword.");
handler.handleError("Invalid token declaration. Expected a STRING value after 'token' keyword.");
}
}
// Handle 'log' declaration
Expand All @@ -77,7 +78,7 @@ else if (currentToken.type() == TokenType.LOG) {
groupedTokens.add(logGroup);
index += logGroup.size() + 1;
} else {
ErrorHandler.handleError("Invalid 'log' declaration.");
handler.handleError("Invalid 'log' declaration.");
break;
}
}
Expand All @@ -90,7 +91,7 @@ else if (currentToken.type() == TokenType.SET_COMMAND_PREFIX){
index += 2; // Move index ahead by 2 (one for 'token', one for the associated STRING value)
} else {
// Handle error or invalid syntax if next token isn't a STRING
ErrorHandler.handleError("Invalid token declaration. Expected a STRING value after 'token' keyword.");
handler.handleError("Invalid token declaration. Expected a STRING value after 'token' keyword.");
}
}

Expand Down Expand Up @@ -124,7 +125,7 @@ else if (currentToken.type() == TokenType.VAR){
groupedTokens.add(List.of(currentToken));
break;
}
ErrorHandler.handleError("Unexpected token: " + currentToken + " Index: "+index);
handler.handleError("Unexpected token: " + currentToken + " Index: "+index);
break;
}
}
Expand All @@ -141,7 +142,7 @@ private List<Object> groupBot(int index) {
botGroup.add(new Token(TokenType.BOT, tokens.get(index + 1).value()));
} else {
// Error handling if bot name is not found after 'bot'
ErrorHandler.handleError("Invalid bot declaration. Expected a BotName after 'bot' keyword.");
handler.handleError("Invalid bot declaration. Expected a BotName after 'bot' keyword.");
return null;
}
return botGroup;
Expand Down Expand Up @@ -174,11 +175,11 @@ private GroupingResult groupCommand(int index) {
onReadyGroup.add(new Token(TokenType.BRACE, tokens.get(index).value()));
index++; // Move past the closing brace
} else {
ErrorHandler.handleError("Expected closing brace '}' in command block.");
handler.handleError("Expected closing brace '}' in command block. but found " + tokens.get(index));
return null;
}
} else {
ErrorHandler.handleError("Expected opening brace '{' in command block.");
handler.handleError("Expected opening brace '{' in command block. but found" + tokens.get(index + 2));
return null;
}

Expand Down Expand Up @@ -213,11 +214,11 @@ private GroupingResult groupOnReady(int index) {
onReadyGroup.add(new Token(TokenType.BRACE, tokens.get(index).value()));
index++; // Move past the closing brace
} else {
ErrorHandler.handleError("Expected closing brace '}' in on_ready block.");
handler.handleError("Expected closing brace '}' in on_ready block.");
return null;
}
} else {
ErrorHandler.handleError("Expected opening brace '{' in on_ready block.");
handler.handleError("Expected opening brace '{' in on_ready block.");
return null;
}
return new GroupingResult(onReadyGroup, index);
Expand All @@ -230,10 +231,18 @@ private List<Object> groupLog(int index) {
if (index + 1 < tokens.size() && tokens.get(index + 1).type() == TokenType.STRING) {
logGroup.add(new Token(TokenType.LOG, tokens.get(index + 1).value()));
} else {
ErrorHandler.handleError("Expected a string value after 'log'");
handler.handleError("Expected a string value after 'log'");
return null;
}

return logGroup;
}

public ErrorHandler getHandler() {
return handler;
}
public boolean isSuccess() {
return handler.getAllErrors().isEmpty();
}

}
48 changes: 19 additions & 29 deletions src/main/java/org/Fusion/Lexer/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,30 @@
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class Tokenizer {

// Map to store TokenType and its corresponding regex pattern
private static final Map<TokenType, String> TOKEN_PATTERNS = new HashMap<>();
private static final Map<TokenType, String> TOKEN_PATTERNS = new LinkedHashMap<>();

static {
// Add regex patterns for each TokenType
// Add regex patterns for each TokenType in the correct order
TOKEN_PATTERNS.put(TokenType.BOT, "bot");
TOKEN_PATTERNS.put(TokenType.TOKEN, "token");
TOKEN_PATTERNS.put(TokenType.ON_READY, "on_ready");
TOKEN_PATTERNS.put(TokenType.SET_COMMAND_PREFIX, "commandPrefix");
TOKEN_PATTERNS.put(TokenType.COMMAND, "command");
TOKEN_PATTERNS.put(TokenType.REPLY, "reply");
TOKEN_PATTERNS.put(TokenType.USER, "\\{user\\}");
TOKEN_PATTERNS.put(TokenType.STRING, "\"[^\"]*\""); // Matches strings inside quotes
TOKEN_PATTERNS.put(TokenType.LOG, "log");
TOKEN_PATTERNS.put(TokenType.SET_COMMAND_PREFIX, "commandPrefix");
TOKEN_PATTERNS.put(TokenType.WHITESPACE, "\\s+");
TOKEN_PATTERNS.put(TokenType.BRACE, "[\\{\\}]");
TOKEN_PATTERNS.put(TokenType.UNKNOWN, ".");
TOKEN_PATTERNS.put(TokenType.EOF, "$");

// Add the new var token type and its regex pattern
TOKEN_PATTERNS.put(TokenType.VAR, "var");

// Add the new int token type and its regex pattern to match any integer
TOKEN_PATTERNS.put(TokenType.USER, "\\{user\\}");
TOKEN_PATTERNS.put(TokenType.STRING, "\"[^\"]*\""); // Matches strings inside quotes
TOKEN_PATTERNS.put(TokenType.INT, "[+-]?\\d+");

// Add the new equals token type and its regex pattern
TOKEN_PATTERNS.put(TokenType.EQUALS, "=");
TOKEN_PATTERNS.put(TokenType.BRACE, "[\\{\\}]");
TOKEN_PATTERNS.put(TokenType.WHITESPACE, "\\s+");
TOKEN_PATTERNS.put(TokenType.UNKNOWN, "."); // Must come last
}

public List<Token> tokenize(String input) {
Expand All @@ -55,15 +48,15 @@ public List<Token> tokenize(String input) {
Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
// Loop through each pattern and check for a match
for (Map.Entry<TokenType, String> entry : TOKEN_PATTERNS.entrySet()) {
String pattern2 = entry.getValue();
String matched = matcher.group();
if (matched != null && matched.matches(pattern2)) {
if (matched.trim().isEmpty()) continue; // Skip empty strings (whitespace)
String matched = matcher.group();

// Skip EOF as it's not an actual token to add
if (entry.getKey() == TokenType.EOF) continue;
// Loop through each pattern to determine the token type
for (Map.Entry<TokenType, String> entry : TOKEN_PATTERNS.entrySet()) {
if (matched.matches(entry.getValue())) {
// Skip whitespace tokens
if (entry.getKey() == TokenType.WHITESPACE) {
break;
}

Token token = new Token(entry.getKey(), matched);
tokens.add(token);
Expand All @@ -72,11 +65,8 @@ public List<Token> tokenize(String input) {
}
}

// Add EOF token at the end if not already added
if (!tokens.isEmpty() && tokens.get(tokens.size() - 1).type() != TokenType.EOF) {
tokens.add(new Token(TokenType.EOF, ""));
}

// Add EOF token at the end
tokens.add(new Token(TokenType.EOF, ""));
return tokens;
}
}
Loading
Loading