1/* 2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15const { NumberIncrease } = require("../tools/common"); 16const re = require("../tools/re"); 17 18/** Enum解析 */ 19function analyzeEnum(data) { 20 let body = re.replaceAll(data, "\n", "").split(",") 21 let result = { 22 element: [], 23 function: [], 24 enumValueType: 0 // 0代表数字,1代表字符串 25 } 26 for (let i in body) { 27 let bodyContent = body[i] 28 while (bodyContent.length > 0 && bodyContent[0] == ' ') { 29 bodyContent = bodyContent.substring(1, bodyContent.length) 30 } 31 while (bodyContent.length > 0 && bodyContent[-1] == ' ') { 32 bodyContent = bodyContent.substring(0, bodyContent.length - 1) 33 } 34 if (bodyContent == "") { 35 break 36 } 37 analyzeEnumResult(result, bodyContent, i) 38 } 39 return result 40} 41 42function analyzeEnumResult(result, bodyContent, index) { 43 let regString = re.match(" *([a-zA-Z0-9_]+) * = *\"([\x21-\x7e]+)*\"", bodyContent) 44 let regSingleQuotes = re.match(" *([a-zA-Z0-9_]+) * = *'([\x21-\x7e]+)*'", bodyContent) 45 let regNumber = re.match(" *([a-zA-Z0-9_]+) * = *([a-zA-Z_0-9<>-]+)", bodyContent) 46 let reg = re.match(" *([a-zA-Z0-9_]+) *", bodyContent) 47 if (regString) { 48 let elementName = re.getReg(bodyContent, regString.regs[1]) 49 let elementValue = re.getReg(bodyContent, regString.regs[2]) 50 result.element.push({ 51 name: elementName, 52 value: elementValue, 53 type: 'string' 54 }) 55 result.enumValueType = 1 56 } else if (regSingleQuotes) { 57 let elementName = re.getReg(bodyContent, regSingleQuotes.regs[1]) 58 let elementValue = re.getReg(bodyContent, regSingleQuotes.regs[2]) 59 result.element.push({ 60 name: elementName, 61 value: elementValue, 62 type: 'string' 63 }) 64 result.enumValueType = 1 65 } else if (regNumber) { 66 let elementName = re.getReg(bodyContent, regNumber.regs[1]) 67 let elementValue = re.getReg(bodyContent, regNumber.regs[2]) 68 typeof (elementValue) 69 result.element.push({ 70 name: elementName, 71 value: elementValue, 72 type: "NUMBER_TYPE_" + NumberIncrease.getAndIncrease() 73 }) 74 result.enumValueType = 0 75 } else if (reg) { 76 let elementName = re.getReg(bodyContent, reg.regs[1]) 77 let elementValue = index 78 result.element.push({ 79 name: elementName, 80 value: elementValue, 81 type: "NUMBER_TYPE_" + NumberIncrease.getAndIncrease() 82 }) 83 result.enumValueType = 0 84 } 85 return result 86} 87 88module.exports = { 89 analyzeEnum, 90 analyzeEnumResult 91}