• 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 { checkOutBody, print } = require("../tools/tool");
17const { FuncType } = require("../tools/common");
18const { NapiLog } = require("../tools/NapiLog");
19
20/**函数参数解析 */
21function analyzeParams(funcName, values) {
22    let result = []
23    let funcType = FuncType.DIRECT
24    let optionalParamCount = 0; //可选参数的个数
25    while (values.length > 0) {
26        let v = checkOutBody(values, 0, ["", ","])
27        if (v == null)
28            v = values
29        values = values.substring(v.length, values.length)
30        let matchs = re.match("([a-zA-Z_0-9\\.]+)(\\?*): *([a-zA-Z<,>|_0-9\\[\\]\\(\\):='{}]+)", v)
31        if (matchs != null) {
32            let type = re.getReg(v, matchs.regs[3])
33            if (type.indexOf("Map") < 0) {
34                type = type.replace(/,/g, "")
35            }
36
37            let optionalFlag = re.getReg(v, matchs.regs[2]) == '?' ? true : false;
38            let checkParamOk = true;
39            if (optionalFlag) {
40                optionalParamCount++;
41            } else if (optionalParamCount > 0) {
42                // 可选参数之后不能再有必选参数,必选都是可选参数。
43                NapiLog.logError("Invalid parameter [%s] of function [%s],".format(v, funcName)
44                        + " the required parameter cannot follow an optional parameter.");
45                checkParamOk = false;
46            }
47            if (checkParamOk) {
48                result.push({ "name": re.getReg(v, matchs.regs[1]), "type": type , "optional": optionalFlag})
49                if (type.indexOf("AsyncCallback") >= 0)
50                    funcType = FuncType.ASYNC
51                if (funcType == FuncType.DIRECT && type.indexOf("Callback") >= 0 && type.indexOf("AsyncCallback") < 0)
52                    funcType = FuncType.SYNC
53            }
54        }
55        else {
56            NapiLog.logError("方法[%s]的参数列表[%s]解析失败。".format(funcName, v));
57            NapiLog.logError("Failed to analyse parameter [%s] of function [%s].".format(v, funcName));
58        }
59    }
60    return [result, funcType]
61}
62
63module.exports = {
64    analyzeParams
65}