@reportman/engine 0.0.0-stage → 0.1.0-alpha.1

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.
Files changed (244) hide show
  1. package/LICENSE +373 -0
  2. package/dist/.tsbuildinfo +1 -0
  3. package/dist/data/columns.d.ts +50 -0
  4. package/dist/data/columns.d.ts.map +1 -0
  5. package/dist/data/columns.js +385 -0
  6. package/dist/data/columns.js.map +1 -0
  7. package/dist/data/databaseinfo.d.ts +41 -0
  8. package/dist/data/databaseinfo.d.ts.map +1 -0
  9. package/dist/data/databaseinfo.js +145 -0
  10. package/dist/data/databaseinfo.js.map +1 -0
  11. package/dist/data/datainfo.d.ts +82 -0
  12. package/dist/data/datainfo.d.ts.map +1 -0
  13. package/dist/data/datainfo.js +449 -0
  14. package/dist/data/datainfo.js.map +1 -0
  15. package/dist/data/errors.d.ts +8 -0
  16. package/dist/data/errors.d.ts.map +1 -0
  17. package/dist/data/errors.js +14 -0
  18. package/dist/data/errors.js.map +1 -0
  19. package/dist/data/host.d.ts +132 -0
  20. package/dist/data/host.d.ts.map +1 -0
  21. package/dist/data/host.js +108 -0
  22. package/dist/data/host.js.map +1 -0
  23. package/dist/data/op.d.ts +21 -0
  24. package/dist/data/op.d.ts.map +1 -0
  25. package/dist/data/op.js +97 -0
  26. package/dist/data/op.js.map +1 -0
  27. package/dist/data/reportdataset.d.ts +116 -0
  28. package/dist/data/reportdataset.d.ts.map +1 -0
  29. package/dist/data/reportdataset.js +396 -0
  30. package/dist/data/reportdataset.js.map +1 -0
  31. package/dist/data/transfer.d.ts +46 -0
  32. package/dist/data/transfer.d.ts.map +1 -0
  33. package/dist/data/transfer.js +100 -0
  34. package/dist/data/transfer.js.map +1 -0
  35. package/dist/data/union.d.ts +88 -0
  36. package/dist/data/union.d.ts.map +1 -0
  37. package/dist/data/union.js +733 -0
  38. package/dist/data/union.js.map +1 -0
  39. package/dist/eval/chinesetables.d.ts +5 -0
  40. package/dist/eval/chinesetables.d.ts.map +1 -0
  41. package/dist/eval/chinesetables.js +8 -0
  42. package/dist/eval/chinesetables.js.map +1 -0
  43. package/dist/eval/evaluator.d.ts +100 -0
  44. package/dist/eval/evaluator.d.ts.map +1 -0
  45. package/dist/eval/evaluator.js +667 -0
  46. package/dist/eval/evaluator.js.map +1 -0
  47. package/dist/eval/exception.d.ts +9 -0
  48. package/dist/eval/exception.d.ts.map +1 -0
  49. package/dist/eval/exception.js +15 -0
  50. package/dist/eval/exception.js.map +1 -0
  51. package/dist/eval/fields.d.ts +60 -0
  52. package/dist/eval/fields.d.ts.map +1 -0
  53. package/dist/eval/fields.js +104 -0
  54. package/dist/eval/fields.js.map +1 -0
  55. package/dist/eval/functions.d.ts +27 -0
  56. package/dist/eval/functions.d.ts.map +1 -0
  57. package/dist/eval/functions.js +810 -0
  58. package/dist/eval/functions.js.map +1 -0
  59. package/dist/eval/identifiers.d.ts +100 -0
  60. package/dist/eval/identifiers.d.ts.map +1 -0
  61. package/dist/eval/identifiers.js +195 -0
  62. package/dist/eval/identifiers.js.map +1 -0
  63. package/dist/eval/parser.d.ts +47 -0
  64. package/dist/eval/parser.d.ts.map +1 -0
  65. package/dist/eval/parser.js +336 -0
  66. package/dist/eval/parser.js.map +1 -0
  67. package/dist/index.d.ts +52 -0
  68. package/dist/index.d.ts.map +1 -0
  69. package/dist/index.js +42 -0
  70. package/dist/index.js.map +1 -0
  71. package/dist/params/param.d.ts +92 -0
  72. package/dist/params/param.d.ts.map +1 -0
  73. package/dist/params/param.js +358 -0
  74. package/dist/params/param.js.map +1 -0
  75. package/dist/params/parameters.d.ts +87 -0
  76. package/dist/params/parameters.d.ts.map +1 -0
  77. package/dist/params/parameters.js +499 -0
  78. package/dist/params/parameters.js.map +1 -0
  79. package/dist/report/barcode/barcodeitem.d.ts +76 -0
  80. package/dist/report/barcode/barcodeitem.d.ts.map +1 -0
  81. package/dist/report/barcode/barcodeitem.js +864 -0
  82. package/dist/report/barcode/barcodeitem.js.map +1 -0
  83. package/dist/report/barcode/code128.d.ts +18 -0
  84. package/dist/report/barcode/code128.d.ts.map +1 -0
  85. package/dist/report/barcode/code128.js +187 -0
  86. package/dist/report/barcode/code128.js.map +1 -0
  87. package/dist/report/barcode/netutil.d.ts +30 -0
  88. package/dist/report/barcode/netutil.d.ts.map +1 -0
  89. package/dist/report/barcode/netutil.js +65 -0
  90. package/dist/report/barcode/netutil.js.map +1 -0
  91. package/dist/report/barcode/pdf417.d.ts +17 -0
  92. package/dist/report/barcode/pdf417.d.ts.map +1 -0
  93. package/dist/report/barcode/pdf417.js +782 -0
  94. package/dist/report/barcode/pdf417.js.map +1 -0
  95. package/dist/report/barcode/pdf417tables.d.ts +11 -0
  96. package/dist/report/barcode/pdf417tables.d.ts.map +1 -0
  97. package/dist/report/barcode/pdf417tables.js +880 -0
  98. package/dist/report/barcode/pdf417tables.js.map +1 -0
  99. package/dist/report/barcode/qrcode.d.ts +14 -0
  100. package/dist/report/barcode/qrcode.d.ts.map +1 -0
  101. package/dist/report/barcode/qrcode.js +214 -0
  102. package/dist/report/barcode/qrcode.js.map +1 -0
  103. package/dist/report/barcode/zxingwidth.d.ts +4 -0
  104. package/dist/report/barcode/zxingwidth.d.ts.map +1 -0
  105. package/dist/report/barcode/zxingwidth.js +247 -0
  106. package/dist/report/barcode/zxingwidth.js.map +1 -0
  107. package/dist/report/chart/chartidens.d.ts +70 -0
  108. package/dist/report/chart/chartidens.d.ts.map +1 -0
  109. package/dist/report/chart/chartidens.js +162 -0
  110. package/dist/report/chart/chartidens.js.map +1 -0
  111. package/dist/report/chart/chartitem.d.ts +110 -0
  112. package/dist/report/chart/chartitem.d.ts.map +1 -0
  113. package/dist/report/chart/chartitem.js +403 -0
  114. package/dist/report/chart/chartitem.js.map +1 -0
  115. package/dist/report/chart/series.d.ts +65 -0
  116. package/dist/report/chart/series.d.ts.map +1 -0
  117. package/dist/report/chart/series.js +74 -0
  118. package/dist/report/chart/series.js.map +1 -0
  119. package/dist/report/chart/vectorchart.d.ts +18 -0
  120. package/dist/report/chart/vectorchart.d.ts.map +1 -0
  121. package/dist/report/chart/vectorchart.js +356 -0
  122. package/dist/report/chart/vectorchart.js.map +1 -0
  123. package/dist/report/convert.d.ts +4 -0
  124. package/dist/report/convert.d.ts.map +1 -0
  125. package/dist/report/convert.js +162 -0
  126. package/dist/report/convert.js.map +1 -0
  127. package/dist/report/errors.d.ts +13 -0
  128. package/dist/report/errors.d.ts.map +1 -0
  129. package/dist/report/errors.js +20 -0
  130. package/dist/report/errors.js.map +1 -0
  131. package/dist/report/identifiers.d.ts +91 -0
  132. package/dist/report/identifiers.d.ts.map +1 -0
  133. package/dist/report/identifiers.js +404 -0
  134. package/dist/report/identifiers.js.map +1 -0
  135. package/dist/report/imageitem.d.ts +66 -0
  136. package/dist/report/imageitem.d.ts.map +1 -0
  137. package/dist/report/imageitem.js +190 -0
  138. package/dist/report/imageitem.js.map +1 -0
  139. package/dist/report/items.d.ts +219 -0
  140. package/dist/report/items.d.ts.map +1 -0
  141. package/dist/report/items.js +1015 -0
  142. package/dist/report/items.js.map +1 -0
  143. package/dist/report/report.d.ts +221 -0
  144. package/dist/report/report.d.ts.map +1 -0
  145. package/dist/report/report.js +1968 -0
  146. package/dist/report/report.js.map +1 -0
  147. package/dist/report/run.d.ts +87 -0
  148. package/dist/report/run.d.ts.map +1 -0
  149. package/dist/report/run.js +121 -0
  150. package/dist/report/run.js.map +1 -0
  151. package/dist/report/section.d.ts +86 -0
  152. package/dist/report/section.d.ts.map +1 -0
  153. package/dist/report/section.js +325 -0
  154. package/dist/report/section.js.map +1 -0
  155. package/dist/report/subreport.d.ts +49 -0
  156. package/dist/report/subreport.d.ts.map +1 -0
  157. package/dist/report/subreport.js +220 -0
  158. package/dist/report/subreport.js.map +1 -0
  159. package/dist/util/base64.d.ts +9 -0
  160. package/dist/util/base64.d.ts.map +1 -0
  161. package/dist/util/base64.js +60 -0
  162. package/dist/util/base64.js.map +1 -0
  163. package/dist/util/chars.d.ts +45 -0
  164. package/dist/util/chars.d.ts.map +1 -0
  165. package/dist/util/chars.js +194 -0
  166. package/dist/util/chars.js.map +1 -0
  167. package/dist/util/compositeformat.d.ts +3 -0
  168. package/dist/util/compositeformat.d.ts.map +1 -0
  169. package/dist/util/compositeformat.js +99 -0
  170. package/dist/util/compositeformat.js.map +1 -0
  171. package/dist/util/culture.d.ts +84 -0
  172. package/dist/util/culture.d.ts.map +1 -0
  173. package/dist/util/culture.js +59 -0
  174. package/dist/util/culture.js.map +1 -0
  175. package/dist/util/cultures.generated.d.ts +3 -0
  176. package/dist/util/cultures.generated.d.ts.map +1 -0
  177. package/dist/util/cultures.generated.js +1043 -0
  178. package/dist/util/cultures.generated.js.map +1 -0
  179. package/dist/util/dateformat.d.ts +21 -0
  180. package/dist/util/dateformat.d.ts.map +1 -0
  181. package/dist/util/dateformat.js +439 -0
  182. package/dist/util/dateformat.js.map +1 -0
  183. package/dist/util/dateparse.d.ts +12 -0
  184. package/dist/util/dateparse.d.ts.map +1 -0
  185. package/dist/util/dateparse.js +1212 -0
  186. package/dist/util/dateparse.js.map +1 -0
  187. package/dist/util/datetime.d.ts +70 -0
  188. package/dist/util/datetime.d.ts.map +1 -0
  189. package/dist/util/datetime.js +265 -0
  190. package/dist/util/datetime.js.map +1 -0
  191. package/dist/util/decimal.d.ts +54 -0
  192. package/dist/util/decimal.d.ts.map +1 -0
  193. package/dist/util/decimal.js +365 -0
  194. package/dist/util/decimal.js.map +1 -0
  195. package/dist/util/delphitext.d.ts +40 -0
  196. package/dist/util/delphitext.d.ts.map +1 -0
  197. package/dist/util/delphitext.js +408 -0
  198. package/dist/util/delphitext.js.map +1 -0
  199. package/dist/util/doubleutil.d.ts +30 -0
  200. package/dist/util/doubleutil.d.ts.map +1 -0
  201. package/dist/util/doubleutil.js +651 -0
  202. package/dist/util/doubleutil.js.map +1 -0
  203. package/dist/util/inflate.d.ts +7 -0
  204. package/dist/util/inflate.d.ts.map +1 -0
  205. package/dist/util/inflate.js +256 -0
  206. package/dist/util/inflate.js.map +1 -0
  207. package/dist/util/netexception.d.ts +59 -0
  208. package/dist/util/netexception.d.ts.map +1 -0
  209. package/dist/util/netexception.js +94 -0
  210. package/dist/util/netexception.js.map +1 -0
  211. package/dist/util/numberformat.d.ts +14 -0
  212. package/dist/util/numberformat.d.ts.map +1 -0
  213. package/dist/util/numberformat.js +828 -0
  214. package/dist/util/numberformat.js.map +1 -0
  215. package/dist/util/numberparse.d.ts +49 -0
  216. package/dist/util/numberparse.d.ts.map +1 -0
  217. package/dist/util/numberparse.js +568 -0
  218. package/dist/util/numberparse.js.map +1 -0
  219. package/dist/util/reportmanres.en.generated.d.ts +2 -0
  220. package/dist/util/reportmanres.en.generated.d.ts.map +1 -0
  221. package/dist/util/reportmanres.en.generated.js +2082 -0
  222. package/dist/util/reportmanres.en.generated.js.map +1 -0
  223. package/dist/util/reportmanres.es.generated.d.ts +2 -0
  224. package/dist/util/reportmanres.es.generated.d.ts.map +1 -0
  225. package/dist/util/reportmanres.es.generated.js +2082 -0
  226. package/dist/util/reportmanres.es.generated.js.map +1 -0
  227. package/dist/util/translator.d.ts +29 -0
  228. package/dist/util/translator.d.ts.map +1 -0
  229. package/dist/util/translator.js +79 -0
  230. package/dist/util/translator.js.map +1 -0
  231. package/dist/variant/coercion.d.ts +9 -0
  232. package/dist/variant/coercion.d.ts.map +1 -0
  233. package/dist/variant/coercion.js +298 -0
  234. package/dist/variant/coercion.js.map +1 -0
  235. package/dist/variant/context.d.ts +25 -0
  236. package/dist/variant/context.d.ts.map +1 -0
  237. package/dist/variant/context.js +22 -0
  238. package/dist/variant/context.js.map +1 -0
  239. package/dist/variant/variant.d.ts +193 -0
  240. package/dist/variant/variant.d.ts.map +1 -0
  241. package/dist/variant/variant.js +1229 -0
  242. package/dist/variant/variant.js.map +1 -0
  243. package/package.json +35 -4
  244. package/README.md +0 -3
