@vaadin-component-factory/vcf-pdf-viewer 0.9.0 → 1.0.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.
Files changed (172) hide show
  1. package/README.md +1 -1
  2. package/package.json +42 -26
  3. package/{src/display → pdfjs/dist}/display_utils.js +344 -139
  4. package/{src/display → pdfjs/dist}/fetch_stream.js +115 -97
  5. package/pdfjs/dist/l10n_utils.js +140 -0
  6. package/{src/shared → pdfjs/dist}/message_handler.js +243 -259
  7. package/{src/display → pdfjs/dist}/network.js +149 -87
  8. package/{src/display/content_disposition.js → pdfjs/dist/network_utils.js} +167 -55
  9. package/{src/display → pdfjs/dist}/node_stream.js +133 -98
  10. package/pdfjs/dist/pdf.js +12778 -0
  11. package/pdfjs/dist/pdf_link_service.js +638 -0
  12. package/pdfjs/dist/pdf_rendering_queue.js +199 -0
  13. package/pdfjs/dist/pdf_thumbnail_viewer.js +819 -0
  14. package/pdfjs/dist/pdf_viewer.js +3598 -0
  15. package/pdfjs/dist/ui_utils.js +1033 -0
  16. package/{src/shared → pdfjs/dist}/util.js +301 -287
  17. package/pdfjs/dist/worker.js +62813 -0
  18. package/src/vcf-pdf-viewer.js +98 -46
  19. package/theme/lumo/vcf-pdf-viewer-styles.js +4 -4
  20. package/theme/material/vcf-pdf-viewer-styles.js +4 -4
  21. package/theme/material/vcf-pdf-viewer.js +2 -2
  22. package/src/core/.eslintrc +0 -13
  23. package/src/core/annotation.js +0 -2948
  24. package/src/core/arithmetic_decoder.js +0 -182
  25. package/src/core/ascii_85_stream.js +0 -98
  26. package/src/core/ascii_hex_stream.js +0 -79
  27. package/src/core/base_stream.js +0 -110
  28. package/src/core/bidi.js +0 -438
  29. package/src/core/calibri_factors.js +0 -308
  30. package/src/core/catalog.js +0 -1459
  31. package/src/core/ccitt.js +0 -1062
  32. package/src/core/ccitt_stream.js +0 -60
  33. package/src/core/cff_font.js +0 -116
  34. package/src/core/cff_parser.js +0 -1949
  35. package/src/core/charsets.js +0 -119
  36. package/src/core/chunked_stream.js +0 -557
  37. package/src/core/cmap.js +0 -1039
  38. package/src/core/colorspace.js +0 -1533
  39. package/src/core/core_utils.js +0 -464
  40. package/src/core/crypto.js +0 -1900
  41. package/src/core/decode_stream.js +0 -170
  42. package/src/core/decrypt_stream.js +0 -59
  43. package/src/core/default_appearance.js +0 -99
  44. package/src/core/document.js +0 -1456
  45. package/src/core/encodings.js +0 -301
  46. package/src/core/evaluator.js +0 -4601
  47. package/src/core/file_spec.js +0 -108
  48. package/src/core/flate_stream.js +0 -402
  49. package/src/core/font_renderer.js +0 -882
  50. package/src/core/fonts.js +0 -3260
  51. package/src/core/fonts_utils.js +0 -221
  52. package/src/core/function.js +0 -1257
  53. package/src/core/glyf.js +0 -706
  54. package/src/core/glyphlist.js +0 -4558
  55. package/src/core/helvetica_factors.js +0 -353
  56. package/src/core/image.js +0 -802
  57. package/src/core/image_utils.js +0 -291
  58. package/src/core/jbig2.js +0 -2572
  59. package/src/core/jbig2_stream.js +0 -73
  60. package/src/core/jpeg_stream.js +0 -105
  61. package/src/core/jpg.js +0 -1416
  62. package/src/core/jpx.js +0 -2343
  63. package/src/core/jpx_stream.js +0 -87
  64. package/src/core/liberationsans_widths.js +0 -221
  65. package/src/core/lzw_stream.js +0 -150
  66. package/src/core/metadata_parser.js +0 -146
  67. package/src/core/metrics.js +0 -2970
  68. package/src/core/murmurhash3.js +0 -139
  69. package/src/core/myriadpro_factors.js +0 -290
  70. package/src/core/name_number_tree.js +0 -153
  71. package/src/core/object_loader.js +0 -149
  72. package/src/core/opentype_file_builder.js +0 -154
  73. package/src/core/operator_list.js +0 -734
  74. package/src/core/parser.js +0 -1416
  75. package/src/core/pattern.js +0 -985
  76. package/src/core/pdf_manager.js +0 -217
  77. package/src/core/predictor_stream.js +0 -238
  78. package/src/core/primitives.js +0 -402
  79. package/src/core/ps_parser.js +0 -272
  80. package/src/core/run_length_stream.js +0 -61
  81. package/src/core/segoeui_factors.js +0 -308
  82. package/src/core/standard_fonts.js +0 -817
  83. package/src/core/stream.js +0 -103
  84. package/src/core/struct_tree.js +0 -335
  85. package/src/core/to_unicode_map.js +0 -103
  86. package/src/core/type1_font.js +0 -421
  87. package/src/core/type1_parser.js +0 -776
  88. package/src/core/unicode.js +0 -1649
  89. package/src/core/worker.js +0 -848
  90. package/src/core/worker_stream.js +0 -135
  91. package/src/core/writer.js +0 -278
  92. package/src/core/xfa/bind.js +0 -652
  93. package/src/core/xfa/builder.js +0 -207
  94. package/src/core/xfa/config.js +0 -1926
  95. package/src/core/xfa/connection_set.js +0 -202
  96. package/src/core/xfa/data.js +0 -82
  97. package/src/core/xfa/datasets.js +0 -76
  98. package/src/core/xfa/factory.js +0 -111
  99. package/src/core/xfa/fonts.js +0 -181
  100. package/src/core/xfa/formcalc_lexer.js +0 -385
  101. package/src/core/xfa/formcalc_parser.js +0 -1340
  102. package/src/core/xfa/html_utils.js +0 -639
  103. package/src/core/xfa/layout.js +0 -383
  104. package/src/core/xfa/locale_set.js +0 -345
  105. package/src/core/xfa/namespaces.js +0 -81
  106. package/src/core/xfa/parser.js +0 -184
  107. package/src/core/xfa/setup.js +0 -38
  108. package/src/core/xfa/signature.js +0 -40
  109. package/src/core/xfa/som.js +0 -338
  110. package/src/core/xfa/stylesheet.js +0 -40
  111. package/src/core/xfa/template.js +0 -6260
  112. package/src/core/xfa/text.js +0 -290
  113. package/src/core/xfa/unknown.js +0 -29
  114. package/src/core/xfa/utils.js +0 -217
  115. package/src/core/xfa/xdp.js +0 -59
  116. package/src/core/xfa/xfa_object.js +0 -1130
  117. package/src/core/xfa/xhtml.js +0 -543
  118. package/src/core/xfa_fonts.js +0 -208
  119. package/src/core/xml_parser.js +0 -507
  120. package/src/core/xref.js +0 -899
  121. package/src/display/annotation_layer.js +0 -2107
  122. package/src/display/annotation_storage.js +0 -113
  123. package/src/display/api.js +0 -3292
  124. package/src/display/base_factory.js +0 -180
  125. package/src/display/canvas.js +0 -2828
  126. package/src/display/font_loader.js +0 -484
  127. package/src/display/metadata.js +0 -41
  128. package/src/display/network_utils.js +0 -100
  129. package/src/display/node_utils.js +0 -83
  130. package/src/display/optional_content_config.js +0 -189
  131. package/src/display/pattern_helper.js +0 -659
  132. package/src/display/svg.js +0 -1709
  133. package/src/display/text_layer.js +0 -847
  134. package/src/display/transport_stream.js +0 -303
  135. package/src/display/worker_options.js +0 -40
  136. package/src/display/xfa_layer.js +0 -204
  137. package/src/doc_helper.js +0 -25
  138. package/src/images/logo.svg +0 -41
  139. package/src/interfaces.js +0 -169
  140. package/src/license_header.js +0 -14
  141. package/src/license_header_libre.js +0 -21
  142. package/src/pdf.image_decoders.js +0 -46
  143. package/src/pdf.js +0 -146
  144. package/src/pdf.sandbox.external.js +0 -181
  145. package/src/pdf.sandbox.js +0 -151
  146. package/src/pdf.scripting.js +0 -25
  147. package/src/pdf.worker.entry.js +0 -19
  148. package/src/pdf.worker.js +0 -23
  149. package/src/scripting_api/aform.js +0 -608
  150. package/src/scripting_api/app.js +0 -621
  151. package/src/scripting_api/color.js +0 -129
  152. package/src/scripting_api/common.js +0 -58
  153. package/src/scripting_api/console.js +0 -38
  154. package/src/scripting_api/constants.js +0 -208
  155. package/src/scripting_api/doc.js +0 -1195
  156. package/src/scripting_api/error.js +0 -23
  157. package/src/scripting_api/event.js +0 -232
  158. package/src/scripting_api/field.js +0 -620
  159. package/src/scripting_api/fullscreen.js +0 -145
  160. package/src/scripting_api/initialization.js +0 -223
  161. package/src/scripting_api/pdf_object.js +0 -24
  162. package/src/scripting_api/print_params.js +0 -146
  163. package/src/scripting_api/proxy.js +0 -139
  164. package/src/scripting_api/thermometer.js +0 -69
  165. package/src/scripting_api/util.js +0 -581
  166. package/src/shared/.eslintrc +0 -13
  167. package/src/shared/cffStandardStrings.js +0 -311
  168. package/src/shared/compatibility.js +0 -114
  169. package/src/shared/fonts_utils.js +0 -429
  170. package/src/shared/is_node.js +0 -27
  171. package/src/shared/scripting_utils.js +0 -85
  172. package/src/worker_loader.js +0 -32
