继续爆肝继续干 2023-01-12 09:27 采纳率: 33.3%
浏览 20

js数据结构的转换(pegJS转义的数据)

使用pegJs转义了一个字符串数据,但是不知道怎么把转义的数据再转换成我们需要的数据结构

转换出的数据

//转义前的数据
let o = '(LeftRightMove == "-20°" && ExceptResult == "65537") && is_support == "False" || (is_explorer == "False") || ExceptResult == "null" && (DownUpMove == "-20°" && Brightness == "1000lux")'

//转移后的数据
{
    "type": "Program",
    "body": [
       {
          "type": "ExpressionStatement",
          "expression": {
             "type": "LogicalExpression",
             "operator": "OR",
             "conditionleft": {
                "type": "LogicalExpression",
                "operator": "OR",
                "conditionleft": {
                   "type": "LogicalExpression",
                   "operator": "AND",
                   "conditionleft": {
                      "type": "LogicalExpression",
                      "operator": "AND",
                      "conditionleft": {
                         "type": "BinaryExpression",
                         "operator": "==",
                         "left": {
                            "type": "Identifier",
                            "name": "LeftRightMove"
                         },
                         "right": {
                            "type": "Literal",
                            "value": "-20°"
                         }
                      },
                      "conditionright": {
                         "type": "BinaryExpression",
                         "operator": "==",
                         "left": {
                            "type": "Identifier",
                            "name": "ExceptResult"
                         },
                         "right": {
                            "type": "Literal",
                            "value": "65537"
                         }
                      }
                   },
                   "conditionright": {
                      "type": "BinaryExpression",
                      "operator": "==",
                      "left": {
                         "type": "Identifier",
                         "name": "is_support"
                      },
                      "right": {
                         "type": "Literal",
                         "value": "False"
                      }
                   }
                },
                "conditionright": {
                   "type": "BinaryExpression",
                   "operator": "==",
                   "left": {
                      "type": "Identifier",
                      "name": "is_explorer"
                   },
                   "right": {
                      "type": "Literal",
                      "value": "False"
                   }
                }
             },
             "conditionright": {
                "type": "LogicalExpression",
                "operator": "AND",
                "conditionleft": {
                   "type": "BinaryExpression",
                   "operator": "==",
                   "left": {
                      "type": "Identifier",
                      "name": "ExceptResult"
                   },
                   "right": {
                      "type": "Literal",
                      "value": "null"
                   }
                },
                "conditionright": {
                   "type": "LogicalExpression",
                   "operator": "AND",
                   "conditionleft": {
                      "type": "BinaryExpression",
                      "operator": "==",
                      "left": {
                         "type": "Identifier",
                         "name": "DownUpMove"
                      },
                      "right": {
                         "type": "Literal",
                         "value": "-20°"
                      }
                   },
                   "conditionright": {
                      "type": "BinaryExpression",
                      "operator": "==",
                      "left": {
                         "type": "Identifier",
                         "name": "Brightness"
                      },
                      "right": {
                         "type": "Literal",
                         "value": "1000lux"
                      }
                   }
                }
             }
          }
       }
    ]
 }

需要的数据结构

const rulesDataDefaultItem = {
  relation: 'AND',
  id: 1,
  conditions: [
    {
      name: 'name',
      operator: '=',
      value: 'value',
      selectvalues: 'AND',
    },
    {
      name: 'name',
      operator: '=',
      value: 'value',
      selectvalues: 'AND',
    },
  ],
  children: [
    {
  relation: 'AND',
  id: 1,
  conditions: [
    {
      name: 'name',
      operator: '=',
      value: 'value',
      selectvalues: 'AND',
    },
    {
      name: 'name',
      operator: '=',
      value: 'value',
      selectvalues: 'AND',
    },
  ],
  children: [],
}
  ],
}

各位小伙伴有没有好的思路或者方法帮一下,万分感谢

  • 写回答

2条回答 默认 最新

  • |__WhoAmI__| 2023-01-12 10:19
    关注

    可以采用递归来解析,通过分析转义后的表达式,抽象出逻辑表达式,二元表达式,一元表达式,分别对应转义后的不同对象,然后通过递归的方式把转义后的对象转换成需要的数据结构
    仅供参考,望采纳,谢谢。

    评论

报告相同问题?

问题事件

  • 创建了问题 1月12日

悬赏问题

  • ¥50 带防重放token(Antireplay-Token)的网站怎么用Python发送请求
  • ¥15 visa版本没问题,串口调试助手调试串口正常使用,但是labview刷新不出来
  • ¥15 java MQTT SSL证书
  • ¥20 求一个手机号或者Wxid转微信号的工具
  • ¥20 msconfig开启安全引导后重启电脑黑屏
  • ¥15 对email表单做了一般邮箱正则校验,现在需要额外输入为hotmail或outlook、gmail时做特定邮箱校验,不符合提示出来。并且保证不影响其他正确的邮箱格式通过校验,通过不会提示。
  • ¥20 Win11,bitblocker密钥恢复失败
  • ¥15 stm32 cubemx配置sdram,初始化后无法读写sdram
  • ¥100 上位机到dtu再到485继电器模块 目前结束命令有百分之2左右概率执行失败求指导
  • ¥15 ifcopenshell库在vs2019下的配置