@Test public void test_parsing_file() throws IOException { CharStream cs = CharStreams.fromFileName("src/test/resources/test1.sol"); SolidityParsingPhase parser = new SolidityParsingPhase(); SourceUnitContext suc = parser.parse(cs); assertThat(suc).isNotNull(); ContractDefinitionContext cdc = suc.contractDefinition().get(0); assertThat(cdc).isNotNull(); List<ContractPartContext> cpcList = cdc.contractPart(); List<FunctionDefinitionContext> funList = cpcList.stream() .map(ContractPartContext::functionDefinition) .filter(Objects::nonNull) .collect(Collectors.toList()); assertThat(funList).hasSize(3); List<StateVariableDeclarationContext> vars = cpcList.stream() .map(ContractPartContext::stateVariableDeclaration) .filter(Objects::nonNull) .collect(Collectors.toList()); assertThat(vars).hasSize(3); BlockContext blckCtx = funList.get(0).block(); List<StatementContext> stmt = blckCtx.statement(); assertThat(stmt.get(0).simpleStatement()).isNotNull(); assertThat(stmt.get(1).simpleStatement()).isNotNull(); assertThat(stmt.get(2).simpleStatement()).isNotNull(); WhileStatementContext whileStmt = stmt.get(3).whileStatement(); assertThat(whileStmt).isNotNull(); assertThat(whileStmt.statement().simpleStatement()).isNotNull(); blckCtx = funList.get(1).block(); stmt = blckCtx.statement(); assertThat(stmt.get(0).returnStatement()).isNotNull(); blckCtx = funList.get(2).block(); stmt = blckCtx.statement(); IfStatementContext ifStmt = stmt.get(0).ifStatement(); assertThat(ifStmt).isNotNull(); blckCtx = ifStmt.statement().get(0).block(); assertThat(blckCtx.statement().get(0).simpleStatement()).isNotNull(); assertThat(parser.comments).hasSize(3); }
public SourceUnitContext parse(String fileContents) { CharStream cs = CharStreams.fromString(fileContents); SolidityLexer sl = new SolidityLexer(cs); CommonTokenStream tokenStream = new CommonTokenStream(sl); tokenStream.fill(); SolidityParser parser = new SolidityParser(tokenStream); parser.setErrorHandler(new ErrorStrategy()); addCommentsToSet(tokenStream.getTokens()); this.tokens = parser.getTokenStream(); return parser.sourceUnit(); }
SolidityParsingPhase { public SourceUnitContext parse(String fileContents) { CharStream cs = CharStreams.fromString(fileContents); SolidityLexer sl = new SolidityLexer(cs); CommonTokenStream tokenStream = new CommonTokenStream(sl); tokenStream.fill(); SolidityParser parser = new SolidityParser(tokenStream); parser.setErrorHandler(new ErrorStrategy()); addCommentsToSet(tokenStream.getTokens()); this.tokens = parser.getTokenStream(); return parser.sourceUnit(); } }
SolidityParsingPhase { public SourceUnitContext parse(String fileContents) { CharStream cs = CharStreams.fromString(fileContents); SolidityLexer sl = new SolidityLexer(cs); CommonTokenStream tokenStream = new CommonTokenStream(sl); tokenStream.fill(); SolidityParser parser = new SolidityParser(tokenStream); parser.setErrorHandler(new ErrorStrategy()); addCommentsToSet(tokenStream.getTokens()); this.tokens = parser.getTokenStream(); return parser.sourceUnit(); } SolidityParsingPhase(); }
SolidityParsingPhase { public SourceUnitContext parse(String fileContents) { CharStream cs = CharStreams.fromString(fileContents); SolidityLexer sl = new SolidityLexer(cs); CommonTokenStream tokenStream = new CommonTokenStream(sl); tokenStream.fill(); SolidityParser parser = new SolidityParser(tokenStream); parser.setErrorHandler(new ErrorStrategy()); addCommentsToSet(tokenStream.getTokens()); this.tokens = parser.getTokenStream(); return parser.sourceUnit(); } SolidityParsingPhase(); SourceUnitContext parse(String fileContents); SourceUnitContext parse(CharStream cs); void addCommentsToSet(List<? extends Token> tokens); Integer getEmptyLines(); void setEmptyLines(Integer emptyLines); Integer getLinesOfComments(); TokenStream getTokens(); }
SolidityParsingPhase { public SourceUnitContext parse(String fileContents) { CharStream cs = CharStreams.fromString(fileContents); SolidityLexer sl = new SolidityLexer(cs); CommonTokenStream tokenStream = new CommonTokenStream(sl); tokenStream.fill(); SolidityParser parser = new SolidityParser(tokenStream); parser.setErrorHandler(new ErrorStrategy()); addCommentsToSet(tokenStream.getTokens()); this.tokens = parser.getTokenStream(); return parser.sourceUnit(); } SolidityParsingPhase(); SourceUnitContext parse(String fileContents); SourceUnitContext parse(CharStream cs); void addCommentsToSet(List<? extends Token> tokens); Integer getEmptyLines(); void setEmptyLines(Integer emptyLines); Integer getLinesOfComments(); TokenStream getTokens(); final Set<Token> comments; }