Skip to content

Commit

Permalink
Version 1.5
Browse files Browse the repository at this point in the history
Organized vbs parser code
Reorganized TODO comments in StatementFactory class
Cleaned up code formatting
  • Loading branch information
OpticFusion1 committed Sep 26, 2022
1 parent 7a00408 commit 7814d85
Show file tree
Hide file tree
Showing 24 changed files with 99 additions and 106 deletions.
15 changes: 13 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>optic_fusion1</groupId>
<artifactId>Kitsune</artifactId>
<version>1.4.3</version>
<version>1.5</version>

<build>
<resources>
Expand Down Expand Up @@ -143,9 +143,20 @@
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.3</version>
<version>1.14.3</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.11.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.12.1</version>
</dependency>
</dependencies>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
package optic_fusion1.kitsune.parser.vbs;

import optic_fusion1.kitsune.parser.vbs.domain.Parameter;
import optic_fusion1.kitsune.parser.vbs.domain.VBStatement;
import optic_fusion1.kitsune.parser.vbs.domain.Comment;
import optic_fusion1.kitsune.parser.vbs.domain.LoopStatement;
import optic_fusion1.kitsune.parser.vbs.domain.ConstStatement;
import optic_fusion1.kitsune.parser.vbs.domain.ElseIfStatement;
import optic_fusion1.kitsune.parser.vbs.domain.VariableInit;
import optic_fusion1.kitsune.parser.vbs.domain.IfStatement;
import optic_fusion1.kitsune.parser.vbs.domain.ElseStatement;
import optic_fusion1.kitsune.parser.vbs.domain.MsgBoxStatement;
import optic_fusion1.kitsune.parser.vbs.domain.SetStatement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static optic_fusion1.kitsune.Kitsune.LOGGER;
import optic_fusion1.kitsune.parser.vbs.domain.Constants;
import optic_fusion1.kitsune.parser.vbs.domain.DataType;
import optic_fusion1.kitsune.parser.vbs.domain.Function;

