FormCalc2JSVisitor.java
21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.forms.formcalc;
import com.adobe.forms.formcalc.ASTAccessor;
import com.adobe.forms.formcalc.ASTAdditive;
import com.adobe.forms.formcalc.ASTArgumentList;
import com.adobe.forms.formcalc.ASTAssignment;
import com.adobe.forms.formcalc.ASTBreak;
import com.adobe.forms.formcalc.ASTContinue;
import com.adobe.forms.formcalc.ASTDeclarationAssignment;
import com.adobe.forms.formcalc.ASTDeclarationOnly;
import com.adobe.forms.formcalc.ASTDownTo;
import com.adobe.forms.formcalc.ASTElseIfExpr;
import com.adobe.forms.formcalc.ASTEquality;
import com.adobe.forms.formcalc.ASTExpressionList;
import com.adobe.forms.formcalc.ASTForEachExpr;
import com.adobe.forms.formcalc.ASTForExpr;
import com.adobe.forms.formcalc.ASTForInit;
import com.adobe.forms.formcalc.ASTFunctionCall;
import com.adobe.forms.formcalc.ASTFunctionDeclaration;
import com.adobe.forms.formcalc.ASTFunctionIf;
import com.adobe.forms.formcalc.ASTIdentifier;
import com.adobe.forms.formcalc.ASTIfExpr;
import com.adobe.forms.formcalc.ASTLogicalAnd;
import com.adobe.forms.formcalc.ASTLogicalOr;
import com.adobe.forms.formcalc.ASTMultiplicative;
import com.adobe.forms.formcalc.ASTNULL;
import com.adobe.forms.formcalc.ASTNumber;
import com.adobe.forms.formcalc.ASTParameterList;
import com.adobe.forms.formcalc.ASTParenExpression;
import com.adobe.forms.formcalc.ASTRelational;
import com.adobe.forms.formcalc.ASTSimpleExpr;
import com.adobe.forms.formcalc.ASTStep;
import com.adobe.forms.formcalc.ASTString;
import com.adobe.forms.formcalc.ASTUnary;
import com.adobe.forms.formcalc.ASTUpTo;
import com.adobe.forms.formcalc.ASTWhile;
import com.adobe.forms.formcalc.CalcTransParserVisitor;
import com.adobe.forms.formcalc.Node;
import com.adobe.forms.formcalc.SimpleNode;
import com.adobe.forms.formcalc.api.FunctionNameMapping;
import com.adobe.forms.formcalc.support.Scopes;
import java.io.PrintWriter;
public class FormCalc2JSVisitor
implements CalcTransParserVisitor {
private final PrintWriter output;
private boolean lineWrap = true;
private final FunctionNameMapping functionNameMapping;
private final Scopes scopes = new Scopes();
private int seed = 0;
public FormCalc2JSVisitor(FunctionNameMapping functionNameMapping, PrintWriter output) {
this.functionNameMapping = functionNameMapping;
this.output = output;
}
@Override
public Object visit(SimpleNode node, Object data) {
return null;
}
@Override
public Object visit(ASTArgumentList node, Object data) {
this.print("(");
int childrenNum = node.jjtGetNumChildren();
for (int i = 0; i < childrenNum; ++i) {
Node agument = node.jjtGetChild(i);
if (i > 0) {
this.print(",");
}
agument.jjtAccept(this, data);
}
this.print(")");
return null;
}
@Override
public Object visit(ASTLogicalOr node, Object data) {
int childNum = node.jjtGetNumChildren();
assert (childNum > 1);
node.jjtGetChild(0).jjtAccept(this, data);
for (int idx = 1; idx < childNum; ++idx) {
this.printSpace();
this.print("||");
this.printSpace();
node.jjtGetChild(idx).jjtAccept(this, data);
}
return null;
}
@Override
public Object visit(ASTLogicalAnd node, Object data) {
int childNum = node.jjtGetNumChildren();
assert (childNum > 1);
node.jjtGetChild(0).jjtAccept(this, data);
for (int idx = 1; idx < childNum; ++idx) {
this.printSpace();
this.print("&&");
this.printSpace();
node.jjtGetChild(idx).jjtAccept(this, data);
}
return null;
}
@Override
public Object visit(ASTEquality node, Object data) {
int childNum = node.jjtGetNumChildren();
assert (childNum > 1);
node.jjtGetChild(0).jjtAccept(this, data);
for (int idx = 1; idx < childNum; ++idx) {
this.printSpace();
if (node.isEquals(idx)) {
this.print("==");
} else {
this.print("!=");
}
this.printSpace();
node.jjtGetChild(idx).jjtAccept(this, data);
}
return null;
}
@Override
public Object visit(ASTRelational node, Object data) {
int childNum = node.jjtGetNumChildren();
assert (childNum > 1);
node.jjtGetChild(0).jjtAccept(this, data);
for (int idx = 1; idx < childNum; ++idx) {
this.printSpace();
if (node.isGT(idx)) {
this.print(">");
} else if (node.isGE(idx)) {
this.print(">=");
} else if (node.isLT(idx)) {
this.print("<");
} else if (node.isLE(idx)) {
this.print("<=");
} else assert (false);
this.printSpace();
node.jjtGetChild(idx).jjtAccept(this, data);
}
return null;
}
@Override
public Object visit(ASTAdditive node, Object data) {
int childNum = node.jjtGetNumChildren();
assert (childNum > 1);
node.jjtGetChild(0).jjtAccept(this, data);
for (int idx = 1; idx < childNum; ++idx) {
this.printSpace();
if (node.isAdd(idx)) {
this.print("+");
} else {
this.print("-");
}
this.printSpace();
node.jjtGetChild(idx).jjtAccept(this, data);
}
return null;
}
@Override
public Object visit(ASTMultiplicative node, Object data) {
int childNum = node.jjtGetNumChildren();
assert (childNum > 1);
node.jjtGetChild(0).jjtAccept(this, data);
for (int idx = 1; idx < childNum; ++idx) {
this.printSpace();
if (node.isMultiple(idx)) {
this.print("*");
} else {
this.print("/");
}
this.printSpace();
node.jjtGetChild(idx).jjtAccept(this, data);
}
return null;
}
@Override
public Object visit(ASTNumber node, Object data) {
this.print(node.getLiteral());
return null;
}
@Override
public Object visit(ASTFunctionCall node, Object data) {
Node iffun;
if (node.jjtGetNumChildren() == 1 && (iffun = node.jjtGetChild(0)) instanceof ASTFunctionIf) {
return iffun.jjtAccept(this, data);
}
String con = this.functionNameMapping.getFunctionToConst(node.getFunName());
if (con != null) {
this.print(con);
this.printSpace();
} else {
this.print(this.functionNameMapping.getJSFunctionIfAny(node.getFunName()));
int childNum = node.jjtGetNumChildren();
if (childNum == 2) {
Node agumentList = node.jjtGetChild(1);
agumentList.jjtAccept(this, data);
} else {
this.print("(");
this.print(")");
}
}
return null;
}
@Override
public Object visit(ASTFunctionIf node, Object data) {
this.print(this.functionNameMapping.getJSFunctionIfAny("IF"));
int childNum = node.jjtGetNumChildren();
assert (childNum < 2);
if (childNum == 1) {
Node agumentList = node.jjtGetChild(0);
agumentList.jjtAccept(this, data);
} else {
this.print("(");
this.print(")");
}
return null;
}
@Override
public Object visit(ASTAccessor node, Object data) {
String nodeLiteral = node.getLiteral();
if (nodeLiteral.contains("[")) {
this.print("FormCalc._ArrayAccessor(" + nodeLiteral + ")");
} else {
this.print("FormCalc._Accessor(" + nodeLiteral + ")");
}
return null;
}
@Override
public Object visit(ASTUnary node, Object data) {
if (node.hasPLUSPrefix()) {
this.print("+");
this.printSpace();
} else if (node.hasMINUSPrefix()) {
this.print("-");
this.printSpace();
} else if (node.hasNOTPrefix()) {
this.print("!");
this.printSpace();
}
Node child = node.jjtGetChild(0);
child.jjtAccept(this, data);
return null;
}
@Override
public Object visit(final ASTAssignment node, final Object data) {
this.scopes.runInScope(Scopes.ASSIGNMENT_EXPRESSION, new Scopes.Action(){
@Override
public void call() {
SimpleNode p = (SimpleNode)node.jjtGetChild(0);
String temp = p.getLiteral();
int startIndex = temp.indexOf("[");
int indexOfDot = temp.lastIndexOf(".");
if (startIndex != -1) {
String prop = "rawValue";
String object = temp.substring(1, temp.length() - 1);
if (indexOfDot > startIndex) {
prop = temp.substring(indexOfDot + 1, temp.length() - 1);
object = temp.substring(1, indexOfDot);
}
FormCalc2JSVisitor.this.print("var a = this.resolveNodes('" + object + "')");
FormCalc2JSVisitor.this.print("\nif (a.length > 0) {");
FormCalc2JSVisitor.this.print("\n a.item(0)." + prop);
FormCalc2JSVisitor.this.print("=");
node.jjtGetChild(1).jjtAccept(FormCalc2JSVisitor.this, data);
FormCalc2JSVisitor.this.print("\n for(var i=1;i<a.length;i++)");
FormCalc2JSVisitor.this.print("\n a.item(i)." + prop + "=a.item(0)." + prop);
FormCalc2JSVisitor.this.print("\n}");
} else {
FormCalc2JSVisitor.this.print(p.getLiteral());
FormCalc2JSVisitor.this.print("=");
node.jjtGetChild(1).jjtAccept(FormCalc2JSVisitor.this, data);
}
}
});
if (this.scopes.generateReturnStatement()) {
this.print(String.format("\nreturn %1$s;", ((SimpleNode)node.jjtGetChild(0)).getLiteral()));
}
return null;
}
@Override
public Object visit(ASTIdentifier node, Object data) {
return null;
}
@Override
public Object visit(ASTNULL node, Object data) {
this.print("null");
return null;
}
@Override
public Object visit(ASTString node, Object data) {
this.print(node.getImage());
return null;
}
@Override
public Object visit(ASTParenExpression node, Object data) {
this.print("(");
int childNum = node.jjtGetNumChildren();
for (int i = 0; i < childNum; ++i) {
node.jjtGetChild(i).jjtAccept(this, data);
}
this.print(")");
return null;
}
@Override
public Object visit(ASTExpressionList node, final Object data) {
int childNum = node.jjtGetNumChildren();
for (int i = 0; i < childNum; ++i) {
final Node child = node.jjtGetChild(i);
if (i == childNum - 1) {
this.scopes.runInScope(Scopes.LAST_EXPRESSION_IN_BLOCK, new Scopes.Action(){
@Override
public void call() {
child.jjtAccept(FormCalc2JSVisitor.this, data);
}
});
} else {
child.jjtAccept(this, data);
}
if (this.isSingleLineExpression(child)) {
this.print(";");
}
this.println();
}
return null;
}
private boolean isSingleLineExpression(Node child) {
if (child instanceof ASTSimpleExpr || child instanceof ASTAssignment || child instanceof ASTDeclarationAssignment || child instanceof ASTBreak || child instanceof ASTContinue) {
return true;
}
return false;
}
@Override
public Object visit(final ASTWhile node, final Object data) {
int childNum = node.jjtGetNumChildren();
assert (childNum == 2);
this.print("while");
this.printCondition(node.jjtGetChild(0), data);
this.scopes.runInScope(node, new Scopes.Action(){
@Override
public void call() {
FormCalc2JSVisitor.this.printBlock(node.jjtGetChild(1), data);
}
});
return null;
}
@Override
public Object visit(final ASTDeclarationAssignment node, final Object data) {
assert (2 == node.jjtGetNumChildren());
this.scopes.runInScope(Scopes.ASSIGNMENT_EXPRESSION, new Scopes.Action(){
@Override
public void call() {
FormCalc2JSVisitor.this.print("var");
FormCalc2JSVisitor.this.printSpace();
ASTIdentifier identifier = (ASTIdentifier)node.jjtGetChild(0);
FormCalc2JSVisitor.this.print(identifier.getName());
FormCalc2JSVisitor.this.print("=");
node.jjtGetChild(1).jjtAccept(FormCalc2JSVisitor.this, data);
}
});
if (this.scopes.generateReturnStatement()) {
this.print(String.format("\nreturn %1$s", ((ASTIdentifier)node.jjtGetChild(0)).getName()));
}
return null;
}
@Override
public Object visit(ASTDeclarationOnly node, Object data) {
assert (1 == node.jjtGetNumChildren());
this.print("var");
this.printSpace();
ASTIdentifier identifier = (ASTIdentifier)node.jjtGetChild(0);
this.print(identifier.getName());
return null;
}
@Override
public Object visit(ASTIfExpr node, final Object data) {
this.print("if");
this.printCondition(node.jjtGetChild(0), data);
final Node exprList = node.jjtGetChild(1);
this.scopes.runInScope(node, new Scopes.Action(){
@Override
public void call() {
FormCalc2JSVisitor.this.printBlock(exprList, data);
}
});
this.handle3rdChildOfIf(node, data);
return null;
}
@Override
public Object visit(final ASTElseIfExpr node, final Object data) {
this.print("else if");
this.printCondition(node.jjtGetChild(0), data);
this.scopes.runInScope(node, new Scopes.Action(){
@Override
public void call() {
FormCalc2JSVisitor.this.printBlock(node.jjtGetChild(1), data);
}
});
this.handle3rdChildOfIf(node, data);
return null;
}
public void handle3rdChildOfIf(Node node, final Object data) {
int childNum = node.jjtGetNumChildren();
if (childNum > 2) {
assert (childNum == 3);
final Node n3 = node.jjtGetChild(2);
if (n3 instanceof ASTElseIfExpr) {
n3.jjtAccept(this, data);
} else {
this.print("else");
this.scopes.runInScope(node, new Scopes.Action(){
@Override
public void call() {
FormCalc2JSVisitor.this.printBlock(n3, data);
}
});
}
}
}
@Override
public Object visit(ASTBreak node, Object data) {
this.print("break");
return null;
}
@Override
public Object visit(ASTContinue node, Object data) {
this.print("continue");
return null;
}
@Override
public Object visit(ASTForInit node, Object data) {
ASTIdentifier id = (ASTIdentifier)node.jjtGetChild(0);
this.print(id.getName());
this.print("=");
node.jjtGetChild(1).jjtAccept(this, data);
return null;
}
@Override
public Object visit(ASTUpTo node, Object data) {
node.jjtGetChild(0).jjtAccept(this, data);
return null;
}
@Override
public Object visit(ASTDownTo node, Object data) {
node.jjtGetChild(0).jjtAccept(this, data);
return null;
}
@Override
public Object visit(ASTStep node, Object data) {
node.jjtGetChild(0).jjtAccept(this, data);
return null;
}
@Override
public Object visit(final ASTForEachExpr node, final Object data) {
Node arguLst = node.jjtGetChild(1);
String wrapperArrayID = "temp" + this.generateID();
this.print(String.format("var %1$s = new Array", wrapperArrayID));
if (arguLst instanceof ASTArgumentList) {
arguLst.jjtAccept(this, data);
}
this.print(";");
this.println();
String indexID = "index" + this.generateID();
this.print(String.format("for(var %1$s =0; %1$s < %2$s.length; %1$s++)", indexID, wrapperArrayID));
this.print("{");
this.println();
this.print(String.format("%1$s = %2$s[%3$s];", node.getLoopVariable(), wrapperArrayID, indexID));
this.scopes.runInScope(node, new Scopes.Action(){
@Override
public void call() {
node.jjtGetChild(node.jjtGetNumChildren() - 1).jjtAccept(FormCalc2JSVisitor.this, data);
}
});
this.print("}");
return null;
}
@Override
public Object visit(ASTForExpr node, final Object data) {
boolean upto;
this.print("for");
this.print("(");
Node forInit = node.jjtGetChild(0);
assert (forInit instanceof ASTForInit);
forInit.jjtAccept(this, data);
this.print(";");
Node TO = node.jjtGetChild(1);
if (TO instanceof ASTUpTo) {
upto = true;
} else {
assert (TO instanceof ASTDownTo);
upto = false;
}
this.print(node.getControl());
this.printSpace();
if (upto) {
this.print("<=");
} else {
this.print(">=");
}
this.printSpace();
TO.jjtAccept(this, data);
this.print(";");
Node loopBody = null;
Node third = node.jjtGetChild(2);
this.print(node.getControl());
this.print("=");
this.print(node.getControl());
if (upto) {
this.print("+");
} else {
this.print("-");
}
this.printSpace();
if (third instanceof ASTStep) {
third.jjtAccept(this, data);
loopBody = node.jjtGetChild(3);
} else {
this.print("1");
loopBody = third;
}
this.print(")");
final Node finalLoopBody = loopBody;
this.scopes.runInScope(node, new Scopes.Action(){
@Override
public void call() {
FormCalc2JSVisitor.this.printBlock(finalLoopBody, data);
}
});
return null;
}
@Override
public Object visit(final ASTFunctionDeclaration node, final Object data) {
this.println();
this.print("function");
this.printSpace();
ASTIdentifier identifier = (ASTIdentifier)node.jjtGetChild(0);
this.print(identifier.getName());
Node parameters = node.jjtGetChild(1);
assert (parameters instanceof ASTParameterList);
parameters.jjtAccept(this, data);
this.print("{");
this.scopes.runInScope(Scopes.FUNCTION_DECLARATION, new Scopes.Action(){
@Override
public void call() {
if (node.jjtGetNumChildren() == 3) {
node.jjtGetChild(2).jjtAccept(FormCalc2JSVisitor.this, data);
}
}
});
this.print("}");
return null;
}
@Override
public Object visit(ASTParameterList node, Object data) {
this.print("(");
int childrenNum = node.jjtGetNumChildren();
for (int i = 0; i < childrenNum; ++i) {
ASTIdentifier param = (ASTIdentifier)node.jjtGetChild(i);
if (i > 0) {
this.print(",");
}
this.print(param.getName());
}
this.print(")");
return null;
}
@Override
public Object visit(ASTSimpleExpr node, Object data) {
if (this.scopes.generateReturnStatement()) {
this.print("return");
this.printSpace();
}
node.jjtGetChild(0).jjtAccept(this, data);
return null;
}
private void printCondition(final Node condition, final Object data) {
this.scopes.runInScope(Scopes.FRAGMENT, new Scopes.Action(){
@Override
public void call() {
FormCalc2JSVisitor.this.print("(");
condition.jjtAccept(FormCalc2JSVisitor.this, data);
FormCalc2JSVisitor.this.print(")");
}
});
}
private void printBlock(Node node, Object data) {
this.print("{");
this.println();
node.jjtAccept(this, data);
this.println();
this.print("}");
this.println();
}
private void println() {
if (this.lineWrap) {
this.output.println();
}
}
private void printSpace() {
this.print(" ");
}
private void print(String s) {
this.output.print(s);
}
private String generateID() {
return String.valueOf(this.seed++);
}
}