From 03edf93023d45697c94935121dc294c2ea0f9bb8 Mon Sep 17 00:00:00 2001 From: OpticFusion1 <37254722+OpticFusion1@users.noreply.github.com> Date: Sun, 25 Sep 2022 23:17:33 -0400 Subject: [PATCH] Version 1.6 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 --- pom.xml | 2 +- .../kitsune/parser/vbs/StatementFactory.java | 8 ++++--- .../kitsune/parser/vbs/VBSParser.java | 8 +++++++ .../parser/vbs/domain/ConstantPool.java | 23 +++++++++++++++++++ .../analyze/analyzer/vbs/VBSAnalyzer.java | 11 ++++----- 5 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 src/main/java/optic_fusion1/kitsune/parser/vbs/domain/ConstantPool.java diff --git a/pom.xml b/pom.xml index 9c5529c..dd45906 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ optic_fusion1 Kitsune - 1.5 + 1.6 diff --git a/src/main/java/optic_fusion1/kitsune/parser/vbs/StatementFactory.java b/src/main/java/optic_fusion1/kitsune/parser/vbs/StatementFactory.java index 0aa61d6..3179fce 100644 --- a/src/main/java/optic_fusion1/kitsune/parser/vbs/StatementFactory.java +++ b/src/main/java/optic_fusion1/kitsune/parser/vbs/StatementFactory.java @@ -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); diff --git a/src/main/java/optic_fusion1/kitsune/parser/vbs/VBSParser.java b/src/main/java/optic_fusion1/kitsune/parser/vbs/VBSParser.java index 1399265..34e681e 100644 --- a/src/main/java/optic_fusion1/kitsune/parser/vbs/VBSParser.java +++ b/src/main/java/optic_fusion1/kitsune/parser/vbs/VBSParser.java @@ -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 containerStack; @@ -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++) { @@ -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(); @@ -231,5 +235,9 @@ private List visitContainersAndExtractRecursively(List stmtList, return parsedStmtList; } + + public ConstantPool getConstantPool() { + return CONSTANT_POOL; + } } diff --git a/src/main/java/optic_fusion1/kitsune/parser/vbs/domain/ConstantPool.java b/src/main/java/optic_fusion1/kitsune/parser/vbs/domain/ConstantPool.java new file mode 100644 index 0000000..61d62c7 --- /dev/null +++ b/src/main/java/optic_fusion1/kitsune/parser/vbs/domain/ConstantPool.java @@ -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 STATEMENTS = new HashMap<>(); + + public Collection 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); + } + +} diff --git a/src/main/java/optic_fusion1/kitsune/tool/impl/analyze/analyzer/vbs/VBSAnalyzer.java b/src/main/java/optic_fusion1/kitsune/tool/impl/analyze/analyzer/vbs/VBSAnalyzer.java index baee022..0b35bd9 100644 --- a/src/main/java/optic_fusion1/kitsune/tool/impl/analyze/analyzer/vbs/VBSAnalyzer.java +++ b/src/main/java/optic_fusion1/kitsune/tool/impl/analyze/analyzer/vbs/VBSAnalyzer.java @@ -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 { @@ -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; @@ -31,7 +31,6 @@ public void analyze(File input) { } if (statement instanceof Comment comment) { LOGGER.info("Comment: " + comment.getText()); - continue; } } }