/**
* Parses a code. The handlers and listeners provided as parameters are called with the results
* of the parsing; i.e. the parsedCodeListener is called with the resulting {@link ParsedCode},
* the parsedTokenHandler is called with the list of parsed tokens etc.
*
* @param input the source code to parse
* @param parsedTokenHandler a handler for lexed tokens
* @param syntaxErrorsListener listener for a list of {@link SyntaxError}s
* @param parsedCodeListener listener for parsed code.
*/
public static void parseCode(String input, ParsedTokenHandler parsedTokenHandler,
ParsedSyntaxErrorHandler syntaxErrorsListener, ParsedCodeHandler parsedCodeListener) {
SyntaxErrorListener syntaxErrorListener = new SyntaxErrorListener();
IEC61131Lexer lexer = lex(input, parsedTokenHandler, syntaxErrorListener);
TopLevelElements ast = parse(new CommonTokenStream(lexer), syntaxErrorListener);
syntaxErrorsListener.accept(syntaxErrorListener.getSyntaxErrors());
// Find types in parsed code
TypeDeclarationVisitor typeVisitor = new TypeDeclarationVisitor();
ast.accept(typeVisitor);
Map<String, Type> definedTypesByName = new HashMap<>();
typeVisitor.getDefinedTypes()
.forEach(type -> definedTypesByName.put(type.getTypeName(), type));
// Find IoVariables in parsed code
VariableVisitor variableVisitor = new VariableVisitor();
ast.accept(variableVisitor);
// Find code blocks in parsed code
BlockVisitor blockVisitor = new BlockVisitor();
ast.accept(blockVisitor);
List<FoldableCodeBlock> foldableCodeBlocks = blockVisitor.getFoldableCodeBlocks();
parsedCodeListener.accept(new ParsedCode(foldableCodeBlocks,
variableVisitor.getDefinedVariables(), typeVisitor.getDefinedTypes()));
}
ParsedCode.java 文件源码
java
阅读 34
收藏 0
点赞 0
评论 0
项目:stvs
作者:
评论列表
文章目录