|
| 1 | +package me.fponzi.tlaplusformatter; |
| 2 | + |
| 3 | +import tla2sany.st.TreeNode; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +/** |
| 10 | + * Compares two SANY parse trees for structural equivalence. |
| 11 | + * Used to verify that formatting preserves the AST structure. |
| 12 | + */ |
| 13 | +public final class AstComparator { |
| 14 | + |
| 15 | + private AstComparator() {} |
| 16 | + |
| 17 | + /** |
| 18 | + * Result of an AST comparison. Contains mismatch details if the trees differ. |
| 19 | + */ |
| 20 | + public static final class Result { |
| 21 | + private final boolean match; |
| 22 | + private final String description; |
| 23 | + private final List<String> nodePath; |
| 24 | + |
| 25 | + private Result(boolean match, String description, List<String> nodePath) { |
| 26 | + this.match = match; |
| 27 | + this.description = description; |
| 28 | + this.nodePath = nodePath; |
| 29 | + } |
| 30 | + |
| 31 | + /** Creates a failure result for cases where re-parsing itself fails. */ |
| 32 | + public Result(String description) { |
| 33 | + this(false, description, Collections.emptyList()); |
| 34 | + } |
| 35 | + |
| 36 | + static Result ok() { |
| 37 | + return new Result(true, null, Collections.emptyList()); |
| 38 | + } |
| 39 | + |
| 40 | + static Result mismatch(String description, List<String> path) { |
| 41 | + return new Result(false, description, path); |
| 42 | + } |
| 43 | + |
| 44 | + public boolean isMatch() { |
| 45 | + return match; |
| 46 | + } |
| 47 | + |
| 48 | + public String getDescription() { |
| 49 | + return description; |
| 50 | + } |
| 51 | + |
| 52 | + /** Path from root to the mismatched node, e.g. ["Module", "OpDef", "InfixExpr"]. */ |
| 53 | + public List<String> getNodePath() { |
| 54 | + return Collections.unmodifiableList(nodePath); |
| 55 | + } |
| 56 | + |
| 57 | + public String formatDiagnostic() { |
| 58 | + if (match) return "ASTs match."; |
| 59 | + StringBuilder sb = new StringBuilder(); |
| 60 | + sb.append("AST verification failed:\n"); |
| 61 | + sb.append(" ").append(description).append("\n"); |
| 62 | + sb.append(" Node path: ").append(String.join(" -> ", nodePath)).append("\n"); |
| 63 | + return sb.toString(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Compare two parse trees for structural equivalence. |
| 69 | + * Compares node kinds, zero/one children, and pre-comments. |
| 70 | + */ |
| 71 | + public static Result compare(TreeNode original, TreeNode formatted) { |
| 72 | + List<String> path = new ArrayList<>(); |
| 73 | + return compareRecursive(original, formatted, path); |
| 74 | + } |
| 75 | + |
| 76 | + private static Result compareRecursive(TreeNode n1, TreeNode n2, List<String> path) { |
| 77 | + String nodeLabel = nodeLabel(n1); |
| 78 | + path.add(nodeLabel); |
| 79 | + |
| 80 | + // Compare zero() children |
| 81 | + if (n1.zero() != null) { |
| 82 | + if (n2.zero() == null) { |
| 83 | + return Result.mismatch( |
| 84 | + "Node zero[] is null in formatted AST but not in original. Node: " + n1.getImage(), |
| 85 | + new ArrayList<>(path)); |
| 86 | + } |
| 87 | + if (n1.zero().length != n2.zero().length) { |
| 88 | + return Result.mismatch( |
| 89 | + "Node zero[] length mismatch: expected " + n1.zero().length |
| 90 | + + " but found " + n2.zero().length |
| 91 | + + ". Node: " + n1.getImage(), |
| 92 | + new ArrayList<>(path)); |
| 93 | + } |
| 94 | + for (int i = 0; i < n1.zero().length; i++) { |
| 95 | + Result childResult = compareRecursive(n1.zero()[i], n2.zero()[i], path); |
| 96 | + if (!childResult.isMatch()) { |
| 97 | + return childResult; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Compare one() children |
| 103 | + if (n1.one() != null) { |
| 104 | + if (n2.one() == null) { |
| 105 | + return Result.mismatch( |
| 106 | + "Node one[] is null in formatted AST but not in original. Node: " + n1.getImage(), |
| 107 | + new ArrayList<>(path)); |
| 108 | + } |
| 109 | + if (n1.one().length != n2.one().length) { |
| 110 | + return Result.mismatch( |
| 111 | + "Node one[] length mismatch: expected " + n1.one().length |
| 112 | + + " but found " + n2.one().length |
| 113 | + + ". Node: " + n1.getImage(), |
| 114 | + new ArrayList<>(path)); |
| 115 | + } |
| 116 | + for (int i = 0; i < n1.one().length; i++) { |
| 117 | + Result childResult = compareRecursive(n1.one()[i], n2.one()[i], path); |
| 118 | + if (!childResult.isMatch()) { |
| 119 | + return childResult; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Compare pre-comments |
| 125 | + Result commentResult = compareComments(n1, n2, path); |
| 126 | + if (!commentResult.isMatch()) { |
| 127 | + return commentResult; |
| 128 | + } |
| 129 | + |
| 130 | + // Compare node kind |
| 131 | + if (n1.getKind() != n2.getKind()) { |
| 132 | + return Result.mismatch( |
| 133 | + "Node kind mismatch: expected " + n1.getKind() + " (" + n1.getImage() + ")" |
| 134 | + + " but found " + n2.getKind() + " (" + n2.getImage() + ")", |
| 135 | + new ArrayList<>(path)); |
| 136 | + } |
| 137 | + |
| 138 | + path.remove(path.size() - 1); |
| 139 | + return Result.ok(); |
| 140 | + } |
| 141 | + |
| 142 | + private static Result compareComments(TreeNode n1, TreeNode n2, List<String> path) { |
| 143 | + boolean has1 = n1.getPreComments() != null && n1.getPreComments().length > 0; |
| 144 | + boolean has2 = n2.getPreComments() != null && n2.getPreComments().length > 0; |
| 145 | + if (has1 != has2) { |
| 146 | + return Result.mismatch( |
| 147 | + "Comment presence mismatch on node: " + n1.getHumanReadableImage() |
| 148 | + + ". Expected comments: " + has1 + ", found: " + has2, |
| 149 | + new ArrayList<>(path)); |
| 150 | + } |
| 151 | + if (has1) { |
| 152 | + if (n1.getPreComments().length != n2.getPreComments().length) { |
| 153 | + return Result.mismatch( |
| 154 | + "Comment count mismatch on node: " + n1.getHumanReadableImage() |
| 155 | + + ". Expected " + n1.getPreComments().length |
| 156 | + + " but found " + n2.getPreComments().length, |
| 157 | + new ArrayList<>(path)); |
| 158 | + } |
| 159 | + for (int i = 0; i < n1.getPreComments().length; i++) { |
| 160 | + if (!n1.getPreComments()[i].equals(n2.getPreComments()[i])) { |
| 161 | + return Result.mismatch( |
| 162 | + "Comment content mismatch on node: " + n1.getHumanReadableImage() |
| 163 | + + ". Expected: \"" + n1.getPreComments()[i] |
| 164 | + + "\" but found: \"" + n2.getPreComments()[i] + "\"", |
| 165 | + new ArrayList<>(path)); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + return Result.ok(); |
| 170 | + } |
| 171 | + |
| 172 | + private static String nodeLabel(TreeNode node) { |
| 173 | + String hri = node.getHumanReadableImage(); |
| 174 | + if (hri != null && !hri.isEmpty()) { |
| 175 | + return hri + " (kind=" + node.getKind() + ")"; |
| 176 | + } |
| 177 | + String image = node.getImage(); |
| 178 | + if (image != null && !image.isEmpty()) { |
| 179 | + return image + " (kind=" + node.getKind() + ")"; |
| 180 | + } |
| 181 | + return "kind=" + node.getKind(); |
| 182 | + } |
| 183 | +} |
0 commit comments