ForStatement { static public ForStatement parse(String statement) throws TokenException { if (!statement.startsWith("(")) { throw new TokenException("For block does not start with parenthese"); } if (!statement.endsWith(")")) { throw new TokenException("For block does not end with parenthese"); } statement = statement.substring(1, statement.length() - 1); if (statement.contains(";")) { String[] generalParts = statement.split(";"); if (generalParts.length != 3) { throw new TokenException("For block has invalid general form [e.g. for (initialization; termination; increment) {]"); } List<String> parts = new ArrayList<>(); for (String s : generalParts) { parts.add(s.trim()); } return new ForStatement(parts); } if (statement.contains(":")) { String[] enhancedParts = statement.split(":"); if (enhancedParts.length != 2) { throw new TokenException("For block has invalid enhanced form [e.g. for (item : items) {]"); } String argsPart = enhancedParts[0].trim(); if (argsPart.equals("")) { throw new TokenException("For block with enhanced form contains an empty string for arguments part (e.g. for (arg : list)"); } boolean hasParenthese = false; if (argsPart.startsWith("(")) { if (!argsPart.endsWith(")")) { throw new TokenException("For block with enhanced form contains invalid arguments section (closing parenthese not found)"); } hasParenthese = true; argsPart = argsPart.substring(1, argsPart.length() - 1); } List<JavaVariable> arguments = null; try { arguments = JavaVariable.parseList(argsPart); } catch (TokenException e) { throw new TokenException(e.getMessage()); } if (arguments.size() > 1 && !hasParenthese) { throw new TokenException("For block with enhanced form contains invalid arguments section (more than 1 argument must be enclosed with parenthese e.g. (k,v))"); } if (arguments.size() > 3) { throw new TokenException("For block with enhanced form contains " + arguments.size() + " arguments (at most 3 supported)"); } String valuePart = enhancedParts[1].trim(); if (valuePart.equals("")) { throw new TokenException("For block with enhanced form contains an empty string for value part (e.g. for (item : value)"); } return new ForStatement(arguments, valuePart.trim()); } throw new TokenException("For block appears to be invalid (neither general or enhanced form detected)"); } ForStatement(List<String> generalParts);  ForStatement(List<JavaVariable> arguments, String valueExpression); Form getForm(); List<String> getGeneralParts(); List<JavaVariable> getArguments(); boolean hasAnyUntypedArguments(); String getValueExpression(); static ForStatement parse(String statement);  }
@Test public void startAndEndingParenthese() throws Exception { try { ForStatement.parse("int x = 0; x < users.size(); x++"); Assert.fail("Expected exception"); } catch (TokenException e) { } try { ForStatement.parse(" (int x = 0; x < users.size(); x++"); Assert.fail("Expected exception"); } catch (TokenException e) { } try { ForStatement.parse("( )"); Assert.fail("Expected exception"); } catch (TokenException e) { } }