/*
TODO: Implement support for Sub functions
Sub SubName( param1, param2 ) || Sub SubName(param1,param2) // Or other variants
// Statements
End Sub
TODO: Implement support for storing the values that variables get set to
*/
// TODO: Implement support for storing the values that variables get set to
// TODO: Implement support for parsing operators (=, +, -, /, *, %)
public class StatementFactory {

/*
Expand All @@ -21,21 +34,18 @@ public class StatementFactory {
End Function
*/
public static Function buildFunctionStatements(int index, String line) {
Pattern pattern = Pattern.compile("( +.*?)\\(", Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile("( +.*?)\\(?", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
matcher.find();
Function func = new Function(matcher.group(1));
func.setLineNumber(index);
func.setText(line);

pattern = Pattern.compile("\\((.*?)\\)", Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(line);
matcher.find();
String paramstr = null;
paramstr = matcher.group(1);

if (paramstr != null) {

String[] params = paramstr.split(",");
for (String param : params) {
param = param.trim();
Expand All @@ -46,12 +56,10 @@ public static Function buildFunctionStatements(int index, String line) {
paramType = DataType.valueOf(pvPair[0].trim());
paramName = pvPair[1].trim();
}

Parameter p = new Parameter(paramName, paramType);
func.getParameters().add(p);
}
}

return func;
}

Expand Down Expand Up @@ -90,55 +98,40 @@ public static VariableInit buildVariableInitStatements(int index, String lineTri
}

public static VBStatement buildGenericStatements(int index, String line) {

VBStatement stmt = new VBStatement();
stmt.setLineNumber(index);
stmt.setText(line);

return stmt;

}

public static LoopStatement buildLoopStatements(int index, String lineTrimmed) {

LoopStatement loop = new LoopStatement();

loop.setText(lineTrimmed);
loop.setLineNumber(index);

String loopStartRegex = Constants.WHILE_IDENTIFIER + "|" + Constants.DO_WHILE_IDENTIFIER + "|" + Constants.FOR_IDENTIFIER;
String condition = lineTrimmed.replaceFirst(loopStartRegex, "");

loop.setCondition(condition);

return loop;
}

public static IfStatement buildIFStatements(int index, String lineTrimmed) {

Pattern pattern = Pattern.compile("if(.+)then", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(lineTrimmed);
matcher.find();

IfStatement ifstmt = new IfStatement();
ifstmt.setText(lineTrimmed);
ifstmt.setLineNumber(index);

String condition = matcher.group(1);

ifstmt.setCondition(condition);

return ifstmt;
}

public static SetStatement buildSetStatmenets(int index, String lineTrimmed) {
Pattern pattern = Pattern.compile("set (.+) ?= ?(.+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(lineTrimmed);
matcher.find();

VBStatement stmt = new VBStatement();
stmt.setText(matcher.group(2));

SetStatement setStmt = new SetStatement(matcher.group(1).trim(), stmt);
return setStmt;
}
Expand All @@ -147,24 +140,18 @@ public static ElseIfStatement buildElseIFStatements(int index, String lineTrimme
Pattern pattern = Pattern.compile("elseif(.+)then", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(lineTrimmed);
matcher.find();

ElseIfStatement ifstmt = new ElseIfStatement();
ifstmt.setText(lineTrimmed);
ifstmt.setLineNumber(index);

String condition = matcher.group(1);

ifstmt.setCondition(condition);

return ifstmt;
}

public static ElseStatement buildElseStatements(int index, String lineTrimmed) {

ElseStatement elsestmt = new ElseStatement();
elsestmt.setText(lineTrimmed);
elsestmt.setLineNumber(index);

return elsestmt;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public class StatementFilter {

private Class filterClass;

public StatementFilter(Class clz) {
this.filterClass = clz;
public StatementFilter(Class clazz) {
this.filterClass = clazz;
}

public Class getFilterClass() {
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/optic_fusion1/kitsune/parser/vbs/VBSParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package optic_fusion1.kitsune.parser.vbs;

import optic_fusion1.kitsune.parser.vbs.util.Utils;
import optic_fusion1.kitsune.parser.vbs.interfaces.IContainer;
import optic_fusion1.kitsune.parser.vbs.domain.VBStatement;
import optic_fusion1.kitsune.parser.vbs.domain.EmptyLineStatement;
import optic_fusion1.kitsune.parser.vbs.domain.Comment;
import optic_fusion1.kitsune.parser.vbs.domain.LoopStatement;
import optic_fusion1.kitsune.parser.vbs.domain.ConstStatement;
import optic_fusion1.kitsune.parser.vbs.domain.ElseIfStatement;
import optic_fusion1.kitsune.parser.vbs.domain.Function;
import optic_fusion1.kitsune.parser.vbs.domain.Statement;
import optic_fusion1.kitsune.parser.vbs.domain.Constants;
import optic_fusion1.kitsune.parser.vbs.domain.VariableInit;
import optic_fusion1.kitsune.parser.vbs.domain.IfStatement;
import optic_fusion1.kitsune.parser.vbs.domain.ElseStatement;
import optic_fusion1.kitsune.parser.vbs.domain.SetStatement;
import optic_fusion1.kitsune.parser.vbs.domain.MsgBoxStatement;
import optic_fusion1.kitsune.parser.vbs.domain.FileContainer;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
Expand Down Expand Up @@ -173,7 +190,6 @@ private IContainer getLastContainerFromStack() {
}

private void popLastContainerFromStack() {

if (containerStack.size() > 1) {
containerStack.remove(containerStack.size() - 1);
}
Expand All @@ -193,23 +209,17 @@ public void setContainer(FileContainer container) {

public List<String> getParsedStatementLines() {
List<String> parsedLines = new ArrayList<>();

parsedLines = visitContainersAndExtractRecursively(parsedLines, fileContainer);

return parsedLines;
}

public List<Statement> getParsedStatements() {

return flatStructuredStatements;
}

private List<String> visitContainersAndExtractRecursively(List<String> stmtList, IContainer container) {

List<String> parsedStmtList = stmtList;

List<Statement> statementsInContainer = container.getStatements();

for (Statement st : statementsInContainer) {
if (st instanceof IContainer iContainer) {
parsedStmtList.add(st.getText());
Expand All @@ -218,7 +228,6 @@ private List<String> visitContainersAndExtractRecursively(List<String> stmtList,
parsedStmtList.add(st.getText());
}
}

return parsedStmtList;

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package optic_fusion1.kitsune.parser.vbs;
package optic_fusion1.kitsune.parser.vbs.domain;

public class Comment extends Statement {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package optic_fusion1.kitsune.parser.vbs;
package optic_fusion1.kitsune.parser.vbs.domain;

public class ConstStatement extends Statement {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
package optic_fusion1.kitsune.parser.vbs;
package optic_fusion1.kitsune.parser.vbs.domain;

public final class Constants {

public static final String FUNCTION_IDENTIFIER = "function";

public static final String END_FUNCTION_IDENTIFIER = "end";

public static final String DIM_IDENTIFIER = "dim";

public static final String FOR_IDENTIFIER = "for";

public static final String DO_WHILE_IDENTIFIER = "do while";

public static final String WHILE_IDENTIFIER = "while";

public static final String END_LOOP_REGEX = "(loop|next|wend)";

public static final String IF_IDENTIFIER = "if";

public static final String ELSE_IF_IDENTIFIER = "elseif";

public static final String END_IF_REGEX = "end if";

public static final String ELSE_IDENTIFIER = "else";

public static final String MSG_IDENTIFIER = "msg";

public static final String MSGBOX_IDENTIFIER = "msgbox";

public static final String SET_IDENTIFIER = "set";

public static final String CONST_IDENTIFIER = "const";

public static final String COMMENT_IDENTIFIER = "'";
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package optic_fusion1.kitsune.parser.vbs;
package optic_fusion1.kitsune.parser.vbs.domain;

public enum DataType {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package optic_fusion1.kitsune.parser.vbs;
package optic_fusion1.kitsune.parser.vbs.domain;

import java.util.ArrayList;
import java.util.List;
import optic_fusion1.kitsune.parser.vbs.interfaces.IContainer;
import optic_fusion1.kitsune.parser.vbs.StatementFilter;
import optic_fusion1.kitsune.parser.vbs.util.Utils;

public class ElseIfStatement extends Statement implements IContainer {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package optic_fusion1.kitsune.parser.vbs;
package optic_fusion1.kitsune.parser.vbs.domain;

import java.util.ArrayList;
import java.util.List;
import optic_fusion1.kitsune.parser.vbs.interfaces.IContainer;
import optic_fusion1.kitsune.parser.vbs.StatementFilter;
import optic_fusion1.kitsune.parser.vbs.util.Utils;

public class ElseStatement extends Statement implements IContainer {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package optic_fusion1.kitsune.parser.vbs;
package optic_fusion1.kitsune.parser.vbs.domain;

public class EmptyLineStatement extends Statement {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package optic_fusion1.kitsune.parser.vbs;
package optic_fusion1.kitsune.parser.vbs.domain;

import java.util.ArrayList;
import java.util.List;
import optic_fusion1.kitsune.parser.vbs.interfaces.IContainer;
import optic_fusion1.kitsune.parser.vbs.StatementFilter;
import optic_fusion1.kitsune.parser.vbs.util.Utils;

public class FileContainer implements IContainer {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package optic_fusion1.kitsune.parser.vbs;
package optic_fusion1.kitsune.parser.vbs.domain;

import java.util.ArrayList;
import java.util.List;
import optic_fusion1.kitsune.parser.vbs.interfaces.IContainer;
import optic_fusion1.kitsune.parser.vbs.StatementFilter;
import optic_fusion1.kitsune.parser.vbs.util.Utils;

public class Function extends Statement implements IContainer {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package optic_fusion1.kitsune.parser.vbs;
package optic_fusion1.kitsune.parser.vbs.domain;

import java.util.ArrayList;
import java.util.List;
import optic_fusion1.kitsune.parser.vbs.interfaces.IContainer;
import optic_fusion1.kitsune.parser.vbs.StatementFilter;
import optic_fusion1.kitsune.parser.vbs.util.Utils;

public class IfStatement extends Statement implements IContainer {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package optic_fusion1.kitsune.parser.vbs;
package optic_fusion1.kitsune.parser.vbs.domain;

import java.util.ArrayList;
import java.util.List;
import optic_fusion1.kitsune.parser.vbs.interfaces.IContainer;
import optic_fusion1.kitsune.parser.vbs.StatementFilter;
import optic_fusion1.kitsune.parser.vbs.util.Utils;

public class LoopStatement extends Statement implements IContainer {

Expand Down Expand Up @@ -34,7 +37,6 @@ public List<Statement> getStatements() {

@Override
public List<Statement> filterStatements(StatementFilter filter) {

return Utils.filterStatements(statements, filter);
}
}
Loading

0 comments on commit 7814d85

Please sign in to comment.