nv-constexpr-paren-exec 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/TEST/func.js +0 -2
- package/TEST/tst.js +4 -2
- package/index.js +43 -134
- package/package.json +1 -20
package/TEST/func.js
CHANGED
package/TEST/tst.js
CHANGED
package/index.js
CHANGED
|
@@ -1,143 +1,52 @@
|
|
|
1
|
-
const _fs
|
|
2
|
-
const _path
|
|
3
|
-
const vm = require("vm");
|
|
1
|
+
const _fs = require("fs");
|
|
2
|
+
const _path = require("path");
|
|
4
3
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var type = s.slice(0,idx).trim().toLowerCase();
|
|
17
|
-
var ison_str = s.slice(idx+2).trim();
|
|
18
|
-
if(type === "name" || type === "nm") {
|
|
19
|
-
return {
|
|
20
|
-
type:"name",
|
|
21
|
-
expr:ison_str
|
|
22
|
-
}
|
|
23
|
-
} else if(type === "action" || type === "act") {
|
|
24
|
-
ison_str = `{${ison_str}}`;
|
|
25
|
-
var ison = parse_from_str(ison_str);
|
|
26
|
-
// .cmt Same As .desc
|
|
27
|
-
// .path
|
|
28
|
-
// .code
|
|
29
|
-
// .code 优先级高于 .path
|
|
30
|
-
if(!Array.isArray(ison) && ison !== null && typeof(ison) === "object") {
|
|
31
|
-
var d = {type:"action"}
|
|
32
|
-
if(ison.cmt !== undefined) {
|
|
33
|
-
d.cmt = String(ison.cmt);
|
|
34
|
-
} else if(ison.desc !== undefined) {
|
|
35
|
-
d.cmt = String(ison.desc)
|
|
36
|
-
} else {}
|
|
37
|
-
if(ison.code) {
|
|
38
|
-
d.code = String(ison.code)
|
|
39
|
-
} else if(ison.path) {
|
|
40
|
-
var code = _fs.readFileSync(_path.resolve(ison.path)).toString();
|
|
41
|
-
if(code.includes("`")) {throw('code can NOT includes "`"');} else {}
|
|
42
|
-
code = code.replace(/[\r\n\t ]+/g," ");
|
|
43
|
-
code = code.trim();
|
|
44
|
-
var rgx0 = /\{(.*)\}$/;
|
|
45
|
-
var r0 = rgx0.exec(code);
|
|
46
|
-
if(r0) {
|
|
47
|
-
d.code = r0[1].trim();
|
|
48
|
-
} else {
|
|
49
|
-
d.code = code;
|
|
50
|
-
}
|
|
51
|
-
} else {}
|
|
52
|
-
if(d.code) {
|
|
53
|
-
return d;
|
|
54
|
-
} else {
|
|
55
|
-
throw(`action content MUST have .path OR .code`);
|
|
56
|
-
}
|
|
57
|
-
} else {
|
|
58
|
-
throw(`action content MUST be object`);
|
|
59
|
-
}
|
|
60
|
-
} else if(type === "indent") {
|
|
61
|
-
var d = {type:"indent"};
|
|
62
|
-
var rgx = /^([0-9]+)(.*)$/;
|
|
63
|
-
var r = rgx.exec(ison_str);
|
|
64
|
-
var depth = parseInt(r[1]);
|
|
65
|
-
if(!Object.is(depth,NaN)) {
|
|
66
|
-
var code = r[2];
|
|
67
|
-
if(code.includes("`")) {throw('code can NOT includes "`"');} else {}
|
|
68
|
-
code = code.replace(/[\t ]+/g," ");
|
|
69
|
-
code = code.replace(/[\r\n]+/g,"\n");
|
|
70
|
-
var lns = code.splice("\n");
|
|
71
|
-
lns = lns.map(ln=>" ".repeat(depth)+ln);
|
|
72
|
-
d.code = "\n" +lns.join("\n") + "\n";
|
|
73
|
-
return d;
|
|
74
|
-
} else {
|
|
75
|
-
throw(`indent :=<int> ${depth} is MUST be int`)
|
|
76
|
-
}
|
|
77
|
-
} else {
|
|
78
|
-
throw(`type can ONLY be : name | action |nm | act | indent`);
|
|
79
|
-
}
|
|
80
|
-
} else {
|
|
81
|
-
throw(`MUST have type(name | action |nm | act): <type>:=... `)
|
|
4
|
+
const topl = (p)=> {
|
|
5
|
+
p = _path.resolve(p);
|
|
6
|
+
var pl = [];
|
|
7
|
+
while(true) {
|
|
8
|
+
var desc = _path.parse(p);
|
|
9
|
+
if(desc.dir !== p) {
|
|
10
|
+
pl.push(desc.name);
|
|
11
|
+
p = desc.dir;
|
|
12
|
+
} else {
|
|
13
|
+
break;
|
|
14
|
+
}
|
|
82
15
|
}
|
|
16
|
+
return pl;
|
|
83
17
|
}
|
|
84
18
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return [false,reason]
|
|
114
|
-
}
|
|
115
|
-
} else if(d.type === "action"){
|
|
116
|
-
var cmt = d.cmt;
|
|
117
|
-
var code = d.code;
|
|
118
|
-
if(cmt !== undefined) {
|
|
119
|
-
return [true,`${code}/*${cmt}*/`];
|
|
120
|
-
} else {
|
|
121
|
-
return [true,`${code}`];
|
|
122
|
-
}
|
|
123
|
-
} else {
|
|
124
|
-
//d.type === "indent"
|
|
125
|
-
return [true, d.code];
|
|
126
|
-
}
|
|
127
|
-
} catch(e) {
|
|
128
|
-
return [false,e.message]
|
|
129
|
-
}
|
|
19
|
+
const run = (path, fn_as_cmt_depth=1)=>{
|
|
20
|
+
path = _path.resolve(path);
|
|
21
|
+
if(_fs.existsSync(path)) {
|
|
22
|
+
var stat = _fs.lstatSync(path);
|
|
23
|
+
if(stat.isFile()) {
|
|
24
|
+
var code = _fs.readFileSync(path).toString();
|
|
25
|
+
if(code.includes("`")) {
|
|
26
|
+
return [false, "macro code can NOT includes '`' "];
|
|
27
|
+
} else {
|
|
28
|
+
code = code.trim();
|
|
29
|
+
code = code.replace(/[\t ]+/g, " ");
|
|
30
|
+
code = code.replace(/[\n\r]+/g, "\n");
|
|
31
|
+
var pl = topl(path);
|
|
32
|
+
var cnt = pl.length;
|
|
33
|
+
pl = pl.slice(0,fn_as_cmt_depth);
|
|
34
|
+
pl.reverse();
|
|
35
|
+
var cmt = pl.join("/");
|
|
36
|
+
if(cmt!== "") {
|
|
37
|
+
code = `${code}/*${cmt}*/`;
|
|
38
|
+
} else {
|
|
39
|
+
}
|
|
40
|
+
return [true,code];
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
return [false,`${path} NOT file`]
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
return [false,`${path} NOT exists`]
|
|
130
47
|
}
|
|
131
48
|
}
|
|
132
49
|
|
|
133
|
-
|
|
134
|
-
const run = (env,code,keep_cmt=true)=>{
|
|
135
|
-
var ctx = creat(env);
|
|
136
|
-
return ctx.run(code);
|
|
137
|
-
}
|
|
50
|
+
run.topl = topl;
|
|
138
51
|
module.exports = run;
|
|
139
|
-
module.exports.parse = parse;
|
|
140
|
-
module.exports.creat = creat;
|
|
141
|
-
module.exports.ParenExec = ParenExec;
|
|
142
|
-
|
|
143
52
|
|
package/package.json
CHANGED
|
@@ -1,26 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nv-constexpr-paren-exec",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"main": "index.js",
|
|
5
|
-
"dependencies": {
|
|
6
|
-
"nv-char-basic": "^1.0.3",
|
|
7
|
-
"nv-char-jsid2": "^1.0.8",
|
|
8
|
-
"nv-char-whitespace": "^1.0.7",
|
|
9
|
-
"nv-constexpr-simple-codify": "^1.0.5",
|
|
10
|
-
"nv-facutil-csutil": "^1.0.21",
|
|
11
|
-
"nv-facutil-untf": "^1.0.2",
|
|
12
|
-
"nv-string-unesc": "^1.0.0",
|
|
13
|
-
"nvison-bw": "^1.0.0",
|
|
14
|
-
"nvison-cfg": "^1.1.0",
|
|
15
|
-
"nvison-obj-class": "^1.2.2",
|
|
16
|
-
"nvison-obj-typedef": "^1.1.0",
|
|
17
|
-
"nvison-parse-d": "^1.2.1",
|
|
18
|
-
"nvison-parse-engine": "^1.2.1",
|
|
19
|
-
"nvison-parse-internal-bw": "^1.2.2",
|
|
20
|
-
"nvison-parse-scope": "^1.1.1",
|
|
21
|
-
"nvison-parse-state": "^1.0.12"
|
|
22
|
-
},
|
|
23
|
-
"devDependencies": {},
|
|
24
5
|
"scripts": {
|
|
25
6
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
26
7
|
},
|