-
Notifications
You must be signed in to change notification settings - Fork 3
/
AstSerializer.java
952 lines (884 loc) · 26.4 KB
/
AstSerializer.java
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
/*******************************************************************************
* Copyright (c) 2020, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.parser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.acceleo.query.ast.Binding;
import org.eclipse.acceleo.query.ast.BooleanLiteral;
import org.eclipse.acceleo.query.ast.Call;
import org.eclipse.acceleo.query.ast.CallType;
import org.eclipse.acceleo.query.ast.ClassTypeLiteral;
import org.eclipse.acceleo.query.ast.CollectionTypeLiteral;
import org.eclipse.acceleo.query.ast.Conditional;
import org.eclipse.acceleo.query.ast.EClassifierTypeLiteral;
import org.eclipse.acceleo.query.ast.EnumLiteral;
import org.eclipse.acceleo.query.ast.Expression;
import org.eclipse.acceleo.query.ast.IntegerLiteral;
import org.eclipse.acceleo.query.ast.Lambda;
import org.eclipse.acceleo.query.ast.Let;
import org.eclipse.acceleo.query.ast.NullLiteral;
import org.eclipse.acceleo.query.ast.RealLiteral;
import org.eclipse.acceleo.query.ast.SequenceInExtensionLiteral;
import org.eclipse.acceleo.query.ast.SetInExtensionLiteral;
import org.eclipse.acceleo.query.ast.StringLiteral;
import org.eclipse.acceleo.query.ast.TypeLiteral;
import org.eclipse.acceleo.query.ast.TypeSetLiteral;
import org.eclipse.acceleo.query.ast.VarRef;
import org.eclipse.acceleo.query.ast.VariableDeclaration;
import org.eclipse.acceleo.query.ast.util.AstSwitch;
/**
* Serialize an {@link Expression}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class AstSerializer extends AstSwitch<Object> {
/**
* id of the String type to use in variable declaration properties.
*/
public static final String STRING_TYPE = "String";
/**
* id of the integer type to use in variable declaration properties.
*/
public static final String INTEGER_TYPE = "Integer";
/**
* id of the real type to use in variable declaration properties.
*/
public static final String REAL_TYPE = "Real";
/**
* id of the boolean type to use in variable declaration properties.
*/
public static final String BOOLEAN_TYPE = "Boolean";
/**
* A dummy {@link Object} to prevent switching in super types.
*/
private static final Object DUMMY = new Object();
/**
* A space caracter.
*/
private static final String SPACE = " ";
/**
* The separator between ecore segments.
*/
public static final String ECORE_SEPARATOR = "::";
/**
* The mapping of an operator service call name to is precedence.
*/
private static final Map<String, Integer> OPERATOR_PRECEDENCE = initOperatorPrecedence();
/**
* The {@link StringBuilder} used to serialize.
*/
private StringBuilder builder;
/**
* Serializes the given {@link Expression}.
*
* @param expression
* the {@link Expression}
* @return the serialized {@link Expression}
*/
public String serialize(Expression expression) {
builder = new StringBuilder();
doSwitch(expression);
return builder.toString();
}
/**
* Initializes the operator precedence.
*
* @return the mapping of an operator service call name to is precedence
*/
private static Map<String, Integer> initOperatorPrecedence() {
final Map<String, Integer> res = new HashMap<String, Integer>();
int precedence = 0;
res.put(AstBuilderListener.NOT_SERVICE_NAME, precedence);
precedence++;
res.put(AstBuilderListener.UNARY_MIN_SERVICE_NAME, precedence);
precedence++;
res.put(AstBuilderListener.MULT_SERVICE_NAME, precedence);
res.put(AstBuilderListener.DIV_SERVICE_NAME, precedence);
precedence++;
res.put(AstBuilderListener.ADD_SERVICE_NAME, precedence);
res.put(AstBuilderListener.SUB_SERVICE_NAME, precedence);
precedence++;
res.put(AstBuilderListener.LESS_THAN_EQUAL_SERVICE_NAME, precedence);
res.put(AstBuilderListener.GREATER_THAN_EQUAL_SERVICE_NAME, precedence);
res.put(AstBuilderListener.DIFFERS_SERVICE_NAME, precedence);
res.put(AstBuilderListener.EQUALS_SERVICE_NAME, precedence);
res.put(AstBuilderListener.LESS_THAN_SERVICE_NAME, precedence);
res.put(AstBuilderListener.GREATER_THAN_SERVICE_NAME, precedence);
precedence++;
res.put(AstBuilderListener.AND_SERVICE_NAME, precedence);
precedence++;
res.put(AstBuilderListener.OR_SERVICE_NAME, precedence);
precedence++;
res.put(AstBuilderListener.XOR_SERVICE_NAME, precedence);
precedence++;
res.put(AstBuilderListener.IMPLIES_SERVICE_NAME, precedence);
return res;
}
@Override
public Object caseBinding(Binding binding) {
final String bindingName = AstBuilder.protectWithUnderscore(binding.getName());
builder.append(bindingName);
if (binding.getType() != null) {
builder.append(SPACE).append(':').append(SPACE);
doSwitch(binding.getType());
}
builder.append(SPACE).append('=').append(SPACE);
doSwitch(binding.getValue());
return DUMMY;
}
@Override
public Object caseBooleanLiteral(BooleanLiteral booleanLiteral) {
builder.append(booleanLiteral.isValue());
return DUMMY;
}
@Override
public Object caseCall(Call call) {
if (AstBuilderListener.FEATURE_ACCESS_SERVICE_NAME.equals(call.getServiceName())) {
aqlFeatureAccessCall(call);
} else if (AstBuilderListener.NOT_SERVICE_NAME.equals(call.getServiceName())) {
notCall(call);
} else if (AstBuilderListener.DIFFERS_SERVICE_NAME.equals(call.getServiceName())) {
differsCall(call);
} else if (AstBuilderListener.EQUALS_SERVICE_NAME.equals(call.getServiceName())) {
equalsCall(call);
} else if (AstBuilderListener.GREATER_THAN_EQUAL_SERVICE_NAME.equals(call.getServiceName())) {
greaterThanEqualCall(call);
} else if (AstBuilderListener.GREATER_THAN_SERVICE_NAME.equals(call.getServiceName())) {
greaterThanCall(call);
} else if (AstBuilderListener.LESS_THAN_EQUAL_SERVICE_NAME.equals(call.getServiceName())) {
lessThanEqualCall(call);
} else if (AstBuilderListener.LESS_THAN_SERVICE_NAME.equals(call.getServiceName())) {
lessThanCall(call);
} else if (AstBuilderListener.DIV_SERVICE_NAME.equals(call.getServiceName())) {
divOpCall(call);
} else if (AstBuilderListener.MULT_SERVICE_NAME.equals(call.getServiceName())) {
multCall(call);
} else if (AstBuilderListener.SUB_SERVICE_NAME.equals(call.getServiceName())) {
subCall(call);
} else if (AstBuilderListener.ADD_SERVICE_NAME.equals(call.getServiceName())) {
addCall(call);
} else if (AstBuilderListener.UNARY_MIN_SERVICE_NAME.equals(call.getServiceName())) {
unaryMinCall(call);
} else if (AstBuilderListener.AND_SERVICE_NAME.equals(call.getServiceName())) {
andCall(call);
} else if (AstBuilderListener.OR_SERVICE_NAME.equals(call.getServiceName())) {
orCall(call);
} else if (AstBuilderListener.XOR_SERVICE_NAME.equals(call.getServiceName())) {
xorCall(call);
} else if (AstBuilderListener.IMPLIES_SERVICE_NAME.equals(call.getServiceName())) {
impliesCall(call);
} else {
call(call);
}
return DUMMY;
}
/**
* A not {@link Call}.
*
* @param call
* the not {@link Call}.
*/
private void notCall(Call call) {
builder.append(AstBuilderListener.NOT_OPERATOR);
builder.append(SPACE);
if (!call.getArguments().isEmpty()) {
doSwitch(call.getArguments().get(0));
}
}
/**
* A differs {@link Call}.
*
* @param call
* the differs {@link Call}.
*/
private void differsCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.DIFFERS_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A equals {@link Call}.
*
* @param call
* the equals {@link Call}.
*/
private void equalsCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.EQUALS_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A greater than equal {@link Call}.
*
* @param call
* the greater than equal {@link Call}.
*/
private void greaterThanEqualCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.GREATER_THAN_EQUAL_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A greater than {@link Call}.
*
* @param call
* the greater than {@link Call}.
*/
private void greaterThanCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.GREATER_THAN_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A less than equal {@link Call}.
*
* @param call
* the less than equal {@link Call}.
*/
private void lessThanEqualCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.LESS_THAN_EQUAL_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A less than {@link Call}.
*
* @param call
* the less than {@link Call}.
*/
private void lessThanCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.LESS_THAN_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A div op {@link Call}.
*
* @param call
* the div op {@link Call}.
*/
private void divOpCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.DIV_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A mult {@link Call}.
*
* @param call
* the mult {@link Call}.
*/
private void multCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.MULT_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A sub {@link Call}.
*
* @param call
* the sub {@link Call}.
*/
private void subCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.SUB_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A add {@link Call}.
*
* @param call
* the add {@link Call}.
*/
private void addCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.ADD_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A unary min {@link Call}.
*
* @param call
* the unary min {@link Call}.
*/
private void unaryMinCall(Call call) {
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
builder.append(AstBuilderListener.UNARY_MIN_OPERATOR);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
}
/**
* A and {@link Call}.
*
* @param call
* the and {@link Call}.
*/
private void andCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.AND_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A or {@link Call}.
*
* @param call
* the or {@link Call}.
*/
private void orCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.OR_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A xor {@link Call}.
*
* @param call
* the xor {@link Call}.
*/
private void xorCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append(')');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.XOR_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* A implies {@link Call}.
*
* @param call
* the implies {@link Call}.
*/
private void impliesCall(Call call) {
final boolean needParenthesis = needParenthesis(call);
if (needParenthesis) {
builder.append('(');
}
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
builder.append(SPACE);
builder.append(AstBuilderListener.IMPLIES_OPERATOR);
builder.append(SPACE);
if (!arguments.isEmpty()) {
final Expression operand = arguments.remove(0);
doSwitch(operand);
}
if (needParenthesis) {
builder.append(')');
}
}
/**
* Tells if the given {@link Call} need to be surrounded with parenthesis.
*
* @param call
* the {@link Call}
* @return <code>true</code> if the given {@link Call} need to be surrounded with parenthesis,
* <code>false</code> otherwise
*/
private boolean needParenthesis(Call call) {
final boolean res;
final Integer precedence = OPERATOR_PRECEDENCE.get(call.getServiceName());
Integer parentPrecedence;
if (call.eContainer() instanceof Call) {
parentPrecedence = OPERATOR_PRECEDENCE.get(((Call)call.eContainer()).getServiceName());
if (parentPrecedence == null && ((Call)call.eContainer()).getArguments().get(0) == call) {
parentPrecedence = -1;
}
} else {
parentPrecedence = null;
}
if (precedence == null || parentPrecedence == null) {
res = false;
} else {
int delta = precedence - parentPrecedence;
if (delta > 0) {
res = true;
} else if (delta == 0) {
final Call parent = (Call)call.eContainer();
res = ((AstBuilderListener.SUB_SERVICE_NAME.equals(parent.getServiceName()))
|| (AstBuilderListener.DIV_SERVICE_NAME.equals(parent.getServiceName()))) && (parent
.getArguments().size() == 2 && parent.getArguments().get(1) == call);
} else {
res = false;
}
}
return res;
}
private void aqlFeatureAccessCall(Call call) {
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
final Expression receiver = arguments.remove(0);
doSwitch(receiver);
builder.append('.');
if (!arguments.isEmpty()) {
final StringLiteral featureNameLiteral = (StringLiteral)arguments.get(0);
final String featureName = AstBuilder.protectWithUnderscore(featureNameLiteral.getValue());
builder.append(featureName);
}
}
private void call(Call call) {
final List<Expression> arguments = new ArrayList<Expression>(call.getArguments());
final Expression receiver = arguments.remove(0);
doSwitch(receiver);
final String serviceName;
if (call.getType() == CallType.COLLECTIONCALL) {
builder.append("->");
if (!arguments.isEmpty() && arguments.get(0) instanceof Lambda) {
serviceName = call.getServiceName();
} else {
serviceName = AstBuilder.protectWithUnderscore(call.getServiceName());
}
} else {
builder.append('.');
serviceName = AstBuilder.protectWithUnderscore(call.getServiceName());
}
builder.append(serviceName);
builder.append('(');
if (!arguments.isEmpty()) {
final StringBuilder previousBuilder = builder;
try {
builder = new StringBuilder();
for (Expression argument : arguments) {
doSwitch(argument);
builder.append(',').append(' ');
}
previousBuilder.append(builder.substring(0, builder.length() - 2));
} finally {
builder = previousBuilder;
}
}
builder.append(')');
}
@Override
public Object caseCollectionTypeLiteral(CollectionTypeLiteral collectionTypeLiteral) {
if (collectionTypeLiteral.getValue() == List.class) {
builder.append("Sequence(");
} else if (collectionTypeLiteral.getValue() == Set.class) {
builder.append("OrderedSet(");
} else {
builder.append("***invalid type of collection ***(");
}
doSwitch(collectionTypeLiteral.getElementType());
builder.append(")");
return DUMMY;
}
@Override
public Object caseConditional(Conditional conditional) {
builder.append("if ");
doSwitch(conditional.getPredicate());
builder.append(" then ");
doSwitch(conditional.getTrueBranch());
builder.append(" else ");
doSwitch(conditional.getFalseBranch());
builder.append(" endif");
return DUMMY;
}
@Override
public Object caseEnumLiteral(EnumLiteral enumLiteral) {
builder.append(AstBuilder.protectWithUnderscore(enumLiteral.getEPackageName()));
builder.append(ECORE_SEPARATOR);
builder.append(AstBuilder.protectWithUnderscore(enumLiteral.getEEnumName()));
builder.append(ECORE_SEPARATOR);
builder.append(AstBuilder.protectWithUnderscore(enumLiteral.getEEnumLiteralName()));
return DUMMY;
}
@Override
public Object caseIntegerLiteral(IntegerLiteral object) {
builder.append(object.getValue());
return DUMMY;
}
@Override
public Object caseLambda(Lambda lambda) {
doSwitch(lambda.getParameters().get(0));
builder.append(" | ");
doSwitch(lambda.getExpression());
return DUMMY;
}
@Override
public Object caseLet(Let let) {
builder.append("let ");
if (!let.getBindings().isEmpty()) {
final StringBuilder previousBuilder = builder;
try {
builder = new StringBuilder();
for (Binding binding : let.getBindings()) {
doSwitch(binding);
builder.append(',').append(' ');
}
previousBuilder.append(builder.substring(0, builder.length() - 2));
} finally {
builder = previousBuilder;
}
}
builder.append(" in ");
doSwitch(let.getBody());
return super.caseLet(let);
}
@Override
public Object caseNullLiteral(NullLiteral nullLiteral) {
builder.append("null");
return DUMMY;
}
@Override
public Object caseSequenceInExtensionLiteral(SequenceInExtensionLiteral sequenceInExtensionLiteral) {
builder.append("Sequence{");
if (!sequenceInExtensionLiteral.getValues().isEmpty()) {
final StringBuilder previousBuilder = builder;
try {
builder = new StringBuilder();
for (Expression value : sequenceInExtensionLiteral.getValues()) {
doSwitch(value);
builder.append(',').append(' ');
}
previousBuilder.append(builder.substring(0, builder.length() - 2));
} finally {
builder = previousBuilder;
}
}
builder.append('}');
return DUMMY;
}
@Override
public Object caseRealLiteral(RealLiteral realLiteral) {
builder.append(realLiteral.getValue());
return DUMMY;
}
@Override
public Object caseSetInExtensionLiteral(SetInExtensionLiteral setInExtensionLiteral) {
builder.append("OrderedSet{");
if (!setInExtensionLiteral.getValues().isEmpty()) {
final StringBuilder previousBuilder = builder;
try {
builder = new StringBuilder();
for (Expression value : setInExtensionLiteral.getValues()) {
doSwitch(value);
builder.append(',').append(' ');
}
previousBuilder.append(builder.substring(0, builder.length() - 2));
} finally {
builder = previousBuilder;
}
}
builder.append('}');
return DUMMY;
}
@Override
public Object caseStringLiteral(StringLiteral stringLiteral) {
builder.append('\'');
builder.append(escape(stringLiteral.getValue()));
builder.append('\'');
return DUMMY;
}
/**
* Escapes the given {@link String}.
*
* @param value
* the {@link String}
* @return the escaped {@link String}
*/
private String escape(String value) {
final StringBuilder res = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
final char currentChar = value.charAt(i);
// TODO special UTF chars depending on the module encoding ?
switch (currentChar) {
case '\b':
res.append("\\b");
break;
case '\t':
res.append("\\t");
break;
case '\n':
res.append("\\n");
break;
case '\f':
res.append("\\f");
break;
case '\r':
res.append("\\r");
break;
case '\\':
res.append("\\\\");
break;
case '\'':
res.append("\\'");
break;
default:
res.append(currentChar);
break;
}
}
return res.toString();
}
@Override
public Object caseTypeSetLiteral(TypeSetLiteral typeSetLiteral) {
builder.append('{');
if (!typeSetLiteral.getTypes().isEmpty()) {
final StringBuilder previousBuilder = builder;
try {
builder = new StringBuilder();
for (TypeLiteral type : typeSetLiteral.getTypes()) {
doSwitch(type);
builder.append(" | ");
}
previousBuilder.append(builder.substring(0, builder.length() - 3));
} finally {
builder = previousBuilder;
}
}
builder.append('}');
return DUMMY;
}
@Override
public Object caseClassTypeLiteral(ClassTypeLiteral classTypeLiteral) {
if (classTypeLiteral.getValue() == Double.class) {
builder.append(REAL_TYPE);
} else {
builder.append(((Class<?>)classTypeLiteral.getValue()).getSimpleName());
}
return DUMMY;
}
@Override
public Object caseEClassifierTypeLiteral(EClassifierTypeLiteral object) {
builder.append(AstBuilder.protectWithUnderscore(object.getEPackageName()));
builder.append(ECORE_SEPARATOR);
builder.append(AstBuilder.protectWithUnderscore(object.getEClassifierName()));
return DUMMY;
}
@Override
public Object caseVariableDeclaration(VariableDeclaration variableDeclaration) {
final String variableName = AstBuilder.protectWithUnderscore(variableDeclaration.getName());
builder.append(variableName);
if (variableDeclaration.getType() != null) {
builder.append(SPACE).append(':').append(SPACE);
doSwitch(variableDeclaration.getType());
}
return DUMMY;
}
@Override
public Object caseVarRef(VarRef varRef) {
final String variableName = AstBuilder.protectWithUnderscore(varRef.getVariableName());
builder.append(variableName);
return DUMMY;
}
}