• 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] === ' ') {
71            t = t.substring(1, t.length); // 去除前面的空格
72        }
73        while (t.length > 0 && t[-1] === ' ') {
74            t = t.substring(0, t.length - 1); // 去除后面的空格
75        }
76        if (t === '') {
77            break; // 如果t为空直接返回
78        }
79        let tt = re.match(' *([a-zA-Z0-9_]+)(\\?*)*: *([a-zA-Z_0-9<>,:{}[\\]| ]+)', t);
80        if (tt && t.indexOf('=>') < 0) { // 接口成员变量, 但不包括带'=>'的成员,带'=>'的接口成员需要按函数处理
81            analyzeInterfaceVariable(t, tt, rsltInterface, result);
82        }
83        tt = re.match("(static )* *(\\$*[A-Za-z0-9_]+) *[:]? *\\(([\n 'a-zA-Z\'\'\"\":;=,_0-9?<>{}()=>|[\\]]*)\\)" +
84            ' *(:|=>)? *([A-Za-z0-9_<>{}:;, .[\\]]+)?', t);
85        if (tt) { // 接口函数成员
86            analyzeInterfaceFunction(t, tt, data, results, interfaceName, result);
87        }
88    }
89    return result;
90}
91
92module.exports = {
93    analyzeInterface,
94    parseNotes
95};
96
97function analyzeInterfaceFunction(t, tt, data, results, interfaceName, result) {
98    let ret = re.getReg(t, tt.regs[5]) === '' ? 'void' : re.getReg(t, tt.regs[5]);
99    let funcDetail = analyzeFunction(data, re.getReg(t, tt.regs[1]) !== '', re.getReg(t, tt.regs[2]),
100        re.getReg(t, tt.regs[3]), ret, results, interfaceName);
101    if (funcDetail !== null && funcDetail !== undefined) {
102        // 完全一样的方法不重复添加 (如同名同参的AsyncCallback和Promise方法)
103        addUniqFunc2List(funcDetail, result.function);
104    }
105}
106
107function analyzeInterfaceVariable(t, tt, rsltInterface, result) {
108    let valueName = re.getReg(t, tt.regs[1]);
109    let valueType = re.getReg(t, tt.regs[3]);
110    let index = valueType.indexOf('number');
111    let optionalFlag = re.getReg(t, tt.regs[2]) === '?' ? true : false;
112    while (index !== -1) {
113        valueType = valueType.replace('number', 'NUMBER_TYPE_' + NumberIncrease.getAndIncrease());
114        index = valueType.indexOf('number');
115    }
116    valueType = analyzeNoNameInterface(valueType, valueName, rsltInterface);
117    result.value.push({
118        name: valueName,
119        type: valueType,
120        realType: valueType,
121        optional: optionalFlag,
122    });
123}
124