Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba0a05c

Browse files
committed
Add pretty-printing for patterns
1 parent 668f445 commit ba0a05c

1 file changed

Lines changed: 52 additions & 6 deletions

File tree

java/ql/lib/semmle/code/java/PrettyPrintAst.qll

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,20 +383,18 @@ private class PpInstanceOfExpr extends PpAst, InstanceOfExpr {
383383
override string getPart(int i) {
384384
i = 1 and result = " instanceof "
385385
or
386-
i = 3 and result = " " and this.isPattern()
386+
i = 3 and result = " " and this.getPattern() instanceof LocalVariableDeclExpr
387387
or
388388
i = 4 and
389-
(
390-
result = this.getPattern().asBindingPattern().getName()
391-
or
392-
result = this.getPattern().asRecordPattern().toString()
393-
)
389+
result = this.getPattern().asBindingPattern().getName()
394390
}
395391

396392
override PpAst getChild(int i) {
397393
i = 0 and result = this.getExpr()
398394
or
399395
i = 2 and result = this.getTypeName()
396+
or
397+
i = 2 and result = this.getPattern().asRecordPattern()
400398
}
401399
}
402400

@@ -748,6 +746,8 @@ private class PpSwitchStmt extends PpAst, SwitchStmt {
748746
}
749747

750748
private class PpSwitchCase extends PpAst, SwitchCase {
749+
PpSwitchCase() { not this instanceof PatternCase }
750+
751751
override string getPart(int i) {
752752
i = 0 and result = "default" and this instanceof DefaultCase
753753
or
@@ -784,6 +784,36 @@ private class PpSwitchCase extends PpAst, SwitchCase {
784784
}
785785
}
786786

787+
private class PpPatternCase extends PpAst, PatternCase {
788+
override string getPart(int i) {
789+
i = 0 and result = "case "
790+
or
791+
i = 2 and this.getPattern() instanceof LocalVariableDeclExpr and result = " "
792+
or
793+
i = 3 and result = this.getPattern().asBindingPattern().getName()
794+
or
795+
i = 2 + this.getPatternOffset() and result = ":" and not this.isRule()
796+
or
797+
i = 2 + this.getPatternOffset() and result = " -> " and this.isRule()
798+
or
799+
i = 4 + this.getPatternOffset() and result = ";" and exists(this.getRuleExpression())
800+
}
801+
802+
private int getPatternOffset() {
803+
if this.getPattern() instanceof LocalVariableDeclExpr then result = 2 else result = 0
804+
}
805+
806+
override PpAst getChild(int i) {
807+
i = 1 and result = this.getPattern().asBindingPattern().getTypeAccess()
808+
or
809+
i = 1 and result = this.getPattern().asRecordPattern()
810+
or
811+
i = 4 and result = this.getRuleExpression()
812+
or
813+
i = 4 and result = this.getRuleStatement()
814+
}
815+
}
816+
787817
private class PpSynchronizedStmt extends PpAst, SynchronizedStmt {
788818
override string getPart(int i) {
789819
i = 0 and result = "synchronized ("
@@ -1039,3 +1069,19 @@ private class PpCallable extends PpAst, Callable {
10391069
i = 5 + 4 * this.getNumberOfParameters() and result = this.getBody()
10401070
}
10411071
}
1072+
1073+
private class PpRecordPattern extends PpAst, RecordPatternExpr {
1074+
override string getPart(int i) {
1075+
i = 0 and result = this.getType().getName()
1076+
or
1077+
i = 1 and result = "("
1078+
or
1079+
i = 1 + ((any(int x | x >= 1 and exists(this.getSubPattern(x)))) * 2) and result = ", "
1080+
or
1081+
i = 1 + (count(this.getSubPattern(_)) * 2) and result = ")"
1082+
}
1083+
1084+
override PpAst getChild(int i) {
1085+
exists(int x | result = this.getSubPattern(x) | i = 2 + (x * 2))
1086+
}
1087+
}

0 commit comments

Comments
 (0)