/
AstNode.java
680 lines (616 loc) · 16.4 KB
/
AstNode.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
/*
* SonarSource Language Recognizer
* Copyright (C) 2010-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package com.sonar.sslr.api;
import com.sonar.sslr.impl.matcher.RuleDefinition;
import org.sonar.sslr.ast.AstSelect;
import org.sonar.sslr.internal.ast.select.AstSelectFactory;
import org.sonar.sslr.internal.grammar.MutableParsingRule;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* the parser is in charge to construct an abstract syntax tree (AST) which is a tree representation of the abstract syntactic structure of
* source code. Each node of the tree is an AstNode and each node denotes a construct occurring in the source code which starts at a given
* Token.
*
* @see Token
*/
public class AstNode {
protected AstNodeType type;
private final String name;
private final Token token;
private List<AstNode> children = Collections.emptyList();
private int childIndex = -1;
private AstNode parent;
private int fromIndex;
private int toIndex;
public AstNode(Token token) {
this(token.getType(), token.getType().getName(), token);
}
public AstNode(AstNodeType type, String name, @Nullable Token token) {
this.type = type;
this.token = token;
this.name = name;
}
/**
* Get the parent of this node in the tree.
*/
public AstNode getParent() {
return parent;
}
public void addChild(AstNode child) {
if (child != null) {
if (children.isEmpty()) {
children = new ArrayList<>();
}
if (child.hasToBeSkippedFromAst()) {
if (child.hasChildren()) {
for (AstNode subChild : child.children) {
addChildToList(subChild);
}
}
} else {
addChildToList(child);
}
}
}
private void addChildToList(AstNode child) {
children.add(child);
child.childIndex = children.size() - 1;
child.parent = this;
}
/**
* @return true if this AstNode has some children.
*/
public boolean hasChildren() {
return !children.isEmpty();
}
/**
* Get the list of children.
*
* @return list of children
*/
public List<AstNode> getChildren() {
return children;
}
public int getNumberOfChildren() {
return children.size();
}
/**
* @deprecated in 1.19.2, use {@link #getFirstChild(AstNodeType...)} instead
*/
@Deprecated
public AstNode getChild(int index) {
if (index >= getNumberOfChildren()) {
throw new IllegalStateException("The AstNode '" + this + "' has only " + getNumberOfChildren()
+ " children. Requested child index is wrong : " + index);
}
return children.get(index);
}
/**
* @deprecated in 1.17, use {@link #getNextAstNode()} instead
*/
@Deprecated
public AstNode nextAstNode() {
return getNextAstNode();
}
/**
* Get the next sibling AstNode in the tree and if this node doesn't exist try to get the next AST Node of the parent.
*
* @since 1.17
*/
public AstNode getNextAstNode() {
AstNode nextSibling = getNextSibling();
if (nextSibling != null) {
return nextSibling;
}
if (parent != null) {
return parent.getNextAstNode();
}
return null;
}
/**
* @deprecated in 1.17, use {@link #getPreviousAstNode()} instead
*/
@Deprecated
public AstNode previousAstNode() {
return getPreviousAstNode();
}
/**
* Get the previous sibling AstNode in the tree and if this node doesn't exist try to get the next AST Node of the parent.
*
* @since 1.17
*/
public AstNode getPreviousAstNode() {
AstNode previousSibling = getPreviousSibling();
if (previousSibling != null) {
return previousSibling;
}
if (parent != null) {
return parent.getPreviousAstNode();
}
return null;
}
/**
* @deprecated in 1.17, use {@link #getNextSibling()} instead
*/
@Deprecated
public AstNode nextSibling() {
return getNextSibling();
}
/**
* Get the next sibling AstNode if exists in the tree.
*
* @return next sibling, or null if not exists
* @since 1.17
*/
public AstNode getNextSibling() {
if (parent == null) {
return null;
}
if (parent.getNumberOfChildren() > childIndex + 1) {
return parent.children.get(childIndex + 1);
}
return null;
}
/**
* @deprecated in 1.17, use {@link #getPreviousSibling()}
*/
@Deprecated
public AstNode previousSibling() {
return getPreviousSibling();
}
/**
* Get the previous sibling AstNode if exists in the tree.
*
* @return previous sibling, or null if not exists
* @since 1.17
*/
public AstNode getPreviousSibling() {
if (parent == null) {
return null;
}
if (childIndex > 0) {
return parent.children.get(childIndex - 1);
}
return null;
}
/**
* Get the Token's value associated to this AstNode
*
* @return token's value
*/
public String getTokenValue() {
if (token == null) {
return null;
}
return token.getValue();
}
/**
* Get the Token's original value associated to this AstNode
*
* @return token's original value
*/
public String getTokenOriginalValue() {
if (token == null) {
return null;
}
return token.getOriginalValue();
}
/**
* Get the Token associated to this AstNode
*/
public Token getToken() {
return token;
}
/**
* Get the Token's line associated to this AstNode
*
* @return token's line
*/
public int getTokenLine() {
return token.getLine();
}
public boolean hasToken() {
return token != null;
}
public String getName() {
return name;
}
public int getFromIndex() {
return fromIndex;
}
public void setFromIndex(int fromIndex) {
this.fromIndex = fromIndex;
}
public int getToIndex() {
return toIndex;
}
/**
* For internal use only.
*/
public boolean hasToBeSkippedFromAst() {
if (type == null) {
return true;
}
final boolean result;
if (AstNodeSkippingPolicy.class.isAssignableFrom(type.getClass())) {
result = ((AstNodeSkippingPolicy) type).hasToBeSkippedFromAst(this);
} else {
result = false;
}
// For LexerlessGrammarBuilder and LexerfulGrammarBuilder
// unwrap AstNodeType to get a real one, i.e. detach node from tree of matchers:
if (type instanceof MutableParsingRule) {
type = ((MutableParsingRule) type).getRealAstNodeType();
} else if (type instanceof RuleDefinition) {
type = ((RuleDefinition) type).getRealAstNodeType();
}
return result;
}
public void setToIndex(int toIndex) {
this.toIndex = toIndex;
}
public boolean is(AstNodeType... types) {
for (AstNodeType expectedType : types) {
if (this.type == expectedType) {
return true;
}
}
return false;
}
public boolean isNot(AstNodeType... types) {
return !is(types);
}
/**
* @deprecated in 1.17, use {@link #getFirstChild(AstNodeType...)} instead
*/
@Deprecated
public AstNode findFirstDirectChild(AstNodeType... nodeTypes) {
return getFirstChild(nodeTypes);
}
/**
* Returns first child of one of specified types.
* <p>
* In the following case, {@code getFirstChild("B")} would return "B2":
* <pre>
* A1
* |__ C1
* | |__ B1
* |__ B2
* |__ B3
* </pre>
*
* @return first child of one of specified types, or null if not found
* @since 1.17
*/
public AstNode getFirstChild(AstNodeType... nodeTypes) {
for (AstNode child : children) {
for (AstNodeType nodeType : nodeTypes) {
if (child.type == nodeType) {
return child;
}
}
}
return null;
}
/**
* @deprecated in 1.17, use {@link #getFirstDescendant(AstNodeType...)} instead
*/
@Deprecated
public AstNode findFirstChild(AstNodeType... nodeTypes) {
return getFirstDescendant(nodeTypes);
}
/**
* Returns first descendant of one of specified types.
* <p>
* In the following case, {@code getFirstDescendant("B")} would return "B1":
* <pre>
* A1
* |__ C1
* | |__ B1
* |__ B2
* |__ B3
* </pre>
*
* @return first descendant of one of specified types, or null if not found
* @since 1.17
*/
public AstNode getFirstDescendant(AstNodeType... nodeTypes) {
for (AstNode child : children) {
if (child.is(nodeTypes)) {
return child;
}
AstNode node = child.getFirstDescendant(nodeTypes);
if (node != null) {
return node;
}
}
return null;
}
/**
* Returns the first child of this node.
*
* @return the first child, or null if there is no child
*/
public AstNode getFirstChild() {
return children.isEmpty() ? null : children.get(0);
}
/**
* @deprecated in 1.17, use {@link #getChildren(AstNodeType...)} instead
*/
@Deprecated
public List<AstNode> findDirectChildren(AstNodeType... nodeTypes) {
return getChildren(nodeTypes);
}
/**
* Returns children of specified types.
* In the following case, {@code getChildren("B")} would return "B2" and "B3":
* <p>
* <pre>
* A1
* |__ C1
* | |__ B1
* |__ B2
* |__ B3
* </pre>
*
* @return children of specified types, never null
* @since 1.17
*/
public List<AstNode> getChildren(AstNodeType... nodeTypes) {
List<AstNode> result = new ArrayList<>();
for (AstNode child : children) {
for (AstNodeType nodeType : nodeTypes) {
if (child.type == nodeType) {
result.add(child);
}
}
}
return result;
}
/**
* @deprecated in 1.17, use {@link #getDescendants(AstNodeType...)} instead, but don't forget that those methods behave differently due to bug SSLR-249
*/
@Deprecated
public List<AstNode> findChildren(AstNodeType... nodeTypes) {
List<AstNode> result = new ArrayList<>();
getDescendants(result, nodeTypes);
return result;
}
/**
* Returns descendants of specified types.
* Be careful, this method searches among all descendants whatever is their depth, so favor {@link #getChildren(AstNodeType...)} when possible.
* <p>
* In the following case, {@code getDescendants("B", "C")} would return "C1", "B1", "B2" and "B3":
* <pre>
* A1
* |__ C1
* | |__ B1
* |__ B2
* |__ D1
* |__ B3
* </pre>
*
* @return descendants of specified types, never null
* @since 1.17
*/
public List<AstNode> getDescendants(AstNodeType... nodeTypes) {
List<AstNode> result = new ArrayList<>();
if (hasChildren()) {
for (AstNode child : children) {
child.getDescendants(result, nodeTypes);
}
}
return result;
}
private void getDescendants(List<AstNode> result, AstNodeType... nodeTypes) {
for (AstNodeType nodeType : nodeTypes) {
if (is(nodeType)) {
result.add(this);
}
}
if (hasChildren()) {
for (AstNode child : children) {
child.getDescendants(result, nodeTypes);
}
}
}
/**
* Returns the last child of this node.
*
* @return the last child, or null if there is no child
*/
public AstNode getLastChild() {
return children.isEmpty() ? null : children.get(children.size() - 1);
}
/**
* Returns last child of one of specified types.
* <p>
* In the following case, {@code getLastChild("B")} would return "B3":
* <pre>
* A1
* |__ C1
* | |__ B1
* |__ B2
* |__ B3
* |__ B4
* </pre>
*
* @return last child of one of specified types, or null if not found
* @since 1.20
*/
@Nullable
public AstNode getLastChild(AstNodeType... nodeTypes) {
for (int i = children.size() - 1; i >= 0; i--) {
AstNode child = children.get(i);
for (AstNodeType nodeType : nodeTypes) {
if (child.type == nodeType) {
return child;
}
}
}
return null;
}
/**
* @return true if this node has some children with the requested node types
*/
public boolean hasDirectChildren(AstNodeType... nodeTypes) {
return getFirstChild(nodeTypes) != null;
}
/**
* @deprecated in 1.17, use {@link #hasDescendant(AstNodeType...)} instead.
* Be careful the name of this method is misleading as the check is done on descendant nodes
* and not only on child nodes.
*/
@Deprecated
public boolean hasChildren(AstNodeType... nodeTypes) {
return hasDescendant(nodeTypes);
}
/**
* @return true if this node has a descendant of one of specified types
* @since 1.17
*/
public boolean hasDescendant(AstNodeType... nodeTypes) {
return getFirstDescendant(nodeTypes) != null;
}
/**
* @deprecated in 1.17, use {@link #hasAncestor(AstNodeType)} instead
*/
@Deprecated
public boolean hasParents(AstNodeType nodeType) {
return hasAncestor(nodeType);
}
/**
* @since 1.19.2
*/
public boolean hasParent(AstNodeType... nodeTypes) {
return parent != null && parent.is(nodeTypes);
}
/**
* @return true if this node has an ancestor of the specified type
* @since 1.17
*/
public boolean hasAncestor(AstNodeType nodeType) {
return getFirstAncestor(nodeType) != null;
}
/**
* @return true if this node has an ancestor of one of specified types
* @since 1.19.2
*/
public boolean hasAncestor(AstNodeType... nodeTypes) {
return getFirstAncestor(nodeTypes) != null;
}
/**
* @deprecated in 1.17, use {@link #getFirstAncestor(AstNodeType)} instead
*/
@Deprecated
public AstNode findFirstParent(AstNodeType nodeType) {
return getFirstAncestor(nodeType);
}
/**
* @return first ancestor of the specified type, or null if not found
* @since 1.17
*/
public AstNode getFirstAncestor(AstNodeType nodeType) {
if (parent == null) {
return null;
} else if (parent.is(nodeType)) {
return parent;
} else {
return parent.getFirstAncestor(nodeType);
}
}
/**
* @return first ancestor of one of specified types, or null if not found
* @since 1.19.2
*/
public AstNode getFirstAncestor(AstNodeType... nodeTypes) {
AstNode result = parent;
while (result != null) {
if (result.is(nodeTypes)) {
return result;
}
result = result.parent;
}
return null;
}
public boolean isCopyBookOrGeneratedNode() {
return getToken().isCopyBook() || getToken().isGeneratedCode();
}
public AstNodeType getType() {
return type;
}
/**
* Return all tokens contained in this tree node. Those tokens can be directly or indirectly attached to this node.
*/
public List<Token> getTokens() {
List<Token> tokens = new ArrayList<>();
getTokens(tokens);
return tokens;
}
private void getTokens(List<Token> tokens) {
if (!hasChildren()) {
if (token != null) {
tokens.add(token);
}
} else {
for (int i = 0; i < children.size(); i++) {
children.get(i).getTokens(tokens);
}
}
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append(name);
if (token != null) {
result.append(" tokenValue='").append(token.getValue()).append("'");
result.append(" tokenLine=").append(token.getLine());
result.append(" tokenColumn=").append(token.getColumn());
}
return result.toString();
}
public Token getLastToken() {
if (!this.hasToken()) {
return null;
}
AstNode currentNode = this;
while (currentNode.hasChildren()) {
for (int i = currentNode.children.size() - 1; i >= 0; i--) {
AstNode child = currentNode.children.get(i);
if (child.hasToken()) {
currentNode = child;
break;
}
}
}
return currentNode.getToken();
}
/**
* @since 1.18
* @deprecated in 1.22
*/
@Deprecated
public AstSelect select() {
return AstSelectFactory.select(this);
}
}