mirror of
https://github.com/uwol/proleap-vb6-parser.git
synced 2025-12-18 20:44:35 +03:00
added analyze code
This commit is contained in:
@@ -17,12 +17,13 @@ import io.proleap.vb6.asg.params.VbParserParams;
|
|||||||
|
|
||||||
public interface VbParserRunner {
|
public interface VbParserRunner {
|
||||||
|
|
||||||
Program analyzeFile(File inputFile) throws IOException;
|
Program analyzeCode(String vbCode, String moduleName, VbParserParams params) throws IOException;
|
||||||
|
|
||||||
Program analyzeFile(File inputFile, VbParserParams params) throws IOException;
|
Program analyzeFile(File vbFile) throws IOException;
|
||||||
|
|
||||||
Program analyzeFiles(List<File> inputFiles) throws IOException;
|
Program analyzeFile(File vbFile, VbParserParams params) throws IOException;
|
||||||
|
|
||||||
Program analyzeFiles(List<File> inputFiles, VbParserParams params) throws IOException;
|
Program analyzeFiles(List<File> vbFiles) throws IOException;
|
||||||
|
|
||||||
|
Program analyzeFiles(List<File> vbFiles, VbParserParams params) throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,9 +9,7 @@
|
|||||||
package io.proleap.vb6.asg.runner.impl;
|
package io.proleap.vb6.asg.runner.impl;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -74,6 +72,18 @@ public class VbParserRunnerImpl implements VbParserRunner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Program analyzeCode(final String vbCode, final String moduleName, final VbParserParams params)
|
||||||
|
throws IOException {
|
||||||
|
final Program program = new ProgramImpl();
|
||||||
|
registerModelElements(program);
|
||||||
|
|
||||||
|
parseCode(vbCode, moduleName, program, params);
|
||||||
|
analyze(program);
|
||||||
|
|
||||||
|
return program;
|
||||||
|
}
|
||||||
|
|
||||||
protected void analyzeDeclarations(final Program program) {
|
protected void analyzeDeclarations(final Program program) {
|
||||||
for (final Module module : program.getModules()) {
|
for (final Module module : program.getModules()) {
|
||||||
final ParserVisitor visitor = new VbDeclarationVisitorImpl(module);
|
final ParserVisitor visitor = new VbDeclarationVisitorImpl(module);
|
||||||
@@ -109,33 +119,33 @@ public class VbParserRunnerImpl implements VbParserRunner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Program analyzeFile(final File inputFile) throws IOException {
|
public Program analyzeFile(final File vbFile) throws IOException {
|
||||||
return analyzeFile(inputFile, createDefaultParams());
|
return analyzeFile(vbFile, createDefaultParams());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Program analyzeFile(final File inputFile, final VbParserParams params) throws IOException {
|
public Program analyzeFile(final File vbFile, final VbParserParams params) throws IOException {
|
||||||
final Program program = new ProgramImpl();
|
final Program program = new ProgramImpl();
|
||||||
registerModelElements(program);
|
registerModelElements(program);
|
||||||
|
|
||||||
parseFile(inputFile, program, params);
|
parseFile(vbFile, program, params);
|
||||||
analyze(program);
|
analyze(program);
|
||||||
|
|
||||||
return program;
|
return program;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Program analyzeFiles(final List<File> inputFiles) throws IOException {
|
public Program analyzeFiles(final List<File> vbFiles) throws IOException {
|
||||||
return analyzeFiles(inputFiles, createDefaultParams());
|
return analyzeFiles(vbFiles, createDefaultParams());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Program analyzeFiles(final List<File> inputFiles, final VbParserParams params) throws IOException {
|
public Program analyzeFiles(final List<File> vbFiles, final VbParserParams params) throws IOException {
|
||||||
final Program program = new ProgramImpl();
|
final Program program = new ProgramImpl();
|
||||||
registerModelElements(program);
|
registerModelElements(program);
|
||||||
|
|
||||||
for (final File inputFile : inputFiles) {
|
for (final File vbFile : vbFiles) {
|
||||||
parseFile(inputFile, program, params);
|
parseFile(vbFile, program, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
analyze(program);
|
analyze(program);
|
||||||
@@ -184,65 +194,78 @@ public class VbParserRunnerImpl implements VbParserRunner {
|
|||||||
return "bas".equals(extension);
|
return "bas".equals(extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void parseFile(final File inputFile, final Program program, final VbParserParams params)
|
protected void parseCode(final String vbCode, final String moduleName, final boolean isClazzModule,
|
||||||
throws IOException {
|
final boolean isStandardModule, final Program program, final VbParserParams params) throws IOException {
|
||||||
if (!inputFile.isFile()) {
|
// run the lexer
|
||||||
LOG.warn("Could not find file {}", inputFile.getAbsolutePath());
|
final VisualBasic6Lexer lexer = new VisualBasic6Lexer(CharStreams.fromString(vbCode));
|
||||||
|
|
||||||
|
if (!params.getIgnoreSyntaxErrors()) {
|
||||||
|
// register an error listener, so that preprocessing stops on errors
|
||||||
|
lexer.removeErrorListeners();
|
||||||
|
lexer.addErrorListener(new ThrowingErrorListener());
|
||||||
|
}
|
||||||
|
|
||||||
|
// get a list of matched tokens
|
||||||
|
final CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
|
|
||||||
|
// pass the tokens to the parser
|
||||||
|
final VisualBasic6Parser parser = new VisualBasic6Parser(tokens);
|
||||||
|
|
||||||
|
if (!params.getIgnoreSyntaxErrors()) {
|
||||||
|
// register an error listener, so that preprocessing stops on errors
|
||||||
|
parser.removeErrorListeners();
|
||||||
|
parser.addErrorListener(new ThrowingErrorListener());
|
||||||
|
}
|
||||||
|
|
||||||
|
// specify our entry point
|
||||||
|
final StartRuleContext ctx = parser.startRule();
|
||||||
|
|
||||||
|
// determine the effective module name
|
||||||
|
final String declaredModuleName = analyzeDeclaredModuleName(ctx);
|
||||||
|
final String effectiveModuleName;
|
||||||
|
|
||||||
|
if (declaredModuleName != null && !declaredModuleName.isEmpty()) {
|
||||||
|
effectiveModuleName = declaredModuleName;
|
||||||
|
} else {
|
||||||
|
effectiveModuleName = moduleName;
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<String> lines = splitLines(vbCode);
|
||||||
|
final ParserVisitor visitor = new VbModuleVisitorImpl(effectiveModuleName, lines, isClazzModule,
|
||||||
|
isStandardModule, tokens, program);
|
||||||
|
|
||||||
|
visitor.visit(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void parseCode(final String vbCode, final String moduleName, final Program program,
|
||||||
|
final VbParserParams params) throws IOException {
|
||||||
|
LOG.info("Parsing module {}.", moduleName);
|
||||||
|
|
||||||
|
parseCode(vbCode, moduleName, true, false, program, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void parseFile(final File vbFile, final Program program, final VbParserParams params) throws IOException {
|
||||||
|
if (!vbFile.isFile()) {
|
||||||
|
LOG.warn("Could not find file {}", vbFile.getAbsolutePath());
|
||||||
} else {
|
} else {
|
||||||
final Charset charset = params.getCharset();
|
final Charset charset = params.getCharset();
|
||||||
|
|
||||||
LOG.info("Parsing file {} with charset {}.", inputFile.getName(), charset);
|
LOG.info("Parsing file {} with charset {}.", vbFile.getName(), charset);
|
||||||
|
|
||||||
final InputStream inputStream = new FileInputStream(inputFile);
|
final String vbCode = FileUtils.readFileToString(vbFile, charset);
|
||||||
final VisualBasic6Lexer lexer = new VisualBasic6Lexer(CharStreams.fromStream(inputStream, charset));
|
|
||||||
|
|
||||||
if (!params.getIgnoreSyntaxErrors()) {
|
|
||||||
// register an error listener, so that preprocessing stops on errors
|
|
||||||
lexer.removeErrorListeners();
|
|
||||||
lexer.addErrorListener(new ThrowingErrorListener());
|
|
||||||
}
|
|
||||||
|
|
||||||
// get a list of matched tokens
|
|
||||||
final CommonTokenStream tokens = new CommonTokenStream(lexer);
|
|
||||||
|
|
||||||
// pass the tokens to the parser
|
|
||||||
final VisualBasic6Parser parser = new VisualBasic6Parser(tokens);
|
|
||||||
|
|
||||||
if (!params.getIgnoreSyntaxErrors()) {
|
|
||||||
// register an error listener, so that preprocessing stops on errors
|
|
||||||
parser.removeErrorListeners();
|
|
||||||
parser.addErrorListener(new ThrowingErrorListener());
|
|
||||||
}
|
|
||||||
|
|
||||||
// specify our entry point
|
|
||||||
final StartRuleContext ctx = parser.startRule();
|
|
||||||
|
|
||||||
// determine the module name
|
// determine the module name
|
||||||
final String declaredModuleName = analyzeDeclaredModuleName(ctx);
|
final String moduleName = getModuleName(vbFile);
|
||||||
final String moduleName;
|
|
||||||
|
|
||||||
if (declaredModuleName != null && !declaredModuleName.isEmpty()) {
|
|
||||||
moduleName = declaredModuleName;
|
|
||||||
} else {
|
|
||||||
moduleName = getModuleName(inputFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
// analyze contained modules and types
|
// analyze contained modules and types
|
||||||
final boolean isClazzModule = isClazzModule(inputFile);
|
final boolean isClazzModule = isClazzModule(vbFile);
|
||||||
final boolean isStandardModule = isStandardModule(inputFile);
|
final boolean isStandardModule = isStandardModule(vbFile);
|
||||||
|
|
||||||
final String input = FileUtils.readFileToString(inputFile, charset);
|
parseCode(vbCode, moduleName, isClazzModule, isStandardModule, program, params);
|
||||||
final List<String> lines = splitLines(input);
|
|
||||||
|
|
||||||
final ParserVisitor visitor = new VbModuleVisitorImpl(moduleName, lines, isClazzModule, isStandardModule,
|
|
||||||
tokens, program);
|
|
||||||
|
|
||||||
LOG.info("Collecting types in file {}.", inputFile.getName());
|
|
||||||
visitor.visit(ctx);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerApiEnumeration(final Program program, final ApiEnumeration apiEnumeration) {
|
protected void registerApiEnumeration(final Program program, final ApiEnumeration apiEnumeration) {
|
||||||
program.getTypeRegistry().registerType(apiEnumeration);
|
program.getTypeRegistry().registerType(apiEnumeration);
|
||||||
program.getApiEnumerationRegistry().registerApiEnumeration(apiEnumeration);
|
program.getApiEnumerationRegistry().registerApiEnumeration(apiEnumeration);
|
||||||
}
|
}
|
||||||
@@ -465,7 +488,7 @@ public class VbParserRunnerImpl implements VbParserRunner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> splitLines(final String input) {
|
protected List<String> splitLines(final String input) {
|
||||||
final Scanner scanner = new Scanner(input);
|
final Scanner scanner = new Scanner(input);
|
||||||
final List<String> result = new ArrayList<String>();
|
final List<String> result = new ArrayList<String>();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user