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