SyntaxTools { public static String handleMissingArgCommas(String line, List<String> methods) { int hashCode = methods.hashCode(); Set<String> hashedMethods = CACHED_METHODS.get(hashCode); if(hashedMethods == null) { hashedMethods = Sets.newHashSetWithExpectedSize(methods.size()); hashedMethods.addAll(methods); CACHED_METHODS.put(hashCode, hashedMethods); } char[] chars = line.toCharArray(); StringBuilder transformed = new StringBuilder(); StringBuilder gather = new StringBuilder(); boolean foundMethod = false; boolean inSingleQuotes = false; boolean inDoubleQuotes = false; int parenCount = 0; for (char c : chars) { if(Character.isWhitespace(c) && !foundMethod) { transformed.append(gather); transformed.append(c); foundMethod = hashedMethods.contains(gather.toString()); gather.setLength(0); } else if(Character.isWhitespace(c) && foundMethod) { if(transformed.charAt(transformed.length() - 1) != ',' && !inSingleQuotes && !inDoubleQuotes && c != '\n') { transformed.append(","); } transformed.append(c); } else if(c == '(' && !foundMethod) { parenCount++; transformed.append(gather); transformed.append(c); foundMethod = hashedMethods.contains(gather.toString()); gather.setLength(0); } else if(c == '(' && foundMethod) { parenCount++; transformed.append(c); } else if(c == ';') { transformed.append(c); foundMethod = false; parenCount = 0; } else if(c == ')') { parenCount--; transformed.append(c); foundMethod = parenCount == 0 ? false : foundMethod; } else if(c == '"') { transformed.append(c); inSingleQuotes = !inSingleQuotes; } else if(c == '\'') { transformed.append(c); inDoubleQuotes = !inDoubleQuotes; } else if(foundMethod) { transformed.append(c); } else { gather.append(c); } } transformed.append(gather); return transformed.toString(); } private  SyntaxTools(); static String handleMissingArgCommas(String line,
            List<String> methods); static String handleShortSyntax(String line, List<String> options);  }
@Test public void testHandleMissingArgsCommasSingleQuotes() { String line = "add 'name' 'jeff nelson' 1"; String expected = "add 'name', 'jeff nelson', 1"; Assert.assertEquals(expected, SyntaxTools.handleMissingArgCommas(line, METHODS)); }
@Test public void testHandleMissingArgsCommas() { String line = "add \"name\" \"jeff nelson\" '1 2'"; String expected = "add \"name\", \"jeff nelson\", '1 2'"; Assert.assertEquals(expected, SyntaxTools.handleMissingArgCommas(line, METHODS)); }
@Test public void testHandleMissingArgsCommasSingleArg() { String line = "foo 1"; String expected = line; Assert.assertEquals(expected, SyntaxTools.handleMissingArgCommas(line, METHODS)); }
@Test public void testHandleMissingArgsCommasArgsInParenthesis() { String line = "add(\"name\" \"jeff nelson\" 1)"; String expected = "add(\"name\", \"jeff nelson\", 1)"; Assert.assertEquals(expected, SyntaxTools.handleMissingArgCommas(line, METHODS)); }
@Test public void testHandleMissingArgsCommasNestedMethods() { String line = "foo(add(1 'hello world' 3) sub(1 2 \"jeff nelson\"))"; String expected = "foo(add(1, 'hello world', 3), sub(1, 2, \"jeff nelson\"))"; Assert.assertEquals(expected, SyntaxTools.handleMissingArgCommas(line, METHODS)); }
@Test public void testHandleMissingArgsCommasOnlySomeMissing() { String line = "add \"name\", \"jeff\" 1"; String expected = "add \"name\", \"jeff\", 1"; Assert.assertEquals(expected, SyntaxTools.handleMissingArgCommas(line, METHODS)); }