Algorithm implements Serializable, Translatable { public static Algorithm parse(final String content) throws ParseException { final AlgorithmParserErrorListener errorListener = new AlgorithmParserErrorListener(); final AlgogoLexer lexer = new AlgogoLexer(CharStreams.fromString(content)); lexer.removeErrorListeners(); lexer.addErrorListener(errorListener); final AlgogoParser parser = new AlgogoParser(new CommonTokenStream(lexer)); parser.removeErrorListeners(); parser.addErrorListener(errorListener); final Algorithm algorithm = new Algorithm(); final AlgogoParser.ScriptContext context = parser.script(); final AlgorithmParserVisitor visitor = new AlgorithmParserVisitor(); algorithm.rootBlock = visitor.visitScript(context); final boolean variablesBlockCount = algorithm.rootBlock.listStatementsById(VariablesBlock.STATEMENT_ID).length == 1; final boolean beginningBlockCount = algorithm.rootBlock.listStatementsById(BeginningBlock.STATEMENT_ID).length == 1; if(!variablesBlockCount || !beginningBlockCount) { throw new ParseException("An algorithm must contain one and only one VARIABLES block and BEGINNING block."); } final AlgogoParser.CommentContext comment = context.header; if(comment != null) { final String credits = visitor.visitComment(comment).getContent(); final Matcher groups = Pattern.compile("(.*) by (.*)").matcher(credits); if(groups.matches() && groups.groupCount() >= 2) { algorithm.setTitle(groups.group(1)); algorithm.setAuthor(groups.group(2)); } else { algorithm.rootBlock.insertStatement(visitor.visitComment(comment), 0); } } return algorithm; } Algorithm();  Algorithm(final String title);  Algorithm(final String title, final String author); String getTitle(); void setTitle(String title); String getAuthor(); void setAuthor(String author); AlgorithmRootBlock getRootBlock(); VariablesBlock getVariablesBlock(); BeginningBlock getBeginningBlock(); Exception evaluate(final InputListener inputListener, final OutputListener outputListener); Exception evaluate(final EvaluationContext context); Exception evaluate(final ExpressionEvaluator evaluator, final EvaluationContext context); String toLanguage(final Language language); static Algorithm parse(final String content);  }
@Test void invalidParseTest() { assertThrows(ParseException.class, () -> Algorithm.parse(invalidFile)); }