解析复杂的JavaScript表达式以另一种格式重写它

时间:2015-03-26 20:59:10

标签: javascript c# json parsing

我想我会因为问这个问题而受到嘲笑,但是很好。

我正在尝试重构遗留应用程序,其中存在数据库中数据列的JavaScript表达式中存在验证和条件呈现逻辑。表达式本质上是布尔值,但有些是相当复杂的。一个例子就是:

([!Var6!] == '4' || [!Var6!] == '5' || [!Var6!] == '6') && ([!Var1!] == '2' || [!Var2!] >= 65 || [!Var3!] == '5' || [!Var4!] == '6')

... [!...!]表示法表示存储在JavaScript中的变量。

我需要在我的C#应用​​程序中将其转换为结构化JSON中更有序的格式以提供另一个应用程序,这类似于下面的符号。表达式的复杂性使得我无法使用自己的解析器...所以我希望有一种方法可以让别人已经写过,或者在Visual Studio中有一个我不知道的巧妙技巧或用于解析此混乱的.NET框架。

{
    "condition": { // expression object
        "expressions": [
            { //expression object representing ([!Var6!] == '4' || [!Var6!] == '5' || [!Var6!] == '6')
                "expressions": [
                    { //expression object representing [!Var6!] == '4'
                        "left_operand": {
                            "type": "variable",
                            "value": "Var6"
                        },
                        "operator": "eq",
                        "right_operand": {
                            "type": "numeric",
                            "value": 4
                        }
                    },
                    { //expression object representing [!Var6!] == '5'
                        "left_operand": {
                            "type": "variable",
                            "value": "Var6"
                        },
                        "operator": "eq",
                        "right_operand": {
                            "type": "numeric",
                            "value": 5
                        }
                    },
                    { //expression object representing [!Var6!] == '6'
                        "left_operand": {
                            "type": "variable",
                            "value": "Var6"
                        },
                        "operator": "eq",
                        "right_operand": {
                            "type": "numeric",
                            "value": 6
                        }
                    }
                ],
                "logical_operation": "or"
            },
            { //expression object representing ([!Var1!] == '2' || [!Var2!] >= 65 || [!Var3!] == '5' || [!Var4!] == '6')
                "expressions": [
                    { //expression representing [!Var1!] == '2'
                        "left_operand": {
                            "type": "variable",
                            "value": "Var1"
                        },
                        "operator": "eq",
                        "right_operand": {
                            "type": "numeric",
                            "value": 2
                        }
                    },
                    { //expression representing [!Var2!] >= 65
                        "left_operand": {
                            "type": "variable",
                            "value": "Var2"
                        },
                        "operator": "gte",
                        "right_operand": {
                            "type": "numeric",
                            "value": 65
                        }
                    },
                    { //expression representing [!Var3!] == '5'
                        "left_operand": {
                            "type": "variable",
                            "value": "Var3"
                        },
                        "operator": "eq",
                        "right_operand": {
                            "type": "numeric",
                            "value": 5
                        }
                    },
                    { //expression representing [!Var4!] == '6'
                        "left_operand": {
                            "type": "variable",
                            "value": "Var4"
                        },
                        "operator": "eq",
                        "right_operand": {
                            "type": "numeric",
                            "value": 6
                        }
                    }
                ],
                "logical_operation": "or"
            }
        ],
        "logical_operation": "and"
    }

1 个答案:

答案 0 :(得分:1)

所以第一个也许是主要的问题是将javascript表达式转换为AST然后可以进一步处理。

使用其中一个开源解析器获取AST。

我建议使用typescript编译器,因为它有一个非常干净的代码库(有效的javascript始终是有效的typescript)。 Uglify也非常适合,其作者保留了有关处理AST的非常好的信息。