• 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("./re");
16const tsc = require("../../node_modules/typescript");
17
18function checkFileError(ifname) {
19    let program = tsc.createProgram([ifname], {target: tsc.ScriptTarget.Latest,})
20    let emitResult = program.emit();
21    let allDiagnostics = tsc.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
22
23    let errorMsg = ''
24    allDiagnostics.forEach(diagnostic => {
25        if (diagnostic.file) {
26            let { line, character } = tsc.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
27            let message = tsc.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
28            errorMsg += `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}\n`;
29        } else {
30            errorMsg += tsc.flattenDiagnosticMessageText(diagnostic.messageText, "\n") + "\n";
31        }
32    });
33
34    if (allDiagnostics.length > 0) {
35        return [false, errorMsg];
36    }
37    return [true, ""];
38}
39
40class FuncType { }
41FuncType.DIRECT = 1
42FuncType.SYNC = 2
43FuncType.ASYNC = 4
44FuncType.PROMISE = 8
45FuncType.ToString = function (type) {
46    if (type == FuncType.DIRECT) return "DIRECT";
47    else if (type == FuncType.SYNC) return "SYNC";
48    else if (type == FuncType.ASYNC) return "ASYNC";
49    else if (type == FuncType.PROMISE) return "PROMISE";
50    return "UNKNOW";
51}
52
53class NumberIncrease { }
54NumberIncrease.num = 1
55NumberIncrease.getAndIncrease = function () {
56    return NumberIncrease.num++;
57}
58NumberIncrease.get = function () {
59    return NumberIncrease.num;
60}
61NumberIncrease.reset = function () {
62    NumberIncrease.num = 1
63}
64
65class InterfaceList { }
66InterfaceList.interfacess_ = [];
67InterfaceList.push = function (ifs) {
68    InterfaceList.interfacess_.push(ifs)
69}
70InterfaceList.pop = function () {
71    InterfaceList.interfacess_.pop()
72}
73InterfaceList.getValue = function (name) {
74    let ifs = InterfaceList.interfacess_[InterfaceList.interfacess_.length - 1]
75    for (let i in ifs) {
76        if (ifs[i].name == name) {
77            return ifs[i].body.allProperties.values;
78        }
79    }
80    return null;
81}
82
83class EnumList { }
84EnumList.enum_ = [];
85EnumList.push = function (ifs) {
86    EnumList.enum_.push(ifs)
87}
88EnumList.pop = function () {
89    EnumList.enum_.pop()
90}
91EnumList.getValue = function (name) {
92    let ifs = EnumList.enum_[EnumList.enum_.length - 1]
93    for (let i in ifs) {
94        if (ifs[i].name == name) {
95            return ifs[i].body.element;
96        }
97    }
98    return null;
99}
100
101function getArrayType(type) {
102    let tt = re.match("Array<([a-zA-Z_0-9]+)>", type)
103    if (tt != null) {
104        return re.getReg(type, tt.regs[1])
105    }
106
107    tt = re.match("Array<{([[a-z:]+)([a-z:]]+)([a-zA-Z_1-9:]+)", type)
108    if (tt != null) {
109        let res = ''
110        let len = tt.regs.length
111        for (let i=1; i<len; i++) {
112            let regs1 = re.getReg(type, tt.regs[i])
113            res += regs1
114        }
115        return res
116    }
117
118    tt = re.match("Array<map<string", type)
119    if (tt != null) {
120        let preStr = 'Array<'
121        let preStrLen = preStr.length
122        let res = type.substring(preStrLen, type.length-1)
123        return res
124    }
125
126    tt = re.match("Array<Map<string", type)
127    if (tt != null) {
128        let preStr = 'Array<'
129        let preStrLen = preStr.length
130        let res = type.substring(preStrLen, type.length-1)
131        return res
132    }
133    return null
134}
135
136function getArrayTypeTwo(type) {
137    let tt = re.match("([a-zA-Z_0-9]+)", type)
138    return re.getReg(type, tt.regs[1])
139}
140
141function jsType2CType(jsTypeName) {
142    if (jsTypeName == "string") {
143        return "std::string"
144    } else if (jsTypeName == "boolean") {
145        return "bool"
146    } else {
147        return jsTypeName
148    }
149}
150
151class EnumValueType { }
152EnumValueType.ENUM_VALUE_TYPE_NUMBER = 0
153EnumValueType.ENUM_VALUE_TYPE_STRING = 1
154
155function isEnum(type, data) {
156    let isEnum = false
157    if (null == data) {
158        return isEnum
159    }
160    for (let i in data.enum) {
161        let enumm = data.enum[i]
162        if (type == enumm.name) {
163            isEnum = true
164        }
165    }
166    return isEnum
167}
168
169function enumIndex(type, data) {
170    let index;
171    if (null == data) {
172        return index
173    }
174    for (let i in data.enum) {
175        let enumm = data.enum[i]
176        if (type == enumm.name) {
177            index = i
178        }
179    }
180    return index
181}
182
183function getMapType(type) {
184    type = type.replace(/\s*/g,"")
185    let ttKey = re.search("Map<([a-zA-Z_0-9]+),", type)
186    let ttValue = re.search(",([a-zA-Z_0-9<>]+)>", type)
187    let ttMap = re.search(",([a-zA-Z_0-9]+)>>", type)
188    let ttArray = re.search("Array<([a-zA-Z_0-9]+)>", type)
189
190    if(ttArray == null) {
191        ttArray = re.search("([a-zA-Z_0-9]+)\\[\\]>", type)
192    }
193
194    let valueType
195    let valueMapType
196    let valueArrayType
197    if (ttKey == null && ttValue == null && ttMap == null) {
198        ttKey = re.search("key:([a-zA-Z_0-9]+)", type)
199        ttValue = re.search(":([a-zA-Z_0-9]+)}", type)
200        ttMap = re.search(":([a-zA-Z_0-9]+)}}", type)
201        ttArray = re.search("Array<([a-zA-Z_0-9]+)>", type)
202        if (ttArray == null) {
203            ttArray = re.search(":([a-zA-Z_0-9]+)\\[\\]}", type)
204        }
205    }
206
207    if (ttValue != null) {
208        valueType = re.getReg(type, ttValue.regs[1])
209        if (valueType.indexOf("Array<") == 0) {
210            valueArrayType = re.getReg(valueType, ttArray.regs[1])
211            valueType = undefined
212        } else if (ttMap != undefined) {
213            valueMapType = re.getReg(type, ttMap.regs[1])
214            valueType = undefined
215        }
216    }
217    if (ttMap != null) {
218        valueMapType = re.getReg(type, ttMap.regs[1])
219    }
220    if (ttArray != null) {
221        valueArrayType = re.getReg(type, ttArray.regs[1])
222    }
223    return [re.getReg(type, ttKey.regs[1]), valueType, valueMapType, valueArrayType]
224}
225
226function getUnionType(type) {
227    type = type.replace(/\s*/g,"")
228    var typeArr = new Array()
229    typeArr = type.split("|")
230    return typeArr
231}
232
233
234module.exports = {
235    FuncType,
236    EnumValueType,
237    NumberIncrease,
238    InterfaceList,
239    getArrayType,
240    getArrayTypeTwo,
241    checkFileError,
242    isEnum,
243    enumIndex,
244    getMapType,
245    EnumList,
246    jsType2CType,
247    getUnionType
248}