3xpr 1.5.4
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/LICENSE +201 -0
- package/README.md +126 -0
- package/__tests__/array.test.d.ts +1 -0
- package/__tests__/array.test.js +37 -0
- package/__tests__/array.test.js.map +1 -0
- package/__tests__/auto/access.test.d.ts +1 -0
- package/__tests__/auto/access.test.js +31 -0
- package/__tests__/auto/access.test.js.map +1 -0
- package/__tests__/auto/arithmetic.test.d.ts +1 -0
- package/__tests__/auto/arithmetic.test.js +20 -0
- package/__tests__/auto/arithmetic.test.js.map +1 -0
- package/__tests__/auto/array.test.d.ts +1 -0
- package/__tests__/auto/array.test.js +25 -0
- package/__tests__/auto/array.test.js.map +1 -0
- package/__tests__/auto/arrow.test.d.ts +1 -0
- package/__tests__/auto/arrow.test.js +29 -0
- package/__tests__/auto/arrow.test.js.map +1 -0
- package/__tests__/auto/assignment.test.d.ts +1 -0
- package/__tests__/auto/assignment.test.js +14 -0
- package/__tests__/auto/assignment.test.js.map +1 -0
- package/__tests__/auto/bitwise.test.d.ts +1 -0
- package/__tests__/auto/bitwise.test.js +16 -0
- package/__tests__/auto/bitwise.test.js.map +1 -0
- package/__tests__/auto/comparison.test.d.ts +1 -0
- package/__tests__/auto/comparison.test.js +65 -0
- package/__tests__/auto/comparison.test.js.map +1 -0
- package/__tests__/auto/constants.test.d.ts +1 -0
- package/__tests__/auto/constants.test.js +15 -0
- package/__tests__/auto/constants.test.js.map +1 -0
- package/__tests__/auto/dateTime.test.d.ts +1 -0
- package/__tests__/auto/dateTime.test.js +38 -0
- package/__tests__/auto/dateTime.test.js.map +1 -0
- package/__tests__/auto/flows.test.d.ts +1 -0
- package/__tests__/auto/flows.test.js +78 -0
- package/__tests__/auto/flows.test.js.map +1 -0
- package/__tests__/auto/nullable.test.d.ts +1 -0
- package/__tests__/auto/nullable.test.js +15 -0
- package/__tests__/auto/nullable.test.js.map +1 -0
- package/__tests__/auto/numeric.test.d.ts +1 -0
- package/__tests__/auto/numeric.test.js +32 -0
- package/__tests__/auto/numeric.test.js.map +1 -0
- package/__tests__/auto/parameters.test.d.ts +1 -0
- package/__tests__/auto/parameters.test.js +25 -0
- package/__tests__/auto/parameters.test.js.map +1 -0
- package/__tests__/auto/sets.test.d.ts +1 -0
- package/__tests__/auto/sets.test.js +18 -0
- package/__tests__/auto/sets.test.js.map +1 -0
- package/__tests__/auto/string.test.d.ts +1 -0
- package/__tests__/auto/string.test.js +45 -0
- package/__tests__/auto/string.test.js.map +1 -0
- package/__tests__/auto/type.test.d.ts +1 -0
- package/__tests__/auto/type.test.js +25 -0
- package/__tests__/auto/type.test.js.map +1 -0
- package/__tests__/cache.test.d.ts +1 -0
- package/__tests__/cache.test.js +282 -0
- package/__tests__/cache.test.js.map +1 -0
- package/__tests__/datetime.test.d.ts +1 -0
- package/__tests__/datetime.test.js +38 -0
- package/__tests__/datetime.test.js.map +1 -0
- package/__tests__/enums.test.d.ts +1 -0
- package/__tests__/enums.test.js +12 -0
- package/__tests__/enums.test.js.map +1 -0
- package/__tests__/flows.test.d.ts +1 -0
- package/__tests__/flows.test.js +19 -0
- package/__tests__/flows.test.js.map +1 -0
- package/__tests__/numeric.test.d.ts +1 -0
- package/__tests__/numeric.test.js +55 -0
- package/__tests__/numeric.test.js.map +1 -0
- package/__tests__/pending/conversion.test.d.ts +1 -0
- package/__tests__/pending/conversion.test.js +20 -0
- package/__tests__/pending/conversion.test.js.map +1 -0
- package/__tests__/pending/groups.test.d.ts +1 -0
- package/__tests__/pending/groups.test.js +27 -0
- package/__tests__/pending/groups.test.js.map +1 -0
- package/__tests__/pending/quick.test.d.ts +1 -0
- package/__tests__/pending/quick.test.js +21 -0
- package/__tests__/pending/quick.test.js.map +1 -0
- package/__tests__/string.test.d.ts +1 -0
- package/__tests__/string.test.js +18 -0
- package/__tests__/string.test.js.map +1 -0
- package/expression/contract/base.d.ts +31 -0
- package/expression/contract/base.js +12 -0
- package/expression/contract/base.js.map +1 -0
- package/expression/contract/context.d.ts +36 -0
- package/expression/contract/context.js +93 -0
- package/expression/contract/context.js.map +1 -0
- package/expression/contract/index.d.ts +5 -0
- package/expression/contract/index.js +22 -0
- package/expression/contract/index.js.map +1 -0
- package/expression/contract/managers.d.ts +73 -0
- package/expression/contract/managers.js +11 -0
- package/expression/contract/managers.js.map +1 -0
- package/expression/contract/operand.d.ts +92 -0
- package/expression/contract/operand.js +69 -0
- package/expression/contract/operand.js.map +1 -0
- package/expression/contract/type.d.ts +50 -0
- package/expression/contract/type.js +164 -0
- package/expression/contract/type.js.map +1 -0
- package/expression/expressions.d.ts +57 -0
- package/expression/expressions.js +232 -0
- package/expression/expressions.js.map +1 -0
- package/expression/helper.d.ts +7 -0
- package/expression/helper.js +14 -0
- package/expression/helper.js.map +1 -0
- package/expression/index.d.ts +4 -0
- package/expression/index.js +21 -0
- package/expression/index.js.map +1 -0
- package/expression/operand/builder.d.ts +11 -0
- package/expression/operand/builder.js +79 -0
- package/expression/operand/builder.js.map +1 -0
- package/expression/operand/coreLibrary.d.ts +20 -0
- package/expression/operand/coreLibrary.js +1162 -0
- package/expression/operand/coreLibrary.js.map +1 -0
- package/expression/operand/evaluators.d.ts +67 -0
- package/expression/operand/evaluators.js +238 -0
- package/expression/operand/evaluators.js.map +1 -0
- package/expression/operand/factory.d.ts +11 -0
- package/expression/operand/factory.js +127 -0
- package/expression/operand/factory.js.map +1 -0
- package/expression/operand/helper.d.ts +17 -0
- package/expression/operand/helper.js +269 -0
- package/expression/operand/helper.js.map +1 -0
- package/expression/operand/index.d.ts +9 -0
- package/expression/operand/index.js +26 -0
- package/expression/operand/index.js.map +1 -0
- package/expression/operand/model.d.ts +34 -0
- package/expression/operand/model.js +282 -0
- package/expression/operand/model.js.map +1 -0
- package/expression/operand/parser.d.ts +42 -0
- package/expression/operand/parser.js +738 -0
- package/expression/operand/parser.js.map +1 -0
- package/expression/operand/type.d.ts +31 -0
- package/expression/operand/type.js +352 -0
- package/expression/operand/type.js.map +1 -0
- package/expression/process/evaluators.d.ts +58 -0
- package/expression/process/evaluators.js +317 -0
- package/expression/process/evaluators.js.map +1 -0
- package/expression/process/factory.d.ts +8 -0
- package/expression/process/factory.js +122 -0
- package/expression/process/factory.js.map +1 -0
- package/expression/process/index.d.ts +3 -0
- package/expression/process/index.js +20 -0
- package/expression/process/index.js.map +1 -0
- package/expression/process/model.d.ts +7 -0
- package/expression/process/model.js +3 -0
- package/expression/process/model.js.map +1 -0
- package/index.d.ts +19 -0
- package/index.js +52 -0
- package/index.js.map +1 -0
- package/jest-unit-config.json +15 -0
- package/package.json +39 -0
|
@@ -0,0 +1,738 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Parser = void 0;
|
|
4
|
+
const h3lp_1 = require("h3lp");
|
|
5
|
+
const contract_1 = require("../contract");
|
|
6
|
+
class Parser {
|
|
7
|
+
constructor(model, expression) {
|
|
8
|
+
this.model = model;
|
|
9
|
+
this.positions = this.normalize(expression);
|
|
10
|
+
this.buffer = this.positions.map(p => p[0]);
|
|
11
|
+
this.length = this.buffer.length;
|
|
12
|
+
this.index = 0;
|
|
13
|
+
this.singleOperators = [];
|
|
14
|
+
this.doubleOperators = [];
|
|
15
|
+
this.tripleOperators = [];
|
|
16
|
+
this.assignmentOperators = [];
|
|
17
|
+
for (const entry of this.model.operators) {
|
|
18
|
+
const name = entry[0];
|
|
19
|
+
const metadata = entry[1];
|
|
20
|
+
if (name.length === 1) {
|
|
21
|
+
this.singleOperators.push(name);
|
|
22
|
+
}
|
|
23
|
+
else if (name.length === 2) {
|
|
24
|
+
this.doubleOperators.push(name);
|
|
25
|
+
if (metadata.priority === 1) {
|
|
26
|
+
this.assignmentOperators.push(name);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
else if (name.length === 3) {
|
|
30
|
+
this.tripleOperators.push(name);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
parse() {
|
|
35
|
+
const operands = [];
|
|
36
|
+
while (!this.end) {
|
|
37
|
+
const operand = this.getExpression(undefined, undefined, ';');
|
|
38
|
+
if (!operand)
|
|
39
|
+
break;
|
|
40
|
+
operands.push(operand);
|
|
41
|
+
}
|
|
42
|
+
return operands.length === 1 ? operands[0] : new contract_1.Operand(new contract_1.Position(0, 0), 'block', contract_1.OperandType.Block, operands);
|
|
43
|
+
}
|
|
44
|
+
getExpression(operand1, operator, _break = '') {
|
|
45
|
+
let expression;
|
|
46
|
+
let operand2;
|
|
47
|
+
let isBreak = false;
|
|
48
|
+
const pos = this.pos();
|
|
49
|
+
while (!this.end) {
|
|
50
|
+
if (!operand1 && !operator) {
|
|
51
|
+
operand1 = this.getOperand();
|
|
52
|
+
operator = this.getOperator();
|
|
53
|
+
if (!operator || _break.includes(this.current)) {
|
|
54
|
+
if (_break.includes(this.current)) {
|
|
55
|
+
this.index += 1;
|
|
56
|
+
}
|
|
57
|
+
expression = operand1;
|
|
58
|
+
isBreak = true;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
operand2 = this.getOperand();
|
|
63
|
+
const nextOperator = this.getOperator();
|
|
64
|
+
if (operator && operand1) {
|
|
65
|
+
if (!nextOperator || _break.includes(this.current)) {
|
|
66
|
+
if (_break.includes(this.current)) {
|
|
67
|
+
this.index += 1;
|
|
68
|
+
}
|
|
69
|
+
expression = new contract_1.Operand(this.pos(operator.length), operator, contract_1.OperandType.Operator, [operand1, operand2]);
|
|
70
|
+
isBreak = true;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
else if (this.model.priority(operator) >= this.model.priority(nextOperator)) {
|
|
74
|
+
operand1 = new contract_1.Operand(this.pos(operator.length), operator, contract_1.OperandType.Operator, [operand1, operand2]);
|
|
75
|
+
operator = nextOperator;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
operand2 = this.getExpression(operand2, nextOperator, _break);
|
|
79
|
+
expression = new contract_1.Operand(this.pos(operator.length), operator, contract_1.OperandType.Operator, [operand1, operand2]);
|
|
80
|
+
isBreak = true;
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (!isBreak && operand1 && operand2) {
|
|
86
|
+
expression = new contract_1.Operand(pos, operator, contract_1.OperandType.Operator, [operand1, operand2]);
|
|
87
|
+
}
|
|
88
|
+
return expression;
|
|
89
|
+
}
|
|
90
|
+
getOperand() {
|
|
91
|
+
let isNegative = false;
|
|
92
|
+
let isNot = false;
|
|
93
|
+
let isBitNot = false;
|
|
94
|
+
let operand = null;
|
|
95
|
+
let char = this.current;
|
|
96
|
+
if (char === '-') {
|
|
97
|
+
isNegative = true;
|
|
98
|
+
this.index += 1;
|
|
99
|
+
char = this.current;
|
|
100
|
+
}
|
|
101
|
+
else if (char === '~') {
|
|
102
|
+
isBitNot = true;
|
|
103
|
+
this.index += 1;
|
|
104
|
+
char = this.current;
|
|
105
|
+
}
|
|
106
|
+
else if (char === '!') {
|
|
107
|
+
isNot = true;
|
|
108
|
+
this.index += 1;
|
|
109
|
+
char = this.current;
|
|
110
|
+
}
|
|
111
|
+
const pos = this.pos();
|
|
112
|
+
if (h3lp_1.h3lp.val.isAlphanumeric(char)) {
|
|
113
|
+
let value = this.getValue();
|
|
114
|
+
if (value === 'function' && this.current === '(') {
|
|
115
|
+
this.index += 1;
|
|
116
|
+
operand = this.getFunctionBlock(pos);
|
|
117
|
+
}
|
|
118
|
+
else if (value === 'if' && this.current === '(') {
|
|
119
|
+
this.index += 1;
|
|
120
|
+
operand = this.getIfBlock(pos);
|
|
121
|
+
}
|
|
122
|
+
else if (value === 'for' && this.current === '(') {
|
|
123
|
+
this.index += 1;
|
|
124
|
+
operand = this.getForBlock(pos);
|
|
125
|
+
}
|
|
126
|
+
else if (value === 'while' && this.current === '(') {
|
|
127
|
+
this.index += 1;
|
|
128
|
+
operand = this.getWhileBlock(pos);
|
|
129
|
+
}
|
|
130
|
+
else if (value === 'switch' && this.current === '(') {
|
|
131
|
+
this.index += 1;
|
|
132
|
+
operand = this.getSwitchBlock(pos);
|
|
133
|
+
}
|
|
134
|
+
else if (!this.end && this.current === '(') {
|
|
135
|
+
this.index += 1;
|
|
136
|
+
if (value.includes('.')) {
|
|
137
|
+
const names = h3lp_1.h3lp.obj.names(value);
|
|
138
|
+
const functionName = names.pop();
|
|
139
|
+
const variableName = names.join('.');
|
|
140
|
+
const variable = new contract_1.Operand(pos, variableName, contract_1.OperandType.Var);
|
|
141
|
+
operand = this.getChildFunc(functionName, variable);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
if (this.current === ')') {
|
|
145
|
+
this.index++;
|
|
146
|
+
operand = new contract_1.Operand(pos, value, contract_1.OperandType.CallFunc, []);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
const args = this.getArgs(')');
|
|
150
|
+
operand = new contract_1.Operand(pos, value, contract_1.OperandType.CallFunc, args);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else if (value === 'try' && this.current === '{') {
|
|
155
|
+
operand = this.getTryCatchBlock(pos);
|
|
156
|
+
}
|
|
157
|
+
else if (value === 'throw') {
|
|
158
|
+
operand = this.getThrow(pos);
|
|
159
|
+
}
|
|
160
|
+
else if (value === 'return') {
|
|
161
|
+
operand = this.getReturn(pos);
|
|
162
|
+
}
|
|
163
|
+
else if (value === 'break') {
|
|
164
|
+
operand = new contract_1.Operand(pos, 'break', contract_1.OperandType.Break);
|
|
165
|
+
}
|
|
166
|
+
else if (value === 'continue') {
|
|
167
|
+
operand = new contract_1.Operand(pos, 'continue', contract_1.OperandType.Continue);
|
|
168
|
+
}
|
|
169
|
+
else if (!this.end && this.current === '[') {
|
|
170
|
+
this.index += 1;
|
|
171
|
+
operand = this.getIndexOperand(value, pos);
|
|
172
|
+
}
|
|
173
|
+
else if (h3lp_1.h3lp.val.isIntegerFormat(value)) {
|
|
174
|
+
if (isNegative) {
|
|
175
|
+
value = parseInt(value) * -1;
|
|
176
|
+
isNegative = false;
|
|
177
|
+
}
|
|
178
|
+
else if (isBitNot) {
|
|
179
|
+
value = ~parseInt(value);
|
|
180
|
+
isBitNot = false;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
value = parseInt(value);
|
|
184
|
+
}
|
|
185
|
+
operand = new contract_1.Operand(pos, value, contract_1.OperandType.Const, [], contract_1.Type.integer);
|
|
186
|
+
}
|
|
187
|
+
else if (h3lp_1.h3lp.val.isDecimalFormat(value)) {
|
|
188
|
+
if (isNegative) {
|
|
189
|
+
value = parseFloat(value) * -1;
|
|
190
|
+
isNegative = false;
|
|
191
|
+
}
|
|
192
|
+
else if (isBitNot) {
|
|
193
|
+
value = ~parseFloat(value);
|
|
194
|
+
isBitNot = false;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
value = parseFloat(value);
|
|
198
|
+
}
|
|
199
|
+
operand = new contract_1.Operand(pos, value, contract_1.OperandType.Const, [], contract_1.Type.decimal);
|
|
200
|
+
}
|
|
201
|
+
else if (this.model.isConstant(value)) {
|
|
202
|
+
const constantValue = this.model.getConstantValue(value);
|
|
203
|
+
operand = new contract_1.Operand(pos, constantValue, contract_1.OperandType.Const, [], contract_1.Type.get(constantValue));
|
|
204
|
+
}
|
|
205
|
+
else if (this.model.isEnum(value)) {
|
|
206
|
+
operand = this.getEnum(value, pos);
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
operand = new contract_1.Operand(pos, value, contract_1.OperandType.Var);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
else if (char === '\'' || char === '"') {
|
|
213
|
+
this.index += 1;
|
|
214
|
+
const result = this.getString(char);
|
|
215
|
+
operand = new contract_1.Operand(pos, result, contract_1.OperandType.Const, [], contract_1.Type.string);
|
|
216
|
+
}
|
|
217
|
+
else if (char === '`') {
|
|
218
|
+
this.index += 1;
|
|
219
|
+
const result = this.getTemplate();
|
|
220
|
+
operand = new contract_1.Operand(pos, result, contract_1.OperandType.Template, [], contract_1.Type.string);
|
|
221
|
+
}
|
|
222
|
+
else if (char === '(') {
|
|
223
|
+
this.index += 1;
|
|
224
|
+
operand = this.getExpression(undefined, undefined, ')');
|
|
225
|
+
}
|
|
226
|
+
else if (char === '{') {
|
|
227
|
+
this.index += 1;
|
|
228
|
+
operand = this.getObject(pos);
|
|
229
|
+
}
|
|
230
|
+
else if (char === '[') {
|
|
231
|
+
this.index += 1;
|
|
232
|
+
const elements = this.getArgs(']');
|
|
233
|
+
operand = new contract_1.Operand(pos, 'array', contract_1.OperandType.List, elements);
|
|
234
|
+
}
|
|
235
|
+
else if (char === '$') {
|
|
236
|
+
let variableName;
|
|
237
|
+
if (this.offset(1) === '{') {
|
|
238
|
+
this.index += 2;
|
|
239
|
+
variableName = this.getValue();
|
|
240
|
+
if (!this.end && this.nextIs('}')) {
|
|
241
|
+
this.index += 1;
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
throw new Error(`Not found character "}" in Environment variable ${variableName}`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
this.index += 1;
|
|
249
|
+
variableName = this.getValue();
|
|
250
|
+
}
|
|
251
|
+
operand = new contract_1.Operand(pos, variableName, contract_1.OperandType.Env);
|
|
252
|
+
}
|
|
253
|
+
if (operand === null) {
|
|
254
|
+
throw new Error('Operand undefined');
|
|
255
|
+
}
|
|
256
|
+
operand = this.solveChain(operand, pos);
|
|
257
|
+
if (isNegative)
|
|
258
|
+
operand = new contract_1.Operand(new contract_1.Position(pos.ln, pos.col - 1), '-', contract_1.OperandType.Operator, [operand]);
|
|
259
|
+
if (isNot)
|
|
260
|
+
operand = new contract_1.Operand(new contract_1.Position(pos.ln, pos.col - 1), '!', contract_1.OperandType.Operator, [operand]);
|
|
261
|
+
if (isBitNot)
|
|
262
|
+
operand = new contract_1.Operand(new contract_1.Position(pos.ln, pos.col - 1), '~', contract_1.OperandType.Operator, [operand]);
|
|
263
|
+
return operand;
|
|
264
|
+
}
|
|
265
|
+
solveChain(operand, pos) {
|
|
266
|
+
if (this.end) {
|
|
267
|
+
return operand;
|
|
268
|
+
}
|
|
269
|
+
if (this.current === '.') {
|
|
270
|
+
this.index += 1;
|
|
271
|
+
const name = this.getValue();
|
|
272
|
+
if (this.current === '(') {
|
|
273
|
+
this.index += 1;
|
|
274
|
+
if (name.includes('.')) {
|
|
275
|
+
// .xxx.xxx(p=> p.xxx)
|
|
276
|
+
const names = h3lp_1.h3lp.obj.names(name);
|
|
277
|
+
const propertyName = names.slice(0, -1).join('.');
|
|
278
|
+
const functionName = names.slice(-1)[0];
|
|
279
|
+
const property = new contract_1.Operand(pos, propertyName, contract_1.OperandType.Property, [operand]);
|
|
280
|
+
return this.solveChain(this.getChildFunc(functionName, property), pos);
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
// .xxx(p=> p.xxx)
|
|
284
|
+
return this.solveChain(this.getChildFunc(name, operand), pos);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
else if (this.current === '[') {
|
|
288
|
+
this.index += 1;
|
|
289
|
+
if (name.includes('.')) {
|
|
290
|
+
// .xxx.xxx[x]
|
|
291
|
+
const property = new contract_1.Operand(pos, name, contract_1.OperandType.Property, [operand]);
|
|
292
|
+
const idx = this.getExpression(undefined, undefined, ']');
|
|
293
|
+
return new contract_1.Operand(pos, '[]', contract_1.OperandType.Operator, [property, idx]);
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
// .xxx[x]
|
|
297
|
+
const property = new contract_1.Operand(pos, name, contract_1.OperandType.Property, [operand]);
|
|
298
|
+
const idx = this.getExpression(undefined, undefined, ']');
|
|
299
|
+
return new contract_1.Operand(pos, '[]', contract_1.OperandType.Operator, [property, idx]);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
// .xxx
|
|
304
|
+
return new contract_1.Operand(pos, name, contract_1.OperandType.Property, [operand]);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
else if (this.current === '[') {
|
|
308
|
+
// xxx[x][x] or xxx[x].xxx[x]
|
|
309
|
+
this.index += 1;
|
|
310
|
+
const idx = this.getExpression(undefined, undefined, ']');
|
|
311
|
+
return new contract_1.Operand(pos, '[]', contract_1.OperandType.Operator, [operand, idx]);
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
return operand;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
getOperator() {
|
|
318
|
+
if (this.end) {
|
|
319
|
+
return undefined;
|
|
320
|
+
}
|
|
321
|
+
let op = null;
|
|
322
|
+
if (this.index + 2 < this.length) {
|
|
323
|
+
const triple = this.current + this.offset(1) + this.offset(2);
|
|
324
|
+
if (this.tripleOperators.includes(triple)) {
|
|
325
|
+
op = triple;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
if (op === null && this.index + 1 < this.length) {
|
|
329
|
+
const double = this.current + this.offset(1);
|
|
330
|
+
if (this.doubleOperators.includes(double)) {
|
|
331
|
+
op = double;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (op === null) {
|
|
335
|
+
if (!this.model.isOperator(this.current)) {
|
|
336
|
+
return undefined;
|
|
337
|
+
}
|
|
338
|
+
op = this.current;
|
|
339
|
+
}
|
|
340
|
+
this.index += op.length;
|
|
341
|
+
return op;
|
|
342
|
+
}
|
|
343
|
+
normalize(expression) {
|
|
344
|
+
let isString = false;
|
|
345
|
+
let quotes = '';
|
|
346
|
+
const buffer = expression.split('');
|
|
347
|
+
const length = buffer.length;
|
|
348
|
+
const result = [];
|
|
349
|
+
let line = 0;
|
|
350
|
+
let col = 0;
|
|
351
|
+
let i = 0;
|
|
352
|
+
while (i < length) {
|
|
353
|
+
const p = buffer[i];
|
|
354
|
+
if (isString && p === quotes) {
|
|
355
|
+
isString = false;
|
|
356
|
+
}
|
|
357
|
+
else if (!isString && (p === '\'' || p === '"' || p === '`')) {
|
|
358
|
+
isString = true;
|
|
359
|
+
quotes = p;
|
|
360
|
+
}
|
|
361
|
+
if (isString) {
|
|
362
|
+
result.push([p, line, col]);
|
|
363
|
+
}
|
|
364
|
+
else if (p === ' ') {
|
|
365
|
+
// Only leave spaces when it's between alphanumeric characters.
|
|
366
|
+
// for example in the case of "} if" there should not be a space
|
|
367
|
+
if (i + 1 < length && i - 1 >= 0 && h3lp_1.h3lp.val.isAlphanumeric(buffer[i - 1]) && h3lp_1.h3lp.val.isAlphanumeric(buffer[i + 1])) {
|
|
368
|
+
result.push([p, line, col]);
|
|
369
|
+
}
|
|
370
|
+
// when there is a block that ends with "}" and then there is an enter , replace the enter with ";"
|
|
371
|
+
// TODO: si estamos dentro de un objecto NO debería agregar ; luego de } sino rompe el obj
|
|
372
|
+
// } else if (p === '\n' && result.length > 0 && result[result.length - 1] === '}') {
|
|
373
|
+
// result.push(';')
|
|
374
|
+
}
|
|
375
|
+
else if (p === '\n') {
|
|
376
|
+
line++;
|
|
377
|
+
col = 0;
|
|
378
|
+
}
|
|
379
|
+
else if (p !== '\r' && p !== '\t') {
|
|
380
|
+
result.push([p, line, col]);
|
|
381
|
+
}
|
|
382
|
+
i++;
|
|
383
|
+
col++;
|
|
384
|
+
}
|
|
385
|
+
if (result[result.length - 1][0] === ';') {
|
|
386
|
+
result.splice(-1);
|
|
387
|
+
}
|
|
388
|
+
return result;
|
|
389
|
+
}
|
|
390
|
+
get end() {
|
|
391
|
+
return this.index >= this.length;
|
|
392
|
+
}
|
|
393
|
+
get current() {
|
|
394
|
+
return this.buffer[this.index];
|
|
395
|
+
}
|
|
396
|
+
offset(offset = 0) {
|
|
397
|
+
return this.buffer[this.index + offset];
|
|
398
|
+
}
|
|
399
|
+
pos(offset = 0) {
|
|
400
|
+
const position = this.positions[this.index - offset];
|
|
401
|
+
return new contract_1.Position(position[1], position[2]);
|
|
402
|
+
}
|
|
403
|
+
nextIs(key) {
|
|
404
|
+
const array = key.split('');
|
|
405
|
+
for (let i = 0; i < array.length; i++) {
|
|
406
|
+
if (this.buffer[this.index + i] !== array[i]) {
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
getValue(increment = true) {
|
|
413
|
+
const buff = [];
|
|
414
|
+
if (increment) {
|
|
415
|
+
while (!this.end && h3lp_1.h3lp.val.isAlphanumeric(this.current)) {
|
|
416
|
+
buff.push(this.current);
|
|
417
|
+
this.index += 1;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
let index = this.index;
|
|
422
|
+
while (!this.end && h3lp_1.h3lp.val.isAlphanumeric(this.buffer[index])) {
|
|
423
|
+
buff.push(this.buffer[index]);
|
|
424
|
+
index += 1;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return buff.join('');
|
|
428
|
+
}
|
|
429
|
+
getString(char) {
|
|
430
|
+
const buff = [];
|
|
431
|
+
while (!this.end) {
|
|
432
|
+
if (this.current === char) {
|
|
433
|
+
if (!((this.index + 1 < this.length && this.offset(1) === char) || (this.offset(-1) === char))) {
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
buff.push(this.current);
|
|
438
|
+
this.index += 1;
|
|
439
|
+
}
|
|
440
|
+
this.index += 1;
|
|
441
|
+
return buff.join('');
|
|
442
|
+
}
|
|
443
|
+
getTemplate() {
|
|
444
|
+
const buff = [];
|
|
445
|
+
while (!this.end) {
|
|
446
|
+
if (this.current === '`') {
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
buff.push(this.current);
|
|
450
|
+
this.index += 1;
|
|
451
|
+
}
|
|
452
|
+
this.index += 1;
|
|
453
|
+
return buff.join('');
|
|
454
|
+
}
|
|
455
|
+
getArgs(end = ')') {
|
|
456
|
+
const args = [];
|
|
457
|
+
while (true) {
|
|
458
|
+
const arg = this.getExpression(undefined, undefined, ',' + end);
|
|
459
|
+
if (arg != null)
|
|
460
|
+
args.push(arg);
|
|
461
|
+
if (this.offset(-1) === end)
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
464
|
+
return args;
|
|
465
|
+
}
|
|
466
|
+
getObject(pos) {
|
|
467
|
+
const attributes = [];
|
|
468
|
+
while (true) {
|
|
469
|
+
let name = null;
|
|
470
|
+
if (this.current === '"' || this.current === '\'') {
|
|
471
|
+
const char = this.current;
|
|
472
|
+
this.index += 1;
|
|
473
|
+
name = this.getString(char);
|
|
474
|
+
}
|
|
475
|
+
else {
|
|
476
|
+
name = this.getValue();
|
|
477
|
+
}
|
|
478
|
+
if (this.current === ':')
|
|
479
|
+
this.index += 1;
|
|
480
|
+
else
|
|
481
|
+
throw new Error('attribute ' + name + ' without value');
|
|
482
|
+
const keyValPos = this.pos();
|
|
483
|
+
const value = this.getExpression(undefined, undefined, ',}');
|
|
484
|
+
const attribute = new contract_1.Operand(keyValPos, name, contract_1.OperandType.KeyVal, [value]);
|
|
485
|
+
attributes.push(attribute);
|
|
486
|
+
if (this.offset(-1) === '}')
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
return new contract_1.Operand(pos, 'obj', contract_1.OperandType.Obj, attributes);
|
|
490
|
+
}
|
|
491
|
+
getBlock() {
|
|
492
|
+
const blockPos = this.pos();
|
|
493
|
+
const lines = [];
|
|
494
|
+
while (true) {
|
|
495
|
+
const line = this.getExpression(undefined, undefined, ';}');
|
|
496
|
+
if (line != null) {
|
|
497
|
+
lines.push(line);
|
|
498
|
+
}
|
|
499
|
+
if (this.offset(-1) === ';' && this.current === '}') {
|
|
500
|
+
this.index += 1;
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
if (this.offset(-1) === '}') {
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return new contract_1.Operand(blockPos, 'block', contract_1.OperandType.Block, lines);
|
|
508
|
+
}
|
|
509
|
+
getControlBlock() {
|
|
510
|
+
if (this.current === '{') {
|
|
511
|
+
this.index += 1;
|
|
512
|
+
return this.getBlock();
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
return this.getExpression(undefined, undefined, ';');
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
getReturn(pos) {
|
|
519
|
+
const value = this.getExpression(undefined, undefined, ';');
|
|
520
|
+
return new contract_1.Operand(pos, 'return', contract_1.OperandType.Return, [value]);
|
|
521
|
+
}
|
|
522
|
+
getTryCatchBlock(pos) {
|
|
523
|
+
const children = [];
|
|
524
|
+
const tryBlock = this.getControlBlock();
|
|
525
|
+
children.push(tryBlock);
|
|
526
|
+
if (this.nextIs('catch')) {
|
|
527
|
+
const catchChildren = [];
|
|
528
|
+
const catchPos = this.pos('catch'.length);
|
|
529
|
+
this.index += 'catch'.length;
|
|
530
|
+
if (this.current === '(') {
|
|
531
|
+
this.index += 1;
|
|
532
|
+
const variable = this.getExpression(undefined, undefined, ')');
|
|
533
|
+
catchChildren.push(variable);
|
|
534
|
+
}
|
|
535
|
+
const catchBlock = this.getControlBlock();
|
|
536
|
+
catchChildren.push(catchBlock);
|
|
537
|
+
const catchNode = new contract_1.Operand(catchPos, 'catch', contract_1.OperandType.Catch, catchChildren);
|
|
538
|
+
children.push(catchNode);
|
|
539
|
+
}
|
|
540
|
+
if (this.current === ';')
|
|
541
|
+
this.index += 1;
|
|
542
|
+
return new contract_1.Operand(pos, 'try', contract_1.OperandType.Try, children);
|
|
543
|
+
}
|
|
544
|
+
getThrow(pos) {
|
|
545
|
+
const exception = this.getExpression(undefined, undefined, ';');
|
|
546
|
+
return new contract_1.Operand(pos, 'throw', contract_1.OperandType.Throw, [exception]);
|
|
547
|
+
}
|
|
548
|
+
getIfBlock(pos) {
|
|
549
|
+
const children = [];
|
|
550
|
+
const condition = this.getExpression(undefined, undefined, ')');
|
|
551
|
+
children.push(condition);
|
|
552
|
+
const block = this.getControlBlock();
|
|
553
|
+
children.push(block);
|
|
554
|
+
while (this.nextIs('elseif(')) {
|
|
555
|
+
const elseIfPos = this.pos();
|
|
556
|
+
this.index += 'elseif('.length;
|
|
557
|
+
const condition = this.getExpression(undefined, undefined, ')');
|
|
558
|
+
const elseIfBlock = this.getControlBlock();
|
|
559
|
+
const elseIfNode = new contract_1.Operand(elseIfPos, 'elseif', contract_1.OperandType.ElseIf, [condition, elseIfBlock]);
|
|
560
|
+
children.push(elseIfNode);
|
|
561
|
+
}
|
|
562
|
+
if (this.nextIs('else')) {
|
|
563
|
+
this.index += 'else'.length;
|
|
564
|
+
const elseBlock = this.getControlBlock();
|
|
565
|
+
children.push(elseBlock);
|
|
566
|
+
}
|
|
567
|
+
return new contract_1.Operand(pos, 'if', contract_1.OperandType.If, children);
|
|
568
|
+
}
|
|
569
|
+
getSwitchBlock(pos) {
|
|
570
|
+
const children = [];
|
|
571
|
+
const value = this.getExpression(undefined, undefined, ')');
|
|
572
|
+
children.push(value);
|
|
573
|
+
if (this.current === '{')
|
|
574
|
+
this.index += 1;
|
|
575
|
+
let next = this.nextIs('case') ? 'case' : this.nextIs('default:') ? 'default:' : '';
|
|
576
|
+
while (next === 'case') {
|
|
577
|
+
this.index += 'case'.length;
|
|
578
|
+
let compare;
|
|
579
|
+
if (this.current === '\'' || this.current === '"') {
|
|
580
|
+
const char = this.current;
|
|
581
|
+
this.index += 1;
|
|
582
|
+
compare = this.getString(char);
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
compare = this.getValue();
|
|
586
|
+
}
|
|
587
|
+
const caseNode = new contract_1.Operand(this.pos(), compare, contract_1.OperandType.Case);
|
|
588
|
+
const block = new contract_1.Operand(this.pos(), 'block', contract_1.OperandType.Block);
|
|
589
|
+
caseNode.children = [block];
|
|
590
|
+
if (this.current === ':')
|
|
591
|
+
this.index += 1;
|
|
592
|
+
const lines = [];
|
|
593
|
+
while (true) {
|
|
594
|
+
const line = this.getExpression(undefined, undefined, ';}');
|
|
595
|
+
if (line !== undefined)
|
|
596
|
+
lines.push(line);
|
|
597
|
+
if (this.nextIs('case')) {
|
|
598
|
+
next = 'case';
|
|
599
|
+
break;
|
|
600
|
+
}
|
|
601
|
+
else if (this.nextIs('default:')) {
|
|
602
|
+
next = 'default:';
|
|
603
|
+
break;
|
|
604
|
+
}
|
|
605
|
+
else if (this.current === '}' || this.offset(-1) === '}') {
|
|
606
|
+
next = 'end';
|
|
607
|
+
break;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
block.children = lines;
|
|
611
|
+
children.push(caseNode);
|
|
612
|
+
}
|
|
613
|
+
if (next === 'default:') {
|
|
614
|
+
this.index += 'default:'.length;
|
|
615
|
+
const defaultNode = new contract_1.Operand(this.pos(), 'default', contract_1.OperandType.Default);
|
|
616
|
+
const block = new contract_1.Operand(this.pos(), 'block', contract_1.OperandType.Block);
|
|
617
|
+
defaultNode.children = [block];
|
|
618
|
+
const lines = [];
|
|
619
|
+
while (true) {
|
|
620
|
+
const line = this.getExpression(undefined, undefined, ';}');
|
|
621
|
+
if (line !== undefined)
|
|
622
|
+
lines.push(line);
|
|
623
|
+
if (this.current === '}' || this.offset(-1) === '}')
|
|
624
|
+
break;
|
|
625
|
+
}
|
|
626
|
+
block.children = lines;
|
|
627
|
+
children.push(defaultNode);
|
|
628
|
+
}
|
|
629
|
+
if (this.current === '}')
|
|
630
|
+
this.index += 1;
|
|
631
|
+
return new contract_1.Operand(pos, 'switch', contract_1.OperandType.Switch, children);
|
|
632
|
+
}
|
|
633
|
+
getWhileBlock(pos) {
|
|
634
|
+
const condition = this.getExpression(undefined, undefined, ')');
|
|
635
|
+
const block = this.getControlBlock();
|
|
636
|
+
return new contract_1.Operand(pos, 'while', contract_1.OperandType.While, [condition, block]);
|
|
637
|
+
}
|
|
638
|
+
getForBlock(pos) {
|
|
639
|
+
const first = this.getExpression(undefined, undefined, '; ');
|
|
640
|
+
if (this.offset(-1) === ';') {
|
|
641
|
+
const condition = this.getExpression(undefined, undefined, ';');
|
|
642
|
+
const increment = this.getExpression(undefined, undefined, ')');
|
|
643
|
+
const block = this.getControlBlock();
|
|
644
|
+
return new contract_1.Operand(pos, 'for', contract_1.OperandType.For, [first, condition, increment, block]);
|
|
645
|
+
}
|
|
646
|
+
else if (this.nextIs('in')) {
|
|
647
|
+
this.index += 2;
|
|
648
|
+
// si hay espacios luego del in debe eliminarlos
|
|
649
|
+
while (this.current === ' ') {
|
|
650
|
+
this.index += 1;
|
|
651
|
+
}
|
|
652
|
+
const list = this.getExpression(undefined, undefined, ')');
|
|
653
|
+
const block = this.getControlBlock();
|
|
654
|
+
return new contract_1.Operand(pos, 'forIn', contract_1.OperandType.ForIn, [first, list, block]);
|
|
655
|
+
}
|
|
656
|
+
throw new Error('expression for error');
|
|
657
|
+
}
|
|
658
|
+
getFunctionBlock(pos) {
|
|
659
|
+
const name = this.getValue();
|
|
660
|
+
if (this.current === '(')
|
|
661
|
+
this.index += 1;
|
|
662
|
+
const argsPos = this.pos();
|
|
663
|
+
const args = this.getArgs();
|
|
664
|
+
const block = this.getBlock();
|
|
665
|
+
const argsOperand = new contract_1.Operand(argsPos, 'args', contract_1.OperandType.Args, args);
|
|
666
|
+
return new contract_1.Operand(pos, name, contract_1.OperandType.Func, [argsOperand, block]);
|
|
667
|
+
}
|
|
668
|
+
getChildFunc(name, parent) {
|
|
669
|
+
let isArrow = false;
|
|
670
|
+
const pos = this.pos();
|
|
671
|
+
const variableName = this.getValue(false);
|
|
672
|
+
if (variableName !== '') {
|
|
673
|
+
// example: p => {name:p.name}
|
|
674
|
+
// example: p -> {name:p.name}
|
|
675
|
+
const i = variableName.length;
|
|
676
|
+
if ((this.offset(i) === '=' || this.offset(i) === '-') && this.offset(i + 1) === '>') {
|
|
677
|
+
isArrow = true;
|
|
678
|
+
this.index += (variableName.length + 2); // [VARIABLE+NAME] + [=>]
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
else if (this.current + this.offset(1) === '()') {
|
|
682
|
+
// example: ()=> {name:name}
|
|
683
|
+
// example: ()-> {name:name}
|
|
684
|
+
if ((this.offset(2) === '=' || this.offset(2) === '-') && this.offset(3) === '>') {
|
|
685
|
+
isArrow = true;
|
|
686
|
+
this.index += 4; // [()=>]
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
else if (this.current + this.offset(1) === '=>' || this.current + this.offset(1) === '->') {
|
|
690
|
+
// example: => {name:name}
|
|
691
|
+
// example: -> {name:name}
|
|
692
|
+
isArrow = true;
|
|
693
|
+
this.index += 2; // [=>]
|
|
694
|
+
}
|
|
695
|
+
if (isArrow) {
|
|
696
|
+
const variable = new contract_1.Operand(pos, variableName, contract_1.OperandType.Var);
|
|
697
|
+
const body = this.getExpression(undefined, undefined, ')');
|
|
698
|
+
return new contract_1.Operand(pos, name, contract_1.OperandType.Arrow, [parent, variable, body]);
|
|
699
|
+
}
|
|
700
|
+
else {
|
|
701
|
+
if (this.current === ')') {
|
|
702
|
+
this.index += 1;
|
|
703
|
+
// Example: xxx.xxx()
|
|
704
|
+
return new contract_1.Operand(pos, name, contract_1.OperandType.ChildFunc, [parent]);
|
|
705
|
+
}
|
|
706
|
+
// Example: xxx.xxx(x)
|
|
707
|
+
const args = this.getArgs(')');
|
|
708
|
+
args.splice(0, 0, parent);
|
|
709
|
+
return new contract_1.Operand(pos, name, contract_1.OperandType.ChildFunc, args);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
getIndexOperand(name, pos) {
|
|
713
|
+
const idx = this.getExpression(undefined, undefined, ']');
|
|
714
|
+
const operand = new contract_1.Operand(pos, name, contract_1.OperandType.Var);
|
|
715
|
+
return new contract_1.Operand(pos, '[]', contract_1.OperandType.Operator, [operand, idx]);
|
|
716
|
+
}
|
|
717
|
+
getEnum(value, pos) {
|
|
718
|
+
if (value.includes('.') && this.model.isEnum(value)) {
|
|
719
|
+
const names = value.split('.');
|
|
720
|
+
const enumName = names[0];
|
|
721
|
+
const enumOption = names[1];
|
|
722
|
+
const enumValue = this.model.getEnumValue(enumName, enumOption);
|
|
723
|
+
return new contract_1.Operand(pos, enumValue, contract_1.OperandType.Const, [], contract_1.Type.get(value));
|
|
724
|
+
}
|
|
725
|
+
else {
|
|
726
|
+
const values = this.model.getEnum(value);
|
|
727
|
+
const attributes = [];
|
|
728
|
+
for (const name in values) {
|
|
729
|
+
const _value = values[name];
|
|
730
|
+
const attribute = new contract_1.Operand(pos, name, contract_1.OperandType.KeyVal, [new contract_1.Operand(pos, _value, contract_1.OperandType.Const, [], contract_1.Type.get(_value))]);
|
|
731
|
+
attributes.push(attribute);
|
|
732
|
+
}
|
|
733
|
+
return new contract_1.Operand(pos, 'obj', contract_1.OperandType.Obj, attributes);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
exports.Parser = Parser;
|
|
738
|
+
//# sourceMappingURL=parser.js.map
|