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"); 17const { addUniqFunc2List } = require("../tools/tool"); 18const { analyzeFunction } = require("./function"); 19 20/* 匿名interface */ 21function analyzeNoNameInterface(valueType, valueName, rsltInterface) { 22 valueType = re.replaceAll(valueType, " ", "") 23 let matchs = re.match("{([A-Za-z0-9_]+:[A-Za-z0-9_,;]+)([A-Za-z0-9_]+:[A-Za-z0-9_]+)}$", valueType) 24 if (matchs) { 25 let number = NumberIncrease.getAndIncrease(); 26 let interfaceTypeName = 'AUTO_INTERFACE_%s_%s'.format(valueName, number) 27 let interfaceBody = valueType.substring(1, valueType.length-1) 28 interfaceBody = re.replaceAll(interfaceBody, ",", ";") 29 rsltInterface.push({ 30 name: interfaceTypeName, 31 body: analyzeInterface(interfaceBody, rsltInterface) 32 }) 33 valueType = interfaceTypeName 34 } 35 return valueType 36} 37 38/**interface解析 */ 39function analyzeInterface(data, rsltInterface = null) {//same as class 40 let body = data 41 42 let notes = data.substring(data.indexOf("//"), data.length); 43 notes = notes.substring(0, notes.indexOf("\n")); 44 while(notes != "") { 45 body = body.replace(notes, ""); 46 notes = body.substring(body.indexOf("//"), body.length); 47 notes = notes.substring(0, notes.indexOf("\n")); 48 } 49 50 body = re.replaceAll(body, "\n", "").split(";") 51 let result = { 52 value: [], 53 function: [] 54 } 55 for (let i in body) { 56 let t = body[i] 57 while (t.length > 0 && t[0] == ' ')//去除前面的空格 58 t = t.substring(1, t.length) 59 while (t.length > 0 && t[-1] == ' ')//去除后面的空格 60 t = t.substring(0, t.length - 1) 61 if (t == "") break//如果t为空直接返回 62 let tt = re.match(" *([a-zA-Z0-9_]+) *: *([a-zA-Z_0-9<>,:{}[\\]| ]+)", t) 63 if (tt && t.indexOf("=>") < 0) { // 接口成员变量, 但不包括带'=>'的成员,带'=>'的接口成员需要按函数处理 64 let valueName = re.getReg(t, tt.regs[1]) 65 let valueType = re.getReg(t, tt.regs[2]) 66 let index = valueType.indexOf("number") 67 while (index !== -1) { 68 valueType = valueType.replace("number", "NUMBER_TYPE_" + NumberIncrease.getAndIncrease()) 69 index = valueType.indexOf("number") 70 } 71 valueType = analyzeNoNameInterface(valueType, valueName, rsltInterface) 72 result.value.push({ 73 name: valueName, 74 type: valueType 75 }) 76 } 77 tt = re.match("(static )* *(\\$*[A-Za-z0-9_]+) *[:]? *\\(([\n 'a-zA-Z:;=,_0-9?<>{}|[\\]]*)\\)" 78 + " *(:|=>) *([A-Za-z0-9_<>{}:, .[\\]]+)", t) 79 if (tt) { // 接口函数成员 80 let funcDetail = analyzeFunction(data, re.getReg(t, tt.regs[1]) != '', re.getReg(t, tt.regs[2]), 81 re.getReg(t, tt.regs[3]), re.getReg(t, tt.regs[5])) 82 if (funcDetail != null) { 83 // 完全一样的方法不重复添加 (如同名同参的AsyncCallback和Promise方法) 84 addUniqFunc2List(funcDetail, result.function) 85 } 86 } 87 } 88 return result 89} 90 91module.exports = { 92 analyzeInterface 93}