@@ -0,0 +1,667 @@
1
+ // Port of Reportman.Reporting.Evaluator: the recursive-descent evaluator of the expression
2
+ // language, with the same structure as the C# code so that the semantics are identical:
3
+ //
4
+ // separator ; (statements, the last value is the result)
5
+ // variables := (assignment of a variable)
6
+ // logicalOR OR
7
+ // logicalAND AND
8
+ // comparations <> >< == = < <= > >= (operands coerced with VariantCoercion)
9
+ // sum_diff + - (+ is Variant.ExpressionSum)
10
+ // mul_div * /
11
+ // dosign unary - + NOT, IIF, function calls
12
+ // parentesis ( )
13
+ // operand identifiers and literals
14
+ //
15
+ // Values travel by reference as in C# (ref Variant): a Ref is written by the callee, so the
16
+ // partial results of C# (a NOT or a sign applied to a previous value...) are the same here.
17
+ import { messageOf, NamedException, UnNamedException } from '../util/netexception.js';
18
+ import { toUpperNet } from '../util/chars.js';
19
+ import { compositeFormat } from '../util/compositeformat.js';
20
+ import { coercion as coercionOf } from '../variant/coercion.js';
21
+ import { createContext, systemNow } from '../variant/context.js';
22
+ import { Variant, VariantType } from '../variant/variant.js';
23
+ import { EvalException } from './exception.js';
24
+ import { IdenField } from './fields.js';
25
+ import { addBuiltinIdentifiers, IdenTime } from './functions.js';
26
+ import { EvalIdentifiers, IdenFunction, IdenVariable } from './identifiers.js';
27
+ import { EvalParser, TokenType } from './parser.js';
28
+ class ValueRef {
29
+ v;
30
+ constructor(v = Variant.null) {
31
+ this.v = v;
32
+ }
33
+ }
34
+ /** ref idenf.Params[i]: a reference to a slot of the parameter array of a function. */
35
+ class ParamRef {
36
+ arr;
37
+ index;
38
+ constructor(arr, index) {
39
+ this.arr = arr;
40
+ this.index = index;
41
+ }
42
+ get v() {
43
+ return this.arr[this.index];
44
+ }
45
+ set v(value) {
46
+ this.arr[this.index] = value;
47
+ }
48
+ }
49
+ /** A host with the JavaScript clock (local time) and no file system (FILEEXISTS is False). */
50
+ export const defaultHost = {
51
+ now: systemNow,
52
+ fileExists: () => false,
53
+ };
54
+ export class Evaluator {
55
+ fExpression = '';
56
+ fChecking = false;
57
+ fResult = Variant.null;
58
+ fEvaluating = false;
59
+ fIdentifierList;
60
+ fParser;
61
+ /** AliasList: the dataset fields an expression can read. */
62
+ aliasList = null;
63
+ /** OnDatasetNeeded: called when a field is not found, before looking for it again. */
64
+ onDatasetNeeded = null;
65
+ /** Language: 0 English, 1 Spanish, 2 Catalan (MONTHNAME, NUMTOTEXT). */
66
+ language;
67
+ context;
68
+ host;
69
+ constructor(options = {}, withIdentifiers = true) {
70
+ this.host = options.host ?? defaultHost;
71
+ const host = this.host;
72
+ this.context = options.context ?? createContext(options.culture ?? 'en-US', { now: () => host.now() });
73
+ this.language = options.language ?? 0;
74
+ this.fParser = new EvalParser(this.context);
75
+ this.fIdentifierList = new EvalIdentifiers(this.context.culture.name);
76
+ if (withIdentifiers)
77
+ addBuiltinIdentifiers((k, i) => this.fIdentifierList.add(k, i), this);
78
+ }
79
+ /** The identifiers (variables, constants and functions). */
80
+ get identifiers() {
81
+ return this.fIdentifierList;
82
+ }
83
+ /** The last result. */
84
+ get result() {
85
+ return this.fResult;
86
+ }
87
+ get expression() {
88
+ return this.fExpression;
89
+ }
90
+ set expression(value) {
91
+ if (this.fEvaluating)
92
+ throw new UnNamedException(this.context.translator.translateStr(450));
93
+ this.fExpression = value;
94
+ }
95
+ /** CheckSyntax. C# parity: Evaluate resets FChecking, so the text is evaluated in full. */
96
+ checkSyntax(text) {
97
+ this.fChecking = true;
98
+ try {
99
+ this.evaluateText(text);
100
+ }
101
+ finally {
102
+ this.fChecking = false;
103
+ }
104
+ }
105
+ /** AddVariable: registers a variable as "M." + NAME. */
106
+ addVariable(name, iden) {
107
+ iden.name = toUpperNet(name);
108
+ this.fIdentifierList.add('M.' + iden.name, iden);
109
+ }
110
+ /** AddIden: registers any identifier (a function, a constant...) under its upper case name. */
111
+ addIden(name, iden) {
112
+ iden.name = toUpperNet(name);
113
+ this.fIdentifierList.add(iden.name, iden);
114
+ }
115
+ /** NewVariable: creates a variable with an initial value. */
116
+ newVariable(name, valueIni) {
117
+ if (this.searchIdentifier(name) !== null)
118
+ throw new NamedException('Variable exists:' + name, name);
119
+ const iden = new IdenVariable(this);
120
+ this.addVariable(name, iden);
121
+ iden.value = valueIni;
122
+ return iden;
123
+ }
124
+ /** SearchIdentifier: an identifier, a variable (M.NAME or NAME) or a dataset field, or null. */
125
+ searchIdentifier(idenname) {
126
+ let iden = null;
127
+ idenname = toUpperNet(idenname);
128
+ let sdataset = '';
129
+ let sfield = idenname;
130
+ const index = idenname.indexOf('.');
131
+ if (index < 0) {
132
+ const found = this.fIdentifierList.get(idenname);
133
+ if (found !== undefined) {
134
+ // TIME came from the Delphi engine when reports of the C# one could already name a field
135
+ // TIME without its dataset: such a field keeps winning
136
+ if (found instanceof IdenTime && this.aliasList !== null) {
137
+ const r = this.aliasList.findField(idenname, '');
138
+ if (r.iden !== null && !r.duplicated)
139
+ return r.iden;
140
+ }
141
+ return found;
142
+ }
143
+ sfield = idenname;
144
+ }
145
+ else {
146
+ sdataset = idenname.substring(0, index);
147
+ sfield = idenname.substring(index + 1);
148
+ if (sdataset === 'M') {
149
+ const found = this.fIdentifierList.get('M.' + sfield);
150
+ if (found !== undefined)
151
+ return found;
152
+ }
153
+ }
154
+ if (sdataset === '') {
155
+ const found = this.fIdentifierList.get('M.' + sfield);
156
+ if (found !== undefined)
157
+ iden = found;
158
+ }
159
+ if (this.aliasList !== null) {
160
+ let r = this.aliasList.findField(sfield, sdataset);
161
+ if (r.iden === null && this.onDatasetNeeded !== null) {
162
+ this.onDatasetNeeded({ evaluator: this, dataset: sdataset });
163
+ r = this.aliasList.findField(sfield, sdataset);
164
+ }
165
+ if (r.duplicated)
166
+ throw new NamedException('Especify variable or file:' + sfield, sfield);
167
+ if (r.iden !== null) {
168
+ if (iden !== null)
169
+ throw new NamedException('Especify variable or file:' + sfield, sfield);
170
+ if (r.iden instanceof IdenField)
171
+ r.iden.translator = this.context.translator;
172
+ iden = r.iden;
173
+ }
174
+ }
175
+ return iden;
176
+ }
177
+ /**
178
+ * GetStreamFromExpression: the bytes of a Binary field (without a Paradox graphic header), of a
179
+ * Binary result, or of the file whose name the expression gives (host.readFile).
180
+ */
181
+ getStreamFromExpression(expre) {
182
+ let iden = this.searchIdentifier(expre);
183
+ let memstream = null;
184
+ if (iden !== null) {
185
+ if (iden instanceof IdenField) {
186
+ const avalue = iden.value;
187
+ if (avalue.varType === VariantType.Binary) {
188
+ memstream = avalue.getBytes();
189
+ if (memstream.length > 8) {
190
+ const view = new DataView(memstream.buffer, memstream.byteOffset, memstream.byteLength);
191
+ const count = view.getInt16(0, true);
192
+ const htype = view.getInt16(2, true);
193
+ const size = view.getInt32(4, true);
194
+ if (count === 1 && htype === 0x0100) {
195
+ // new byte[hd.Size] of a negative size
196
+ if (size < 0)
197
+ throw new UnNamedException('Arithmetic operation resulted in an overflow.');
198
+ memstream = memstream.slice(8, 8 + size);
199
+ }
200
+ }
201
+ }
202
+ else if (avalue.varType !== VariantType.Null)
203
+ iden = null;
204
+ }
205
+ else
206
+ iden = null;
207
+ }
208
+ if (iden === null) {
209
+ const avalue = this.evaluateText(expre);
210
+ if (avalue.varType === VariantType.Binary)
211
+ memstream = avalue.getBytes();
212
+ else {
213
+ if (avalue.varType !== VariantType.String)
214
+ throw new NamedException('Field or file not found:' + expre, expre);
215
+ const path = avalue.toString(this.context);
216
+ if (!this.host.fileExists(path))
217
+ throw new NamedException('File not found:' + path, path);
218
+ const bytes = this.host.readFile?.(path);
219
+ if (bytes === undefined)
220
+ throw new NamedException('File not found:' + path, path);
221
+ memstream = new Uint8Array(bytes);
222
+ }
223
+ }
224
+ return memstream;
225
+ }
226
+ // -------------------------------------------------------------------------------------------
227
+ // The grammar
228
+ T(index) {
229
+ return this.context.translator.translateStr(index);
230
+ }
231
+ error(index) {
232
+ return new EvalException(this.T(index), this.fParser.sourceLine, this.fParser.sourcePos, '');
233
+ }
234
+ /** "Unknown identifier: X" whatever the translation ends with. */
235
+ unknownIdentifier(name) {
236
+ return this.T(440).replace(/[ :\r]+$/, '') + ': ' + name;
237
+ }
238
+ operand(fvalue) {
239
+ const p = this.fParser;
240
+ switch (p.currentToken()) {
241
+ case TokenType.Symbol: {
242
+ const iden = this.searchIdentifier(p.tokenString());
243
+ if (iden === null)
244
+ throw new EvalException(this.unknownIdentifier(p.tokenString()), p.sourceLine, p.sourcePos, '');
245
+ // C# parity: a function reached here (after NOT or a sign: NOT ISNUMERIC(...)) is read
246
+ // with the parameters of its previous call, and its arguments are left unparsed.
247
+ fvalue.v = iden.value;
248
+ p.nextToken();
249
+ break;
250
+ }
251
+ case TokenType.String:
252
+ fvalue.v = Variant.fromString(p.tokenString());
253
+ p.nextToken();
254
+ break;
255
+ case TokenType.Integer:
256
+ fvalue.v = Variant.fromInteger(p.asInteger);
257
+ p.nextToken();
258
+ break;
259
+ case TokenType.Double:
260
+ fvalue.v = Variant.fromDouble(p.asDouble);
261
+ p.nextToken();
262
+ break;
263
+ case TokenType.Decimal:
264
+ fvalue.v = Variant.fromDecimal(p.asDecimal);
265
+ p.nextToken();
266
+ break;
267
+ case TokenType.Operator:
268
+ // C# parity: an operator here leaves the value as it is and consumes nothing.
269
+ break;
270
+ default:
271
+ throw this.error(449);
272
+ }
273
+ }
274
+ parentesis(fvalue) {
275
+ const p = this.fParser;
276
+ if (p.currentToken() === TokenType.Operator) {
277
+ let aoperator = p.tokenString();
278
+ if (aoperator === '(') {
279
+ p.nextToken();
280
+ this.separator(fvalue);
281
+ aoperator = '';
282
+ if (p.currentToken() === TokenType.Operator)
283
+ aoperator = p.tokenString();
284
+ if (aoperator !== ')')
285
+ throw this.error(441);
286
+ p.nextToken();
287
+ }
288
+ else
289
+ this.operand(fvalue);
290
+ }
291
+ else
292
+ this.operand(fvalue);
293
+ }
294
+ executeIIF(fvalue) {
295
+ const p = this.fParser;
296
+ const auxiliar = new ValueRef();
297
+ let aoperator = '';
298
+ if (p.currentToken() === TokenType.Operator)
299
+ aoperator = p.tokenString();
300
+ if (aoperator !== '(')
301
+ throw this.error(441);
302
+ p.nextToken();
303
+ this.separator(fvalue);
304
+ if (fvalue.v.isNull)
305
+ fvalue.v = Variant.fromBoolean(false);
306
+ if (fvalue.v.varType !== VariantType.Boolean && !this.fChecking)
307
+ throw this.error(438);
308
+ aoperator = '';
309
+ if (p.currentToken() === TokenType.Operator)
310
+ aoperator = p.tokenString();
311
+ if (aoperator !== ',')
312
+ throw this.error(449);
313
+ p.nextToken();
314
+ if (this.fChecking) {
315
+ this.separator(fvalue);
316
+ aoperator = '';
317
+ if (p.currentToken() === TokenType.Operator)
318
+ aoperator = p.tokenString();
319
+ if (aoperator !== ',')
320
+ throw this.error(449);
321
+ p.nextToken();
322
+ this.separator(auxiliar);
323
+ }
324
+ else {
325
+ let oldFChecking;
326
+ if (fvalue.v.toBool(this.context)) {
327
+ this.separator(fvalue);
328
+ aoperator = '';
329
+ if (p.currentToken() === TokenType.Operator)
330
+ aoperator = p.tokenString();
331
+ if (aoperator !== ',')
332
+ throw this.error(449);
333
+ p.nextToken();
334
+ oldFChecking = this.fChecking;
335
+ this.fChecking = true;
336
+ this.separator(auxiliar);
337
+ this.fChecking = oldFChecking;
338
+ }
339
+ else {
340
+ oldFChecking = this.fChecking;
341
+ this.fChecking = true;
342
+ this.separator(auxiliar);
343
+ aoperator = '';
344
+ if (p.currentToken() === TokenType.Operator)
345
+ aoperator = p.tokenString();
346
+ if (aoperator !== ',')
347
+ throw this.error(449);
348
+ p.nextToken();
349
+ this.fChecking = oldFChecking;
350
+ this.separator(fvalue);
351
+ }
352
+ }
353
+ aoperator = '';
354
+ if (p.currentToken() === TokenType.Operator)
355
+ aoperator = p.tokenString();
356
+ if (aoperator !== ')')
357
+ throw this.error(441);
358
+ p.nextToken();
359
+ }
360
+ /** Unary operators (-, +, NOT), IIF and function calls. */
361
+ dosign(fvalue) {
362
+ const p = this.fParser;
363
+ let aoperator = '';
364
+ let iden = null;
365
+ let idenf = null;
366
+ if (p.currentToken() === TokenType.Operator) {
367
+ aoperator = p.tokenString();
368
+ if (aoperator === '-' || aoperator === '+' || aoperator === 'NOT' || aoperator === 'IIF')
369
+ p.nextToken();
370
+ }
371
+ else if (p.currentToken() === TokenType.Symbol) {
372
+ iden = this.searchIdentifier(p.tokenString());
373
+ if (iden === null)
374
+ throw new EvalException(this.unknownIdentifier(p.tokenString()), p.sourceLine, p.sourcePos, '');
375
+ if (iden instanceof IdenFunction) {
376
+ idenf = iden;
377
+ if (idenf.paramCount > 0) {
378
+ p.nextToken();
379
+ if (p.currentToken() !== TokenType.Operator)
380
+ throw this.error(441);
381
+ if (p.tokenString() !== '(')
382
+ throw this.error(441);
383
+ }
384
+ for (let i = 0; i < idenf.paramCount; i++) {
385
+ p.nextToken();
386
+ this.separator(new ParamRef(idenf.params, i));
387
+ if (idenf.paramCount > i + 1) {
388
+ aoperator = '';
389
+ if (p.currentToken() === TokenType.Operator)
390
+ aoperator = p.tokenString();
391
+ if (aoperator !== ',') {
392
+ throw new EvalException(compositeFormat(this.T(448), [',']), p.sourceLine, p.sourcePos, '');
393
+ }
394
+ }
395
+ }
396
+ if (idenf.paramCount > 0) {
397
+ if (p.currentToken() !== TokenType.Operator)
398
+ throw this.error(441);
399
+ if (p.tokenString() !== ')')
400
+ throw this.error(441);
401
+ p.nextToken();
402
+ }
403
+ }
404
+ else
405
+ iden = null;
406
+ aoperator = '';
407
+ }
408
+ if (iden === null) {
409
+ if (aoperator === 'IIF')
410
+ this.executeIIF(fvalue);
411
+ else
412
+ this.parentesis(fvalue);
413
+ }
414
+ else if (idenf.paramCount < 1)
415
+ p.nextToken();
416
+ if (iden !== null) {
417
+ if (!this.fChecking)
418
+ fvalue.v = idenf.value;
419
+ else
420
+ fvalue.v = Variant.null;
421
+ }
422
+ else if (aoperator === '-') {
423
+ if (!this.fChecking)
424
+ fvalue.v = Variant.negate(fvalue.v, this.context);
425
+ }
426
+ else if (aoperator === 'NOT') {
427
+ if (!this.fChecking)
428
+ fvalue.v = Variant.fromBoolean(!fvalue.v.toBool(this.context));
429
+ }
430
+ }
431
+ mulDiv(fvalue) {
432
+ const p = this.fParser;
433
+ const auxiliar = new ValueRef();
434
+ this.dosign(fvalue);
435
+ while (p.currentToken() === TokenType.Operator) {
436
+ const aoperator = p.tokenString();
437
+ if (aoperator === '*') {
438
+ p.nextToken();
439
+ const auxiliar2 = fvalue.v;
440
+ this.dosign(auxiliar);
441
+ if (!this.fChecking)
442
+ fvalue.v = Variant.mul(auxiliar2, auxiliar.v, this.context);
443
+ }
444
+ else if (aoperator === '/') {
445
+ p.nextToken();
446
+ const auxiliar2 = fvalue.v;
447
+ this.dosign(auxiliar);
448
+ if (!this.fChecking)
449
+ fvalue.v = Variant.div(auxiliar2, auxiliar.v, this.context);
450
+ }
451
+ else
452
+ break;
453
+ }
454
+ }
455
+ sumDiff(fvalue) {
456
+ const p = this.fParser;
457
+ const auxiliar = new ValueRef();
458
+ this.mulDiv(fvalue);
459
+ while (p.currentToken() === TokenType.Operator) {
460
+ const aoperator = p.tokenString();
461
+ if (aoperator === '+') {
462
+ p.nextToken();
463
+ const auxiliar2 = fvalue.v;
464
+ this.mulDiv(auxiliar);
465
+ // C# parity: computed also while checking (no FChecking test around the +).
466
+ fvalue.v = Variant.expressionSum(auxiliar2, auxiliar.v, this.context);
467
+ }
468
+ else if (aoperator === '-') {
469
+ p.nextToken();
470
+ const auxiliar2 = fvalue.v;
471
+ this.mulDiv(auxiliar);
472
+ fvalue.v = Variant.sub(auxiliar2, auxiliar.v, this.context);
473
+ }
474
+ else
475
+ break;
476
+ }
477
+ }
478
+ comparations(fvalue) {
479
+ const p = this.fParser;
480
+ const auxiliar = new ValueRef();
481
+ const ctx = this.context;
482
+ this.sumDiff(fvalue);
483
+ while (p.currentToken() === TokenType.Operator) {
484
+ const aoperator = p.tokenString();
485
+ let op = null;
486
+ if (aoperator === '<>' || aoperator === '><')
487
+ op = (a, b) => Variant.notEquals(a, b, ctx);
488
+ else if (aoperator === '==' || aoperator === '=')
489
+ op = (a, b) => Variant.equals(a, b, ctx);
490
+ else if (aoperator === '<')
491
+ op = (a, b) => Variant.lessThan(a, b, ctx);
492
+ else if (aoperator === '<=')
493
+ op = (a, b) => Variant.lessOrEqual(a, b, ctx);
494
+ else if (aoperator === '>')
495
+ op = (a, b) => Variant.greaterThan(a, b, ctx);
496
+ else if (aoperator === '>=')
497
+ op = (a, b) => Variant.greaterOrEqual(a, b, ctx);
498
+ else
499
+ break;
500
+ p.nextToken();
501
+ let auxiliar2 = fvalue.v;
502
+ this.sumDiff(auxiliar);
503
+ if (!this.fChecking) {
504
+ let right = auxiliar.v;
505
+ [auxiliar2, right] = coerceOperands(auxiliar2, right, ctx);
506
+ auxiliar.v = right;
507
+ fvalue.v = Variant.fromBoolean(op(auxiliar2, right));
508
+ }
509
+ }
510
+ }
511
+ logicalAND(fvalue) {
512
+ const p = this.fParser;
513
+ this.comparations(fvalue);
514
+ let aoperator = '';
515
+ if (p.currentToken() === TokenType.Operator)
516
+ aoperator = toUpperNet(p.tokenString());
517
+ while (aoperator === 'AND') {
518
+ const auxiliar = new ValueRef();
519
+ const auxiliar2 = fvalue.v;
520
+ p.nextToken();
521
+ this.comparations(auxiliar);
522
+ if (!this.fChecking)
523
+ fvalue.v = Variant.fromBoolean(auxiliar2.toBool(this.context) && auxiliar.v.toBool(this.context));
524
+ aoperator = '';
525
+ if (p.currentToken() === TokenType.Operator)
526
+ aoperator = toUpperNet(p.tokenString());
527
+ }
528
+ }
529
+ logicalOR(fvalue) {
530
+ const p = this.fParser;
531
+ this.logicalAND(fvalue);
532
+ let aoperator = '';
533
+ if (p.currentToken() === TokenType.Operator)
534
+ aoperator = toUpperNet(p.tokenString());
535
+ while (aoperator === 'OR') {
536
+ const auxiliar = new ValueRef();
537
+ const auxiliar2 = fvalue.v;
538
+ p.nextToken();
539
+ this.logicalAND(auxiliar);
540
+ if (!this.fChecking)
541
+ fvalue.v = Variant.fromBoolean(auxiliar2.toBool(this.context) || auxiliar.v.toBool(this.context));
542
+ aoperator = '';
543
+ if (p.currentToken() === TokenType.Operator)
544
+ aoperator = toUpperNet(p.tokenString());
545
+ }
546
+ }
547
+ variables(fvalue) {
548
+ const p = this.fParser;
549
+ if (p.currentToken() === TokenType.Symbol) {
550
+ const iden = this.searchIdentifier(p.tokenString());
551
+ if (iden === null) {
552
+ throw new EvalException(this.unknownIdentifier(p.tokenString()), p.sourceLine, p.sourcePos, p.tokenString());
553
+ }
554
+ if (iden instanceof IdenVariable) {
555
+ if (p.isAssignment()) {
556
+ p.nextToken();
557
+ p.nextToken();
558
+ this.separator(fvalue);
559
+ // C# parity: the separator above evaluates the rest of the statement list before the
560
+ // assignment, so "X := 5; X + 1" assigns X + 1 computed with the old X, not 5.
561
+ if (!this.fChecking)
562
+ iden.value = fvalue.v;
563
+ let aoperator = '';
564
+ if (p.currentToken() === TokenType.Operator)
565
+ aoperator = p.tokenString();
566
+ if (aoperator === ';')
567
+ this.separator(fvalue);
568
+ }
569
+ else
570
+ this.logicalOR(fvalue);
571
+ }
572
+ else
573
+ this.logicalOR(fvalue);
574
+ }
575
+ else
576
+ this.logicalOR(fvalue);
577
+ }
578
+ separator(fvalue) {
579
+ const p = this.fParser;
580
+ let aoperator = '';
581
+ if (p.currentToken() === TokenType.Operator)
582
+ aoperator = p.tokenString();
583
+ if (aoperator === ';') {
584
+ while (aoperator === ';') {
585
+ p.nextToken();
586
+ if (p.currentToken() !== TokenType.Eof)
587
+ this.variables(fvalue);
588
+ aoperator = '';
589
+ if (p.currentToken() === TokenType.Operator)
590
+ aoperator = p.tokenString();
591
+ }
592
+ }
593
+ else {
594
+ this.variables(fvalue);
595
+ aoperator = '';
596
+ if (p.currentToken() === TokenType.Operator)
597
+ aoperator = p.tokenString();
598
+ while (aoperator === ';') {
599
+ p.nextToken();
600
+ if (p.currentToken() !== TokenType.Eof)
601
+ this.variables(fvalue);
602
+ aoperator = '';
603
+ if (p.currentToken() === TokenType.Operator)
604
+ aoperator = p.tokenString();
605
+ }
606
+ }
607
+ }
608
+ evaluateExpression() {
609
+ const fpartial = new ValueRef();
610
+ try {
611
+ this.fEvaluating = true;
612
+ try {
613
+ this.separator(fpartial);
614
+ }
615
+ finally {
616
+ this.fEvaluating = false;
617
+ }
618
+ if (this.fParser.currentToken() !== TokenType.Eof)
619
+ throw this.error(449);
620
+ }
621
+ catch (e) {
622
+ if (e instanceof EvalException)
623
+ throw e;
624
+ throw new EvalException(messageOf(e), this.fParser.sourceLine, this.fParser.sourcePos, '');
625
+ }
626
+ return fpartial.v;
627
+ }
628
+ /** EvaluateText: evaluates a text (in a nested evaluator sharing the identifiers when evaluating). */
629
+ evaluateText(text) {
630
+ if (this.fEvaluating) {
631
+ const nested = new Evaluator({ context: this.context, host: this.host, language: this.language }, false);
632
+ nested.aliasList = this.aliasList;
633
+ nested.fIdentifierList = this.fIdentifierList;
634
+ nested.expression = text;
635
+ nested.evaluate();
636
+ return nested.result;
637
+ }
638
+ this.expression = text;
639
+ this.evaluate();
640
+ return this.result;
641
+ }
642
+ /** Evaluate: the value of the expression (True for an empty one). */
643
+ evaluate() {
644
+ this.fParser.expression = this.fExpression;
645
+ this.fChecking = false;
646
+ if (this.fParser.currentToken() === TokenType.Eof)
647
+ this.fResult = Variant.fromBoolean(true);
648
+ else
649
+ this.fResult = this.evaluateExpression();
650
+ return this.fResult;
651
+ }
652
+ }
653
+ /**
654
+ * Evaluator.CoerceOperands: VariantCoercion.Coercion when the types differ, but a date and a number,
655
+ * which Variant.compare compares as numbers (as Delphi compares a TDateTime).
656
+ */
657
+ function coerceOperands(a, b, ctx) {
658
+ if ((a.varType === VariantType.DateTime && b.isNumber()) || (b.varType === VariantType.DateTime && a.isNumber()))
659
+ return [a, b];
660
+ if (a.varType !== b.varType) {
661
+ const c = coercionOf(a, b, ctx);
662
+ if (c !== null)
663
+ return c;
664
+ }
665
+ return [a, b];
666
+ }
667
+ //# sourceMappingURL=evaluator.js.map