Skip to content

Commit

Permalink
Version 1.6
Browse files Browse the repository at this point in the history
StatementFactory#buildMsgBoxStatement now uses regex
StatementFactory#buildConstStatements's regex is now case insensitive
Added ConstantPool
Added VBSParser#getConstantPool
VBSAnalyzer now uses the new getConstantPool method
  • Loading branch information
OpticFusion1 committed Sep 26, 2022
1 parent 7814d85 commit 03edf93
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion 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.5</version>
<version>1.6</version>

<build>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,15 @@ public static ElseStatement buildElseStatements(int index, String lineTrimmed) {
}

public static MsgBoxStatement buildMsgBoxStatement(int index, String lineTrimmed) {
String message = lineTrimmed.replaceFirst("msg", "").replaceFirst("MsgBox", "");
MsgBoxStatement statement = new MsgBoxStatement(message);
Pattern pattern = Pattern.compile("(msg|MsgBox) (.*)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(lineTrimmed);
matcher.find();
MsgBoxStatement statement = new MsgBoxStatement(matcher.group(2));
return statement;
}

public static ConstStatement buildConstStatements(int index, String lineTrimmed) {
Pattern pattern = Pattern.compile("Const (.*) = (.*)");
Pattern pattern = Pattern.compile("Const (.*) = (.*)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(lineTrimmed);
matcher.find();
String name = matcher.group(1);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/optic_fusion1/kitsune/parser/vbs/VBSParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static optic_fusion1.kitsune.Kitsune.LOGGER;
import optic_fusion1.kitsune.parser.vbs.domain.ConstantPool;

// TODO: Split this into a VBSFile class
public class VBSParser {

private static final ConstantPool CONSTANT_POOL = new ConstantPool();
private File vbsFile;
private FileContainer fileContainer;
private List<IContainer> containerStack;
Expand All @@ -47,6 +49,7 @@ public VBSParser(File vbsFile) {
containerStack.add(fileContainer);
}

// TODO: Once the split to VBSFile and VBSParser is made, this should accept a VBSFile class
public void parse() {
sourceLines = Utils.getLines(vbsFile);
for (int i = 0; i < sourceLines.size(); i++) {
Expand Down Expand Up @@ -155,6 +158,7 @@ private void identifyAndConvert(final int index, final String line) {
statement.setParent(parent);
parent.getStatements().add(statement);
flatStructuredStatements.add(statement);
CONSTANT_POOL.addStatement(statement);
} else if (lineTrimmed.toLowerCase().startsWith(Constants.COMMENT_IDENTIFIER)) {
Comment comment = StatementFactory.buildComments(index, lineTrimmed);
IContainer parent = getLastContainerFromStack();
Expand Down Expand Up @@ -231,5 +235,9 @@ private List<String> visitContainersAndExtractRecursively(List<String> stmtList,
return parsedStmtList;

}

public ConstantPool getConstantPool() {
return CONSTANT_POOL;
}

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

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;

public class ConstantPool {

private static final HashMap<String, ConstStatement> STATEMENTS = new HashMap<>();

public Collection<ConstStatement> getStatements() {
return Collections.unmodifiableCollection(STATEMENTS.values());
}

public ConstStatement getStatement(String name) {
return STATEMENTS.get(name);
}

public void addStatement(ConstStatement stmnt) {
STATEMENTS.putIfAbsent(stmnt.getName(), stmnt);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import java.io.File;
import static optic_fusion1.kitsune.Kitsune.LOGGER;
import optic_fusion1.kitsune.parser.vbs.VBSParser;
import optic_fusion1.kitsune.parser.vbs.domain.Comment;
import optic_fusion1.kitsune.parser.vbs.domain.ConstStatement;
import optic_fusion1.kitsune.parser.vbs.domain.MsgBoxStatement;
import optic_fusion1.kitsune.parser.vbs.domain.SetStatement;
import optic_fusion1.kitsune.parser.vbs.domain.Statement;
import optic_fusion1.kitsune.parser.vbs.VBSParser;
import optic_fusion1.kitsune.tool.impl.analyze.analyzer.Analyzer;

public class VBSAnalyzer extends Analyzer {
Expand All @@ -16,11 +16,11 @@ public class VBSAnalyzer extends Analyzer {
public void analyze(File input) {
VBSParser parser = new VBSParser(input);
parser.parse();
for (ConstStatement stmnt : parser.getConstantPool().getStatements()) {
LOGGER.info("Const: " + stmnt.getName().trim() + " Value: " + stmnt.getValue());
}

for (Statement statement : parser.getParsedStatements()) {
if (statement instanceof ConstStatement stmnt) {
LOGGER.info("Const: " + stmnt.getName() + " Value: " + stmnt.getValue());
continue;
}
if (statement instanceof MsgBoxStatement stmnt) {
LOGGER.info("MsgBox: " + stmnt.getMessage());
continue;
Expand All @@ -31,7 +31,6 @@ public void analyze(File input) {
}
if (statement instanceof Comment comment) {
LOGGER.info("Comment: " + comment.getText());
continue;
}
}
}
Expand Down

0 comments on commit 03edf93

Please sign in to comment.