Java - 解析用户定义的函数

Java - parse user defined functions

我正在开发一个程序,我希望用户在其中定义一个简单的函数,例如 randomInt(0,10) 要么 randomString(10) 而不是静态参数。解析和处理此类函数的最佳方法是什么?

我还没有找到任何此类问题的示例,解析器不必非常高效,不会经常调用它,但主要是我想关注良好的代码可读性和可扩展性。

用户输入示例:

"This is user randomString(5) and he is randomInt(18,60) years old!"

预期输出:

"This is user phiob and he is 45 years old!"

"This is user sdfrt and he is 30 years old!"

您可以使用如下内容: 警告,我还没有测试过。刚开始的东西

public String parseInput(String input){
    String[] inputArray = input.split(" ");
    String output = "";
    for(String in : inputArray){ //run through each word of the user input
        if(in.contains("randomString(")){ //if the user is calling randomString
            String params = in.replace("randomString(", ""); //strip away function to get to params
            params = in.replace("(", ""); //strip away function to get to params
            String[] paramsArray = params.split(",");  //these are string integers, and could be converted
            //send off these split apart parameters to your randomString method
            String out = randomString(paramsArray); //method parses string integers, outputs string
            output += out + " ";
        }else if(in.contains("randomInt(")){ //if the user is calling randomInt
            String params = in.replace("randomInt(", ""); //strip away function to get to params
            params = in.replace("(", ""); //strip away function to get to params
            String[] paramsArray = params.split(","); //these are string integers, and could be converted
            //send off these split apart parameters to your randomInt method
            String out = randomInt(paramsArray); //method parses string integers, outputs string
            output += out + " ";
        }else{ //if the user is just entering text
            output += in + " "; //concat the output with what the user wrote plus a space
        }

    }
    return output;
}

一种选择是使用Spring SPEL。但它迫使您稍微更改表达式并使用 Spring library:

表达式可以如下所示:

'This is user ' + randomString(5) + ' and he is ' + randomInt(18,60) + ' years old!'

或者这个:

This is user #{randomString(5)} and he is #{randomInt(18,60)} years old!

或者您可以通过自定义 TemplateParserContext.

来实现您自己的

这是代码:

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class SomeTest {

        @Test
        public void test() {
            ExpressionParser parser = new SpelExpressionParser();

            Expression exp = parser.parseExpression(
                "This is user #{randomString(5)} and he is #{randomInt(18,60)} years old!", 
                new TemplateParserContext() );

            //alternative
            //Expression exp = parser.parseExpression(
            //        "'This is user ' + randomString(5) + ' and he is ' + randomInt(18,60) + ' years old!'");
            //    String message = (String) exp.getValue( new StandardEvaluationContext(this) );


            String message = (String) exp.getValue( new StandardEvaluationContext(this) );
        }

        public String randomString(int i) {
            return "rs-" + i;
        }

        public String randomInt(int i, int j) {
            return "ri-" + i + ":" + "j";
        }

    }

您传递给 StandardEvaluationContext 的任何对象都应该具有这些方法。我将它们放在同样运行表达式的 class 中。