• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, ",", ";\n")
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, results, interfaceName = '') { // same as class
55    let body = data
56    body = body.indexOf("//") < 0 ? body : parseNotes(body)
57    let arr  =  [...body.matchAll(/;\s*\n+/g)]
58    for (let i = 0; i < arr.length; i++) {
59        let result = arr[i]
60        body = re.replaceAll(body, result[0], ";\n")
61    }
62    body = body.split(";\n")
63    let result = {
64        value: [],
65        function: []
66    }
67    for (let i in body) {
68        let t = body[i]
69        t = re.replaceAll(t, "\n", "")
70        while (t.length > 0 && t[0] == ' ') t = t.substring(1, t.length) // 去除前面的空格
71        while (t.length > 0 && t[-1] == ' ') t = t.substring(0, t.length - 1) // 去除后面的空格
72        if (t == "") break // 如果t为空直接返回
73        let tt = re.match(" *([a-zA-Z0-9_]+)(\\?*)*: *([a-zA-Z_0-9<>,:{}[\\]| ]+)", t)
74        if (tt && t.indexOf("=>") < 0) { // 接口成员变量, 但不包括带'=>'的成员,带'=>'的接口成员需要按函数处理
75            let valueName = re.getReg(t, tt.regs[1])
76            let valueType = re.getReg(t, tt.regs[3])
77            let index = valueType.indexOf("number")
78            let optionalFlag = re.getReg(t, tt.regs[2]) == '?' ? true : false;
79            while (index !== -1) {
80                valueType = valueType.replace("number", "NUMBER_TYPE_" + NumberIncrease.getAndIncrease())
81                index = valueType.indexOf("number")
82            }
83            valueType = analyzeNoNameInterface(valueType, valueName, rsltInterface)
84            result.value.push({
85                name: valueName,
86                type: valueType,
87                optional: optionalFlag
88            })
89        }
90        tt = re.match("(static )* *(\\$*[A-Za-z0-9_]+) *[:]? *\\(([\n 'a-zA-Z\'\'\"\":;=,_0-9?<>{}()=>|[\\]]*)\\)"
91            + " *(:|=>)? *([A-Za-z0-9_<>{}:;, .[\\]]+)?", t)
92        if (tt) { // 接口函数成员
93            let ret = re.getReg(t, tt.regs[5]) == ''? 'void': re.getReg(t, tt.regs[5])
94            let funcDetail = analyzeFunction(data, re.getReg(t, tt.regs[1]) != '', re.getReg(t, tt.regs[2]),
95                re.getReg(t, tt.regs[3]), ret, results, interfaceName)
96            if (funcDetail != null) {
97                // 完全一样的方法不重复添加 (如同名同参的AsyncCallback和Promise方法)
98                addUniqFunc2List(funcDetail, result.function)
99            }
100        }
101    }
102    return result
103}
104
105module.exports = {
106    analyzeInterface,
107    parseNotes
108}