@Test public void parseFunction() { t2("a(b,c)", "a", "b,c"); t2("abc(b,c,d(e,f))", "abc", "b,c,d(e,f)"); }
public static List<String> parseFunction(String str) { FunctionState state = FunctionState.START; ArrayList<String> list = new ArrayList<>(2); field.setLength(0); boolean parameters_ok = false; int quoteCount_InParentheses = 0; int leftNotMatchCount_InParentheses = 0; for (char c : str.toCharArray()) { switch (state) { case START: if (c == whitespace) { } else if (parameters_ok) { throw new RuntimeException("已经解析出参数了，后面不能继续有非空字符了"); } else if (c == leftParentheses) { throw new RuntimeException("缺少函数名字"); } else { field.setLength(0); field.append(c); state = FunctionState.NAME; } break; case NAME: if (c == leftParentheses) { list.add(field.toString()); field.setLength(0); quoteCount_InParentheses = 0; leftNotMatchCount_InParentheses = 1; state = FunctionState.IN_PARENTHESES; } else { field.append(c); } break; case IN_PARENTHESES: if (c == quote) { quoteCount_InParentheses++; } if (quoteCount_InParentheses % 2 == 1) { field.append(c); } else if (c == leftParentheses) { leftNotMatchCount_InParentheses++; field.append(c); } else if (c == rightParentheses) { leftNotMatchCount_InParentheses--; if (leftNotMatchCount_InParentheses > 0) { field.append(c); } if (leftNotMatchCount_InParentheses == 0) { list.add(field.toString()); parameters_ok = true; state = FunctionState.START; } } else { field.append(c); } break; } } if (list.size() != 2) { throw new RuntimeException("个数不符" + list.size()); } return list; }
NestListParser { public static List<String> parseFunction(String str) { FunctionState state = FunctionState.START; ArrayList<String> list = new ArrayList<>(2); field.setLength(0); boolean parameters_ok = false; int quoteCount_InParentheses = 0; int leftNotMatchCount_InParentheses = 0; for (char c : str.toCharArray()) { switch (state) { case START: if (c == whitespace) { } else if (parameters_ok) { throw new RuntimeException("已经解析出参数了，后面不能继续有非空字符了"); } else if (c == leftParentheses) { throw new RuntimeException("缺少函数名字"); } else { field.setLength(0); field.append(c); state = FunctionState.NAME; } break; case NAME: if (c == leftParentheses) { list.add(field.toString()); field.setLength(0); quoteCount_InParentheses = 0; leftNotMatchCount_InParentheses = 1; state = FunctionState.IN_PARENTHESES; } else { field.append(c); } break; case IN_PARENTHESES: if (c == quote) { quoteCount_InParentheses++; } if (quoteCount_InParentheses % 2 == 1) { field.append(c); } else if (c == leftParentheses) { leftNotMatchCount_InParentheses++; field.append(c); } else if (c == rightParentheses) { leftNotMatchCount_InParentheses--; if (leftNotMatchCount_InParentheses > 0) { field.append(c); } if (leftNotMatchCount_InParentheses == 0) { list.add(field.toString()); parameters_ok = true; state = FunctionState.START; } } else { field.append(c); } break; } } if (list.size() != 2) { throw new RuntimeException("个数不符" + list.size()); } return list; } }
NestListParser { public static List<String> parseFunction(String str) { FunctionState state = FunctionState.START; ArrayList<String> list = new ArrayList<>(2); field.setLength(0); boolean parameters_ok = false; int quoteCount_InParentheses = 0; int leftNotMatchCount_InParentheses = 0; for (char c : str.toCharArray()) { switch (state) { case START: if (c == whitespace) { } else if (parameters_ok) { throw new RuntimeException("已经解析出参数了，后面不能继续有非空字符了"); } else if (c == leftParentheses) { throw new RuntimeException("缺少函数名字"); } else { field.setLength(0); field.append(c); state = FunctionState.NAME; } break; case NAME: if (c == leftParentheses) { list.add(field.toString()); field.setLength(0); quoteCount_InParentheses = 0; leftNotMatchCount_InParentheses = 1; state = FunctionState.IN_PARENTHESES; } else { field.append(c); } break; case IN_PARENTHESES: if (c == quote) { quoteCount_InParentheses++; } if (quoteCount_InParentheses % 2 == 1) { field.append(c); } else if (c == leftParentheses) { leftNotMatchCount_InParentheses++; field.append(c); } else if (c == rightParentheses) { leftNotMatchCount_InParentheses--; if (leftNotMatchCount_InParentheses > 0) { field.append(c); } if (leftNotMatchCount_InParentheses == 0) { list.add(field.toString()); parameters_ok = true; state = FunctionState.START; } } else { field.append(c); } break; } } if (list.size() != 2) { throw new RuntimeException("个数不符" + list.size()); } return list; }  }
NestListParser { public static List<String> parseFunction(String str) { FunctionState state = FunctionState.START; ArrayList<String> list = new ArrayList<>(2); field.setLength(0); boolean parameters_ok = false; int quoteCount_InParentheses = 0; int leftNotMatchCount_InParentheses = 0; for (char c : str.toCharArray()) { switch (state) { case START: if (c == whitespace) { } else if (parameters_ok) { throw new RuntimeException("已经解析出参数了，后面不能继续有非空字符了"); } else if (c == leftParentheses) { throw new RuntimeException("缺少函数名字"); } else { field.setLength(0); field.append(c); state = FunctionState.NAME; } break; case NAME: if (c == leftParentheses) { list.add(field.toString()); field.setLength(0); quoteCount_InParentheses = 0; leftNotMatchCount_InParentheses = 1; state = FunctionState.IN_PARENTHESES; } else { field.append(c); } break; case IN_PARENTHESES: if (c == quote) { quoteCount_InParentheses++; } if (quoteCount_InParentheses % 2 == 1) { field.append(c); } else if (c == leftParentheses) { leftNotMatchCount_InParentheses++; field.append(c); } else if (c == rightParentheses) { leftNotMatchCount_InParentheses--; if (leftNotMatchCount_InParentheses > 0) { field.append(c); } if (leftNotMatchCount_InParentheses == 0) { list.add(field.toString()); parameters_ok = true; state = FunctionState.START; } } else { field.append(c); } break; } } if (list.size() != 2) { throw new RuntimeException("个数不符" + list.size()); } return list; }  static List<String> parseNestList(String str); static List<String> parseFunction(String str); }
NestListParser { public static List<String> parseFunction(String str) { FunctionState state = FunctionState.START; ArrayList<String> list = new ArrayList<>(2); field.setLength(0); boolean parameters_ok = false; int quoteCount_InParentheses = 0; int leftNotMatchCount_InParentheses = 0; for (char c : str.toCharArray()) { switch (state) { case START: if (c == whitespace) { } else if (parameters_ok) { throw new RuntimeException("已经解析出参数了，后面不能继续有非空字符了"); } else if (c == leftParentheses) { throw new RuntimeException("缺少函数名字"); } else { field.setLength(0); field.append(c); state = FunctionState.NAME; } break; case NAME: if (c == leftParentheses) { list.add(field.toString()); field.setLength(0); quoteCount_InParentheses = 0; leftNotMatchCount_InParentheses = 1; state = FunctionState.IN_PARENTHESES; } else { field.append(c); } break; case IN_PARENTHESES: if (c == quote) { quoteCount_InParentheses++; } if (quoteCount_InParentheses % 2 == 1) { field.append(c); } else if (c == leftParentheses) { leftNotMatchCount_InParentheses++; field.append(c); } else if (c == rightParentheses) { leftNotMatchCount_InParentheses--; if (leftNotMatchCount_InParentheses > 0) { field.append(c); } if (leftNotMatchCount_InParentheses == 0) { list.add(field.toString()); parameters_ok = true; state = FunctionState.START; } } else { field.append(c); } break; } } if (list.size() != 2) { throw new RuntimeException("个数不符" + list.size()); } return list; }  static List<String> parseNestList(String str); static List<String> parseFunction(String str);  }