@@ -1,1340 +0,0 @@
1
- /* Copyright 2021 Mozilla Foundation
2
- *
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
- */
15
-
16
- import { Lexer, TOKEN } from "./formcalc_lexer.js";
17
-
18
- const Errors = {
19
- assignment: "Invalid token in assignment.",
20
- block: "Invalid token in do ... end declaration.",
21
- elseif: "Invalid elseif declaration.",
22
- for: "Invalid token in for ... endfor declaration.",
23
- foreach: "Invalid token in foreach ... endfor declaration.",
24
- func: "Invalid token in func declaration.",
25
- if: "Invalid token if ... endif declaration.",
26
- index: "Invalid token in index.",
27
- params: "Invalid token in parameter list.",
28
- var: "Invalid token in var declaration.",
29
- while: "Invalid token while ... endwhile declaration.",
30
- };
31
-
32
- const BUILTINS = new Set([
33
- // Arithmetic.
34
- "abs",
35
- "avg",
36
- "ceil",
37
- "count",
38
- "floor",
39
- "max",
40
- "min",
41
- "mod",
42
- "round",
43
- "sum",
44
- // Date and time.
45
- "date",
46
- "date2num",
47
- "datefmt",
48
- "isodate2num",
49
- "isotime2num",
50
- "localdatefmt",
51
- "localtimefmt",
52
- "num2date",
53
- "num2gmtime",
54
- "num2time",
55
- "time",
56
- "time2num",
57
- "timefmt",
58
- // Financial.
59
- "apr",
60
- "cterm",
61
- "fv",
62
- "ipmt",
63
- "npv",
64
- "pmt",
65
- "ppmt",
66
- "pv",
67
- "rate",
68
- "term",
69
- // Logical.
70
- "choose",
71
- "exists",
72
- "hasvalue",
73
- "oneof",
74
- "within",
75
- // String.
76
- "at",
77
- "concat",
78
- "decode",
79
- "encode",
80
- "format",
81
- "left",
82
- "len",
83
- "lower",
84
- "ltrim",
85
- "parse",
86
- "replace",
87
- "right",
88
- "rtrim",
89
- "space",
90
- "str",
91
- "stuff",
92
- "substr",
93
- "uuid",
94
- "upper",
95
- "wordnum",
96
- // Url.
97
- "get",
98
- "post",
99
- "put",
100
- // Miscellaneous.
101
- "eval",
102
- "ref",
103
- "unitvalue",
104
- "unittype",
105
- // Undocumented.
106
- "acos",
107
- "asin",
108
- "atan",
109
- "cos",
110
- "deg2rad",
111
- "exp",
112
- "log",
113
- "pi",
114
- "pow",
115
- "rad2deg",
116
- "sin",
117
- "sqrt",
118
- "tan",
119
- ]);
120
-
121
- const LTR = true;
122
- const RTL = false;
123
-
124
- const Operators = {
125
- dot: { id: 0, prec: 0, assoc: RTL, nargs: 0, repr: "." },
126
- dotDot: { id: 1, prec: 0, assoc: RTL, nargs: 0, repr: ".." },
127
- dotHash: { id: 2, prec: 0, assoc: RTL, nargs: 0, repr: ".#" },
128
-
129
- call: { id: 1, prec: 1, assoc: LTR, nargs: 0 },
130
-
131
- // Unary operators.
132
- minus: { id: 4, nargs: 1, prec: 2, assoc: RTL, repr: "-", op: x => -x },
133
- plus: { id: 5, nargs: 1, prec: 2, assoc: RTL, repr: "+", op: x => +x },
134
- not: {
135
- id: 6,
136
- nargs: 1,
137
- prec: 2,
138
- assoc: RTL,
139
- repr: "!",
140
- op: x => (!x ? 1 : 0),
141
- },
142
-
143
- mul: { id: 7, nargs: 2, prec: 3, assoc: LTR, repr: "*", op: (x, y) => x * y },
144
- div: { id: 8, nargs: 2, prec: 3, assoc: LTR, repr: "/", op: (x, y) => x / y },
145
-
146
- add: { id: 9, nargs: 2, prec: 4, assoc: LTR, repr: "+", op: (x, y) => x + y },
147
- sub: {
148
- id: 10,
149
- nargs: 2,
150
- prec: 4,
151
- assoc: LTR,
152
- repr: "-",
153
- op: (x, y) => x - y,
154
- },
155
-
156
- lt: {
157
- id: 11,
158
- nargs: 2,
159
- prec: 5,
160
- assoc: LTR,
161
- repr: "<",
162
- op: (x, y) => (x < y ? 1 : 0),
163
- },
164
- le: {
165
- id: 12,
166
- nargs: 2,
167
- prec: 5,
168
- assoc: LTR,
169
- repr: "<=",
170
- op: (x, y) => (x <= y ? 1 : 0),
171
- },
172
- gt: {
173
- id: 13,
174
- nargs: 2,
175
- prec: 5,
176
- assoc: LTR,
177
- repr: ">",
178
- op: (x, y) => (x > y ? 1 : 0),
179
- },
180
- ge: {
181
- id: 14,
182
- nargs: 2,
183
- prec: 5,
184
- assoc: LTR,
185
- repr: ">=",
186
- op: (x, y) => (x >= y ? 1 : 0),
187
- },
188
-
189
- eq: {
190
- id: 15,
191
- nargs: 2,
192
- prec: 6,
193
- assoc: LTR,
194
- repr: "===",
195
- op: (x, y) => (x === y ? 1 : 0),
196
- },
197
- ne: {
198
- id: 16,
199
- nargs: 2,
200
- prec: 6,
201
- assoc: LTR,
202
- repr: "!==",
203
- op: (x, y) => (x !== y ? 1 : 0),
204
- },
205
-
206
- and: {
207
- id: 17,
208
- nargs: 2,
209
- prec: 7,
210
- assoc: LTR,
211
- repr: "&&",
212
- op: (x, y) => (x && y ? 1 : 0),
213
- },
214
-
215
- or: {
216
- id: 18,
217
- nargs: 2,
218
- prec: 8,
219
- assoc: LTR,
220
- repr: "||",
221
- op: (x, y) => (x || y ? 1 : 0),
222
- },
223
-
224
- // Not real operators.
225
- paren: { id: 19, prec: 9, assoc: RTL, nargs: 0 },
226
- subscript: { id: 20, prec: 9, assoc: RTL, nargs: 0 },
227
- };
228
-
229
- const OPERATOR = true;
230
- const OPERAND = false;
231
-
232
- // How it works...
233
- //
234
- // There is two stacks: one for operands and one for operators.
235
- // Each time an operand is met (number, identifier, ...),
236
- // it's pushed on operands stack.
237
- // Unary operators such as + or - are guessed according to the last pushed
238
- // thing:
239
- // for example, if an operand has been push then a '-' is a subtraction
240
- // but if an operator has been push (e.g. '*') then a '-' is the negate
241
- // operation ('... * - ...' can't be a subtraction).
242
- // Each time an operator is met its precedence is compared with the one of the
243
- // operator on top of operators stack:
244
- // - if top has precendence on operator then top is applied to the operands
245
- // on their stack;
246
- // - else just push the operator.
247
- // For example: 1 + 2 * 3
248
- // round 1: operands: [1], operators: []
249
- // round 2: operands: [1], operators: [+]
250
- // round 3: operands: [1, 2], operators: [+]
251
- //
252
- // + has not the precedence on *
253
- // round 4: operands: [1, 2], operators: [+, *]
254
- // round 5: operands: [1, 2, 3], operators: [+, *]
255
- // no more token: apply operators on operands:
256
- // round 6: operands: [1, 6], operators: [+]
257
- // round 7: operands: [7], operators: []
258
- // Parenthesis are treated like an operator with no precedence on the real ones.
259
- // As a consequence, any operation is done before this fake one and when
260
- // a right parenthesis is met then we can apply operators to operands
261
- // until the opening parenthesis is met.
262
- //
263
- class SimpleExprParser {
264
- constructor(lexer) {
265
- this.lexer = lexer;
266
- this.operands = [];
267
- this.operators = [];
268
- this.last = OPERATOR;
269
- }
270
-
271
- reset() {
272
- this.operands.length = 0;
273
- this.operators.length = 0;
274
- this.last = OPERATOR;
275
- }
276
-
277
- parse(tok) {
278
- tok = tok || this.lexer.next();
279
-
280
- while (true) {
281
- // Token ids (see form_lexer.js) are consecutive in order
282
- // to have switch table with no holes.
283
- switch (tok.id) {
284
- case TOKEN.and:
285
- if (this.last === OPERAND) {
286
- this.pushOperator(Operators.and);
287
- break;
288
- }
289
- return [tok, this.getNode()];
290
- case TOKEN.divide:
291
- if (this.last === OPERAND) {
292
- this.pushOperator(Operators.div);
293
- break;
294
- }
295
- return [tok, this.getNode()];
296
- case TOKEN.dot:
297
- if (this.last === OPERAND) {
298
- this.pushOperator(Operators.dot);
299
- break;
300
- }
301
- return [tok, this.getNode()];
302
- case TOKEN.dotDot:
303
- if (this.last === OPERAND) {
304
- this.pushOperator(Operators.dotDot);
305
- break;
306
- }
307
- return [tok, this.getNode()];
308
- case TOKEN.dotHash:
309
- if (this.last === OPERAND) {
310
- this.pushOperator(Operators.dotHash);
311
- break;
312
- }
313
- return [tok, this.getNode()];
314
- case TOKEN.dotStar:
315
- if (this.last === OPERAND) {
316
- this.pushOperator(Operators.dot);
317
- this.pushOperand(new AstEveryOccurence());
318
- break;
319
- }
320
- return [tok, this.getNode()];
321
- case TOKEN.eq:
322
- if (this.last === OPERAND) {
323
- this.pushOperator(Operators.eq);
324
- break;
325
- }
326
- return [tok, this.getNode()];
327
- case TOKEN.ge:
328
- if (this.last === OPERAND) {
329
- this.pushOperator(Operators.ge);
330
- break;
331
- }
332
- return [tok, this.getNode()];
333
- case TOKEN.gt:
334
- if (this.last === OPERAND) {
335
- this.pushOperator(Operators.gt);
336
- break;
337
- }
338
- return [tok, this.getNode()];
339
- case TOKEN.le:
340
- if (this.last === OPERAND) {
341
- this.pushOperator(Operators.le);
342
- break;
343
- }
344
- return [tok, this.getNode()];
345
- case TOKEN.leftBracket:
346
- if (this.last === OPERAND) {
347
- this.flushWithOperator(Operators.subscript);
348
- const operand = this.operands.pop();
349
- const index = SimpleExprParser.parseIndex(this.lexer);
350
- this.operands.push(new AstSubscript(operand, index));
351
- this.last = OPERAND;
352
- break;
353
- }
354
- return [tok, this.getNode()];
355
- case TOKEN.leftParen:
356
- if (this.last === OPERAND) {
357
- const lastOperand = this.operands[this.operands.length - 1];
358
- if (!(lastOperand instanceof AstIdentifier)) {
359
- return [tok, this.getNode()];
360
- }
361
- lastOperand.toLowerCase();
362
- const name = lastOperand.id;
363
-
364
- this.flushWithOperator(Operators.call);
365
- const callee = this.operands.pop();
366
- const params = SimpleExprParser.parseParams(this.lexer);
367
-
368
- if (callee instanceof AstIdentifier && BUILTINS.has(name)) {
369
- this.operands.push(new AstBuiltinCall(name, params));
370
- } else {
371
- this.operands.push(new AstCall(callee, params));
372
- }
373
-
374
- this.last = OPERAND;
375
- } else {
376
- this.operators.push(Operators.paren);
377
- this.last = OPERATOR;
378
- }
379
- break;
380
- case TOKEN.lt:
381
- if (this.last === OPERAND) {
382
- this.pushOperator(Operators.lt);
383
- break;
384
- }
385
- return [tok, this.getNode()];
386
- case TOKEN.minus:
387
- if (this.last === OPERATOR) {
388
- this.pushOperator(Operators.minus);
389
- } else {
390
- this.pushOperator(Operators.sub);
391
- }
392
- break;
393
- case TOKEN.ne:
394
- if (this.last === OPERAND) {
395
- this.pushOperator(Operators.ne);
396
- break;
397
- }
398
- return [tok, this.getNode()];
399
- case TOKEN.not:
400
- if (this.last === OPERAND) {
401
- this.pushOperator(Operators.not);
402
- break;
403
- }
404
- return [tok, this.getNode()];
405
- case TOKEN.null:
406
- if (this.last === OPERATOR) {
407
- this.pushOperand(new AstNull());
408
- break;
409
- }
410
- return [tok, this.getNode()];
411
- case TOKEN.number:
412
- if (this.last === OPERATOR) {
413
- this.pushOperand(new AstNumber(tok.value));
414
- break;
415
- }
416
- return [tok, this.getNode()];
417
- case TOKEN.or:
418
- if (this.last === OPERAND) {
419
- this.pushOperator(Operators.or);
420
- break;
421
- }
422
- return [tok, this.getNode()];
423
- case TOKEN.plus:
424
- if (this.last === OPERATOR) {
425
- this.pushOperator(Operators.plus);
426
- } else {
427
- this.pushOperator(Operators.add);
428
- }
429
- break;
430
- case TOKEN.rightBracket:
431
- if (!this.flushUntil(Operators.subscript.id)) {
432
- return [tok, this.getNode()];
433
- }
434
- break;
435
- case TOKEN.rightParen:
436
- if (!this.flushUntil(Operators.paren.id)) {
437
- return [tok, this.getNode()];
438
- }
439
- break;
440
- case TOKEN.string:
441
- if (this.last === OPERATOR) {
442
- this.pushOperand(new AstString(tok.value));
443
- break;
444
- }
445
- return [tok, this.getNode()];
446
- case TOKEN.this:
447
- if (this.last === OPERATOR) {
448
- this.pushOperand(new AstThis());
449
- break;
450
- }
451
- return [tok, this.getNode()];
452
- case TOKEN.times:
453
- if (this.last === OPERAND) {
454
- this.pushOperator(Operators.mul);
455
- break;
456
- }
457
- return [tok, this.getNode()];
458
- case TOKEN.identifier:
459
- if (this.last === OPERATOR) {
460
- this.pushOperand(new AstIdentifier(tok.value));
461
- break;
462
- }
463
- return [tok, this.getNode()];
464
- default:
465
- return [tok, this.getNode()];
466
- }
467
- tok = this.lexer.next();
468
- }
469
- }
470
-
471
- static parseParams(lexer) {
472
- const parser = new SimpleExprParser(lexer);
473
- const params = [];
474
- while (true) {
475
- const [tok, param] = parser.parse();
476
- if (param) {
477
- params.push(param);
478
- }
479
- if (tok.id === TOKEN.rightParen) {
480
- return params;
481
- } else if (tok.id !== TOKEN.comma) {
482
- throw new Error(Errors.params);
483
- }
484
- parser.reset();
485
- }
486
- }
487
-
488
- static parseIndex(lexer) {
489
- let tok = lexer.next();
490
- if (tok.id === TOKEN.times) {
491
- tok = lexer.next();
492
- if (tok.id !== TOKEN.rightBracket) {
493
- throw new Error(Errors.index);
494
- }
495
- return new AstEveryOccurence();
496
- }
497
- const [token, expr] = new SimpleExprParser(lexer).parse(tok);
498
- if (token.id !== TOKEN.rightBracket) {
499
- throw new Error(Errors.index);
500
- }
501
- return expr;
502
- }
503
-
504
- pushOperator(op) {
505
- this.flushWithOperator(op);
506
- this.operators.push(op);
507
- this.last = OPERATOR;
508
- }
509
-
510
- pushOperand(op) {
511
- this.operands.push(op);
512
- this.last = OPERAND;
513
- }
514
-
515
- operate(op) {
516
- if (op.nargs === 1) {
517
- const arg = this.operands.pop();
518
- this.operands.push(AstUnaryOperator.getOperatorOrValue(op, arg));
519
- } else {
520
- const arg2 = this.operands.pop();
521
- const arg1 = this.operands.pop();
522
- this.operands.push(AstBinaryOperator.getOperatorOrValue(op, arg1, arg2));
523
- }
524
- }
525
-
526
- flushWithOperator(op) {
527
- while (true) {
528
- const top = this.operators[this.operators.length - 1];
529
- if (top) {
530
- if (top.id >= 0 && SimpleExprParser.checkPrecedence(top, op)) {
531
- this.operators.pop();
532
- this.operate(top);
533
- continue;
534
- }
535
- }
536
- return;
537
- }
538
- }
539
-
540
- flush() {
541
- while (true) {
542
- const op = this.operators.pop();
543
- if (!op) {
544
- return;
545
- }
546
- this.operate(op);
547
- }
548
- }
549
-
550
- flushUntil(id) {
551
- while (true) {
552
- const op = this.operators.pop();
553
- if (!op) {
554
- return false;
555
- }
556
- if (op.id === id) {
557
- return true;
558
- }
559
- this.operate(op);
560
- }
561
- }
562
-
563
- getNode() {
564
- this.flush();
565
- return this.operands.pop();
566
- }
567
-
568
- static checkPrecedence(left, right) {
569
- return (
570
- left.prec < right.prec || (left.prec === right.prec && left.assoc === LTR)
571
- );
572
- }
573
- }
574
-
575
- class Leaf {
576
- dump() {
577
- throw new Error("Not implemented method");
578
- }
579
-
580
- isSomPredicate() {
581
- return false;
582
- }
583
-
584
- isDotExpression() {
585
- return false;
586
- }
587
-
588
- isConstant() {
589
- return false;
590
- }
591
-
592
- toNumber() {
593
- return 0;
594
- }
595
-
596
- toComparable() {
597
- return null;
598
- }
599
- }
600
-
601
- class AstCall extends Leaf {
602
- constructor(callee, params) {
603
- super();
604
- this.callee = callee;
605
- this.params = params;
606
- }
607
-
608
- dump() {
609
- return {
610
- callee: this.callee.dump(),
611
- params: this.params.map(x => x.dump()),
612
- };
613
- }
614
- }
615
-
616
- class AstBuiltinCall extends Leaf {
617
- constructor(id, params) {
618
- super();
619
- this.id = id;
620
- this.params = params;
621
- }
622
-
623
- dump() {
624
- return {
625
- builtin: this.id,
626
- params: this.params.map(x => x.dump()),
627
- };
628
- }
629
- }
630
-
631
- class AstSubscript extends Leaf {
632
- constructor(operand, index) {
633
- super();
634
- this.operand = operand;
635
- this.index = index;
636
- }
637
-
638
- dump() {
639
- return {
640
- operand: this.operand.dump(),
641
- index: this.index.dump(),
642
- };
643
- }
644
- }
645
-
646
- class AstBinaryOperator extends Leaf {
647
- constructor(id, left, right, repr) {
648
- super();
649
- this.id = id;
650
- this.left = left;
651
- this.right = right;
652
- this.repr = repr;
653
- }
654
-
655
- dump() {
656
- return {
657
- operator: this.repr,
658
- left: this.left.dump(),
659
- right: this.right.dump(),
660
- };
661
- }
662
-
663
- isDotExpression() {
664
- return Operators.id.dot <= this.id && this.id <= Operators.id.dotHash;
665
- }
666
-
667
- isSomPredicate() {
668
- return (
669
- this.isDotExpression() ||
670
- (Operators.id.lt <= this.id &&
671
- this.id <= Operators.id.or &&
672
- ((this.left.isDotExpression() && this.right.isConstant()) ||
673
- (this.left.isConstant() && this.right.isDotExpression()) ||
674
- (this.left.isDotExpression() && this.right.isDotExpression())))
675
- );
676
- }
677
-
678
- static getOperatorOrValue(operator, left, right) {
679
- if (!left.isConstant() || !right.isConstant()) {
680
- return new AstBinaryOperator(operator.id, left, right, operator.repr);
681
- }
682
-
683
- if (
684
- Operators.lt.id <= operator.id &&
685
- operator.id <= Operators.ne.id &&
686
- !(left instanceof AstNumber) &&
687
- !(right instanceof AstNumber)
688
- ) {
689
- return new AstNumber(
690
- operator.op(left.toComparable(), right.toComparable())
691
- );
692
- }
693
-
694
- return new AstNumber(operator.op(left.toNumber(), right.toNumber()));
695
- }
696
- }
697
-
698
- class AstUnaryOperator extends Leaf {
699
- constructor(id, arg, repr) {
700
- super();
701
- this.id = id;
702
- this.arg = arg;
703
- this.repr = repr;
704
- }
705
-
706
- dump() {
707
- return {
708
- operator: this.repr,
709
- arg: this.arg.dump(),
710
- };
711
- }
712
-
713
- static getOperatorOrValue(operator, arg) {
714
- if (!arg.isConstant()) {
715
- return new AstUnaryOperator(operator.id, arg, operator.repr);
716
- }
717
-
718
- return new AstNumber(operator.op(arg.toNumber()));
719
- }
720
- }
721
-
722
- class AstNumber extends Leaf {
723
- constructor(number) {
724
- super();
725
- this.number = number;
726
- }
727
-
728
- dump() {
729
- return this.number;
730
- }
731
-
732
- isConstant() {
733
- return true;
734
- }
735
-
736
- toNumber() {
737
- return this.number;
738
- }
739
- }
740
-
741
- class AstString extends Leaf {
742
- constructor(str) {
743
- super();
744
- this.str = str;
745
- }
746
-
747
- dump() {
748
- return this.str;
749
- }
750
-
751
- isConstant() {
752
- return true;
753
- }
754
-
755
- toNumber() {
756
- return !isNaN(this.str) ? parseFloat(this.str) : 0;
757
- }
758
-
759
- toComparable() {
760
- return this.str;
761
- }
762
- }
763
-
764
- class AstThis extends Leaf {
765
- dump() {
766
- return { special: "this" };
767
- }
768
- }
769
-
770
- class AstIdentifier extends Leaf {
771
- constructor(id) {
772
- super();
773
- this.id = id;
774
- }
775
-
776
- dump() {
777
- return { id: this.id };
778
- }
779
-
780
- toLowerCase() {
781
- this.id = this.id.toLowerCase();
782
- }
783
- }
784
-
785
- class AstNull extends Leaf {
786
- dump() {
787
- return { special: null };
788
- }
789
-
790
- isConstant() {
791
- return true;
792
- }
793
-
794
- toComparable() {
795
- return null;
796
- }
797
- }
798
-
799
- class AstEveryOccurence {
800
- dump() {
801
- return { special: "*" };
802
- }
803
- }
804
-
805
- class VarDecl extends Leaf {
806
- constructor(id, expr) {
807
- super();
808
- this.id = id;
809
- this.expr = expr;
810
- }
811
-
812
- dump() {
813
- return {
814
- var: this.id,
815
- expr: this.expr.dump(),
816
- };
817
- }
818
- }
819
-
820
- class Assignment extends Leaf {
821
- constructor(id, expr) {
822
- super();
823
- this.id = id;
824
- this.expr = expr;
825
- }
826
-
827
- dump() {
828
- return {
829
- assignment: this.id,
830
- expr: this.expr.dump(),
831
- };
832
- }
833
- }
834
-
835
- class FuncDecl extends Leaf {
836
- constructor(id, params, body) {
837
- super();
838
- this.id = id;
839
- this.params = params;
840
- this.body = body;
841
- }
842
-
843
- dump() {
844
- return {
845
- func: this.id,
846
- params: this.params,
847
- body: this.body.dump(),
848
- };
849
- }
850
- }
851
-
852
- class IfDecl extends Leaf {
853
- constructor(condition, thenClause, elseIfClause, elseClause) {
854
- super();
855
- this.condition = condition;
856
- this.then = thenClause;
857
- this.elseif = elseIfClause;
858
- this.else = elseClause;
859
- }
860
-
861
- dump() {
862
- return {
863
- decl: "if",
864
- condition: this.condition.dump(),
865
- then: this.then.dump(),
866
- elseif: this.elseif ? this.elseif.map(x => x.dump()) : null,
867
- else: this.else ? this.else.dump() : null,
868
- };
869
- }
870
- }
871
-
872
- class ElseIfDecl extends Leaf {
873
- constructor(condition, thenClause) {
874
- super();
875
- this.condition = condition;
876
- this.then = thenClause;
877
- }
878
-
879
- dump() {
880
- return {
881
- decl: "elseif",
882
- condition: this.condition.dump(),
883
- then: this.then.dump(),
884
- };
885
- }
886
- }
887
-
888
- class WhileDecl extends Leaf {
889
- constructor(condition, whileClause) {
890
- super();
891
- this.condition = condition;
892
- this.body = whileClause;
893
- }
894
-
895
- dump() {
896
- return {
897
- decl: "while",
898
- condition: this.condition.dump(),
899
- body: this.body.dump(),
900
- };
901
- }
902
- }
903
-
904
- class ForDecl extends Leaf {
905
- constructor(assignment, upto, end, step, body) {
906
- super();
907
- this.assignment = assignment;
908
- this.upto = upto;
909
- this.end = end;
910
- this.step = step;
911
- this.body = body;
912
- }
913
-
914
- dump() {
915
- return {
916
- decl: "for",
917
- assignment: this.assignment.dump(),
918
- type: this.upto ? "upto" : "downto",
919
- end: this.end.dump(),
920
- step: this.step ? this.step.dump() : null,
921
- body: this.body.dump(),
922
- };
923
- }
924
- }
925
-
926
- class ForeachDecl extends Leaf {
927
- constructor(id, params, body) {
928
- super();
929
- this.id = id;
930
- this.params = params;
931
- this.body = body;
932
- }
933
-
934
- dump() {
935
- return {
936
- decl: "foreach",
937
- id: this.id,
938
- params: this.params.map(x => x.dump()),
939
- body: this.body.dump(),
940
- };
941
- }
942
- }
943
-
944
- class BlockDecl extends Leaf {
945
- constructor(body) {
946
- super();
947
- this.body = body;
948
- }
949
-
950
- dump() {
951
- return {
952
- decl: "block",
953
- body: this.body.dump(),
954
- };
955
- }
956
- }
957
-
958
- class ExprList extends Leaf {
959
- constructor(expressions) {
960
- super();
961
- this.expressions = expressions;
962
- }
963
-
964
- dump() {
965
- return this.expressions.map(x => x.dump());
966
- }
967
- }
968
-
969
- class BreakDecl extends Leaf {
970
- dump() {
971
- return { special: "break" };
972
- }
973
- }
974
-
975
- class ContinueDecl extends Leaf {
976
- dump() {
977
- return { special: "continue" };
978
- }
979
- }
980
-
981
- class Parser {
982
- constructor(code) {
983
- this.lexer = new Lexer(code);
984
- }
985
-
986
- parse() {
987
- const [tok, decls] = this.parseExprList();
988
- if (tok.id !== TOKEN.eof) {
989
- throw new Error("Invalid token in Form code");
990
- }
991
- return decls;
992
- }
993
-
994
- parseExprList() {
995
- const expressions = [];
996
- let tok = null,
997
- expr;
998
- while (true) {
999
- [tok, expr] = this.parseExpr(tok);
1000
- if (!expr) {
1001
- return [tok, new ExprList(expressions)];
1002
- }
1003
- expressions.push(expr);
1004
- }
1005
- }
1006
-
1007
- parseExpr(tok) {
1008
- tok = tok || this.lexer.next();
1009
- switch (tok.id) {
1010
- case TOKEN.identifier:
1011
- return this.parseAssigmentOrExpr(tok);
1012
- case TOKEN.break:
1013
- return [null, new BreakDecl()];
1014
- case TOKEN.continue:
1015
- return [null, new ContinueDecl()];
1016
- case TOKEN.do:
1017
- return this.parseBlock();
1018
- case TOKEN.for:
1019
- return this.parseFor();
1020
- case TOKEN.foreach:
1021
- return this.parseForeach();
1022
- case TOKEN.func:
1023
- return this.parseFuncDecl();
1024
- case TOKEN.if:
1025
- return this.parseIf();
1026
- case TOKEN.var:
1027
- return this.parseVarDecl();
1028
- case TOKEN.while:
1029
- return this.parseWhile();
1030
- default:
1031
- return this.parseSimpleExpr(tok);
1032
- }
1033
- }
1034
-
1035
- parseAssigmentOrExpr(tok) {
1036
- const savedTok = tok;
1037
-
1038
- tok = this.lexer.next();
1039
- if (tok.id === TOKEN.assign) {
1040
- const [tok1, expr] = this.parseSimpleExpr(null);
1041
- return [tok1, new Assignment(savedTok.value, expr)];
1042
- }
1043
-
1044
- const parser = new SimpleExprParser(this.lexer);
1045
- parser.pushOperand(new AstIdentifier(savedTok.value));
1046
-
1047
- return parser.parse(tok);
1048
- }
1049
-
1050
- parseBlock() {
1051
- const [tok1, body] = this.parseExprList();
1052
-
1053
- const tok = tok1 || this.lexer.next();
1054
- if (tok.id !== TOKEN.end) {
1055
- throw new Error(Errors.block);
1056
- }
1057
-
1058
- return [null, new BlockDecl(body)];
1059
- }
1060
-
1061
- parseVarDecl() {
1062
- // 'var' Identifier ('=' SimpleExpression)?
1063
- let tok = this.lexer.next();
1064
- if (tok.id !== TOKEN.identifier) {
1065
- throw new Error(Errors.var);
1066
- }
1067
-
1068
- const identifier = tok.value;
1069
-
1070
- tok = this.lexer.next();
1071
- if (tok.id !== TOKEN.assign) {
1072
- return [tok, new VarDecl(identifier, null)];
1073
- }
1074
-
1075
- const [tok1, expr] = this.parseSimpleExpr();
1076
- return [tok1, new VarDecl(identifier, expr)];
1077
- }
1078
-
1079
- parseFuncDecl() {
1080
- // 'func' Identifier ParameterList 'do' ExpressionList 'endfunc'.
1081
- let tok = this.lexer.next();
1082
- if (tok.id !== TOKEN.identifier) {
1083
- throw new Error(Errors.func);
1084
- }
1085
-
1086
- const identifier = tok.value;
1087
- const params = this.parseParamList();
1088
-
1089
- tok = this.lexer.next();
1090
- if (tok.id !== TOKEN.do) {
1091
- throw new Error(Errors.func);
1092
- }
1093
-
1094
- const [tok1, body] = this.parseExprList();
1095
-
1096
- tok = tok1 || this.lexer.next();
1097
- if (tok.id !== TOKEN.endfunc) {
1098
- throw new Error(Errors.func);
1099
- }
1100
-
1101
- return [null, new FuncDecl(identifier, params, body)];
1102
- }
1103
-
1104
- parseParamList() {
1105
- // '(' Identifier * ')'.
1106
- const params = [];
1107
-
1108
- let tok = this.lexer.next();
1109
- if (tok.id !== TOKEN.leftParen) {
1110
- throw new Error(Errors.func);
1111
- }
1112
-
1113
- tok = this.lexer.next();
1114
- if (tok.id === TOKEN.rightParen) {
1115
- return params;
1116
- }
1117
-
1118
- while (true) {
1119
- if (tok.id !== TOKEN.identifier) {
1120
- throw new Error(Errors.func);
1121
- }
1122
- params.push(tok.value);
1123
- tok = this.lexer.next();
1124
- if (tok.id === TOKEN.rightParen) {
1125
- return params;
1126
- }
1127
- if (tok.id !== TOKEN.comma) {
1128
- throw new Error(Errors.func);
1129
- }
1130
- tok = this.lexer.next();
1131
- }
1132
- }
1133
-
1134
- parseSimpleExpr(tok = null) {
1135
- return new SimpleExprParser(this.lexer).parse(tok);
1136
- }
1137
-
1138
- parseIf() {
1139
- // 'if' '(' SimpleExpression ')' then ExpressionList
1140
- // ('elseif' '(' SimpleExpression ')' then ExpressionList )*
1141
- // ('else' ExpressionList)?
1142
- // 'endif'.
1143
- let elseIfClause = [];
1144
- let tok = this.lexer.next();
1145
- if (tok.id !== TOKEN.leftParen) {
1146
- throw new Error(Errors.if);
1147
- }
1148
-
1149
- const [tok1, condition] = this.parseSimpleExpr();
1150
-
1151
- tok = tok1 || this.lexer.next();
1152
- if (tok.id !== TOKEN.rightParen) {
1153
- throw new Error(Errors.if);
1154
- }
1155
-
1156
- tok = this.lexer.next();
1157
- if (tok.id !== TOKEN.then) {
1158
- throw new Error(Errors.if);
1159
- }
1160
-
1161
- const [tok2, thenClause] = this.parseExprList();
1162
- tok = tok2 || this.lexer.next();
1163
-
1164
- while (tok.id === TOKEN.elseif) {
1165
- tok = this.lexer.next();
1166
- if (tok.id !== TOKEN.leftParen) {
1167
- throw new Error(Errors.elseif);
1168
- }
1169
-
1170
- const [tok3, elseIfCondition] = this.parseSimpleExpr();
1171
-
1172
- tok = tok3 || this.lexer.next();
1173
- if (tok.id !== TOKEN.rightParen) {
1174
- throw new Error(Errors.elseif);
1175
- }
1176
-
1177
- tok = this.lexer.next();
1178
- if (tok.id !== TOKEN.then) {
1179
- throw new Error(Errors.elseif);
1180
- }
1181
-
1182
- const [tok4, elseIfThenClause] = this.parseExprList();
1183
- elseIfClause.push(new ElseIfDecl(elseIfCondition, elseIfThenClause));
1184
-
1185
- tok = tok4 || this.lexer.next();
1186
- }
1187
-
1188
- if (elseIfClause.length === 0) {
1189
- elseIfClause = null;
1190
- }
1191
-
1192
- if (tok.id === TOKEN.endif) {
1193
- return [null, new IfDecl(condition, thenClause, elseIfClause, null)];
1194
- }
1195
-
1196
- if (tok.id !== TOKEN.else) {
1197
- throw new Error(Errors.if);
1198
- }
1199
-
1200
- const [tok5, elseClause] = this.parseExprList();
1201
-
1202
- tok = tok5 || this.lexer.next();
1203
- if (tok.id !== TOKEN.endif) {
1204
- throw new Error(Errors.if);
1205
- }
1206
-
1207
- return [null, new IfDecl(condition, thenClause, elseIfClause, elseClause)];
1208
- }
1209
-
1210
- parseWhile() {
1211
- // 'while' '(' SimpleExpression ')' 'do' ExprList 'endwhile'
1212
- let tok = this.lexer.next();
1213
- if (tok.id !== TOKEN.leftParen) {
1214
- throw new Error(Errors.while);
1215
- }
1216
-
1217
- const [tok1, condition] = this.parseSimpleExpr();
1218
-
1219
- tok = tok1 || this.lexer.next();
1220
- if (tok.id !== TOKEN.rightParen) {
1221
- throw new Error(Errors.while);
1222
- }
1223
-
1224
- tok = this.lexer.next();
1225
- if (tok.id !== TOKEN.do) {
1226
- throw new Error(Errors.while);
1227
- }
1228
-
1229
- const [tok2, whileClause] = this.parseExprList();
1230
-
1231
- tok = tok2 || this.lexer.next();
1232
- if (tok.id !== TOKEN.endwhile) {
1233
- throw new Error(Errors.while);
1234
- }
1235
-
1236
- return [null, new WhileDecl(condition, whileClause)];
1237
- }
1238
-
1239
- parseAssignment() {
1240
- let tok = this.lexer.next();
1241
- let hasVar = false;
1242
- if (tok.id === TOKEN.var) {
1243
- hasVar = true;
1244
- tok = this.lexer.next();
1245
- }
1246
-
1247
- if (tok.id !== TOKEN.identifier) {
1248
- throw new Error(Errors.assignment);
1249
- }
1250
-
1251
- const identifier = tok.value;
1252
-
1253
- tok = this.lexer.next();
1254
- if (tok.id !== TOKEN.assign) {
1255
- throw new Error(Errors.assignment);
1256
- }
1257
-
1258
- const [tok1, expr] = this.parseSimpleExpr();
1259
- if (hasVar) {
1260
- return [tok1, new VarDecl(identifier, expr)];
1261
- }
1262
- return [tok1, new Assignment(identifier, expr)];
1263
- }
1264
-
1265
- parseFor() {
1266
- // 'for' Assignment ('upto'|'downto') Expr ('step' Expr)? 'do'
1267
- // ExprList 'endfor'
1268
- let tok,
1269
- step = null;
1270
- let upto = false;
1271
- const [tok1, assignment] = this.parseAssignment();
1272
-
1273
- tok = tok1 || this.lexer.next();
1274
- if (tok.id === TOKEN.upto) {
1275
- upto = true;
1276
- } else if (tok.id !== TOKEN.downto) {
1277
- throw new Error(Errors.for);
1278
- }
1279
-
1280
- const [tok2, end] = this.parseSimpleExpr();
1281
-
1282
- tok = tok2 || this.lexer.next();
1283
- if (tok.id === TOKEN.step) {
1284
- [tok, step] = this.parseSimpleExpr();
1285
- tok = tok || this.lexer.next();
1286
- }
1287
-
1288
- if (tok.id !== TOKEN.do) {
1289
- throw new Error(Errors.for);
1290
- }
1291
-
1292
- const [tok3, body] = this.parseExprList();
1293
-
1294
- tok = tok3 || this.lexer.next();
1295
- if (tok.id !== TOKEN.endfor) {
1296
- throw new Error(Errors.for);
1297
- }
1298
-
1299
- return [null, new ForDecl(assignment, upto, end, step, body)];
1300
- }
1301
-
1302
- parseForeach() {
1303
- // 'for' Identifier 'in' '(' ArgumentList ')' 'do'
1304
- // ExprList 'endfor'
1305
- let tok = this.lexer.next();
1306
- if (tok.id !== TOKEN.identifier) {
1307
- throw new Error(Errors.foreach);
1308
- }
1309
-
1310
- const identifier = tok.value;
1311
-
1312
- tok = this.lexer.next();
1313
- if (tok.id !== TOKEN.in) {
1314
- throw new Error(Errors.foreach);
1315
- }
1316
-
1317
- tok = this.lexer.next();
1318
- if (tok.id !== TOKEN.leftParen) {
1319
- throw new Error(Errors.foreach);
1320
- }
1321
-
1322
- const params = SimpleExprParser.parseParams(this.lexer);
1323
-
1324
- tok = this.lexer.next();
1325
- if (tok.id !== TOKEN.do) {
1326
- throw new Error(Errors.foreach);
1327
- }
1328
-
1329
- const [tok1, body] = this.parseExprList();
1330
-
1331
- tok = tok1 || this.lexer.next();
1332
- if (tok.id !== TOKEN.endfor) {
1333
- throw new Error(Errors.foreach);
1334
- }
1335
-
1336
- return [null, new ForeachDecl(identifier, params, body)];
1337
- }
1338
- }
1339
-
1340
- export { Errors, Parser };