-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathTreeAdapter.java
More file actions
1041 lines (856 loc) · 28.7 KB
/
TreeAdapter.java
File metadata and controls
1041 lines (856 loc) · 28.7 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
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2009-2015 CWI All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which accompanies this
* distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI
* Tijs van der Storm - Tijs.van.der.Storm@cwi.nl
* Paul Klint - Paul.Klint@cwi.nl - CWI
* Mark Hills - Mark.Hills@cwi.nl (CWI)
* Arnold Lankamp - Arnold.Lankamp@cwi.nl
* Michael Steindorfer - Michael.Steindorfer@cwi.nl - CWI
*******************************************************************************/
package org.rascalmpl.values.parsetrees;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Attribute;
import org.fusesource.jansi.Ansi.Color;
import org.rascalmpl.exceptions.ImplementationError;
import org.rascalmpl.interpreter.utils.LimitedResultWriter;
import org.rascalmpl.values.RascalValueFactory;
import org.rascalmpl.values.ValueFactoryFactory;
import org.rascalmpl.values.parsetrees.visitors.TreeVisitor;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IInteger;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.IListWriter;
import io.usethesource.vallang.ISet;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IValue;
public class TreeAdapter {
private static final String CHARACTER_TREE_ITEM = "character";
private static final String NO_POSITION_INFORMATION_ERROR = "locate assumes position information on the tree";
private static final String NO_ARGS_EXCEPTION_MESSAGE = "Node has no args: ";
public static final String NORMAL = "Normal";
public static final String TYPE = "Type";
public static final String IDENTIFIER = "Identifier";
public static final String VARIABLE = "Variable";
public static final String CONSTANT = "Constant";
public static final String COMMENT = "Comment";
public static final String TODO = "Todo";
public static final String QUOTE = "Quote";
public static final String META_AMBIGUITY = "MetaAmbiguity";
public static final String META_VARIABLE = "MetaVariable";
public static final String META_KEYWORD = "MetaKeyword";
public static final String META_SKIPPED = "MetaSkipped";
public static final String NONTERMINAL_LABEL = "NonterminalLabel";
public static final String RESULT = "Result";
public static final String STDOUT = "StdOut";
public static final String STDERR = "StdErr";
private TreeAdapter() {
super();
}
public static boolean isTree(IConstructor cons) {
return cons instanceof ITree;
}
public static boolean isAppl(ITree tree) {
return tree.isAppl();
}
private static int findLabelPosition(ITree tree, String label) {
if (!TreeAdapter.isAppl(tree)) {
throw new ImplementationError("can not call getArg on a non-tree");
}
IConstructor prod = TreeAdapter.getProduction(tree);
if (!ProductionAdapter.isDefault(prod)) {
return -1;
}
IList syms = ProductionAdapter.getSymbols(prod);
for (int i = 0; i < syms.length(); i++) {
IConstructor sym = (IConstructor) syms.get(i);
while (SymbolAdapter.isConditional(sym)) {
sym = SymbolAdapter.getSymbol(sym);
}
if (SymbolAdapter.isLabel(sym) && SymbolAdapter.getLabel(sym).equals(label)) {
return i;
}
}
return -1;
}
public static ITree getArg(ITree tree, String label) {
return (ITree) getArgs(tree).get(findLabelPosition(tree, label));
}
public static ITree setArg(ITree tree, String label, IConstructor newArg) {
return setArgs(tree, getArgs(tree).put(findLabelPosition(tree, label), newArg));
}
public static boolean isAmb(ITree tree) {
return tree.isAmb();
}
public static boolean isTop(ITree tree) {
return SymbolAdapter.isStartSort(getType(tree));
}
public static boolean isChar(ITree tree) {
return tree.isChar();
}
public static boolean isCycle(ITree tree) {
return tree.isCycle();
}
public static boolean isComment(ITree tree) {
IConstructor treeProd = getProduction(tree);
if (treeProd != null) {
String treeProdCategory = ProductionAdapter.getCategory(treeProd);
if (treeProdCategory != null && treeProdCategory.equals(COMMENT))
return true;
}
return false;
}
public static IConstructor getProduction(ITree tree) {
return tree.getProduction();
}
/**
* This function assumes that getLabeledField does not return null for the same parameters!
*/
public static ITree putLabeledField(ITree tree, String field, ITree repl) {
if (isAppl(tree)) {
IConstructor prod = TreeAdapter.getProduction(tree);
if (ProductionAdapter.isDefault(prod)) {
int index = SymbolAdapter.indexOfLabel(ProductionAdapter.getSymbols(prod), field);
IList args = getArgs(tree);
if (index != -1) {
return setArgs(tree, args.put(index, repl));
}
}
else if (ProductionAdapter.isRegular(prod)) {
IConstructor sym = ProductionAdapter.getType(prod);
IList args = getArgs(tree);
IList syms;
int index;
switch (sym.getName()) {
case "seq":
syms = SymbolAdapter.getSymbols(sym);
index = SymbolAdapter.indexOfLabel(syms, field);
if (index != -1) {
return setArgs(tree, args.put(index, repl));
}
break;
case "opt":
sym = SymbolAdapter.getSymbol(sym);
if (SymbolAdapter.isLabel(sym) && SymbolAdapter.getLabel(sym).equals(field)) {
if (args.length() == 0) {
return setArgs(tree, args.append(repl));
}
else {
return setArgs(tree, args.put(0, repl));
}
}
break;
case "alt":
syms = SymbolAdapter.getSymbols(sym);
index = SymbolAdapter.indexOfLabel(syms, field);
if (index != -1) {
sym = (IConstructor) syms.get(index);
if (SymbolAdapter.isEqual(getType((ITree) args.get(0)), sym)) {
return setArgs(tree, args.put(0, repl));
}
}
break;
default:
return null;
}
}
}
return null;
}
public static class FieldResult {
public IConstructor symbol;
public ITree tree;
public FieldResult(IConstructor symbol, ITree tree) {
this.symbol = symbol;
this.tree = tree;
}
}
public static FieldResult getLabeledField(ITree tree, String field) {
if (isAppl(tree)) {
IConstructor prod = TreeAdapter.getProduction(tree);
if (ProductionAdapter.isDefault(prod)) {
IList syms = ProductionAdapter.getSymbols(prod);
int index = SymbolAdapter.indexOfLabel(syms, field);
if (index != -1) {
IConstructor sym = (IConstructor) syms.get(index);
return new FieldResult(SymbolAdapter.stripLabelsAndConditions(sym),
(ITree) getArgs(tree).get(index));
}
}
else if (ProductionAdapter.isRegular(prod)) {
IConstructor sym = ProductionAdapter.getType(prod);
IList args = getArgs(tree);
IList syms;
int index;
switch (sym.getName()) {
case "seq":
syms = SymbolAdapter.getSymbols(sym);
index = SymbolAdapter.indexOfLabel(syms, field);
if (index != -1) {
sym = (IConstructor) syms.get(index);
return new FieldResult(SymbolAdapter.stripLabelsAndConditions(sym),
(ITree) args.get(index));
}
break;
case "opt":
if (args.length() == 0) {
return null;
}
sym = SymbolAdapter.getSymbol(sym);
if (SymbolAdapter.isLabel(sym) && SymbolAdapter.getLabel(sym).equals(field)) {
return new FieldResult(SymbolAdapter.stripLabelsAndConditions(sym), (ITree) args.get(0));
}
break;
case "alt":
ISet alts = SymbolAdapter.getAlternatives(sym);
for (IValue elt : alts) {
sym = (IConstructor) elt;
// first find a matching label in the alt symbol
if (SymbolAdapter.isLabel(sym) && SymbolAdapter.getLabel((IConstructor) elt).equals(field)) {
sym = SymbolAdapter.stripLabelsAndConditions(sym);
// now find the tree with the type that matches the label in the args list
for (IValue arg : args) {
if (SymbolAdapter.isEqual(getType((ITree) arg), sym)) {
return new FieldResult(sym, (ITree) arg);
}
}
}
}
break;
default:
return null;
}
}
}
return null;
}
public static IConstructor getType(ITree tree) {
if (isAppl(tree)) {
IConstructor sym = ProductionAdapter.getType(getProduction(tree));
if (SymbolAdapter.isStarList(sym) && !getArgs(tree).isEmpty()) {
sym = SymbolAdapter.starToPlus(sym);
}
return sym;
}
else if (isCycle(tree)) {
return (IConstructor) tree.get("symbol");
}
else if (isChar(tree)) {
return SymbolAdapter.charClass(TreeAdapter.getCharacter(tree));
}
else if (isAmb(tree)) {
// ambiguities are never empty
return getType((ITree) getAlternatives(tree).iterator().next());
}
throw new ImplementationError("ITree does not have a type");
}
public static String getSortName(ITree tree) {
return ProductionAdapter.getSortName(getProduction(tree));
}
/*
* (non-Javadoc)
* @see org.rascalmpl.values.uptr.ProductionAdapter#getConstructorName(IConstructor tree)
*/
public static String getConstructorName(ITree tree) {
return ProductionAdapter.getConstructorName(getProduction(tree));
}
public static boolean isProduction(ITree tree, String sortName, String consName) {
IConstructor prod = getProduction(tree);
return ProductionAdapter.getSortName(prod).equals(sortName)
&& ProductionAdapter.getConstructorName(prod).equals(consName);
}
public static boolean isContextFree(ITree tree) {
return isAppl(tree) && ProductionAdapter.isContextFree(getProduction(tree));
}
public static boolean isList(ITree tree) {
return isAppl(tree) && ProductionAdapter.isList(getProduction(tree));
}
public static boolean isOpt(ITree tree) {
return isAppl(tree) && ProductionAdapter.isOpt(getProduction(tree));
}
public static IList getArgs(ITree tree) {
return tree.getArgs();
}
public static ITree setArgs(ITree tree, IList args) {
if (isAppl(tree)) {
return (ITree) tree.set("args", args);
}
throw new ImplementationError(NO_ARGS_EXCEPTION_MESSAGE + tree.getName());
}
public static ITree setProduction(ITree tree, IConstructor prod) {
if (isAppl(tree)) {
return (ITree) tree.set("prod", prod);
}
throw new ImplementationError(NO_ARGS_EXCEPTION_MESSAGE + tree.getName());
}
public static boolean isLiteral(ITree tree) {
return isAppl(tree) && ProductionAdapter.isLiteral(getProduction(tree));
}
public static IList getListASTArgs(ITree tree) {
if (!isList(tree)) {
throw new ImplementationError("This is not a context-free list production: " + tree);
}
IList children = getArgs(tree);
IListWriter writer = ValueFactoryFactory.getValueFactory().listWriter();
for (int i = 0; i < children.length(); i += 2) {
IValue kid = children.get(i);
writer.append(kid);
// skip layout and/or separators
if (isSeparatedList(tree)) {
i += getSeparatorCount(tree) - 1;
}
}
return writer.done();
}
public static int getSeparatorCount(ITree tree) {
IConstructor nt = ProductionAdapter.getType(getProduction(tree));
return SymbolAdapter.isSepList(nt) ? SymbolAdapter.getSeparators(nt).length() : 0;
}
public static boolean isLexical(ITree tree) {
return isAppl(tree) && ProductionAdapter.isLexical(getProduction(tree));
}
public static boolean isSort(ITree tree) {
return isAppl(tree) && ProductionAdapter.isSort(getProduction(tree));
}
public static boolean isLayout(ITree tree) {
return isAppl(tree) && ProductionAdapter.isLayout(getProduction(tree));
}
public static boolean isSeparatedList(ITree tree) {
return isAppl(tree) && isList(tree) && ProductionAdapter.isSeparatedList(getProduction(tree));
}
public static IList getASTArgs(ITree tree) {
if (SymbolAdapter.isStartSort(TreeAdapter.getType(tree))) {
return getArgs(tree).delete(0).delete(1);
}
if (isLexical(tree)) {
throw new ImplementationError("This is not a context-free production: " + tree);
}
IList children = getArgs(tree);
IListWriter writer = ValueFactoryFactory.getValueFactory().listWriter();
for (int i = 0; i < children.length(); i += 2 /* skip layout */) {
ITree kid = (ITree) children.get(i);
if (!isLiteral(kid) && !isCILiteral(kid)) {
writer.append(kid);
}
}
return writer.done();
}
public static boolean isCILiteral(ITree tree) {
return isAppl(tree) && ProductionAdapter.isCILiteral(getProduction(tree));
}
public static ISet getAlternatives(ITree tree) {
if (isAmb(tree)) {
return (ISet) tree.get("alternatives");
}
throw new ImplementationError("Node has no alternatives");
}
public static ISourceLocation getLocation(ITree tree) {
return (ISourceLocation) tree.asWithKeywordParameters().getParameter(RascalValueFactory.Location);
}
public static ITree setLocation(ITree tree, ISourceLocation loc) {
return (ITree) tree.asWithKeywordParameters().setParameter(RascalValueFactory.Location, loc);
}
public static int getCharacter(ITree tree) {
return ((IInteger) tree.get(CHARACTER_TREE_ITEM)).intValue();
}
private static class Unparser extends TreeVisitor<IOException> {
protected final Writer fStream;
private final boolean fHighlight;
private final Map<String, Ansi> ansiOpen = new HashMap<>();
private final Map<String, Ansi> ansiClose = new HashMap<>();
public Unparser(Writer stream, boolean highlight) {
fStream = stream;
fHighlight = highlight;
ansiOpen.put(NORMAL, Ansi.ansi().a(Attribute.ITALIC_OFF).a(Attribute.INTENSITY_BOLD_OFF).fg(Color.DEFAULT)
.fgBright(Color.DEFAULT));
ansiClose.put(NORMAL, Ansi.ansi().a(Attribute.ITALIC_OFF).a(Attribute.INTENSITY_BOLD_OFF).fg(Color.DEFAULT)
.fgBright(Color.DEFAULT));
ansiOpen.put(NONTERMINAL_LABEL, Ansi.ansi().a(Attribute.ITALIC).fg(Color.CYAN));
ansiClose.put(NONTERMINAL_LABEL, Ansi.ansi().a(Attribute.ITALIC_OFF).fg(Color.DEFAULT));
ansiOpen.put(META_KEYWORD, Ansi.ansi().fg(Color.MAGENTA));
ansiClose.put(META_KEYWORD, Ansi.ansi().fg(Color.DEFAULT));
ansiOpen.put(META_VARIABLE, Ansi.ansi().a(Attribute.ITALIC).fgBright(Color.GREEN));
ansiClose.put(META_VARIABLE, Ansi.ansi().a(Attribute.ITALIC_OFF).fgBright(Color.DEFAULT));
ansiOpen.put(META_AMBIGUITY, Ansi.ansi().a(Attribute.INTENSITY_BOLD).fgBright(Color.RED));
ansiClose.put(META_AMBIGUITY, Ansi.ansi().a(Attribute.INTENSITY_BOLD_OFF).fgBright(Color.DEFAULT));
ansiOpen.put(META_SKIPPED, Ansi.ansi().bgBright(Color.RED));
ansiClose.put(META_SKIPPED, Ansi.ansi().bgBright(Color.WHITE));
ansiOpen.put(COMMENT, Ansi.ansi().a(Attribute.ITALIC).fg(Color.GREEN));
ansiClose.put(COMMENT, Ansi.ansi().a(Attribute.ITALIC_OFF).fg(Color.DEFAULT));
}
/**
* This Visitor tries to find if this tree contains a cycle, without going in to the amb parts
*/
private static class CycleDetector extends TreeVisitor<IOException> {
private boolean result = false;
@Override
public ITree visitTreeCycle(ITree arg) throws IOException {
result = true;
return arg;
}
@Override
public ITree visitTreeAppl(ITree arg) throws IOException {
if (!result) {
IList children = (IList) arg.get("args");
for (IValue child : children) {
child.accept(this);
if (result) {
break;
}
}
}
return arg;
}
@Override
public ITree visitTreeAmb(ITree arg) throws IOException {
// don't go into other amb trees with cycles
return arg;
}
@Override
public ITree visitTreeChar(ITree arg) throws IOException {
return arg;
}
public static boolean detect(ITree tree) throws IOException {
CycleDetector look = new CycleDetector();
tree.accept(look);
return look.result;
}
}
public ITree visitTreeAmb(ITree arg) throws IOException {
ISet alts = TreeAdapter.getAlternatives(arg);
if (alts.isEmpty()) {
return arg;
}
Iterator<IValue> alternatives = alts.iterator();
// do not try to print the alternative with the cycle in it.
// so lets try to find the tree without the cycle
ITree tree = (ITree) alternatives.next();
while (alternatives.hasNext() && CycleDetector.detect(tree)) {
tree = (ITree) alternatives.next();
}
tree.accept(this);
return arg;
}
public ITree visitTreeCycle(ITree arg) throws IOException {
return arg;
}
public ITree visitTreeChar(ITree arg) throws IOException {
fStream.write(Character.toChars(((IInteger) arg.get(CHARACTER_TREE_ITEM)).intValue()));
return arg;
}
public ITree visitTreeAppl(ITree arg) throws IOException {
boolean reset = false;
String category = null;
if (fHighlight) {
IConstructor prod = TreeAdapter.getProduction(arg);
category = ProductionAdapter.getCategory(prod);
if (category == null && (TreeAdapter.isLiteral(arg) || TreeAdapter.isCILiteral(arg))) {
category = META_KEYWORD;
for (IValue child : TreeAdapter.getArgs(arg)) {
int c = TreeAdapter.getCharacter((ITree) child);
if (c != '-' && !Character.isJavaIdentifierPart(c)) {
category = null;
}
}
}
if (category != null) {
Ansi code = ansiOpen.get(category);
if (code != null) {
fStream.write(code.toString());
reset = true;
}
}
}
IList children = (IList) arg.get("args");
for (IValue child : children) {
child.accept(this);
}
if (fHighlight && reset) {
Ansi code = ansiClose.get(category);
if (code != null) {
fStream.write(code.toString());
}
}
return arg;
}
}
private static class UnparserWithFocus extends Unparser {
private ISourceLocation focus;
private final String BACKGROUND_ON = Ansi.ansi().bgBright(Color.CYAN).toString();
private final String BACKGROUND_OFF = Ansi.ansi().bg(Color.DEFAULT).toString();
private boolean insideFocus = false;
public UnparserWithFocus(Writer stream, ISourceLocation focus) {
super(stream, true);
this.focus = focus;
}
@Override
public ITree visitTreeChar(ITree arg) throws IOException {
char[] chars = Character.toChars(((IInteger) arg.get(CHARACTER_TREE_ITEM)).intValue());
if (insideFocus) {
for (int i = 0; i < chars.length; i++) {
if (chars[i] == '\n') {
fStream.write(BACKGROUND_OFF);
fStream.write(chars[i]);
fStream.write(BACKGROUND_ON);
}
else {
fStream.write(chars[i]);
}
}
}
else {
fStream.write(Character.toChars(((IInteger) arg.get(CHARACTER_TREE_ITEM)).intValue()));
}
return arg;
}
@Override
public ITree visitTreeAppl(ITree arg) throws IOException {
ISourceLocation argLoc = getLocation(arg);
if (argLoc != null && argLoc.getOffset() == focus.getOffset() && argLoc.getLength() == focus.getLength()) {
fStream.write(BACKGROUND_ON);
insideFocus = true;
super.visitTreeAppl(arg);
insideFocus = false;
fStream.write(BACKGROUND_OFF);
}
else {
super.visitTreeAppl(arg);
}
return arg;
}
}
public static IConstructor locateLexical(ITree tree, int offset) {
ISourceLocation l = TreeAdapter.getLocation(tree);
if (l == null) {
throw new IllegalArgumentException(NO_POSITION_INFORMATION_ERROR);
}
if (TreeAdapter.isLexical(tree)) {
if (l.getOffset() <= offset && offset < l.getOffset() + l.getLength()) {
return tree;
}
return null;
}
if (TreeAdapter.isAmb(tree)) {
return null;
}
if (TreeAdapter.isAppl(tree)) {
IList children = TreeAdapter.getASTArgs(tree);
for (IValue child : children) {
ISourceLocation childLoc = TreeAdapter.getLocation((ITree) child);
if (childLoc == null) {
continue;
}
if (childLoc.getOffset() <= offset && offset < childLoc.getOffset() + childLoc.getLength()) {
IConstructor result = locateLexical((ITree) child, offset);
if (result != null) {
return result;
}
break;
}
}
if (l.getOffset() <= offset && l.getOffset() + l.getLength() >= offset) {
return tree;
}
}
return null;
}
/**
* Locate a lexical by line and column position
*
* @param tree is the haystack
* @param line line position of the lexical
* @param column column offset
* @return
*/
public static ITree locateLexical(ITree tree, int line, int column) {
ISourceLocation l = TreeAdapter.getLocation(tree);
if (l == null) {
throw new IllegalArgumentException("no position info");
}
if (!l.hasLineColumn()) {
return null;
}
if (TreeAdapter.isLexical(tree)) {
if (l.getBeginLine() == line && l.getBeginColumn() <= column && column <= l.getEndColumn()) {
// found a lexical that has the cursor inside of it
return tree;
}
return null;
}
if (TreeAdapter.isAmb(tree)) {
return null;
}
if (TreeAdapter.isAppl(tree)) {
IList children = TreeAdapter.getASTArgs(tree);
for (IValue child : children) {
ISourceLocation childLoc = TreeAdapter.getLocation((ITree) child);
if (childLoc == null) {
continue;
}
// only go down in the right range, such that
// finding the lexical is in O(log filesize)
if (childLoc.getBeginLine() <= line && line <= childLoc.getEndLine()) {
if (childLoc.getBeginLine() == line && childLoc.getEndColumn() == line) {
// go down to the right column
if (childLoc.getBeginColumn() <= column && column <= childLoc.getEndColumn()) {
ITree result = locateLexical((ITree) child, line, column);
if (result != null) {
return result;
}
}
}
else { // in the line range, but not on the exact line yet
ITree result = locateLexical((ITree) child, line, column);
if (result != null) {
return result;
}
}
}
}
}
return null;
}
/**
* This finds the most specific (smallest) annotated tree which has its yield around the given
* offset.
*/
public static ITree locateAnnotatedTree(ITree tree, String label, int offset) {
ISourceLocation l = TreeAdapter.getLocation(tree);
if (l == null) {
throw new IllegalArgumentException(NO_POSITION_INFORMATION_ERROR);
}
if (TreeAdapter.isAmb(tree)) {
if (tree.asWithKeywordParameters().hasParameter(label)) {
return tree;
}
return null;
}
if (TreeAdapter.isAppl(tree) && !TreeAdapter.isLexical(tree)) {
IList children = TreeAdapter.getArgs(tree);
for (IValue child : children) {
ISourceLocation childLoc = TreeAdapter.getLocation((ITree) child);
if (childLoc == null) {
continue;
}
if (childLoc.getOffset() <= offset && offset < childLoc.getOffset() + childLoc.getLength()) {
ITree result = locateAnnotatedTree((ITree) child, label, offset);
if (result != null) {
return result;
}
}
}
}
if (l.getOffset() <= offset && l.getOffset() + l.getLength() >= offset
&& tree.asWithKeywordParameters().hasParameter(label)) {
return tree;
}
return null;
}
public static void unparse(IConstructor tree, Writer stream) throws IOException {
unparse(tree, false, stream);
}
public static void unparse(IConstructor tree, boolean highlight, Writer stream) throws IOException {
if (tree instanceof ITree) {
tree.accept(new Unparser(stream, highlight));
}
else {
throw new ImplementationError("Can not unparse this " + tree + " (type = " + tree.getType() + ")");
}
}
public static void unparseWithFocus(IConstructor tree, Writer stream, ISourceLocation focus) throws IOException {
if (tree instanceof ITree) {
tree.accept(new UnparserWithFocus(stream, focus));
}
else {
throw new ImplementationError("Can not unparse this " + tree + " (type = " + tree.getType() + ")");
}
}
public static String yield(IConstructor tree, boolean highlight, int limit) {
Writer stream = new LimitedResultWriter(limit);
try {
unparse(tree, highlight, stream);
return stream.toString();
}
catch (/*IOLimitReachedException*/ RuntimeException e) {
return stream.toString();
}
catch (IOException e) {
throw new ImplementationError("Method yield failed", e);
}
}
public static String yield(IConstructor tree, int limit) {
return TreeAdapter.yield(tree, false, limit);
}
public static String yield(IConstructor tree) {
return TreeAdapter.yield(tree, false);
}
public static String yield(IConstructor tree, boolean highlight) {
try {
Writer stream = new CharArrayWriter();
unparse(tree, highlight, stream);
return stream.toString();
}
catch (IOException e) {
throw new ImplementationError("Method yield failed", e);
}
}
public static void yield(IConstructor tree, Writer out) throws IOException {
unparse(tree, out);
}
public static void yield(IConstructor tree, boolean highlight, Writer out) throws IOException {
unparse(tree, highlight, out);
}
public static boolean isInjectionOrSingleton(ITree tree) {
IConstructor prod = getProduction(tree);
if (isAppl(tree)) {
if (ProductionAdapter.isDefault(prod)) {
return ProductionAdapter.getSymbols(prod).length() == 1;
}
else if (ProductionAdapter.isList(prod)) {
return getArgs(tree).length() == 1;
}
}
return false;
}
public static boolean isAmbiguousList(ITree tree) {
if (isAmb(tree)) {
ITree first = (ITree) getAlternatives(tree).iterator().next();
if (isList(first)) {
return true;
}
}
return false;
}
public static boolean isNonEmptyStarList(ITree tree) {
if (isAppl(tree)) {
IConstructor prod = getProduction(tree);
if (ProductionAdapter.isList(prod)) {
IConstructor sym = ProductionAdapter.getType(prod);
if (SymbolAdapter.isIterStar(sym) || SymbolAdapter.isIterStarSeps(sym)) {
return getArgs(tree).length() > 0;
}
}
}
return false;
}
public static boolean isPlusList(ITree tree) {
if (isAppl(tree)) {
IConstructor prod = getProduction(tree);
if (ProductionAdapter.isList(prod)) {
IConstructor sym = ProductionAdapter.getType(prod);
if (SymbolAdapter.isIterPlus(sym) || SymbolAdapter.isIterPlusSeps(sym)) {
return true;
}
}
}
return false;
}
/**
* @return true if the tree does not have any characters, it's just an empty derivation
*/
public static boolean isEpsilon(ITree tree) {
if (isAppl(tree)) {
for (IValue arg : getArgs(tree)) {
if (!isEpsilon((ITree) arg)) {
return false;
}
}
return true;
}
if (isAmb(tree)) {
return isEpsilon((ITree) getAlternatives(tree).iterator().next());
}
// if it is not a cycle, it is a character
return isCycle(tree);
}
public static IList searchCategory(ITree tree, String category) {
IListWriter writer = ValueFactoryFactory.getValueFactory().listWriter();
if (isAppl(tree)) {
String s = ProductionAdapter.getCategory(getProduction(tree));
if (s == category)
writer.append(tree);
else {
IList z = getArgs(tree);
for (IValue q : z) {
if (!(q instanceof IConstructor))
continue;
IList p = searchCategory((ITree) q, category);
writer.appendAll(p);
}
}
}
return writer.done();
}
public static boolean isRascalLexical(ITree tree) {
return SymbolAdapter.isLex(getType(tree));
}
public static IConstructor locateDeepestContextFreeNode(ITree tree, int offset) {
ISourceLocation l = TreeAdapter.getLocation(tree);
if (l == null) {
throw new IllegalArgumentException(NO_POSITION_INFORMATION_ERROR);
}
if (TreeAdapter.isLexical(tree)) {
if (l.getOffset() <= offset && offset < l.getOffset() + l.getLength()) {
return tree;
}
return null;
}
if (TreeAdapter.isAmb(tree)) {
return null;
}
if (TreeAdapter.isAppl(tree)) {
IList children = TreeAdapter.getASTArgs(tree);
for (IValue child : children) {
ISourceLocation childLoc = TreeAdapter.getLocation((ITree) child);
if (childLoc == null) {
continue;
}
if (childLoc.getOffset() <= offset && offset < childLoc.getOffset() + childLoc.getLength()) {
IConstructor result = locateDeepestContextFreeNode((ITree) child, offset);
if (result != null) {
return result;
}
break;
}
}