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 re = require("../tools/re"); 16const { NumberIncrease } = require("../tools/common"); 17 18const { analyzeFunction } = require("./function"); 19 20/**interface解析 */ 21function analyzeClass(data) { 22 // # replace(" ", ""). 23 let body = re.replaceAll(data, "\n", "").split(";") 24 let result = { 25 value: [], 26 function: [] 27 } 28 for (let i in body) { 29 let classBody = body[i] 30 // 去除前面的空格 31 while (classBody.length > 0 && classBody[0] == ' ') { 32 classBody = classBody.substring(1, classBody.length) 33 } 34 // 去除后面的空格 35 while (classBody.length > 0 && classBody[-1] == ' ') { 36 classBody = classBody.substring(0, classBody.length - 1) 37 } 38 // 如果t为空直接返回 39 if (classBody == "") break 40 let matcher = re.match(" *([a-zA-Z0-9_]+) *: *([a-zA-Z_0-9<>]+)", classBody) 41 if (matcher) { 42 let valueName = re.getReg(classBody, matcher.regs[1]) 43 let valueType = re.getReg(classBody, matcher.regs[2]) 44 if (valueType.indexOf("number") >= 0) { 45 valueType = valueType.replace("number", "NUMBER_TYPE_" + NumberIncrease.getAndIncrease()) 46 } 47 result.value.push({ 48 name: valueName, 49 type: valueType 50 }) 51 } 52 let rules = "(static )? *([A-Za-z0-9_]+)\\(([\n a-zA-Z:;=,_0-9?<>{}|]*)\\) *: *([A-Za-z0-9_<>{}:, .]+)"; 53 matcher = re.match(rules, classBody) 54 if (matcher) { 55 let funcDetail = analyzeFunction(data, 56 re.getReg(classBody, matcher.regs[1]) != '', re.getReg(classBody, matcher.regs[2]), 57 re.getReg(classBody, matcher.regs[3]), re.getReg(classBody, matcher.regs[4])) 58 if (funcDetail != null) 59 result.function.push(funcDetail) 60 } 61 } 62 return result 63} 64 65module.exports = { 66 analyzeClass 67}